-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19dec2019
316 lines (278 loc) · 7.77 KB
/
19dec2019
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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
#define COMMAND_HELP 0
#define COMMAND_EXIT 1
#define COMMAND_RECORDEXPENSE 2
#define COMMAND_GENERATECLAIM 3
#define COMMAND_PRINTCATINFO 4
#define COMMAND_INVALID 7
#define MAXNO 50
class info {
public:
float amt;
char claimable;
string desc;
};
vector<info> lunch, dinner, otherFood, transport, misc;
//TODO: currently, file is only reading and not appending. need to fix
fstream lunchfp ("lunch_dec19.txt", ios_base::in | ios_base::out | ios_base::app);
fstream dinnerfp ("dinner_dec19.txt", ios_base::app | ios_base::in);
fstream otherFoodfp ("otherFood_dec19.txt", ios_base::app | ios_base::in);
fstream transportfp ("transport_dec19.txt", ios_base::app | ios_base::in);
fstream miscfp ("misc_dec19.txt", ios_base::app | ios_base::in);
//interactive functions
void record_expense();
void calc_claim();
void pre_printInfo();
void printInfo(vector<info>, string);
//utility functions
void read_fdata();
int parse_command();
int check_command(string);
void print_help();
int main(){
read_fdata();
cout << "Welcome to the scrappy claims calculator!" << endl;
cout << "Type \"help\" for more information..." << endl;
parse_command();
return 0;
}
void record_expense() {
string str;
while (1) {
cout << "\nTypes of expenses:\n- lunch\n- dinner\n- other food\n- transport\n- misc\n" << endl;
cout << "Enter type of expense: ";
cin >> str;
if (str[0] == 'l') {
lunch.push_back(info());
int n = lunch.size()-1;
cout << "Enter lunch amount: ";
cin >> lunch[n].amt;
cout << "Is this lunch claimable? ";
cin >> lunch[n].claimable;
cout << "Enter description (optional): "; //but this is only reading in 1 word
cin >> lunch[n].desc;
lunchfp << lunch[n].amt << " " << lunch[n].claimable << " " << lunch[n].desc << endl;
break;
}
else if (str[0] == 'd') {
dinner.push_back(info());
int n = dinner.size()-1;
cout << "Enter dinner amount: ";
cin >> dinner[n].amt;
cout << "Is this dinner claimable? ";
cin >> dinner[n].claimable;
cout << "Enter description (optional): ";
cin >> dinner[n].desc;
dinnerfp << dinner[n].amt << " " << dinner[n].claimable << " " << dinner[n].desc << endl;
break;
}
else if (str[0] == 'o') {
otherFood.push_back(info());
int n = otherFood.size()-1;
cout << "Enter other food amount: ";
cin >> otherFood[n].amt;
cout << "Is this other food claimable? ";
cin >> otherFood[n].claimable;
cout << "Enter description (optional): ";
cin >> otherFood[n].desc;
otherFoodfp << otherFood[n].amt << " " << otherFood[n].claimable << " " << otherFood[n].desc << endl;
break;
}
else if (str[0] == 't') {
transport.push_back(info());
int n = transport.size()-1;
cout << "Enter transport amount: ";
cin >> transport[n].amt;
cout << "Is this transport claimable? ";
cin >> transport[n].claimable;
cout << "Enter description (optional): ";
cin >> transport[n].desc;
transportfp << transport[n].amt << " " << transport[n].claimable << " " << transport[n].desc << endl;
break;
}
else if (str[0] == 'm') {
misc.push_back(info());
int n = misc.size()-1;
cout << "Enter misc amount: ";
cin >> misc[n].amt;
cout << "Is this misc claimable? ";
cin >> misc[n].claimable;
cout << "Enter description (optional): ";
cin >> misc[n].desc;
miscfp << misc[n].amt << " " << misc[n].claimable << " " << misc[n].desc << endl;
break;
}
else cout << "Invalid expense type!" << endl;
}
cout << "Success! Expense recorded." << endl;
}
//calculate total claim
void calc_claim() {
int count = 0; float totalClaim = 0;
for (int i = 0, n = lunch.size(); i < n; i++) if (lunch[i].claimable == 'y') count++;
for (int i = 0, n = dinner.size(); i < n; i++) if (dinner[i].claimable == 'y') count++;
for (int i = 0, n = otherFood.size(); i < n; i++) if (otherFood[i].claimable == 'y') count++;
for (int i = 0, n = transport.size(); i < n; i++) if (transport[i].claimable == 'y') totalClaim += transport[i].amt;
for (int i = 0, n = misc.size(); i < n; i++) if (misc[i].claimable == 'y') totalClaim += misc[i].amt;
totalClaim += count*8;
cout << "Total claim amount: " << totalClaim << endl;
}
//print info by category
void pre_printInfo() {
int i; string str;
while (1) {
cout << "\nInfo categories:\n- lunch\n- dinner\n- other food\n- transport\n- misc\n" << endl;
cout << "Which category would you like to print? " << endl;
cin >> str;
if (str[0] == 'l') {
printInfo(lunch, "lunch");
break;
}
else if (str[0] == 'd') {
printInfo(dinner, "dinner");
break;
}
else if (str[0] == 'o') {
printInfo(otherFood, "otherFood");
break;
}
else if (str[0] == 't') {
printInfo(transport, "transport");
break;
}
else if (str[0] == 'm') {
printInfo(misc, "misc");
break;
}
else cout << "Invalid category type!" << endl;
}
}
void printInfo(vector<info> var, string str) {
cout << "\nPrinting " << str << " info..." << endl;
for (int i = 0, n = var.size(); i < n; i++) {
cout << var[i].amt << " " << var[i].claimable << " " << var[i].desc << endl;
}
cout << endl;
}
void read_fdata() {
for (int i = 0; i < MAXNO; i++) {
lunch.push_back(info());
lunchfp >> lunch[i].amt;
if (lunch[i].amt == 0) {
lunch.pop_back();
break;
}
lunchfp >> lunch[i].claimable;
lunchfp >> lunch[i].desc;
}
for (int i = 0; i < MAXNO; i++) {
dinner.push_back(info());
dinnerfp >> dinner[i].amt;
if (dinner[i].amt == 0) {
dinner.pop_back();
break;
}
dinnerfp >> dinner[i].claimable;
dinnerfp >> dinner[i].desc;
}
for (int i = 0; i < MAXNO; i++) {
otherFood.push_back(info());
otherFoodfp >> otherFood[i].amt;
if (otherFood[i].amt == 0) {
otherFood.pop_back();
break;
}
otherFoodfp >> otherFood[i].claimable;
otherFoodfp >> otherFood[i].desc;
}
for (int i = 0; i < MAXNO; i++) {
transport.push_back(info());
transportfp >> transport[i].amt;
if (transport[i].amt == 0) {
transport.pop_back();
break;
}
transportfp >> transport[i].claimable;
transportfp >> transport[i].desc;
}
for (int i = 0; i < MAXNO; i++) {
misc.push_back(info());
miscfp >> misc[i].amt;
if (misc[i].amt == 0) {
misc.pop_back();
break;
}
miscfp >> misc[i].claimable;
miscfp >> misc[i].desc;
}
}
//parse_command works in a REPL style.
int parse_command() {
string tmp_command;
int command_code;
while (1) {
cout << "Waiting for command..." << endl;
cin >> tmp_command;
command_code = check_command(tmp_command);
if (command_code == COMMAND_HELP) {
print_help();
}
else if (command_code == COMMAND_EXIT) {
cout << "See you!" << endl;
lunchfp.close();
dinnerfp.close();
otherFoodfp.close();
transportfp.close();
miscfp.close(); //might want to throw all these into a single close fn
break;
}
else if (command_code == COMMAND_RECORDEXPENSE) {
record_expense();
}
else if (command_code == COMMAND_GENERATECLAIM) {
calc_claim();
}
else if (command_code == COMMAND_PRINTCATINFO) {
pre_printInfo();
}
else {
cout << "No such command: "<< tmp_command << ", please input command again!" << endl;
}
}
return 0;
}
//This function takes a command as input and returns an integer as output.
/* INPUT OUTPUT
help 0
exit 1
recordExpense 2
generateClaim 3
printCatInfo 4
*/
int check_command(string input) {
if (input == "help") {
return COMMAND_HELP;
}
else if (input == "exit") {
return COMMAND_EXIT;
}
else if (input[0] == 'r') {
return COMMAND_RECORDEXPENSE;
}
else if (input[0] == 'g') {
return COMMAND_GENERATECLAIM;
}
else if (input[0] == 'p') {
return COMMAND_PRINTCATINFO;
}
else {
return COMMAND_INVALID;
}
}
void print_help() {
cout << "Commands available: recordExpense, generateClaim, printCatInfo, help, exit" << endl;
}