-
Notifications
You must be signed in to change notification settings - Fork 0
/
Approach 1.cpp
133 lines (111 loc) · 4.16 KB
/
Approach 1.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
// in this approach i will try to solve the problem in the brute force way which is the way
// we use in our daily life which is simple to understand and implement
// so what we do here is that if a person A has given the money in place of some group of people
// then he should get that amount from that group of people so the balance should be
// (- amount paid) similarly the amount for each is added in their accounts as +ve value
#include <bits/stdc++.h>
using namespace std;
class Manager {
public:
string name;
double balance;
Manager(string name_) {
name = name_;
balance = 0;
}
void addAmount(double amount) {
balance += amount;
}
void subtractAmount(double amount) {
balance -= amount;
}
double getBalance() const {
return balance;
}
};
int main() {
cout << "Welcome to the program: " << endl << endl;
cout << "How many people are there in your group whose expenses you want to manage: ";
int number_of_people;
cin >> number_of_people;
cout << endl;
vector<Manager> people;
for (int i = 0; i < number_of_people; ++i) {
cout << "Enter the name of person " << i + 1 << ": ";
string name;
cin >> name;
people.push_back(Manager(name));
}
cout << endl;
cout << "Accounts for " << number_of_people << " people have been created successfully!" << endl << endl;
while (true) {
cout << "Functionalities: " << endl;
cout << "1. Add Transaction" << endl;
cout << "2. Remove a Transaction" << endl;
cout << "3. End Current Session" << endl;
cout << "4. End and Settle Up" << endl;
int options;
cout << "Choose a functionality that you want to use: ";
cin >> options;
if (options == 1) {
cout << "Enter the name of the person who paid: ";
string payer;
cin >> payer;
cout << "Enter the amount paid: ";
double amount;
cin >> amount;
cout << "Enter the number of people sharing this expense (including the payer): ";
int share_count;
cin >> share_count;
vector<string> participants;
cout << "Enter the names of the participants: ";
for (int i = 0; i < share_count; i++) {
string participant;
cin >> participant;
participants.push_back(participant);
}
double individual_share = amount / share_count;
for (Manager &person : people) {
if (person.name == payer) {
person.subtractAmount(amount);
}
for (const string &participant : participants) {
if (person.name == participant) {
person.addAmount(individual_share);
}
}
}
cout << "Transaction added successfully!" << endl << endl;
}
else if (options == 2) {
// will make it
cout << "Remove Transaction functionality is not implemented in this example." << endl << endl;
}
else if (options == 3) {
// will make it
cout << "Ending current session. All data will be preserved for future use." << endl;
break;
}
else if (options == 4) {
// End and settle up
cout << "Settling up..." << endl;
for (Manager &person : people) {
if (person.getBalance() > 0) {
cout << person.name << " should receive " << person.getBalance() << " units." << endl;
}
else if (person.getBalance() < 0) {
cout << person.name << " owes " << -person.getBalance() << " units." << endl;
}
else {
cout << person.name << " is settled up." << endl;
}
}
cout << "All transactions settled. Ending session." << endl;
break;
}
else {
cout << "Invalid option. Please try again." << endl << endl;
}
}
return 0;
}