-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodev1.cpp
More file actions
359 lines (349 loc) · 7.13 KB
/
codev1.cpp
File metadata and controls
359 lines (349 loc) · 7.13 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <vector>
#include <iomanip>
using namespace std;
class instructor
{
protected:
char *name = new char;
int ID;
int numberofclients;
public:
inline virtual void set()
{
cout << "enter employee name" << endl;
fflush(stdin);
cin.getline(name, 20);
cout << "enter instructor id" << endl;
cin >> ID;
}
inline virtual float calculate(char a[8])
{
return 0;
}
};
class weights : virtual public instructor
{
protected:
int w = 1000, tw = 1000;
};
class zumba : virtual public instructor
{
protected:
int tz = 600;
};
class mma : virtual public instructor
{
protected:
int m = 700, tm = 800;
};
class cardio : virtual public instructor
{
protected:
int c = 500;
};
class client : public weights, public zumba, public mma, public cardio
{
protected:
char *cname = new char;
int membershipf = 0;
public:
void set()
{
cout << "enter your name" << endl;
fflush(stdin);
cin.getline(cname, 20);
}
float calculate(char a[8]) // calculates fees
{
int k;
for (int i = 0; i < 8; i++)
{
if (a[i] == '1')
{
membershipf += w;
cout << "do you want a personal instructor for weights?" << endl
<< "if yes enter 1 else enter 0" << endl;
cin >> k;
if (k == 1)
membershipf += tw;
}
else if (a[i] == '2')
{
membershipf += tz;
}
else if (a[i] == '3')
{
membershipf += m;
cout << "do you want a personal instructor for mma?" << endl;
cout << "if yes enter1 else enter 0" << endl;
cin >> k;
if (k == 1)
membershipf += tm;
}
else if (a[i] == '4')
membershipf += c;
}
return membershipf;
}
void alter(int mode) // deletes membership
{
int flag = 0, result;
string line, name;
vector<string> lines;
ifstream iofile("output.txt");
if (iofile.fail())
{
cout << "unable to open file for reading" << endl;
return;
}
while (getline(iofile, line))
{
lines.push_back(line);
}
iofile.close();
cout << "enter your name:" << endl;
fflush(stdin);
cin.getline(cname, 30);
name = cname;
ofstream w("output.txt");
if (w.fail())
{
cout << "unable to open file for writing" << endl;
return;
}
for (int i = 0; i < lines.size(); i++)
{
if (name == lines[i].substr(0, name.length()))
{
if (mode == 1 && flag == 0)
{
flag = 1;
cout << "choose your new facility" << endl;
cout << "1:weights 2:zumba 3:mma 4:cardio" << endl;
fflush(stdin);
cin.getline(cname, 8);
result = calculate(cname);
w << lines[i].substr(0, name.length()) << " " << result << "\n";
cout << "you're facility choice has been updated" << endl;
continue;
}
if (mode == 2)
{
flag = 1;
cout << "memebership deleted" << endl;
continue;
}
}
w << lines[i] << "\n";
}
w.close();
if (flag == 0)
cout << "INVALID NAME ENETERED" << endl;
}
void renew_membership(string &l) // renews membership for n months
{
string t;
int flag = 0, months;
ifstream read("output.txt");
if (read.fail())
{
cout << "unable to open file" << endl;
return;
}
while (getline(read, t))
{
if (t.substr(0, l.length()) == l)
{
flag = 1;
t = t.substr(l.length(), t.length());
cout << "By how many months do you want too extend your membership?" << endl;
cin >> months;
try
{
cout << "Your membership fees for " << months << " months is:" << renew(t, months) << endl;
}
catch (...)
{
cerr << "invalid name entered or an error has occurred" << endl;
}
}
}
read.close();
if (flag == 0)
{
cout << "INVALID NAME ENTERED!!" << endl;
return;
}
}
int renew(string &t, int m) // renews gym membership
{
membershipf = stoi(t);
membershipf = membershipf * m;
if (m <= 3)
{
membershipf = membershipf - (membershipf * 20 / 100);
}
else if (m > 3 && m < 12)
{
membershipf = membershipf - (membershipf * 30 / 100);
}
else if (m == 12)
{
membershipf = membershipf - (membershipf * 40 / 100);
}
else if (m > 12)
{
membershipf = membershipf - (membershipf * 50 / 100);
}
return membershipf;
}
friend ostream &operator<<(ostream &out, client &c);
};
ostream &operator<<(ostream &out, client &c) // overloaded << operator for entering object directly to file
{
out << c.cname << " ";
out << c.membershipf << endl;
return out;
}
class expense : public instructor // to calculate salary
{
int e = 8000;
public:
void salary() // calculates salary of instructor
{
int id, flag = 0;
ifstream inputFile("instructors.txt");
if (inputFile.fail())
{
cout << "unable to open file for reading" << endl;
return;
}
cout << "enter instructor id" << endl;
cin >> id;
while (inputFile >> ID >> numberofclients)
{
if (id == ID)
{
flag = 1;
inputFile.close();
try
{
if (numberofclients != 0)
{
e = e + numberofclients * 500;
display();
}
else
{
throw numberofclients;
}
}
catch (int)
{
cout <<setw(40)<< "instructor has zero clients" << endl;
cout << setw(34)<<"stipend is 8000" << endl;
}
}
}
if (flag == 0)
cout << "invalid ID entered" << endl;
}
void instructors_list()
{
ifstream read("instructors.txt");
if (read.fail())
{
cout << "unable to open file for reading" << endl;
return;
}
cout << setw(5) << "ID" << setw(20) << "number of clients" << endl;
while (read >> ID >> numberofclients)
{
cout << setw(6) << ID << setw(10) << numberofclients << endl;
}
read.close();
cout << endl;
}
void display()
{
cout << "instructor salary is:" << e << endl;
}
};
int main()
{
ifstream openf("instructors.txt");
if(openf){}
else
{
ofstream wf("instructors.txt");
wf.close();
}
openf.close();
int ch;
while (1)
{
cout << "1:create member" << endl;
cout << "2:renew membership" << endl;
cout << "3:change facility" << endl;
cout << "4:cancel membership" << endl;
cout << "5:instructor's list" << endl;
cout << "6:calculate instructor salary" << endl;
cout << "7:EXIT" << endl;
cin >> ch;
client cli;
instructor *cl = &cli;
expense E;
char *a = new char;
ofstream add("output.txt", ios::app);
string s;
switch (ch)
{
case 1:
cl->set();
cout << "Choose desired facility from following:" << endl;
cout << "1:weights 2:zumba 3:mma 4:cardio" << endl;
fflush(stdin);
cin.getline(a, 8);
cout << "your gym fees are:" << cl->calculate(a) << endl;
delete a;
add << cli;
add.close();
break;
case 2:
cout << "enter your name" << endl;
fflush(stdin);
cin.getline(a, 30);
s = a;
delete a;
dynamic_cast<client *>(cl)->renew_membership(s);
break;
case 3:
dynamic_cast<client *>(cl)->alter(1);
break;
case 4:
cout << "do you really want to cancel your membership?enter 1 if yes else 0" << endl;
cin >> ch;
if (ch == 1)
dynamic_cast<client *>(cl)->alter(2);
break;
case 5:
cl = &E;
dynamic_cast<expense *>(cl)->instructors_list();
break;
case 6:
cl = &E;
dynamic_cast<expense *>(cl)->salary();
break;
case 7:
exit(1);
break;
default:
cout << "Enter a valid option" << endl;
}
}
return 0;
}