-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCourse4_week1_codes.cpp
304 lines (235 loc) · 5.56 KB
/
Course4_week1_codes.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
Exercise-1
#include <iostream>
using namespace std;
//add class definitions below this line
class Fruit {
public:
Fruit(string n, string c) {
name = n;
color = c;
}
string GetName() {
return name;
}
string GetColor() {
return color;
}
void SetName(string n) {
name = n;
}
void SetColor(string c) {
color = c;
}
private:
string name;
string color;
};
//add class definitions above this line
int main() {
//DO NOT EDIT CODE BELOW THIS LINE
Fruit fruit("apple", "red");
cout << fruit.GetName() << endl;
cout << fruit.GetColor() << endl;
fruit.SetName("orange");
fruit.SetColor("orange");
cout << fruit.GetName() << endl;
cout << fruit.GetColor() << endl;
//DO NOT EDIT CODE ABOVE THIS LINE
return 0;
}
==========================
Exercise-2
class Watch {
public:
Watch(string mf, string mo, int d, int w, string mt) {
manufacturer = mf;
model = mo;
diameter = d;
water_resistance = w;
material = mt;
}
string GetManufacturer() {
return manufacturer;
}
void SetManufacturer(string new_manufacturer) {
manufacturer = new_manufacturer;
}
string GetModel() {
return model;
}
void SetModel(string new_model) {
model = new_model;
}
int GetDiameter() {
return diameter;
}
void SetDiameter(int new_diameter) {
diameter = new_diameter;
}
int GetWaterResistance() {
return water_resistance;
}
void SetWaterResistance(int new_water_resistance) {
water_resistance = new_water_resistance;
}
string GetMaterial() {
return material;
}
void SetMaterial(string new_material) {
material = new_material;
}
void Summary() {
cout << "Manufacturer: " << manufacturer << endl;
cout << "Model: " << model << endl;
cout << "Diameter: " << diameter << " mm" << endl;
cout << "Water Resistance: " << water_resistance << " m" << endl;
cout << "Material: " << material << endl;
}
private:
string manufacturer;
string model;
int diameter;
int water_resistance;
string material;
};
==========================
Exercise-3
public:
Song(string ar, string t, string al) {
artist = ar;
title = t;
album = al;
}
string GetArtist() {
return artist;
}
void SetArtist(string new_artist) {
artist = new_artist;
}
string GetTitle() {
return title;
}
void SetTitle(string new_title) {
title = new_title;
}
string GetAlbum() {
return album;
}
void SetAlbum(string new_album) {
album = new_album;
}
int GetPlayCount() {
return play_count;
}
double GetMoneyEarned() {
return money_earned;
}
double GetPayRate() {
return pay_rate;
}
void Play(int count) {
for (int i = 0; i < count; i++) {
UpdatePlayCount();
UpdateMoneyEarned();
}
}
void Stats() {
cout << artist << endl;
cout << title << endl;
cout << album << endl;
cout << play_count << endl;
cout << pay_rate << endl;
cout << money_earned << endl;
}
===================================
Exercise-4
class Atm {
public:
Atm() {}
double GetMoney() {
return money;
}
void Deposit(double amount) {
if (amount <= 0) {
cout << "You cannot deposit a negative or 0 amount of money." << endl;
}
else {
money += amount;
}
}
void Withdraw(double amount) {
if (amount <= 0) {
cout << "You cannot withdraw a negative or 0 amount of money." << endl;
}
else if (amount > money) {
cout << "You do not have enough funds to withdraw that amount." <<endl;
}
else {
money -= amount;
}
}
private:
double money = 0;
};
=====================================
Exercise-5
class CardBinder {
public:
CardBinder() {}
int GetGold() {
return gold_card;
}
int GetSilver() {
return silver_card;
}
int GetTotal() {
return gold_card + silver_card;
}
void AddGold(int amount) {
if (amount <= 0) {
cout << "You cannot add a negative or 0 amount of cards." << endl;
}
else if (amount + gold_card + silver_card > 20) {
cout << "You do not have enough binder room." << endl;
}
else {
gold_card += amount;
}
}
void RemoveGold(int amount) {
if (amount <= 0) {
cout << "You cannot remove a negative or 0 amount of cards." << endl;
}
else if (gold_card - amount < 0) {
cout << "You do not have enough gold cards to remove." << endl;
}
else {
gold_card -= amount;
}
}
void AddSilver(int amount) {
if (amount <= 0) {
cout << "You cannot add a negative or 0 amount of cards." << endl;
}
else if (amount + gold_card +silver_card > 20) {
cout << "You do not have enough binder room." << endl;
}
else {
silver_card += amount;
}
}
void RemoveSilver(int amount) {
if (amount <= 0) {
cout << "You cannot remove a negative or 0 amount of cards." << endl;
}
else if (silver_card - amount < 0) {
cout << "You do not have enough silver cards to remove." << endl;
}
else {
silver_card -= amount;
}
}
private:
int gold_card = 0;
int silver_card = 0;
};