-
Notifications
You must be signed in to change notification settings - Fork 549
/
Copy path05_homework_05_answer.cpp
72 lines (69 loc) · 1.69 KB
/
05_homework_05_answer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <bits/stdc++.h>
using namespace std;
class Time {
private:
// Put on separate lines for code clarity
int hours;
int minutes;
int seconds;
public:
Time(int hours, int minutes, int seconds) {
// There is member function doing the same task
// Avoid code duplication
SetTime(hours, minutes, seconds);
}
void SetTime(int hours, int minutes, int seconds) {
// Put on separate lines for code clarity
// Still there is a missing point her.
// We better use setters as they validate/change input
this->hours = hours;
this->minutes = minutes;
this->seconds = seconds;
}
int GetTotalSeconds() {
return hours * 60 * 60 + minutes * 60 + seconds;
}
int GetTotalMinutes() {
return hours * 60 + minutes;
}
void PrintHHMMSS() {
// Code duplication!
cout<<ToSring(":")<<"\n";
}
string ToSring(string seperator = "-") {
ostringstream oss;
oss << hours << seperator << minutes << seperator << seconds;
return oss.str();
}
int GetHours() {
return hours;
}
void SetHours(int hours) {
// Be careful from missing verifications. This is important for "data-integrity"
if (hours < 0)
hours = 0;
this->hours = hours;
}
int GetMinutes() {
return minutes;
}
void SetMinutes(int minutes) {
if (minutes < 0)
minutes = 0;
this->minutes = minutes;
}
int GetSeconds() {
return seconds;
}
void SetSeconds(int seconds) {
if (seconds < 0)
seconds = 0;
this->seconds = seconds;
}
};
int main() {
// This will print wrongly. E.g. every 60 second should be added as a minute. Every 60 minutes should be added as an hour
Time t(0, 120, 120);
t.PrintHHMMSS(); // proper printing: 2:2:0
return 0;
}