Splitwise
unknown
java
a month ago
5.3 kB
1
Indexable
Never
import java.util.*; // Enum to represent the status of a transaction (optional) public enum TransactionStatus { PENDING, SETTLED } // Interface for User public interface User { String getId(); String getName(); String getEmail(); void addExpense(Expense expense); List<Expense> getExpenses(); } // Interface for Expense import java.util.Map; public interface Expense { String getId(); String getDescription(); double getTotalAmount(); User getPaidBy(); Map<User, Double> getSplitDetails(); } // Interface for Settlement public interface Settlement { User getOwedBy(); User getOwedTo(); double getAmount(); } // Implementation of User public class UserImpl implements User { private String id; private String name; private String email; private List<Expense> expenses; public UserImpl(String id, String name, String email) { this.id = id; this.name = name; this.email = email; this.expenses = new ArrayList<>(); } @Override public String getId() { return id; } @Override public String getName() { return name; } @Override public String getEmail() { return email; } @Override public void addExpense(Expense expense) { expenses.add(expense); } @Override public List<Expense> getExpenses() { return expenses; } } // Implementation of Expense import java.util.Map; public class ExpenseImpl implements Expense { private String id; private String description; private double totalAmount; private User paidBy; private Map<User, Double> splitDetails; public ExpenseImpl(String id, String description, double totalAmount, User paidBy, Map<User, Double> splitDetails) { this.id = id; this.description = description; this.totalAmount = totalAmount; this.paidBy = paidBy; this.splitDetails = splitDetails; } @Override public String getId() { return id; } @Override public String getDescription() { return description; } @Override public double getTotalAmount() { return totalAmount; } @Override public User getPaidBy() { return paidBy; } @Override public Map<User, Double> getSplitDetails() { return splitDetails; } } // Implementation of Settlement public class SettlementImpl implements Settlement { private User owedBy; private User owedTo; private double amount; public SettlementImpl(User owedBy, User owedTo, double amount) { this.owedBy = owedBy; this.owedTo = owedTo; this.amount = amount; } @Override public User getOwedBy() { return owedBy; } @Override public User getOwedTo() { return owedTo; } @Override public double getAmount() { return amount; } } // Main Application to demonstrate the Splitwise-like system public class SplitwiseApp { private List<User> users; private List<Expense> expenses; private List<Settlement> settlements; public SplitwiseApp() { this.users = new ArrayList<>(); this.expenses = new ArrayList<>(); this.settlements = new ArrayList<>(); } public void addUser(User user) { users.add(user); } public void addExpense(Expense expense) { expenses.add(expense); expense.getPaidBy().addExpense(expense); calculateSettlements(expense); } private void calculateSettlements(Expense expense) { User paidBy = expense.getPaidBy(); Map<User, Double> splitDetails = expense.getSplitDetails(); for (Map.Entry<User, Double> entry : splitDetails.entrySet()) { User user = entry.getKey(); double amountOwed = entry.getValue(); if (user != paidBy) { settlements.add(new SettlementImpl(user, paidBy, amountOwed)); } } } public List<Settlement> getSettlements() { return settlements; } public static void main(String[] args) { SplitwiseApp app = new SplitwiseApp(); // Create users User user1 = new UserImpl(UUID.randomUUID().toString(), "Alice", "alice@example.com"); User user2 = new UserImpl(UUID.randomUUID().toString(), "Bob", "bob@example.com"); User user3 = new UserImpl(UUID.randomUUID().toString(), "Charlie", "charlie@example.com"); app.addUser(user1); app.addUser(user2); app.addUser(user3); // Create an expense Map<User, Double> splitDetails = new HashMap<>(); splitDetails.put(user1, 30.0); splitDetails.put(user2, 40.0); splitDetails.put(user3, 30.0); Expense expense = new ExpenseImpl(UUID.randomUUID().toString(), "Dinner", 100.0, user2, splitDetails); app.addExpense(expense); // Get and print settlements List<Settlement> settlements = app.getSettlements(); for (Settlement settlement : settlements) { System.out.println(settlement.getOwedBy().getName() + " owes " + settlement.getOwedTo().getName() + ": $" + settlement.getAmount()); } } }
Leave a Comment