- The system should allow users to create accounts and manage their profile information.
- Users should be able to create groups and add other users to the groups.
- Users should be able to add expenses within a group, specifying the amount, description, and participants.
- The system should automatically split the expenses among the participants based on their share.
- Users should be able to view their individual balances with other users and settle up the balances.
- The system should support different split methods, such as equal split, percentage split, and exact amounts.
- Users should be able to view their transaction history and group expenses.
- The system should handle concurrent transactions and ensure data consistency.
- The User class represents a user in the Splitwise system, with properties such as ID, name, email, and a map to store balances with other users.
- The Group class represents a group in Splitwise, containing a list of member users and a list of expenses.
- The Expense class represents an expense within a group, with properties such as ID, amount, description, the user who paid, and a list of splits.
- The Split class is an abstract class representing the split of an expense. It is extended by EqualSplit, PercentSplit, and ExactSplit classes to handle different split methods.
- The Transaction class represents a transaction between two users, with properties such as ID, sender, receiver, and amount.
- The SplitwiseService class is the main class that manages the Splitwise system. It follows the Singleton pattern to ensure only one instance of the service exists.
- The SplitwiseService class provides methods for adding users, groups, and expenses, splitting expenses, updating balances, settling balances, and creating transactions.
- Multi-threading is achieved using concurrent data structures such as ConcurrentHashMap and CopyOnWriteArrayList to handle concurrent access to shared resources.
- The SplitwiseDemo class demonstrates the usage of the Splitwise system by creating users, a group, adding an expense, settling balances, and printing user balances.
- Splitting strategies is changed into an interface
- Adds a composite key for saving balance - The balance keys are constructed as strings ("user1:user2"). This could lead to inconsistencies and is error-prone. A better approach is to use a composite key or a Pair of user IDs with a canonical ordering to ensure consistency.
- Adds seperate service for handling the logic - Currently, the SplitwiseService class handles multiple responsibilities, including managing users, groups, expenses, and transactions. Consider separating these concerns into distinct services or managers: UserService: Handles user management. GroupService: Manages groups and their members. ExpenseService: Deals with adding expenses and splitting them. TransactionService: Handles creating and managing transactions.