Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C# APPLICATION Create a new C# test console application which should do the foll

ID: 3802113 • Letter: C

Question

C# APPLICATION

Create a new C# test console application which should do the following:

The data for the entered time objects should be kept in a list of time2 objects ( List) so you can use LINQ to run the queries that will produce the reports

Start with a loop to ask the user to enter data for a number of time2 and time2tz objects. The user should be given the choice to keep entering data or stop after data for each object is entered.

Example Output:

Which type of object you wish to enter?:

1 – time2

2 – time2tz

3 – Stop entering data

1

Enter Hours:

6

Enter Minutes:

25

Enter Seconds:

50

Which type of object you wish to enter?:

1 – time2

2 – time2tz

3 – Stop entering data

2

Enter Hours:

15

Enter Minutes:

12

Enter Seconds:

24

Enter timezone

CST

Which type of object you wish to enter?:

1 – time2

2 – time2tz

3 – Stop entering data

3

What report do you want:

1 – All objects

2 – All objects with AM times

3 – All objects with PM times

4 - QUIT

Display the list of requested objects in increasing order

After the report is produced, the user should have the choice to produce another report or quit.

Extra credit: (10 Points) Have your classes throw an exception when adding time takes the time value to more than 23:59:59 and modify the Test program to handle the exception.

/ Fig. 10.5: Time2.cs
// Time2 class declaration with overloaded constructors.
using System; // for class ArgumentOutOfRangeException

public class Time2
{
private int hour; // 0 - 23
private int minute; // 0 - 59
private int second; // 0 - 59
// constructor can be called with zero, one, two or three arguments

// Time2 constructor: another Time2 object supplied as an argument
public Time2(int h = 0, int m = 0, int s = 0)
{
SetTime(h, m, s); // invoke SetTime to validate time
} // end Time2 three-argument constructor
public Time2(Time2 time)
: this(time.Hour, time.Minute, time.Second) { }
// set a new time value using universal time; ensure that
// the data remains consistent by setting invalid values to zero
public void SetTime(int h, int m, int s)
{
Hour = h; // set the Hour property
Minute = m; // set the Minute property
Second = s; // set the Second property
} // end method SetTime

// property that gets and sets the hour
public int Hour
{
get
{
return hour;
} // end get
set
{
if (value >= 0 && value < 24)
hour = value;
else
throw new ArgumentOutOfRangeException("Hour", value, "Hour must be 0-23");
} // end set
} // end property Hour
// property that gets and sets the minute
public int Minute
{
get
{
return minute;
} // end get
set
{
if (value >= 0 && value < 60)
minute = value;
else
throw new ArgumentOutOfRangeException("Minute", value, "Minute must be 0-59");
} // end set
} // end property Minute

// property that gets and sets the second
public int Second
{
get
{
return second;
} // end get
set
{
if (value >= 0 && value < 60)
second = value;
else
throw new ArgumentOutOfRangeException("Second", value, "Second must be 0-59");
} // end set
} // end property Second

// convert to string in universal-time format (HH:MM:SS)
public string ToUniversalString()
{
return string.Format("{0:D2}:{1:D2}:{2:D2}", Hour, Minute, Second);
} // end method ToUniversalString

// convert to string in standard-time format (H:MM:SS AM or PM)
public override string ToString()
{
return string.Format("{0}:{1:D2}:{2:D2} {3}", ((Hour == 0 || Hour == 12) ? 12 : Hour % 12), Minute, Second, (Hour < 12 ? "AM" : "PM"));
} // end method ToString
}

//Time2tz.cs

