diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/checkstyle-idea.xml b/.idea/checkstyle-idea.xml new file mode 100644 index 0000000..6fb7092 --- /dev/null +++ b/.idea/checkstyle-idea.xml @@ -0,0 +1,16 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..639900d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/mock-machine-coding-2.iml b/.idea/mock-machine-coding-2.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/mock-machine-coding-2.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..3e41112 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/sonarlint/issuestore/index.pb b/.idea/sonarlint/issuestore/index.pb new file mode 100644 index 0000000..e69de29 diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/docs/problem-statement.md b/docs/problem-statement.md index 3dbbb2b..52dfaf9 100755 --- a/docs/problem-statement.md +++ b/docs/problem-statement.md @@ -59,8 +59,8 @@ User3 owes User4: 240 (0+240) ## Requirements -- User: Each user should have a userId, name, email, mobile number. -- Expense: Could either be EQUAL, EXACT or PERCENT +- entity.User: Each user should have a userId, name, email, mobile number. +- entity.SplitType: Could either be EQUAL, EXACT or PERCENT - Users can add any amount, select any type of expense and split with any of the available users. - The percent and amount provided could have decimals upto two decimal places. - In case of percent, you need to verify if the total sum of percentage shares is 100 or not. @@ -73,7 +73,7 @@ User3 owes User4: 240 (0+240) - You can create a few users in your main method. No need to take it as input. - There will be 3 types of input: - - Expense in the format: ```EXPENSE ``` + - entity.SplitType in the format: ```EXPENSE ``` - Show balances for all: ```SHOW``` - Show balances for a single user: ```SHOW ``` diff --git a/out/production/snake-ladder/Driver.class b/out/production/snake-ladder/Driver.class new file mode 100644 index 0000000..2528dab Binary files /dev/null and b/out/production/snake-ladder/Driver.class differ diff --git a/out/production/snake-ladder/SnakeLadderManager.class b/out/production/snake-ladder/SnakeLadderManager.class new file mode 100644 index 0000000..d092340 Binary files /dev/null and b/out/production/snake-ladder/SnakeLadderManager.class differ diff --git a/out/production/snake-ladder/SnakeLadderService$1.class b/out/production/snake-ladder/SnakeLadderService$1.class new file mode 100644 index 0000000..b67d171 Binary files /dev/null and b/out/production/snake-ladder/SnakeLadderService$1.class differ diff --git a/out/production/snake-ladder/SnakeLadderService.class b/out/production/snake-ladder/SnakeLadderService.class new file mode 100644 index 0000000..cd6d8be Binary files /dev/null and b/out/production/snake-ladder/SnakeLadderService.class differ diff --git a/out/production/snake-ladder/models/User.class b/out/production/snake-ladder/models/User.class new file mode 100644 index 0000000..be11121 Binary files /dev/null and b/out/production/snake-ladder/models/User.class differ diff --git a/out/production/snake-ladder/models/object/Dice.class b/out/production/snake-ladder/models/object/Dice.class new file mode 100644 index 0000000..8889594 Binary files /dev/null and b/out/production/snake-ladder/models/object/Dice.class differ diff --git a/out/production/snake-ladder/models/object/GameObject.class b/out/production/snake-ladder/models/object/GameObject.class new file mode 100644 index 0000000..d21574e Binary files /dev/null and b/out/production/snake-ladder/models/object/GameObject.class differ diff --git a/out/production/snake-ladder/models/object/Ladder.class b/out/production/snake-ladder/models/object/Ladder.class new file mode 100644 index 0000000..b386260 Binary files /dev/null and b/out/production/snake-ladder/models/object/Ladder.class differ diff --git a/out/production/snake-ladder/models/object/ObjectType.class b/out/production/snake-ladder/models/object/ObjectType.class new file mode 100644 index 0000000..0ac2cea Binary files /dev/null and b/out/production/snake-ladder/models/object/ObjectType.class differ diff --git a/out/production/snake-ladder/models/object/Snake.class b/out/production/snake-ladder/models/object/Snake.class new file mode 100644 index 0000000..db6a5f3 Binary files /dev/null and b/out/production/snake-ladder/models/object/Snake.class differ diff --git a/out/production/splitwise/CommandManager.class b/out/production/splitwise/CommandManager.class new file mode 100644 index 0000000..3e034da Binary files /dev/null and b/out/production/splitwise/CommandManager.class differ diff --git a/out/production/splitwise/SplitwiseMain.class b/out/production/splitwise/SplitwiseMain.class new file mode 100644 index 0000000..cc7998e Binary files /dev/null and b/out/production/splitwise/SplitwiseMain.class differ diff --git a/out/production/splitwise/entity/EqualSplit.class b/out/production/splitwise/entity/EqualSplit.class new file mode 100644 index 0000000..90ae02e Binary files /dev/null and b/out/production/splitwise/entity/EqualSplit.class differ diff --git a/out/production/splitwise/entity/ExactSplit.class b/out/production/splitwise/entity/ExactSplit.class new file mode 100644 index 0000000..591ca23 Binary files /dev/null and b/out/production/splitwise/entity/ExactSplit.class differ diff --git a/out/production/splitwise/entity/ExpenseManager.class b/out/production/splitwise/entity/ExpenseManager.class new file mode 100644 index 0000000..667d94c Binary files /dev/null and b/out/production/splitwise/entity/ExpenseManager.class differ diff --git a/out/production/splitwise/entity/PercentSplit.class b/out/production/splitwise/entity/PercentSplit.class new file mode 100644 index 0000000..8fadaab Binary files /dev/null and b/out/production/splitwise/entity/PercentSplit.class differ diff --git a/out/production/splitwise/entity/Split.class b/out/production/splitwise/entity/Split.class new file mode 100644 index 0000000..ce22bc1 Binary files /dev/null and b/out/production/splitwise/entity/Split.class differ diff --git a/out/production/splitwise/entity/SplitManager.class b/out/production/splitwise/entity/SplitManager.class new file mode 100644 index 0000000..f07f593 Binary files /dev/null and b/out/production/splitwise/entity/SplitManager.class differ diff --git a/out/production/splitwise/entity/SplitType.class b/out/production/splitwise/entity/SplitType.class new file mode 100644 index 0000000..13a7643 Binary files /dev/null and b/out/production/splitwise/entity/SplitType.class differ diff --git a/out/production/splitwise/entity/User.class b/out/production/splitwise/entity/User.class new file mode 100644 index 0000000..c0c1a02 Binary files /dev/null and b/out/production/splitwise/entity/User.class differ diff --git a/out/production/splitwise/input.txt b/out/production/splitwise/input.txt new file mode 100644 index 0000000..a991f8e --- /dev/null +++ b/out/production/splitwise/input.txt @@ -0,0 +1,10 @@ +SHOW +SHOW U1 +EXPENSE U1 1000 4 U1 U2 U3 U4 EQUAL +SHOW U4 +SHOW U1 +EXPENSE U1 1250 2 U2 U3 EXACT 370 880 +SHOW +EXPENSE U4 1200 4 U1 U2 U3 U4 PERCENT 40 20 20 20 +SHOW U1 +SHOW \ No newline at end of file diff --git a/snake-ladder/resources/input.txt b/snake-ladder/resources/input.txt new file mode 100644 index 0000000..5be0177 --- /dev/null +++ b/snake-ladder/resources/input.txt @@ -0,0 +1,22 @@ +9 +62 5 +33 6 +49 9 +88 16 +41 20 +56 53 +98 64 +93 73 +95 75 +8 +2 37 +27 46 +10 32 +51 68 +61 79 +65 84 +71 91 +81 100 +2 +Gaurav +Sagar \ No newline at end of file diff --git a/snake-ladder/snake-ladder.iml b/snake-ladder/snake-ladder.iml new file mode 100644 index 0000000..2b6d115 --- /dev/null +++ b/snake-ladder/snake-ladder.iml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/snake-ladder/src/Driver.java b/snake-ladder/src/Driver.java new file mode 100644 index 0000000..3595892 --- /dev/null +++ b/snake-ladder/src/Driver.java @@ -0,0 +1,34 @@ +import java.io.File; +import java.io.FileNotFoundException; +import java.util.Scanner; + +public class Driver { + public static void main(String[] args) throws FileNotFoundException { + SnakeLadderManager snakeLadderManager = new SnakeLadderManager(); + File file = new File("/Users/varsha.lalwani/personal/mock-machine-coding-2/snake-ladder/resources/input.txt"); + Scanner scanner = new Scanner(file); + String command = scanner.nextLine(); + Integer numOfSnakes = Integer.valueOf(command); + for (int i=0; i objectsPosMap; + + private Map userPosMap; + + SnakeLadderManager() { + objectsPosMap = new HashMap<>(); + userPosMap = new HashMap<>(); + } + + public void addSnake(Integer head, Integer tail) { + SnakeLadderService.addObject(ObjectType.SNAKE, head, tail); + objectsPosMap.put(head, tail); + } + + public void addLadder(Integer head, Integer tail) { + SnakeLadderService.addObject(ObjectType.LADDER, head, tail); + objectsPosMap.put(head, tail); + } + + public void addUser(String name) { + User user = new User(name); + userPosMap.put(user, 0); + } + + public void playGame() { + while (true) { + boolean winnerDeclared = false; + for (User user : userPosMap.keySet()) { + Integer currVal = Dice.getDiceValue(1, 6); + Integer currPos = userPosMap.get(user); + if (currPos + currVal == 100) { + printMove(user, currVal, currPos, currPos+currVal); + printWinner(user); + winnerDeclared = true; + break; + } + if (currPos + currVal > 100) { + printMove(user, currVal, currPos, currPos); + continue; + } + Integer newPos = currPos + currVal; + if (objectsPosMap.containsKey(newPos)) { + newPos = objectsPosMap.get(newPos); + } + userPosMap.put(user, newPos); + printMove(user, currVal, currPos, newPos); + } + if (winnerDeclared) { + break; + } + } + } + + public void printMove(User user, Integer diceMove, Integer initialPos, Integer newPos) { + System.out.printf("%s rolled a %s and moved from %s to %s %n", user.getName(), diceMove, + initialPos, newPos); + } + public void printWinner(User user) { + System.out.printf("%s wins the game %n", user.getName()); + } + +} diff --git a/snake-ladder/src/SnakeLadderService.java b/snake-ladder/src/SnakeLadderService.java new file mode 100644 index 0000000..647dd96 --- /dev/null +++ b/snake-ladder/src/SnakeLadderService.java @@ -0,0 +1,19 @@ +import models.object.GameObject; +import models.object.Ladder; +import models.object.ObjectType; +import models.object.Snake; + +public class SnakeLadderService { + + public static GameObject addObject(ObjectType objectType, Integer head, Integer tail) { + switch (objectType) { + case SNAKE: + return new Snake(head, tail); + case LADDER: + return new Ladder(head, tail); + default: + return null; + } + } + +} diff --git a/snake-ladder/src/models/User.java b/snake-ladder/src/models/User.java new file mode 100644 index 0000000..8d4c4b6 --- /dev/null +++ b/snake-ladder/src/models/User.java @@ -0,0 +1,27 @@ +package models; + +import java.util.UUID; + +public class User { + + private UUID id; + + private String name; + + public User(final String name){ + this.name = name; + id = UUID.randomUUID(); + } + + public void setName(String name) { + this.name = name; + } + + public UUID getId() { + return id; + } + + public String getName() { + return name; + } +} diff --git a/snake-ladder/src/models/object/Dice.java b/snake-ladder/src/models/object/Dice.java new file mode 100644 index 0000000..2370787 --- /dev/null +++ b/snake-ladder/src/models/object/Dice.java @@ -0,0 +1,10 @@ +package models.object; + +import java.util.concurrent.ThreadLocalRandom; + +public class Dice { + public static Integer getDiceValue(Integer diceMinValue, Integer diceMaxValue) { + int randomNum = ThreadLocalRandom.current().nextInt(diceMinValue, diceMaxValue + 1); + return randomNum; + } +} diff --git a/snake-ladder/src/models/object/GameObject.java b/snake-ladder/src/models/object/GameObject.java new file mode 100644 index 0000000..9824c5e --- /dev/null +++ b/snake-ladder/src/models/object/GameObject.java @@ -0,0 +1,32 @@ +package models.object; + +public abstract class GameObject { + + private Integer head; + + private Integer tail; + + GameObject( + final Integer head, + final Integer tail) { + this.head = head; + this.tail = tail; + } + + public void setHead(Integer head) { + this.head = head; + } + + public void setTail(Integer tail) { + this.tail = tail; + } + + public Integer getHead() { + return head; + } + + public Integer getTail() { + return tail; + } + +} diff --git a/snake-ladder/src/models/object/Ladder.java b/snake-ladder/src/models/object/Ladder.java new file mode 100644 index 0000000..27a1930 --- /dev/null +++ b/snake-ladder/src/models/object/Ladder.java @@ -0,0 +1,8 @@ +package models.object; + +public class Ladder extends GameObject{ + + public Ladder(Integer head, Integer tail){ + super(head, tail); + } +} diff --git a/snake-ladder/src/models/object/ObjectType.java b/snake-ladder/src/models/object/ObjectType.java new file mode 100644 index 0000000..498b978 --- /dev/null +++ b/snake-ladder/src/models/object/ObjectType.java @@ -0,0 +1,7 @@ +package models.object; + +public enum ObjectType { + SNAKE, + + LADDER +} diff --git a/snake-ladder/src/models/object/Snake.java b/snake-ladder/src/models/object/Snake.java new file mode 100644 index 0000000..81fa579 --- /dev/null +++ b/snake-ladder/src/models/object/Snake.java @@ -0,0 +1,8 @@ +package models.object; + +public class Snake extends GameObject{ + + public Snake(Integer head, Integer tail){ + super(head, tail); + } +} diff --git a/snake-ladder/src/models/object/package-info.java b/snake-ladder/src/models/object/package-info.java new file mode 100644 index 0000000..c96601d --- /dev/null +++ b/snake-ladder/src/models/object/package-info.java @@ -0,0 +1 @@ +package models.object; \ No newline at end of file diff --git a/snake-ladder/src/models/package-info.java b/snake-ladder/src/models/package-info.java new file mode 100644 index 0000000..ff011d5 --- /dev/null +++ b/snake-ladder/src/models/package-info.java @@ -0,0 +1 @@ +package models; \ No newline at end of file diff --git a/splitwise/splitwise.iml b/splitwise/splitwise.iml new file mode 100644 index 0000000..2b6d115 --- /dev/null +++ b/splitwise/splitwise.iml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/splitwise/src/CommandManager.java b/splitwise/src/CommandManager.java new file mode 100644 index 0000000..686abda --- /dev/null +++ b/splitwise/src/CommandManager.java @@ -0,0 +1,80 @@ +import java.util.ArrayList; +import java.util.List; + +import entity.ExpenseManager; +import entity.Split; +import entity.SplitManager; +import entity.SplitType; +import entity.User; + +public class CommandManager { + + private ExpenseManager expenseManager; + + private SplitManager splitManager; + + public CommandManager(final ExpenseManager expenseManager) { + this.expenseManager = expenseManager; + this.splitManager = new SplitManager(); + } + + public void runCommand(String command) { + String[] splitCommand = command.split(" "); + if(splitCommand[0].equals("EXPENSE")){ + addExpense(command); + } else if(splitCommand[0].equals("SHOW")){ + showExpense(command); + } + } + + public void showExpense(String command) { + String[] splitCommand = command.split(" "); + if (splitCommand.length==1) { + expenseManager.showForAllUsers(); + } else { + User user = new User(splitCommand[1]); + expenseManager.showForUser(user); + } + } + + public void addExpense(String command) { + String[] splitCommand = command.split(" "); + User userWhoPaid = new User(splitCommand[1]); + double totalAmountPaid = Double.valueOf(splitCommand[2]); + Integer totalNumOfUsers = Integer.valueOf(splitCommand[3]); + int index = 4; + List userList = new ArrayList<>(); + for (int i=1; i<=totalNumOfUsers; i++){ + User user = new User(splitCommand[index++]); + userList.add(user); + } + if(splitCommand[index].equals(SplitType.EQUAL.toString())) { + for(User user: userList) { + Split split = splitManager.splitExpense(user, totalNumOfUsers, totalAmountPaid); + if(!split.getUser().equals(userWhoPaid)) { + expenseManager.addSplitExpense(userWhoPaid, split); + } + } + } else if(splitCommand[index].equals(SplitType.EXACT.toString())) { + index++; + for (int i=1; i<=totalNumOfUsers; i++){ + Double amount = Double.valueOf(splitCommand[index++]); + Split split = splitManager.splitExpense(userList.get(i-1), amount); + if(!split.getUser().equals(userWhoPaid)) { + expenseManager.addSplitExpense(userWhoPaid, split); + } + } + } else if(splitCommand[index].equals(SplitType.PERCENT.toString())) { + index++; + for (int i=1; i<=totalNumOfUsers; i++){ + Double percent = Double.valueOf(splitCommand[index++]); + Split split = splitManager.splitExpense(userList.get(i-1), percent, + totalAmountPaid); + if(!split.getUser().equals(userWhoPaid)) { + expenseManager.addSplitExpense(userWhoPaid, split); + } + } + } + + } +} diff --git a/splitwise/src/SplitwiseMain.java b/splitwise/src/SplitwiseMain.java new file mode 100644 index 0000000..5a1360f --- /dev/null +++ b/splitwise/src/SplitwiseMain.java @@ -0,0 +1,42 @@ +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import entity.ExpenseManager; +import entity.User; + +public class SplitwiseMain { + public static void main(String[] args) throws IOException { + List userList = new ArrayList<>(); + User user1 = new User("U1", "User1", "User1@gmail.com", + "9798512549"); + User user2 = new User("U2", "User2", "User2@gmail.com", + "9798512548"); + User user3 = new User("U3", "User3", "User3@gmail.com", + "9798512547"); + User user4 = new User("U4", "User4", "User4@gmail.com", + "9798512546"); + userList.add(user1); + userList.add(user2); + userList.add(user3); + userList.add(user4); + ExpenseManager expenseManager = new ExpenseManager(userList); + + CommandManager commandManager = new CommandManager(expenseManager); + + File file = new File("input.txt"); + BufferedReader br = new BufferedReader(new FileReader(file)); + + String command; + while((command = br.readLine()) != null) { + if (command.equals("exit")) { + System.exit(0); + } else { + commandManager.runCommand(command); + } + } + } +} diff --git a/splitwise/src/entity/EqualSplit.java b/splitwise/src/entity/EqualSplit.java new file mode 100644 index 0000000..1c74e17 --- /dev/null +++ b/splitwise/src/entity/EqualSplit.java @@ -0,0 +1,7 @@ +package entity; + +public class EqualSplit extends Split{ + public EqualSplit(final User user, Integer countOfUsers, final double totalAmount) { + super(user, totalAmount/countOfUsers); + } +} diff --git a/splitwise/src/entity/ExactSplit.java b/splitwise/src/entity/ExactSplit.java new file mode 100644 index 0000000..199dd4a --- /dev/null +++ b/splitwise/src/entity/ExactSplit.java @@ -0,0 +1,7 @@ +package entity; + +public class ExactSplit extends Split{ + public ExactSplit(final User user, double amount) { + super(user, amount); + } +} diff --git a/splitwise/src/entity/ExpenseManager.java b/splitwise/src/entity/ExpenseManager.java new file mode 100644 index 0000000..ad8c482 --- /dev/null +++ b/splitwise/src/entity/ExpenseManager.java @@ -0,0 +1,74 @@ +package entity; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class ExpenseManager { + + private List users; + + private Map , Double> userOweMap; + + public ExpenseManager() { + users = new ArrayList<>(); + userOweMap = new HashMap<>(); + } + + public ExpenseManager(List userList) { + users = userList; + userOweMap = new HashMap<>(); + } + + public ExpenseManager(List userList, Map , Double> userOweMap) { + users = userList; + userOweMap = userOweMap; + } + + public Map , Double> getWhoOweWhomMap() { + return userOweMap; + } + + public List getUsers() { + return users; + } + + private void showForUsers(List userList) { + boolean isAnyBalanceDue = false; + for (User owedUser : users) { + for (User oweUser : users) { + if ((userList.contains(owedUser) || userList.contains(oweUser)) + && (userOweMap.containsKey(Map.of(owedUser, oweUser)))) { + Double amount = userOweMap.get(Map.of(owedUser, oweUser)); + isAnyBalanceDue = true; + if (amount > 0) { + System.out.printf("%s owes %s: %s %n", owedUser.getUserId(), + oweUser.getUserId(), amount); + } + } + } + } + + if(!isAnyBalanceDue) { + System.out.println("No Balances"); + } + } + public void showForAllUsers() { + showForUsers(users); + } + + public void showForUser(User user) { + showForUsers(Collections.singletonList(user)); + } + + public void addSplitExpense(User owedUser, Split split) { + double prevAmt = 0; + if(userOweMap.containsKey(Map.of(owedUser, split.getUser()))){ + prevAmt = userOweMap.get(Map.of(owedUser, split.getUser())); + } + userOweMap.put(Map.of(owedUser, split.getUser()), prevAmt-split.getAmount()); + userOweMap.put(Map.of(split.getUser(), owedUser), split.getAmount()-prevAmt); + } +} diff --git a/splitwise/src/entity/PercentSplit.java b/splitwise/src/entity/PercentSplit.java new file mode 100644 index 0000000..1e23d2e --- /dev/null +++ b/splitwise/src/entity/PercentSplit.java @@ -0,0 +1,19 @@ + +package entity; + +public class PercentSplit extends Split{ + double percent; + public PercentSplit(final User user, double percent, double totalAmount) { + + super(user, (totalAmount*percent/100)); + this.percent = percent; + } + + public double getPercent() { + return percent; + } + + public void setPercent(double percent) { + this.percent = percent; + } +} diff --git a/splitwise/src/entity/Split.java b/splitwise/src/entity/Split.java new file mode 100644 index 0000000..e6dbb95 --- /dev/null +++ b/splitwise/src/entity/Split.java @@ -0,0 +1,27 @@ +package entity; +public abstract class Split { + private User user; + + double amount; + + public Split(User user, double amount) { + this.user = user; + this.amount = amount; + } + + public void setUser(User user) { + this.user = user; + } + + public User getUser(){ + return this.user; + } + + public void setAmount(double amount) { + this.amount = amount; + } + + public double getAmount(){ + return this.amount; + } +} diff --git a/splitwise/src/entity/SplitManager.java b/splitwise/src/entity/SplitManager.java new file mode 100644 index 0000000..300219a --- /dev/null +++ b/splitwise/src/entity/SplitManager.java @@ -0,0 +1,25 @@ +package entity; + +public class SplitManager { + + + private Split split; + + public SplitManager(){ + } + + public Split splitExpense(User user, double amount) { + split = new ExactSplit(user, amount); + return split; + } + + public Split splitExpense(User user, Integer numOfUsers, double amount) { + split = new EqualSplit(user, numOfUsers, amount); + return split; + } + + public Split splitExpense(User user, double percent, double amount) { + split = new PercentSplit(user, percent, amount); + return split; + } +} diff --git a/splitwise/src/entity/SplitType.java b/splitwise/src/entity/SplitType.java new file mode 100644 index 0000000..fdb7a47 --- /dev/null +++ b/splitwise/src/entity/SplitType.java @@ -0,0 +1,9 @@ +package entity; + +public enum SplitType { + EQUAL, + + EXACT, + + PERCENT +} diff --git a/splitwise/src/entity/User.java b/splitwise/src/entity/User.java new file mode 100644 index 0000000..f0fbf58 --- /dev/null +++ b/splitwise/src/entity/User.java @@ -0,0 +1,54 @@ +package entity; + +import java.util.Objects; + +public class User { + public String id; + private String name; + private String email; + private String contactNum; + + public User(final String id) { + this.id = id; + } + + public User( + final String id, + final String name, + final String email, + final String contactNum) { + this.id = id; + this.name = name; + this.email = email; + this.contactNum = contactNum; + } + + public String getUserId() { + return id; + } + + public String getUserName() { + return name; + } + + public String getUserEmail() { + return email; + } + + public String getUserContactNum() { + return contactNum; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o==null || o.getClass() != getClass()) return false; + User that = (User) o; + return Objects.equals(that.id, this.id); + } + + @Override + public int hashCode() { + return Objects.hash(id); + } +} diff --git a/splitwise/src/input.txt b/splitwise/src/input.txt new file mode 100644 index 0000000..a991f8e --- /dev/null +++ b/splitwise/src/input.txt @@ -0,0 +1,10 @@ +SHOW +SHOW U1 +EXPENSE U1 1000 4 U1 U2 U3 U4 EQUAL +SHOW U4 +SHOW U1 +EXPENSE U1 1250 2 U2 U3 EXACT 370 880 +SHOW +EXPENSE U4 1200 4 U1 U2 U3 U4 PERCENT 40 20 20 20 +SHOW U1 +SHOW \ No newline at end of file