diff --git a/a.out b/a.out new file mode 100755 index 0000000..a9697ee Binary files /dev/null and b/a.out differ diff --git a/driver.cpp b/driver.cpp new file mode 100644 index 0000000..17ad3fb --- /dev/null +++ b/driver.cpp @@ -0,0 +1,100 @@ +#include "split_service.h" + +int main() +{ + SplitService *ser = new SplitService(4); + + User *U1 = new User(0, "u1", "Susmita", "sm@com", "11890"); + ser->set_user_table("u1", U1); + + User *U2 = new User(1, "u2", "Ayan", "ay@com", "11890"); + ser->set_user_table("u2", U2); + + User *U3 = new User(2, "u3", "Moumita", "mm@com", "11890"); + ser->set_user_table("u3", U3); + + User *U4 = new User(3, "u4", "Subhayan", "sb@com", "11890"); + ser->set_user_table("u4", U4); + + int cont = 0; + do{ + cout << "Enter operation " << endl; + string op; + cin >> op; + if (op == "SHOW") + { + cout << "for a specific user?" << endl; + string ans; + cin >> ans; + if (ans == "YES" || ans == "Yes" || ans == "yes") + { + cout << "Enter user id" << endl; + string u; + cin >> u; + ser->SHOW(u, 0); + } + else + { + ser->SHOW_ALL(); + } + } + else if (op == "EXPENSE") + { + int num = 0; + string payer; + vector users; + string type; + int total; + cout << "Enter payer " << endl; + cin >> payer; + cout << "Enter total " << endl; + cin >> total; + cout << "Enter no of users " << endl; + cin >> num; + cout << "enter user_ids " << endl; + for (int i = 0; i> u; + users.push_back(u); + } + cout << "Enter type of expense, EXACT, EQUAL or PERCENT " << endl; + cin >> type; + if (type == "EQUAL") + ser->EXPENSE_EQUAL(total, payer, users); + else if (type == "EXACT") + { + cout << "Enter amounts " << endl; + vector amounts; + for (int i = 0; i> amount; + amounts.push_back(amount); + } + ser->EXPENSE_EXACT(total, payer, users, amounts); + } + else if (type == "PERCENT") + { + cout << "Enter percents " << endl; + vector percents; + for (int i = 0; i> percent; + percents.push_back(percent); + } + ser->EXPENSE_PERCENT(total, payer, users, percents); + } + else + cout << "INVALID TYPE" << endl; + } + else + cout << "INVALID OPERTAION " << endl; + + cout << "Press 1 to continue" << endl; + cin >> cont; + }while (cont == 1); + + return 0; +} diff --git a/output.txt b/output.txt new file mode 100644 index 0000000..b461780 --- /dev/null +++ b/output.txt @@ -0,0 +1,207 @@ +Enter operation +SHOW +for a specific user? +n +No balances +Press 1 to continue +1 +Enter operation +SHOW +for a specific user? +YES +Enter user id +u1 +No balances +Press 1 to continue +1 +Enter operation +EXPENSE +Enter payer +u2 +Enter total +1000 +Enter no of users +4 +enter user_ids +u1 u2 u3 u4 +Enter type of expense, EXACT, EQUAL or PERCENT +EQUAL +Press 1 to continue +1 +Enter operation +SHOW +for a specific user? +yes +Enter user id +u1 +u1 owes u2: 250 +Press 1 to continue +1 +Enter operation +SHOW +for a specific user? +yes +Enter user id +u2 +u1 owes u2: 250 +u3 owes u2: 250 +u4 owes u2: 250 +Press 1 to continue +1 +Enter operation +SHOW +for a specific user? +n +u1 owes u2: 250 +u3 owes u2: 250 +u4 owes u2: 250 +Press 1 to continue +1 +Enter operation +EXPENSE +Enter payer +u3 +Enter total +2000 +Enter no of users +3 +enter user_ids +u1 u2 u4 +Enter type of expense, EXACT, EQUAL or PERCENT +EXACT +Enter amounts +1000 500 500 +Press 1 to continue +1 +Enter operation +SHOW +for a specific user? +yes +Enter user id +u3 +u1 owes u3: 1000 +u2 owes u3: 250 +u4 owes u3: 500 +Press 1 to continue +1 +Enter operation +EXPENSE +Enter payer +u1 +Enter total +1000 +Enter no of users +1 +enter user_ids +u3 +Enter type of expense, EXACT, EQUAL or PERCENT +PERCENT +Enter percents +100 +Press 1 to continue +1 +Enter operation +SHOW +for a specific user? +yes +Enter user id +u1 +u1 owes u2: 250 +Press 1 to continue +1 +Enter operation +SHOW +for a specific user? +yes +Enter user id +u3 +u2 owes u3: 250 +u4 owes u3: 500 +Press 1 to continue +1 +Enter operation +EXPENSE +Enter payer +u2 +Enter total +250 +Enter no of users +1 +enter user_ids +u3 +Enter type of expense, EXACT, EQUAL or PERCENT +EXACT +Enter amounts +250 +Press 1 to continue +1 +Enter operation +SHOW +for a specific user? +n +u1 owes u2: 250 +u4 owes u2: 250 +u4 owes u3: 500 +Press 1 to continue +1 +Enter operation +EXPENSE +Enter payer +u4 +Enter total +750 +Enter no of users +2 +enter user_ids +u2 u3 +Enter type of expense, EXACT, EQUAL or PERCENT +PERCENT +Enter percents +25 70 +invalid_entry!!! +Press 1 to continue +1 +Enter operation +EXPENSE +Enter payer +u4 +Enter total +750 +Enter no of users +2 +enter user_ids +u2 u3 +Enter type of expense, EXACT, EQUAL or PERCENT +EXACT +Enter amounts +250 500 +Press 1 to continue +1 +Enter operation +SHOW +for a specific user? +n +u1 owes u2: 250 +Press 1 to continue +1 +Enter operation +EXPENSE +Enter payer +u1 +Enter total +250 +Enter no of users +1 +enter user_ids +u2 +Enter type of expense, EXACT, EQUAL or PERCENT +EQUAL +Press 1 to continue +1 +Enter operation +SHOW +for a specific user? +n +No balances +Press 1 to continue +0 diff --git a/split_service.cpp b/split_service.cpp new file mode 100644 index 0000000..fd89d32 --- /dev/null +++ b/split_service.cpp @@ -0,0 +1,150 @@ +#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 table) +{ + int count = table.size(); + int per_share = total/count; + unordered_map ::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 ::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 table, vector 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 ::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 ::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 table, vector 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 ::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 ::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::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; +} + diff --git a/split_service.h b/split_service.h new file mode 100644 index 0000000..5906c41 --- /dev/null +++ b/split_service.h @@ -0,0 +1,40 @@ +#include +#include "user.h" + +class SplitService +{ + /*** this map will contain all the user details with user id as key **/ + unordered_map user_table; + + /*** this table will contain all the expense details between users ***/ + float **expense_table; + + /*** this is a table of user ids with user seq no as index **********/ + vector users; + + int num_of_user; + + public: + SplitService(int no) + { + num_of_user = no; + expense_table = new float*[num_of_user]; + for (int i = 0; i table); + void EXPENSE_EXACT(int total, string payer, vector table, vector amount); + void EXPENSE_PERCENT(int total, string payer, vector table, vector amount); +}; + + diff --git a/user.h b/user.h new file mode 100644 index 0000000..765911d --- /dev/null +++ b/user.h @@ -0,0 +1,31 @@ +#include +#include +#include + +using namespace std; + +class User +{ + string user_id; + string email_id; + string mobile_no; + string name; + int seq_no; + public: + User(int s, string uid, string n, string email, string mob) + { + seq_no = s; + user_id = uid; + name = n; + email_id = email; + mobile_no = mob; + } + string get_user_id() + { + return user_id; + } + int get_seq() + { + return seq_no; + } +};