-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpenseManager.java
78 lines (66 loc) · 2.29 KB
/
ExpenseManager.java
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
package splitwise;
import java.util.*;
import java.util.Map.Entry;
public class ExpenseManager {
List<Expense> expenses;
// userMap: userId to User
Map<String, User> userMap;
Map<User, Map<User, Double>> balanceSheet;
public ExpenseManager() {
this.expenses = new ArrayList<Expense>();
this.userMap = new HashMap<String, User>();
this.balanceSheet = new HashMap<User, Map<User, Double>>();
}
public void addUser(User user) {
userMap.put(user.getId(), user);
balanceSheet.put(user, new HashMap<User, Double>());
}
public void addExpense (ExpenseType type, double amount, String userId, List<Split> splits, String label) {
//TODO add expense, add to expenses list, add to balance sheet
User paidBy = userMap.get(userId);
Expense newExpense = SplitwiseService.createExpense(type, amount, paidBy, splits, label);
expenses.add(newExpense);
for (Split split : newExpense.getSplits()) {
User paidTo = split.getUser();
Map<User, Double> balances = balanceSheet.get(paidBy);
if (!balances.containsKey(paidTo)) {
balances.put(paidTo, 0.0);
}
balances.put(paidTo, balances.get(paidTo)+split.getAmount());
balances = balanceSheet.get(paidTo);
if (!balances.containsKey(paidBy)) {
balances.put(paidBy, 0.0);
}
balances.put(paidBy, balances.get(paidBy) - split.getAmount());
}
}
public void showBalanceForUser (String userId) {
//TODO
User thisUser = userMap.get(userId);
if (thisUser == null){
System.out.println("User doesn't exist");
return;
}
for (Map.Entry<User, Double> userBalance : balanceSheet.get(thisUser).entrySet()) {
//We don't want to print balance to self, which will be Zero ideally
if (userBalance.getKey()!=thisUser) {
printBalances(userId, userBalance);
}
}
}
private void printBalances(String userId, Entry<User, Double> userBalance) {
//TODO
if (userBalance.getValue()!= 0) {
if (userBalance.getValue()<0) {
System.out.println(userMap.get(userId).getName()+" owes "+Math.abs(userBalance.getValue())+" to "+userBalance.getKey().getName());
return;
}
System.out.println(userBalance.getKey().getName()+" owes "+Math.abs(userBalance.getValue())+" to "+userMap.get(userId).getName());
return;
}
System.out.println("No balances for "+userId);
}
public void showAllBalances () {
//TODO
}
}