using System;
public class Program
{
static void Main(string[] args)
{
var dateTime = DateTime.Now;

Console.WriteLine(dateTime);

// Display Time Zone of your System

Console.WriteLine(TimeZoneInfo.Local);


// Convert Current Date Time to EST Date Time
Console.WriteLine(" Eastern Standard Time");
var est = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
var eastern = TimeZoneInfo.ConvertTime(dateTime, est);
Console.WriteLine(eastern);

// Convert Current Date Time to CST Date Time
Console.WriteLine(" Central Standard Time");
var cst = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
var central = TimeZoneInfo.ConvertTime(dateTime, cst);
Console.WriteLine(central);

// Convert Current Date Time to MST Date Time
Console.WriteLine(" Mountain Standard Time");
var mst = TimeZoneInfo.FindSystemTimeZoneById("Mountain Standard Time");
var mountain = TimeZoneInfo.ConvertTime(dateTime, mst);
Console.WriteLine(mountain);

// Convert Current Date Time to PST Date Time
Console.WriteLine(" Pacific Standard Time");
var pst = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var pacific = TimeZoneInfo.ConvertTime(dateTime, pst);
Console.WriteLine(pacific);

Console.ReadLine();
}
}

Explanation / Answer

Here is the Code for all three classes, Feel free to ask any question in case of any issues.

Time2.cs

// Time2 class declaration with overloaded constructors.
using System; // for class ArgumentOutOfRangeException
namespace ConsoleApp1
{
public class Time2
{
private int hour; // 0 - 23
private int minute; // 0 - 59
private int second; // 0 - 59
// constructor can be called with zero, one, two or three arguments
// Time2 constructor: another Time2 object supplied as an argument
public Time2(int h = 0, int m = 0, int s = 0)
{
SetTime(h, m, s); // invoke SetTime to validate time
} // end Time2 three-argument constructor
public Time2(Time2 time)
: this(time.Hour, time.Minute, time.Second) { }
// set a new time value using universal time; ensure that
// the data remains consistent by setting invalid values to zero
public void SetTime(int h, int m, int s)
{
Hour = h; // set the Hour property
Minute = m; // set the Minute property
Second = s; // set the Second property
} // end method SetTime
// property that gets and sets the hour
public int Hour
{
get
{
return hour;
} // end get
set
{
if (value >= 0 && value < 24)
hour = value;
else
throw new ArgumentOutOfRangeException("Hour", value, "Hour must be 0-23");
} // end set
} // end property Hour
// property that gets and sets the minute
public int Minute
{
get
{
return minute;
} // end get
set
{
if (value >= 0 && value < 60)
minute = value;
else
throw new ArgumentOutOfRangeException("Minute", value, "Minute must be 0-59");
} // end set
} // end property Minute
// property that gets and sets the second
public int Second
{
get
{
return second;
} // end get
set
{
if (value >= 0 && value < 60)
second = value;
else
throw new ArgumentOutOfRangeException("Second", value, "Second must be 0-59");
} // end set
} // end property Second
// convert to string in universal-time format (HH:MM:SS)
public string ToUniversalString()
{
return string.Format("{0:D2}:{1:D2}:{2:D2}", Hour, Minute, Second);
} // end method ToUniversalString
// convert to string in standard-time format (H:MM:SS AM or PM)
public override string ToString()
{
return string.Format("{0}:{1:D2}:{2:D2} {3}", ((Hour == 0 || Hour == 12) ? 12 : Hour % 12), Minute, Second, (Hour < 12 ? "AM" : "PM"));
} // end method ToString
}
}

Time2Tz.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
class Time2tz : Time2
{
DateTime dateTime = DateTime.Now;
DateTime dateTimeByZone;
public Time2tz(DateTime dt, String timeZone)
{
dateTime = dt;
GetTimeZone(timeZone);
} // end Time2 three-argument constructor

public void GetTimeZone(string timeZone)
{
if (timeZone == "EST")
{
var est = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
dateTimeByZone = TimeZoneInfo.ConvertTime(dateTime, est);
}
else if (timeZone == "CST")
{
var cst = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
dateTimeByZone = TimeZoneInfo.ConvertTime(dateTime, cst);
}
else if (timeZone == "MST")
{
var mst = TimeZoneInfo.FindSystemTimeZoneById("Mountain Standard Time");
dateTimeByZone = TimeZoneInfo.ConvertTime(dateTime, mst);
}
else if (timeZone == "PST")
{
var pst = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
dateTimeByZone = TimeZoneInfo.ConvertTime(dateTime, pst);
}
else
throw new ArgumentOutOfRangeException("Please Enter a valid TimeZone as EST , CST, MST, PST");
}   
}
}

