#include <cassert>
#include <iostream>
#include "limit_counter.h"
#include "overflow_counter.h"
#include "clock.h"
int main()
{
Clock c{23, 59, 58};
// NOTE: c.Update increments the clock
// BEFORE it's hh:mm:ss is shown
c.Update(std::cout);
c.Update(std::cout);
c.Update(std::cout);
// c.setHours(3);
// c.setMinutes(59);
// c.setSeconds(59);
c.Update(std::cout);
c.Reset();
c.Update(std::cout);
}
#include "limit_counter.h"
LimitCounter::LimitCounter(const unsigned value, const unsigned limit)
: value_{value}, limit_{limit}
{
}
void LimitCounter::SetValue(const unsigned value)
{
value_ = value;
}
unsigned LimitCounter::GetValue() const { return value_; }
bool LimitCounter::Count()
{
// ... TBD
return false;
}
void LimitCounter::Reset() {
// ... TBD
}
#ifndef LIMIT_COUNTER_H
#define LIMIT_COUNTER_H
class LimitCounter {
private:
unsigned value_;
const unsigned limit_;
public:
LimitCounter(unsigned value, unsigned limit);
virtual ~LimitCounter() = default;
void SetValue(unsigned value);
unsigned GetValue() const;
virtual bool Count();
virtual void Reset();
};
#endif // include guard
#include "overflow_counter.h"
OverflowCounter::OverflowCounter(const unsigned value, const unsigned limit,
LimitCounter& next_counter)
: LimitCounter{value, limit}, next_counter_{next_counter}
{
}
void OverflowCounter::Reset()
{
LimitCounter::Reset();
next_counter_.Reset();
}
bool OverflowCounter::Count()
{
auto is_overflow = false;
// ... TBD
return is_overflow;
}
#ifndef OVERFLOW_COUNTER_H
#define OVERFLOW_COUNTER_H
#include "limit_counter.h"
class OverflowCounter : public LimitCounter {
private:
LimitCounter& next_counter_;
public:
OverflowCounter(unsigned value, unsigned limit, LimitCounter& next_counter);
void Reset() override;
bool Count() override;
};
#endif // include guard
#ifndef CLOCK_H
#define CLOCK_H
#include <iosfwd>
#include "limit_counter.h"
#include "overflow_counter.h"
class Clock {
private:
LimitCounter hours_;
OverflowCounter minutes_;
OverflowCounter seconds_;
public:
Clock(unsigned hours, unsigned minutes, unsigned seconds);
void SetHours(unsigned hours);
void SetMinutes(unsigned minutes);
void SetSeconds(unsigned seconds);
unsigned GetHours() const;
unsigned GetMinutes() const;
unsigned GetSeconds() const;
void Reset();
void Update(std::ostream&);
private:
void Show(std::ostream&) const;
};
#endif // include guard
#include "clock.h"
#include <iomanip>
#include <iostream>
Clock::Clock(const unsigned hours, const unsigned minutes,
const unsigned seconds)
: hours_{hours, 24},
minutes_{minutes, 60, hours_},
seconds_{seconds, 60, minutes_}
{
}
// void Clock::SetHours(const unsigned hours) ... TBD
// void Clock::SetMinutes(const unsigned minutes) ... TBD
// void Clock::SetSeconds(const unsigned seconds) ... TBD
unsigned Clock::GetHours() const { return hours_.GetValue(); }
unsigned Clock::GetMinutes() const { return minutes_.GetValue(); }
unsigned Clock::GetSeconds() const { return seconds_.GetValue(); }
void Clock::Reset() {
// ... TBD
}
void Clock::Update(std::ostream& os)
{
seconds_.Count();
Show(os);
}
void Clock::Show(std::ostream& os) const
{
os << "Clock [hh:mm:ss] " << std::setfill('0') << std::setw(2)
<< GetHours() << ":" << std::setfill('0') << std::setw(2)
<< GetMinutes() << ":" << std::setfill('0') << std::setw(2)
<< GetSeconds() << "\n";
}