Этот код позволяет пользователю добавлять время.
using System;
using System.Collections.Generic;
// ...
class Program
{
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Cyan;
TimeCalculator time = new TimeCalculator();
time.InsertTime(0, 0, 59);
time.InsertTime(0, 0, 1);
int[] totaltime = time.GetTime();
Console.WriteLine($"Hours: {totaltime[0]}");
Console.WriteLine($"Minutes: {totaltime[1]}");
Console.WriteLine($"Seconds: {totaltime[2]}");
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write("Press any key to quit . . . ");
Console.ReadKey(true);
}
}
class TimeCalculator
{
int _hours;
int _minutes;
int _seconds;
List<TimeCalculator> timelist = new List<TimeCalculator>();
public void InsertTime(int hours, int minutes, int seconds)
{
timelist.Add(new TimeCalculator { _hours = hours, _minutes = minutes, _seconds = seconds });
AddTime();
}
public void AddTime()
{
int sumHours = 0;
int sumMinutes = 0;
int sumSeconds = 0;
foreach (var item in timelist)
{
sumHours += item._hours;
sumMinutes += item._minutes;
sumSeconds += item._seconds;
}
_hours = sumHours + (sumMinutes / 60);
_minutes = (sumMinutes + (sumSeconds / 60)) % 60;
_seconds = sumSeconds % 60;
// % 60 restarts clock at 60
// / 60 gives value at 60
}
public int[] GetTime()
{
return new int[] { _hours, _minutes, _seconds };
}
}