-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplitwise.cpp
More file actions
584 lines (564 loc) · 17.8 KB
/
Copy pathsplitwise.cpp
File metadata and controls
584 lines (564 loc) · 17.8 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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
#include<iostream>
#include<fstream>
#include<conio.h>
using namespace std;
//Global Variables
char members[30] , group_name[30];
string GROUP , checkName;
int num_of_GroupMembers=0;
bool GroupAvailable = false;
//Function prototypes
int createAccount();
int createGroups();
int AddExpenses();
void DisplayDetails();
int Menu();
int choice();
int settleUp();
void clearScreen();
bool loggedIn();
bool checkGroup();
int stringLength(string);
int main()
{
int selectOperation , checkSignUp;
bool checkLogin;
while(true) //this loop runs for sign up and login
{
int select = choice();
if(select == 1)
{
checkSignUp = createAccount();
if(checkSignUp == 0)
checkLogin = false;
else
{
checkLogin = true;
break;
}
}
else if(select == 2)
{
checkLogin = loggedIn();
if(checkLogin)
break;
}
else
{
cout<<"\nInvalid choice!"<<endl<<endl;
}
}
clearScreen();
if(checkLogin) //if login or signup is true then this loop works for operations for the user
{
while(true)
{
selectOperation = Menu();
switch(selectOperation)
{
case 1:
createGroups();
break;
case 2:
AddExpenses();
break;
case 3:
settleUp();
break;
case 4:
DisplayDetails();
break;
case 5:
cout<<"\nThanks for using the application!"<<endl<<endl;
return 0;
default:
cout<<"Invalid choice! Enter between (1-5)!"<<endl;
break;
}
clearScreen();
}
}
return 0;
}
//This function is to check if the user has an account or not when user tries to log in
bool loggedIn()
{
string check ,phone , checkPhone , name , password ,checkPass;
cout<<"Enter Your name: "<<endl;
cin.ignore();
getline(cin , name);
checkName = name;
cout<<"Enter phone number: "<<endl;
getline(cin , phone);
cout<<"Enter password: "<<endl;
cin>>password;
cin.ignore();
ifstream fin;
fin.open("Accounts.txt" , ios::in);
if(!fin)
{
cout<<"File not found!"<<endl;
return 0;
}
while(!fin.eof()) //This loops runs till the end of file
{
getline(fin , checkName);
if(stringLength(checkName) == 0)
continue;
getline(fin , checkPhone);
getline(fin , checkPass);
if(checkName == name && phone == checkPhone && password == checkPass)
{
cout<<"Welcome "<<name<<"!"<<endl;
return true;
}
}
cout<<"Account does not Exist! Please signup!"<<endl;
return false;
}
//This function is for signup
int createAccount()
{
string checkPhone, checkNamefromFile , checkPass;
string phone , name , password;
cout<<"Enter Your name: "<<endl;
cin.ignore();
getline(cin , name);
checkName = name;
cout<<"Enter phone number: "<<endl;
getline(cin , phone);
cout<<"Enter password: "<<endl;
cin>>password;
cin.ignore();
ifstream fin;
ofstream createFile;
createFile.open("Accounts.txt" , ios::out);
createFile.close();
fin.open("Accounts.txt" , ios::in);
while(!fin.eof()) //This loop checks if there is already an account with the same details as user sign up
{
getline(fin , checkNamefromFile);
if(stringLength(checkNamefromFile) == 0)
continue;
getline(fin , checkPhone);
getline(fin , checkPass);
if(name == checkNamefromFile)
{
cout<<"Name not Available!"<<endl;
return 0;
}
else if(checkPhone == phone)
{
cout<<"Phone number not Available!"<<endl;
return 0;
}
else if(checkPass == password)
{
cout<<"Password not Available!"<<endl;
return 0;
}
}
ofstream fout;
fout.open("Accounts.txt" , ios::app);
fout<<endl<<name<<endl<<phone<<endl<<password<<endl;
cout<<"Your Account has been opened!"<<endl;
cout<<"Welcome "<<name<<"!"<<endl;
fout.close();
return 1;
}
int createGroups() //This function creates the group for a particular user
{
float individual_expense = 0;
string name;
cout<<"\nEnter name of the Group: "<<endl;
cin.ignore();
getline(cin , name);
int create=1;
ofstream fout;
fout.open("groups.txt",ios::app);
fout<<name<<endl;
cout<<"Enter the number of group members: ";
cin>>num_of_GroupMembers;
fout<<num_of_GroupMembers<<endl;
cout<<"Enter name of members of the group: ";
cin.ignore();
while(create<=num_of_GroupMembers) //This loop add members to a group upto user's choice
{
cin.getline(members , 30);
fout<<members<<endl<<individual_expense<<endl;
create++;
}
fout<<endl<<endl;
fout.close();
cout<<"Group has been succesfully created!"<<endl;
return 1;
}
int AddExpenses() //this function adds expenses to the group entered by the user
{
GroupAvailable = false;
bool check = false;
int k , counter=0;
string match, match1 , oneLine , Group_Name , person;
float splitted,temp;
ifstream fin;
ofstream fout;
ifstream fin2;
char name[30];
float expense , sum=0;
int num_of_members , split , userID;
int ID[] = {0,1,2,3,4,5,6,7,8,9};
fin.open("groups.txt" , ios::in);
if(!fin)
{
cout<<"File not found!"<<endl;
return 0;
}
if(!checkGroup()) //This checks if the user is in the group entered or not / there is a group in the file or not
{
cout<<"Group not found! It may not be created by you!"<<endl;
return 0;
}
cout<<"Enter the name of person who paid the bill: ";
getline(cin , person);
cout<<"Enter the total expense: ";
cin>>expense;
cout<<"Enter the split option: "<<endl;
cout<<"1. Equal split"<<endl;
cout<<"2. Unequal split"<<endl;
cin>>split;
cin.ignore();
fout.open("tempfile.txt", ios::out);
while(getline(fin , Group_Name))
{
if(stringLength(Group_Name) == 0)
continue;
if(Group_Name == GROUP) //If group entered by user matches with group in the file and user can perform further operations
{
fin>>num_of_GroupMembers;
string Members[num_of_GroupMembers];
int Expenses[num_of_GroupMembers];
fin.ignore();
if(split == 1) //if split is equal
{
cout<<"Enter the total members who will pay the bill: ";
cin>>num_of_members;
if(num_of_members<=0 || num_of_members>num_of_GroupMembers)
{
cout<<"Invalid input for number of members!"<<endl;
return 0;
}
cin.ignore();
fout<<Group_Name<<endl;
fout<<num_of_GroupMembers<<endl;
for(int j=0 ; j<num_of_GroupMembers ; j++)
{
getline(fin , Members[j]);
fin>>Expenses[j];
Expenses[j] = 0;
fin.ignore();
}
for(int l=0 ; l<num_of_GroupMembers ; l++)
{
cout<<"Name: "<<Members[l]<<" ID: "<<ID[l]<<endl; //Shows name of members with their ID's
}
for(int j=0 ; j<num_of_members ; j++)
{
check = false;
cout<<"Enter the ID of Member "<<Members[j]<<": ";
cin>>userID;
cin.ignore();
for(k=0 ; k<num_of_GroupMembers ; k++)
{
if(userID == ID[k])
{
splitted = expense/num_of_members;
Expenses[k] += splitted;
check = true;
break;
}
}
if(check == false)
{
cout<<"Member not found!"<<endl;
j--;
}
}
for(int k=0 ; k<num_of_GroupMembers ; k++)
{
fout<<Members[k]<<endl;
fout<<Expenses[k]<<endl;
}
fout<<"The members have to pay money to "<<person<<endl;
fout<<endl<<endl;
}
else if(split == 2) //If split is unequal
{
cout<<"Enter the total members who will pay the bill: ";
cin>>num_of_members;
if(num_of_members<=0 || num_of_members>num_of_GroupMembers)
{
cout<<"Invalid input for number of members!"<<endl;
return 0;
}
cin.ignore();
fout<<Group_Name<<endl;
fout<<num_of_GroupMembers<<endl;
for(int j=0 ; j<num_of_GroupMembers ; j++)
{
getline(fin , Members[j]);
fin>>Expenses[j];
Expenses[j] = 0;
fin.ignore();
}
for(int l=0 ; l<num_of_GroupMembers ; l++)
{
cout<<"Name: "<<Members[l]<<" ID: "<<ID[l]<<endl;
}
for(int j=0 ; j<num_of_members && sum<expense ; j++)
{
check = false;
cout<<"Enter the ID of Member "<<Members[j]<<": ";
cin>>userID;
cin.ignore();
for(k=0 ; k<num_of_GroupMembers ; k++)
{
if(userID == ID[k])
{
cout<<"Enter the amount to be paid by Member "<<Members[k]<<": ";
cin>>splitted;
if((sum+splitted)>expense)
{
splitted = expense-sum;
}
sum += splitted;
cin.ignore();
Expenses[k] += splitted;
check = true;
break;
}
}
if(check == false)
{
cout<<"Member not found!"<<endl;
j--;
}
}
for(int k=0 ; k<num_of_GroupMembers ; k++)
{
fout<<Members[k]<<endl;
fout<<Expenses[k]<<endl;
}
fout<<"The members have to pay money to "<<person<<endl;
fout<<endl<<endl;
}
counter++;
}
else{ //All groups will remain as it is in the file
fin>>num_of_GroupMembers;
fin.ignore();
int Expenses[num_of_GroupMembers];
fout<<Group_Name<<endl;
fout<<num_of_GroupMembers<<endl;
for(int i=0 ; i<num_of_GroupMembers; i++)
{
fin.getline(members , 30);
fin>>Expenses[i];
fout<<members<<endl;
fout<<Expenses[i]<<endl;
fin.ignore();
}
getline(fin , oneLine);
fout<<oneLine<<endl;
fout<<endl<<endl;
}
getline(fin , oneLine);
}
if(counter == 0)
{
cout<<"\nGroup Not Found!"<<endl;
return 0;
}
fin.close();
fout.close();
remove("groups.txt"); //This function deletes the file
rename("tempfile.txt" , "groups.txt"); //This function renames the temp file to deleted file name
cout<<"\nExpense has been added! Operation Successful!"<<endl;
return 1;
}
void DisplayDetails() //this function displays all the details of groups of user
{
int counter=0;
cout<<endl<<"All Groups Details!"<<endl<<endl;
string oneLine , Group_Name;
ifstream fin;
fin.open("groups.txt" , ios::in);
if(!fin)
{
cout<<"File not found!"<<endl;
return;
}
while(!fin.eof())
{
bool checkAllGroups = false;
getline(fin , Group_Name);
if(stringLength(Group_Name) == 0)
continue;
fin>>num_of_GroupMembers;
int Expenses[num_of_GroupMembers];
string Members[num_of_GroupMembers];
fin.ignore();
for(int i=0 ; i<num_of_GroupMembers ; i++)
{
getline(fin , Members[i]);
fin>>Expenses[i];
fin.ignore();
}
getline(fin , oneLine);
for(int i=0 ; i<num_of_GroupMembers ; i++) //This loop checks if user is in the group or not
{
if(checkName == Members[i])
{
checkAllGroups = true;
break;
}
}
if(checkAllGroups) //If user is in the group then it's details will be displayed
{
cout<<"GROUP: "<<Group_Name<<endl;
cout<<"MEMBERS: "<<num_of_GroupMembers<<endl;
for(int i=0 ; i<num_of_GroupMembers ; i++)
{
cout<<"Name: "<<Members[i]<<" Expense: "<<Expenses[i]<<endl;
}
cout<<oneLine;
counter++;
}
cout<<endl<<endl;
}
if(counter==0)
{
cout<<"No groups found!"<<endl;
}
}
int choice()
{
int choice;
cout<<"\nWelcome to the Expense-sharing APP!"<<endl;
cout<<"1. Sign Up"<<endl;
cout<<"2. Login"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
return choice;
}
int Menu() //Menu function
{
int choice;
cout<<"\nYou can perform the following operations!"<<endl;
cout<<"1. Create Groups"<<endl;
cout<<"2. Add Expenses"<<endl;
cout<<"3. Settle up"<<endl;
cout<<"4. Display Expense Details"<<endl;
cout<<"5. Log out"<<endl;
cout<<"NOTE: You need to create a group first for other operations!"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
return choice;
}
int settleUp() //this function displays the details of particular group entered by the user
{
GroupAvailable = false;
string oneLine , Group_Name , searchGroup , MEMBERS;
float expense;
if(!checkGroup())
{
cout<<"Group not found! It may not be created by you!"<<endl;
return 0;
}
ifstream fin;
fin.open("groups.txt" , ios::in);
if(!fin)
{
cout<<"File not found!"<<endl;
return 0;
}
while(getline(fin , searchGroup))
{
if(stringLength(searchGroup) == 0)
continue;
if(searchGroup == GROUP)
{
fin>>num_of_GroupMembers;
float Expenses[num_of_GroupMembers];
string Members[num_of_GroupMembers];
fin.ignore();
cout<<"\nGroup: "<<GROUP<<endl;
cout<<"MEMBERS: "<<num_of_GroupMembers<<endl;
for(int i=1 ; i<=num_of_GroupMembers ; i++)
{
getline(fin , MEMBERS);
fin>>expense;
fin.ignore();
cout<<"Name: "<<MEMBERS<<" Expense: "<<expense<<endl;
}
getline(fin , oneLine);
cout<<oneLine<<endl;
}
}
fin.close();
return 0;
}
void clearScreen()
{
cout<<"\nPress Any Key to Continue.....\n";
getch();
system("CLS");
}
bool checkGroup() //this function checks if the group entered by the user is accessible to the user or not
{
ifstream fin;
string oneLine , Group_Name , searchGroup;
cout<<"Enter the name of the Group: ";
cin.ignore();
getline(cin , Group_Name);
GROUP = Group_Name;
fin.open("groups.txt" , ios::in);
if(!fin)
{
cout<<"File not found!"<<endl;
return 0;
}
while(getline(fin , searchGroup))
{
if(stringLength(searchGroup) == 0)
continue;
if(Group_Name == searchGroup)
{
fin>>num_of_GroupMembers;
float Expenses[num_of_GroupMembers];
string Members[num_of_GroupMembers];
fin.ignore();
for(int j=0 ; j<num_of_GroupMembers ; j++)
{
getline(fin , Members[j]);
fin>>Expenses[j];
fin.ignore();
}
for(int j=0 ; j<num_of_GroupMembers ; j++) //This loops verifies if group is accessible by the user or not
{
if(Members[j] == checkName)
{
cout<<"Group found!"<<endl;
return true;
}
}
}
}
fin.close();
return false;
}
int stringLength(string STRING) //This function finds the string length
{
int length;
for(length=0 ; STRING[length]!='\0' ; length++);
return length;
}