Looking Holes overall and Bad Practices (at this level).
Program.cs
namespace Homework
{
internal class Program
{
static void Main (string[] args)
{
Console.WriteLine("CodeWithMosh: Intermediate: Section 1: Exercises");
StopWatch_v1.Run ();
}
}
}
StopWatch_v1 (v1 because v2 will keep a series of start/stops and show each and total)
namespace Homework
{
internal class StopWatch_v1
{
enum Button : byte
{
start = 1,
stop = 2,
view = 3,
exit = 4,
cls = 5
}
private static StopWatch myStopwatch = new StopWatch();
public static void Run ()
{
while ( true )
{
var buttonPress = GetInteraction();
switch ( buttonPress )
{
case Button.start:
if ( !myStopwatch.Start() ) Console.WriteLine("Stopwatch is already running.");
else Console.WriteLine("Stopwatch Started");
break;
case Button.stop:
if ( !myStopwatch.Stop() ) Console.WriteLine("Stopwatch is not running.");
else Console.WriteLine("Stopwatch Stopped");
break;
case Button.view:
View();
break;
case Button.exit:
return;
case Button.cls:
Console.Clear();
break;
default:
throw new Exception("Error: Button Selection Not Programed For");
}
}
}
private static Button GetInteraction ()
{
int input = 0;
do
{
Console.WriteLine("\n\nStopwatch Menu\n");
if ( !myStopwatch.IsTimerOn )
{
Console.WriteLine("1) Start Timer");
Console.WriteLine("3) View Last Timespan");
}
if ( myStopwatch.IsTimerOn )
{
Console.WriteLine("2) Stop Timer");
Console.WriteLine("3) View Running Timespan");
}
Console.WriteLine("4) Exit");
Console.WriteLine("5) Clear Screen");
Console.Write("\nSelection: ");
var inputString = Console.ReadLine();
if ( int.TryParse(inputString, out input) )
if ( input >= 1 && input <= 5 ) break;
Console.WriteLine("Invalid Entry, Try Again");
} while (true );
return (Button) input;
}
private static void View ()
{
if ( myStopwatch.IsTimerOn )
{
Console.Write("\nTimer has been running for ");
}
else
{
Console.Write("\nTimer last ran for ");
}
Console.Write($"{myStopwatch.Days}d : ");
Console.Write($"{myStopwatch.Hours}h : ");
Console.Write($"{myStopwatch.Minutes}m : ");
Console.Write($"{myStopwatch.Seconds}s : ");
Console.Write($"{myStopwatch.Milliseconds}ms : ");
Console.Write($"{myStopwatch.Microseconds}mc : ");
Console.WriteLine($"{myStopwatch.Nanoseconds}n");
}
}
}
StopWatch.cs
namespace Homework
{
public class StopWatch
{
// Fields
private bool _timerOn;
private DateTime _startTime;
private DateTime _endTime;
// Properties
public bool IsTimerOn
{
get
{
return _timerOn;
}
}
public TimeSpan Time
{
get
{
if ( _timerOn )
{
return DateTime.Now - _startTime;
}
else
{
return _endTime - _startTime;
}
}
}
public int Days
{
get
{
return (int) Double.Truncate(Time.TotalDays);
}
}
public int Hours
{
get
{
return (int) Double.Truncate(Time.TotalHours % 24);
}
}
public int Minutes
{
get
{
return (int) Double.Truncate(Time.TotalMinutes % 60);
}
}
public int Seconds
{
get
{
return (int) Double.Truncate(Time.TotalSeconds % 60);
}
}
public int Milliseconds
{
get
{
return (int) Double.Truncate(Time.TotalMilliseconds % 1000);
}
}
public int Microseconds
{
get
{
return (int) Double.Truncate(Time.TotalMicroseconds % 1000);
}
}
public int Nanoseconds
{
get
{
return (int) Double.Truncate(Time.TotalNanoseconds % 1000);
}
}
// Constructor
public StopWatch ()
{
var now = DateTime.Now;
_startTime = now;
_endTime = now;
_timerOn = false;
}
// Methods
public bool Start ()
{
if ( _timerOn ) return false;
_startTime = DateTime.Now;
_timerOn = true;
return true;
}
public bool Stop ()
{
if ( !_timerOn ) return false;
_endTime = DateTime.Now;
_timerOn = false;
return true;
}
}
}