Program.cs

using System;
public class Program
{
static List<Time2> t2List = new List<Time2>();
static List<Time2tz> t2zList = new List<Time2tz>();
static void Main(string[] args)
{
int result = 0;
while (result != 3)
{
Console.WriteLine("Which type of object you wish to enter?: 1 – time2 2 – time2tz 3 – Stop entering data");
result = Convert.ToInt16(Console.ReadLine());

switch (result)
{
case 1:
setTime2(1);
break;
case 2:
setTime2(2);
break;
case 3:
result = 3;
break;
}
}   
  
while (result != 4)
{
Console.WriteLine("What report do you want: 1 – All objects 2 – All objects with AM times 3 – All objects with PM times 4 - QUIT");
result = Convert.ToInt16(Console.ReadLine());

switch (result)
{
case 1:
for (int i = 0; i <= t2List.Count - 1; i++)
{
Console.WriteLine(t2List[i].ToString());
}
for (int i = 0; i <= t2zList.Count - 1; i++)
{
Console.WriteLine(t2zList[i].ToString());
}
break;
case 2:
var listAM = t2List.Where(x => x.Hour < 12).ToList();
for (int i = 0; i <= listAM.Count - 1; i++)
{
Console.WriteLine(listAM[i].ToString());
}
var listAM1 = t2zList.Where(x => x.Hour < 12).ToList();
for (int i = 0; i <= listAM1.Count - 1; i++)
{
Console.WriteLine(listAM1[i].ToString());
}
break;
case 3:
var listPM = t2List.Where(x => x.Hour >= 12).ToList();
for (int i = 0; i <= listPM.Count - 1; i++)
{
Console.WriteLine(listPM[i].ToString());
}
var listPM1 = t2zList.Where(x => x.Hour >= 12).ToList();
for (int i = 0; i <= listPM1.Count - 1; i++)
{
Console.WriteLine(listPM1[i].ToString());
}
break;
case 4:
result = 4;
break;
}
}
}
  
static void setTime2(int type)
{

bool validTime = false, validMinutes = false, validSeconds = false, validTimeZone = false;
Time2 time2 = new Time2();
while (!validTime)
{
try
{
Console.WriteLine("Enter Hours:");
time2.Hour = Convert.ToInt16(Console.ReadLine());
validTime = true;
}
catch (ArgumentOutOfRangeException ex)
{
Console.WriteLine(ex.Message.ToString());
}
}
while (!validMinutes)
{
try
{
Console.WriteLine("Enter Minutes:");
time2.Minute = Convert.ToInt16(Console.ReadLine());
validMinutes = true;
}
catch (ArgumentOutOfRangeException ex)
{
Console.WriteLine(ex.Message.ToString());
}
}
while (!validSeconds)
{
try
{
Console.WriteLine("Enter Seconds:");
time2.Second = Convert.ToInt16(Console.ReadLine());
validSeconds = true;
}
catch (ArgumentOutOfRangeException ex)
{
Console.WriteLine(ex.Message.ToString());
}
}
if (type == 2)
{
while (!validTimeZone)
{
try
{
Console.WriteLine("Enter Time Zone:");
string timeZone = Console.ReadLine();
Time2tz time2Tz = new Time2tz(Convert.ToDateTime(time2.ToString()), timeZone);
time2Tz.Hour = time2.Hour;
time2Tz.Minute = time2.Minute;
time2Tz.Second = time2.Second;
validTimeZone = true;
t2zList.Add(time2Tz);
}
catch (ArgumentOutOfRangeException ex)
{
Console.WriteLine(ex.Message.ToString());
}
}
}
else
{
time2 = new Time2(time2.Hour, time2.Minute, time2.Hour);
t2List.Add(time2);
}
}
}