0% found this document useful (0 votes)
21 views7 pages

3.1P - Clock Class

COS20007 c# OOP

Uploaded by

brian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views7 pages

3.1P - Clock Class

COS20007 c# OOP

Uploaded by

brian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

3.

1P - Clock Class
Program source code:
Clock.cs
public class Clock
{
Counter _seccond = new Counter("Seccond");
Counter _minute = new Counter("Minute");
Counter _hour = new Counter("Hour");

public void Tick()


{
_seccond.Increment();
if (_seccond.Tick > 59)
{
_minute.Increment();
_seccond.Reset();
if (_minute.Tick > 59)
{
_hour.Increment();
_minute.Reset();
if (_hour.Tick > 23)
{
Reset();
}
}
}
}

public void SetTime(string s)


{
string[] array = s.Split(":");

_hour = new Counter("hour", int.Parse(array[0]));


_minute = new Counter("minute", int.Parse(array[1]));
_seccond = new Counter("second", int.Parse(array[2]));
}

public void Reset()


{
_seccond.Reset();
_minute.Reset();
_hour.Reset();
}

public string CurrentTime()


{
return $"{_hour.Tick:D2}:{_minute.Tick:D2}:{_seccond.Tick:D2}";
}
}
Counter.cs
public class Counter
{
private int _count;
private string _name;
public Counter(string name)
{
_name = name;
_count = 0;
}

public Counter(string name, int count)


{
_name = name;
_count = count;
}
public void Increment()
{
_count++;
}

public void Reset()


{
_count = 0;
}

public string Name


{
get { return _name; }
set { _name = value; }
}

public int Tick


{
get { return _count; }
}

}
Program.cs
public class MainClass
{
class Program
{
static void Main(string[] args)
{
Clock clock = new Clock();
int i;

for (i = 0; i < 86400; i++)


{
Thread.Sleep(100);
Console.Clear();
clock.Tick();
Console.WriteLine(clock.CurrentTime());
}
}
}

}
Test source code:
TestClock.cs
namespace TestClock
{
public class Tests
{
private Clock _clock;

// private object _clock;

[SetUp]
public void Setup()
{
_clock = new Clock();

[Test]
public void TestReset()
{
int i;
for (i = 0; i < 86400; i++)
{
_clock.Tick();
}
_clock.Reset();
ClassicAssert.AreEqual("00:00:00", _clock.CurrentTime());

[TestCase(0, "00:00:00")]
[TestCase(60, "00:01:00")]
[TestCase(120, "00:02:00")]
[TestCase(86340, "23:59:00")]

public void TestRunning(int tick, string currenttime)


{
int i;
for (i = 0; i < tick; i++)
{
_clock.Tick();
}
ClassicAssert.AreEqual(currenttime, _clock.CurrentTime());

[TestCase("00:01:00", "00:00:59")] //Roll to 1 min


[TestCase("01:00:00", "00:59:59")] //Roll to 1 hr
[TestCase("00:00:00", "23:59:59")] //Roll to 1 day
public void TestClockRollover(string exp, string settime)
{
_clock.SetTime(settime);
_clock.Tick();
ClassicAssert.AreEqual(exp, _clock.CurrentTime());
}
}

}
TestCounter.cs
using CounterTask;
using NUnit.Framework.Legacy;

namespace TestClock
{
internal class TestCounter
{
Counter _countertest;

[SetUp]
public void Setup()
{
_countertest = new Counter("Test");
}

[Test]
public void test_start9()
{
ClassicAssert.AreEqual(0, _countertest.Tick);
}

[Test]
public void test_name()
{
ClassicAssert.AreEqual("Test", _countertest.Name);
}

[Test]
public void test_count_reset()
{
_countertest.Increment();
_countertest.Reset();
ClassicAssert.AreEqual(0, _countertest.Tick);
}

[TestCase(60, 60)]
[TestCase(100, 100)]
public void test_increment(int tick, int result)
{
int i;
for (i = 0; i < tick; i++)
{
_countertest.Increment();
}
ClassicAssert.AreEqual(result, _countertest.Tick);
}
}

}
Screenshot of unit test results

Screenshot of program execution


UML Diagram:

You might also like