-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAlarm.cpp
152 lines (129 loc) · 3.91 KB
/
Alarm.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "Alarm.h" // class implemented
#include<string>
using namespace std;
// File scope starts here
/////////////////////////////// PUBLIC ///////////////////////////////////////
//============================= LIFECYCLE ====================================
// Alarm Default+Overloaded Constructor
Alarm::Alarm(int aHour, int aMinute, int aSecond, int aVolume, const string& aRing) : Time(aHour, aMinute, aSecond), mVolume(aVolume) {
this->SetAlarm(aVolume, aRing);
}
// end Alarm constructor
// Alarm Type conversion Constructor
Alarm::Alarm(const Time& aTime) : Time(aTime), mVolume(0), mAlarmType(None) { }
// end Alarm constructor
//============================= OPERATORS ====================================
// Stream Extraction
istream& operator >>(istream& is, Alarm& a)
{
int hr, min, sec, vLevel;
string ringTone;
cout << "Setting Alarm" << endl;
cout << "Enter hour: ";
is >> hr;
cout << "Enter minute: ";
is >> min;
cout << "Enter second: ";
is >> sec;
a.SetHour(hr);
a.SetMinute(min);
a.SetSecond(sec);
cout << "Enter Volume level(0-5): ";
is >> vLevel;
a.SetVolume(vLevel);
cout << "Enter ring tone: ";
cin.ignore();
getline(cin, ringTone);
a.SetAlarmType(ringTone);
return is;
}
// end stream extraction
// subscript operator to set volume in valid range (0-5).
void Alarm::operator[](int aVolume)
{
this->SetVolume(aVolume);
}
// end subscript operator
// Function-operator for type conversion.
Alarm::operator Time() {
return this->GetTime();
}
// end function-operator
//============================= OPERATIONS ===================================
// function that displays alarm
void Alarm::DisplayAlarm()const
{
cout << "Alarm Time: ";
this->Display24();
cout << "Volume level: " << this->GetVolume() << endl;
cout << "Ringtone: ";
cout << this->GetAlarmType() << endl;
}
// end function DisplayAlarm
//============================= ACESS ===================================
// function that sets volume of Alarm
void Alarm::SetVolume(int aVolume) {
if (aVolume > 5) {
cout << "Voume out of maximum range: Setting it to 5." << endl;
this->mVolume = 5;
}
else if (aVolume < 0) {
cout << "Voume below minimum range: Setting it to 0." << endl;
this->mVolume = 0;
}
else
this->mVolume = aVolume;
}
// end function SetVolume
// function that sets alarm type
void Alarm::SetAlarmType(const string& aAlarmType) {
if (aAlarmType == "Melody")
this->mAlarmType = Melody;
else if (aAlarmType == "Vibration")
this->mAlarmType = Vibration;
else if (aAlarmType == "Melody+Vibration")
this->mAlarmType = MelodyAndVibration;
else
this->mAlarmType = None;
}
// end function SetAlarmType
// function that sets Alarm
void Alarm::SetAlarm(int aHour, int aMinute, int aSecond, int aVolume, const string& aRing) {
this->SetTime(aHour, aMinute, aSecond);
this->SetAlarm(aVolume, aRing);
}
// end function SetAlarm
// overloaded function that sets the Alarm
void Alarm::SetAlarm(int aVolume, const string& aRing){
this->SetVolume(aVolume);
this->SetAlarmType(aRing);
}
// end function SetAlarm
// overloaded function that sets the Alarm
void Alarm::SetAlarm(const Alarm& aAlarm) {
this->SetTime(aAlarm.GetTime());
this->SetAlarm(aAlarm.GetVolume(), aAlarm.GetAlarmType());
}
// end function SetAlarm
// function that gets volume of Alarm
int Alarm::GetVolume()const {
return this->mVolume;
}
// end function GetVolume
// function that gets alarm type
const string Alarm::GetAlarmType()const {
if (this->mAlarmType == Melody)
return "Melody";
else if (this->mAlarmType == Vibration)
return "Vibration";
else if (this->mAlarmType == MelodyAndVibration)
return "MelodyAndVibration";
else
return "None";
}
// end function GetAlarmType
// function that gets the Alarm
const Alarm& Alarm::GetAlarm()const {
return *this;
}
// end function GetAlarm