Reputation: 23
I wrote a Time Class which I need for a Programm. I know I could do it with the TimeSpan Class but I would like to write my own but there is a mistake I didnt understand. First of all the Class:
public class Time
{
public int Minutes { get; set; }
public int Seconds { get; set; }
public int MilliSeconds { get; set; }
public Time(int minutes, int seconds, int milliSeconds)
{
Minutes = minutes;
Seconds = seconds;
MilliSeconds = milliSeconds;
}
public Time(string time)
{
if (time.Equals("DNF"))
{
Minutes = -1;
Seconds = -1;
MilliSeconds = -1;
}
else
{
Minutes = Convert.ToInt32(time.Split(':')[0]);
Seconds = Convert.ToInt32(time.Split(':')[1].Split(',')[0]);
MilliSeconds = Convert.ToInt32(time.Split(':')[1].Split(',')[1]);
}
}
public Time(long milliSeconds)
{
int minutes = 0;
while ((milliSeconds - 60000) > 0)
{
milliSeconds -= 60000;
minutes++;
}
int seconds = 0;
while ((milliSeconds - 1000) > 0)
{
milliSeconds -= 1000;
seconds++;
}
Minutes = minutes;
Seconds = seconds;
MilliSeconds = Convert.ToInt32(milliSeconds);
}
public override string ToString()
{
return Minutes == -1 && Seconds == -1 && MilliSeconds == -1 ? "DNF" : AddZeroWR(Minutes) + ":" + AddZeroWR(Seconds) + "," + AddZeroWR(MilliSeconds);
}
private string AddZeroWR(int i)
{
return i < 10 ? "0" + i : i.ToString();
}
public long ToMilliSeconds()
{
long minutesMilliSeconds = Minutes * 60000;
long secondsMilliSeconds = Seconds * 1000;
return MilliSeconds + minutesMilliSeconds + secondsMilliSeconds;
}
public static List<Time> SortTimes(List<Time> times)
{
List<Time> times_ = new List<Time>();
List<long> milliSeconds_ = new List<long>();
foreach(Time time in times)
{
if(time.ToString() != "DNF")
milliSeconds_.Add(time.ToMilliSeconds());
}
milliSeconds_.Sort();
foreach (long l in milliSeconds_)
{
Time time = new Time(l);
times_.Add(time);
}
return times_;
}
public static Time CalculateAverage(List<Time> list)
{
long timeTogether = 0;
foreach (Time time in list)
{
timeTogether += time.ToMilliSeconds();
}
double d = timeTogether / list.Count;
return new Time(Convert.ToInt64(Math.Round(d)));
}
public static Time CalculateBest(List<Time> list)
{
return SortTimes(list)[0];
}
public static Time CalculateWorst(List<Time> list)
{
return SortTimes(list)[SortTimes(list).Count - 1];
}
public static List<Time> RemoveTime(List<Time> list, Time time)
{
List<Time> times = new List<Time>();
foreach (Time t in list)
{
if(!(t.Minutes == time.Minutes && t.Seconds == time.Seconds && t.MilliSeconds == time.MilliSeconds))
times.Add(t);
}
return times;
}
}
The Problem is the SortTimes Method: I have a list with 4 Times in it for example (mm:ss,ms): 1. 44:44,44 , 2. 22:22,22 , 3. 33:33,33 , 4. 11:11,11 . So, when I sort the List with my Method there are some times which are not the same as before for example: 3. => SORT => 33:39,13 or something like that, I dont know why. I think when I convert from Time to long and back there is a mistake.
Upvotes: 1
Views: 214
Reputation: 71
You mentioned in your comment that 99:99,99 became something different. That is because 99 seconds is 1 minute and 39 seconds, so it will become 100:39,99 after you have converted it from Time
to long
and back again to Time
. Check if you can produce the same error while the value of seconds is no more than 59.
Also, you probably want to change the code within your constructor so it checks whether milliSeconds
is greater or equal to zero:
int minutes = 0;
while ((milliSeconds - 60000) >= 0)
{
milliSeconds -= 60000;
minutes++;
}
int seconds = 0;
while ((milliSeconds - 1000) >= 0)
{
milliSeconds -= 1000;
seconds++;
}
I'd have added this as a comment, but I cannot yet.
Upvotes: 2