-
Notifications
You must be signed in to change notification settings - Fork 0
/
dummy.cpp
416 lines (376 loc) · 12.1 KB
/
dummy.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
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
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class member_node
{
public:
// Data Members: Name, Phone_no, Cost
string member_name;
string phone_no;
float cost;
member_node *next; // node for users
member_node *owes; // node for storing cost to be given to other user
member_node(string name, string ph)
{ // constructor to initializing default setting of a node
member_name = name;
phone_no = ph;
cost = 0;
next = NULL;
owes = NULL;
}
};
bool isValidPhoneNumber(string phoneNumber)
{
int length = phoneNumber.length();
int digitCount = 0;
// Use traditional for loop instead of range-based for loop
for (int i = 0; i < length; ++i)
{
char c = phoneNumber[i];
if (isdigit(c))
{
++digitCount;
}
else if (c != '+' && c != '-' && c != ' ')
{
return false; // Invalid character found
}
}
// Check if the phone number has exactly 10 digits (excluding symbols)
return (digitCount == 10);
}
// class for all collection of functions
class group
{
protected:
member_node *head = NULL;
member_node *temp = NULL;
public:
void create_group();
void display_group();
int total_Members();
};
// Function to create users of one group only
void group::create_group()
{
int yes = 1, i = 1; // 'yes' for checking if we have more nodes to add and 'i' is total number of user
cout << "\n-----------------------------\n";
cout << " CREATE A NEW GROUP \n";
cout << "-----------------------------\n";
while (yes == 1)
{
// Taking input from user
string name;
string ph;
cout << "\nMember " << i << " : " << endl;
cout << "Name: ";
cin >> name;
cout << "Phone No.: ";
cin >> ph;
while (!isValidPhoneNumber(ph))
{
cout << "Enter 10 digit phone number.\n";
cout << "Phone No.: ";
cin >> ph;
}
// creating a new node
member_node *newnode = new member_node(name, ph);
if (head == NULL)
{
head = newnode; // If their is no user then newnode is directly initialized to head
}
else
{
temp = head; // temp node pointing to head
while (temp->next != NULL)
{ // loop to reach at last node (temp will point to last node)
temp = temp->next;
}
temp->next = newnode; // Adding newnode(user) to last node
}
cout << "\nAdd more members? \n1: Yes\n0: No\nENTER HERE: ";
cin >> yes;
i++; // Addition of users till yes becomes zero
}
member_node *temp = head; // here temp for iterating user
member_node *j = head; // j to point the current node in which we need to add owes
while (--i)
{
member_node *temp_owes = j; // temp_owes to iterate over owes
while (temp != NULL)
{
if (j->member_name != temp->member_name)
{
string n = j->member_name + " owes to " + temp->member_name; // string in form "A owes to B"
member_node *newowes = new member_node(n, temp->phone_no); // make newnode with the name as the above string
temp_owes->owes = newowes; // Add newnode to current temp_owes
temp_owes = temp_owes->owes; // temp_owes points to last node
temp = temp->next; // goes to next user(this node "temp" is needed because we want to add name of member in string "n")
}
else
{
temp = temp->next; // "A owes to A" does make sense so when "same_name owes to same_name" then we are ignoring it
}
}
temp = head; // Pointing temp to head so in next iteration it will give us all user again
j = j->next; // j will go to next user and all the owes for next user will be added further
}
cout << "\n\nGroup successfully created!\n";
}
void group::display_group()
{
cout << "\n-----------------------------------" << endl;
cout << setw(20) << "GROUP EXPENSES SUMMARY" << endl;
cout << "-----------------------------------" << endl;
member_node *temp = head;
while (temp != NULL)
{
member_node *temp_owes = temp->owes;
while (temp_owes != NULL)
{
cout << left << setw(25) << temp_owes->member_name << "= " << fixed << setprecision(2) << temp_owes->cost << endl;
temp_owes = temp_owes->owes;
}
temp = temp->next;
}
cout << "-----------------------------------\n"
<< endl;
}
int group::total_Members()
{
int count = 1;
member_node *temp = head;
while (temp->next != NULL)
{
count++;
temp = temp->next;
}
return count;
}
class category_node
{
public:
string category;
float amt;
category_node *next;
category_node(string name, float amount)
{
category = name;
amt = amount;
next = NULL;
}
};
class manageExpenses : public group
{
public:
string who_paid;
float amount;
int flag;
string categoryName;
category_node *first = NULL; // Node for storing categories
void getdata()
{
cout << "\nWho paid the amount? : ";
cin >> who_paid;
cout << "How much was the transaction? : ";
cin >> amount;
cout << "What was the expense for (e.g., movie, grocery, etc.)? : ";
cin >> categoryName;
flag = 0;
category_node *t = first;
category_node *newnode = new category_node(categoryName, amount);
if (first == NULL)
{
first = newnode;
cout << "Inside first" << endl;
}
else
{
while (t != NULL)
{
cout << t->category << " : " << categoryName << endl;
if (t->category == categoryName)
{
t->amt += amount;
flag = 1;
cout << "1" << endl;
break;
}
t = t->next;
}
if (flag != 1)
{
t = first;
while (t->next != NULL)
{
t = t->next;
}
t->next = newnode;
cout << "Done" << endl;
}
}
cout << "OK" << endl;
}
void split(string who_paid, float amount, string m1);
void split(string who_paid, float amount);
};
void manageExpenses::split(string who_paid, float amount, string m1)
{
member_node *temp = head;
int remaining = amount;
while (temp != NULL)
{
if (temp->member_name != who_paid)
{
member_node *temp_owes = temp; // goes to owes node for the given user node which here we named as "temp"
float how_Much_to_Other;
cout<<"How much "<<temp_owes->member_name<<" will pay ? : ";
cin>>how_Much_to_Other;
if(how_Much_to_Other > remaining){
cout<<"You entered amount greater than remaining amount\nEnter value again : ";
cin>>how_Much_to_Other;
}
string n = temp_owes->member_name + " owes to " + who_paid;
while (temp_owes != NULL)
{
if (temp_owes->member_name == n)
{
temp_owes->cost += how_Much_to_Other;
remaining -= how_Much_to_Other;
cout<<"Amount remaining : "<<remaining<<endl;
}
temp_owes = temp_owes->owes;
}
}
temp = temp->next;
}
cout<<"Expense Added"<<endl;
}
void manageExpenses::split(string who_paid, float amount)
{
member_node *temp = head;
int member_count = total_Members();
float divideEqually = amount / member_count;
while (temp != NULL)
{
if (temp->member_name != who_paid)
{
member_node *temp_owes = temp; // goes to owes node for the given user node which here we named as "temp"
while (temp_owes != NULL)
{
string n = temp->member_name + " owes to " + who_paid;
if (temp_owes->member_name == n)
{
temp_owes->cost += divideEqually;
}
temp_owes = temp_owes->owes;
}
}
temp = temp->next;
}
cout<<"Expense Added"<<endl;
}
class analysis : public manageExpenses
{
public:
float group_Expenditure();
void categorical_Cost();
};
float analysis::group_Expenditure()
{
category_node *temp = first;
float total = 0;
while (temp != NULL)
{
total += temp->amt;
temp = temp->next;
}
return total;
}
void analysis::categorical_Cost()
{
category_node *temp = first;
float t = group_Expenditure();
cout << setw(20) << "Categories" << setw(20) << "Cost Spend" << setw(20) << "Percentage" << endl;
while (temp != NULL)
{
cout << setw(20) << temp->category << setw(20) << temp->amt << setw(20) << (temp->amt * 100) / t << endl;
temp = temp->next;
}
}
int main()
{
analysis A;
int operation = 100;
while (operation)
{
cout << "\n\n-----------------------------------\n";
cout << setw(25) << "EXPENSE EASE" << endl;
cout << setw(27) << "Your expense tracker" << endl;
cout << "------------------------------------\n";
cout << "Enter the operation you want to perform\n";
cout << "1. Create Group" << endl;
cout << "2. Display Group" << endl;
cout << "3. Add Expense" << endl;
cout << "Analysis" << endl;
cout << "4. Display Groups Expenditure till now" << endl;
cout << "5. Categorical Analysis" << endl;
cout << "EXIT = 0" << endl;
cout << "Enter the number given in front of the operation: ";
cin >> operation;
switch (operation)
{
case 0:
cout << "PROGRAM ENDED" << endl;
break;
case 1:
A.create_group();
break;
case 2:
A.display_group();
break;
case 3:
{
A.getdata(); // Gather who_paid and amount inside the object
int choice;
cout << "\nDo you want to split equally or unequally?" << endl;
cout << "1. Equally" << endl;
cout << "2. Unequally (between two members)" << endl;
cout << "Enter choice: ";
cin >> choice;
while(choice < 1 && choice > 2){
cout << "Invalid choice. Try entering choice again" << endl;
cin >> choice;
}
if (choice == 1)
{
A.split(A.who_paid, A.amount); // Pass the member variables as arguments
}
else if (choice == 2)
{
A.split(A.who_paid, A.amount, "unequal"); // Pass the member variables and the specific member name
}
else
{
cout << "Invalid choice." << endl;
}
break;
}
case 4:
cout << A.group_Expenditure() << endl;
break;
case 5:
A.categorical_Cost();
cout << endl;
break;
case 6:
cout << "\nPROGRAM ENDED" << endl;
break;
default:
cout << "\nInvalid operation. Please try again." << endl;
break;
}
}
return 0;
}