-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit_service.cpp
More file actions
150 lines (144 loc) · 4.4 KB
/
split_service.cpp
File metadata and controls
150 lines (144 loc) · 4.4 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
#include "split_service.h"
void SplitService::set_user_table(string user_id, User* t)
{
user_table[user_id] = t;
users.push_back(user_id);
}
void SplitService::EXPENSE_EQUAL(int total, string payer, vector <string> table)
{
int count = table.size();
int per_share = total/count;
unordered_map <string, User*>::iterator itr = user_table.find(payer);
if (itr == user_table.end())
{
cout << "no such user exists!! " << endl;
return;
}
else
{
int row = itr->second->get_seq();
for (int i = 0; i < count; i++)
{
unordered_map <string, User*>::iterator itr1 = user_table.find(table[i]);
if (itr1 != user_table.end())
{
int col = itr1->second->get_seq();
expense_table[row][col] = expense_table[row][col] + per_share;
expense_table[col][row] = expense_table[col][row] - per_share;
}
}
}
}
void SplitService::EXPENSE_EXACT(int total, string payer, vector <string> table, vector<int> amount)
{
int count = table.size();
int total_per = 0;
for (int i = 0; i < count; i++)
{
total_per += amount[i];
}
if (total_per != total)
{
cout << "invalid_entry!!!" << endl;
return;
}
unordered_map <string, User*>::iterator itr = user_table.find(payer);
if (itr == user_table.end())
{
cout << "no such user exists!! " << endl;
return;
}
else
{
int row = itr->second->get_seq();
for (int i = 0; i < count; i++)
{
unordered_map <string, User*>::iterator itr1 = user_table.find(table[i]);
if (itr1 != user_table.end())
{
int col = itr1->second->get_seq();
expense_table[row][col] = expense_table[row][col] + amount[i];
expense_table[col][row] = expense_table[col][row] - amount[i];
}
}
}
}
void SplitService::EXPENSE_PERCENT(int total, string payer, vector <string> table, vector<float> percent)
{
int count = table.size();
int total_per = 0;
for (int i = 0; i < count; i++)
{
total_per += percent[i];
}
if (total_per != 100)
{
cout << "invalid_entry!!!" << endl;
return;
}
unordered_map <string, User*>::iterator itr = user_table.find(payer);
if (itr == user_table.end())
{
cout << "no such user exists!! " << endl;
return;
}
else
{
int row = itr->second->get_seq();
for (int i = 0; i < count; i++)
{
float amount = float(percent[i]/100);
unordered_map <string, User*>::iterator itr1 = user_table.find(table[i]);
if (itr1 != user_table.end())
{
int col = itr1->second->get_seq();
expense_table[row][col] = expense_table[row][col] + (float(total) * amount);
expense_table[col][row] = expense_table[col][row] - (float(total) * amount);
}
}
}
}
void SplitService::SHOW_ALL()
{
int balance_exist = 0;
for (int i = 0; i<num_of_user; i++)
{
if (SHOW(users[i], 1) == 1)
balance_exist = 1;
}
if (balance_exist == 0)
cout << "No balances" << endl;
}
int SplitService::SHOW(string u, int all)
{
unordered_map <string, User*>::iterator itr = user_table.find(u);
int balance_exist = 0;
if (itr == user_table.end())
{
cout << "no such user exists!! " << endl;
return 0;
}
int seq_no = itr->second->get_seq();
for (int i = 0; i< num_of_user; i++)
{
if (i != seq_no)
{
if (expense_table[seq_no][i] != 0)
{
balance_exist = 1;
if (expense_table[seq_no][i] > 0)
{
cout << users[i] << " owes " << u << ": " << expense_table[seq_no][i] << endl;
}
else
{
if (all == 0)
cout << u << " owes " << users[i] << ": " << 0 - expense_table[seq_no][i] << endl;
}
}
}
}
if (balance_exist == 0 && (all == 0))
cout << "No balances" << endl;
return balance_exist;
}