-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
Copy pathExpense.cpp
57 lines (50 loc) · 2.21 KB
/
Expense.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
#include "Expense.hpp"
#include <iostream>
#include <iomanip>
Expense::Expense(std::string expenseId, std::string description, double totalAmount,
std::string paidBy, const std::vector<std::string>& participants,
ExpenseType type)
: expenseId(expenseId), description(description), totalAmount(totalAmount),
paidBy(paidBy), participants(participants), type(type) {
timestamp = std::time(nullptr);
if (type == ExpenseType::EQUAL) {
calculateEqualShares();
}
}
std::string Expense::getExpenseId() const { return expenseId; }
std::string Expense::getDescription() const { return description; }
double Expense::getTotalAmount() const { return totalAmount; }
std::string Expense::getPaidBy() const { return paidBy; }
const std::vector<std::string>& Expense::getParticipants() const { return participants; }
const std::map<std::string, double>& Expense::getShares() const { return shares; }
ExpenseType Expense::getType() const { return type; }
std::time_t Expense::getTimestamp() const { return timestamp; }
void Expense::setShares(const std::map<std::string, double>& shares) {
this->shares = shares;
}
void Expense::calculateEqualShares() {
double equalShare = totalAmount / participants.size();
for (const auto& participant : participants) {
shares[participant] = equalShare;
}
}
void Expense::displayInfo() const {
std::cout << "\nExpense Details:" << std::endl;
std::cout << "ID: " << expenseId << std::endl;
std::cout << "Description: " << description << std::endl;
std::cout << "Amount: $" << std::fixed << std::setprecision(2) << totalAmount << std::endl;
std::cout << "Paid by: " << paidBy << std::endl;
std::cout << "Type: ";
switch (type) {
case ExpenseType::EQUAL: std::cout << "Equal"; break;
case ExpenseType::EXACT: std::cout << "Exact"; break;
case ExpenseType::PERCENT: std::cout << "Percent"; break;
}
std::cout << std::endl;
std::cout << "Shares:" << std::endl;
for (const auto& share : shares) {
std::cout << share.first << ": $" << std::fixed << std::setprecision(2)
<< share.second << std::endl;
}
std::cout << "Time: " << std::ctime(×tamp);
}