-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathSplitwiseSystem.cpp
148 lines (120 loc) · 4.76 KB
/
SplitwiseSystem.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
#include "SplitwiseSystem.hpp"
#include <iostream>
#include <algorithm>
#include <numeric>
SplitwiseSystem::SplitwiseSystem() : userIdCounter(1), expenseIdCounter(1) {}
SplitwiseSystem::~SplitwiseSystem() {
for (auto user : users) delete user;
for (auto expense : expenses) delete expense;
}
User* SplitwiseSystem::registerUser(const std::string& name, const std::string& email) {
std::string userId = generateUserId();
User* user = new User(userId, name, email);
users.push_back(user);
return user;
}
void SplitwiseSystem::removeUser(const std::string& userId) {
auto it = std::find_if(users.begin(), users.end(),
[userId](User* user) { return user->getUserId() == userId; });
if (it != users.end()) {
delete *it;
users.erase(it);
}
}
Expense* SplitwiseSystem::addExpense(const std::string& description, double amount,
const std::string& paidBy,
const std::vector<std::string>& participants,
ExpenseType type) {
if (!findUser(paidBy)) return nullptr;
std::string expenseId = generateExpenseId();
Expense* expense = new Expense(expenseId, description, amount, paidBy, participants, type);
expenses.push_back(expense);
if (type == ExpenseType::EQUAL) {
settleExpense(expenseId);
}
return expense;
}
bool SplitwiseSystem::setExpenseShares(const std::string& expenseId,
const std::map<std::string, double>& shares) {
Expense* expense = findExpense(expenseId);
if (!expense) return false;
// Validate total shares equals expense amount
double totalShares = std::accumulate(shares.begin(), shares.end(), 0.0,
[](double sum, const auto& pair) { return sum + pair.second; });
if (std::abs(totalShares - expense->getTotalAmount()) > 0.01) return false;
expense->setShares(shares);
settleExpense(expenseId);
return true;
}
void SplitwiseSystem::settleExpense(const std::string& expenseId) {
Expense* expense = findExpense(expenseId);
if (!expense) return;
const std::string& paidBy = expense->getPaidBy();
const auto& shares = expense->getShares();
for (const auto& share : shares) {
if (share.first != paidBy) {
// Update balances for both users
User* payer = findUser(paidBy);
User* participant = findUser(share.first);
if (payer && participant) {
payer->updateBalance(share.first, share.second);
participant->updateBalance(paidBy, -share.second);
}
}
}
}
void SplitwiseSystem::showBalance(const std::string& userId) const {
User* user = findUser(userId);
if (!user) return;
user->displayBalances();
}
void SplitwiseSystem::showAllBalances() const {
std::cout << "\nAll Balances:" << std::endl;
for (const auto& user : users) {
user->displayBalances();
}
}
void SplitwiseSystem::displayUsers() const {
std::cout << "\nRegistered Users:" << std::endl;
for (const auto& user : users) {
user->displayInfo();
std::cout << "------------------------" << std::endl;
}
}
void SplitwiseSystem::displayExpenses() const {
std::cout << "\nAll Expenses:" << std::endl;
for (const auto& expense : expenses) {
expense->displayInfo();
std::cout << "------------------------" << std::endl;
}
}
void SplitwiseSystem::displayUserExpenses(const std::string& userId) const {
User* user = findUser(userId);
if (!user) return;
std::cout << "\nExpenses for " << user->getName() << ":" << std::endl;
for (const auto& expense : expenses) {
if (expense->getPaidBy() == userId ||
std::find(expense->getParticipants().begin(),
expense->getParticipants().end(),
userId) != expense->getParticipants().end()) {
expense->displayInfo();
std::cout << "------------------------" << std::endl;
}
}
}
User* SplitwiseSystem::findUser(const std::string& userId) const {
auto it = std::find_if(users.begin(), users.end(),
[userId](User* user) { return user->getUserId() == userId; });
return it != users.end() ? *it : nullptr;
}
Expense* SplitwiseSystem::findExpense(const std::string& expenseId) const {
auto it = std::find_if(expenses.begin(), expenses.end(),
[expenseId](Expense* expense) { return expense->getExpenseId() == expenseId; });
return it != expenses.end() ? *it : nullptr;
}
std::string SplitwiseSystem::generateUserId() {
return "U" + std::to_string(userIdCounter++);
}
std::string SplitwiseSystem::generateExpenseId() {
return "E" + std::to_string(expenseIdCounter++);
}