diff --git a/splitwise/Balance.java b/splitwise/Balance.java new file mode 100644 index 0000000..b58c076 --- /dev/null +++ b/splitwise/Balance.java @@ -0,0 +1,41 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package splitwise; + +import java.util.HashMap; +import java.util.Map; + +/** + * + * @author rajeshkumar.yadav + */ +public class Balance { + + Map pay; + Map getBack; + + public Balance() { + pay = new HashMap(); + getBack = new HashMap(); + } + + public Map getPay() { + return pay; + } + + public void setPay(Map pay) { + this.pay = pay; + } + + public Map getGetBack() { + return getBack; + } + + public void setGetBack(Map getBack) { + this.getBack = getBack; + } + +} diff --git a/splitwise/Equal.java b/splitwise/Equal.java new file mode 100644 index 0000000..1b17866 --- /dev/null +++ b/splitwise/Equal.java @@ -0,0 +1,39 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package splitwise; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +/** + * + * @author rajeshkumar.yadav + */ + +public class Equal extends ExpenseType{ + + public Equal(int amount) { + super(amount); + } + + public void split(List l1){ + + User PaidByUSer = l1.get(0); + double divamount = getAmount()/l1.size(); + l1.remove(0); + List amount = new ArrayList<>(); + for(int i=0; i l1, List l2, User paidByUser){ + + Balance paidByUserBalance = paidByUser.getBalance(); + int paidByUserId = paidByUser.getUseId(); + + for(int i=0; i m = b.getPay(); + + if(m.get(paidByUserId) != null) + { + m.put(paidByUserId, m.get(paidByUserId) + l2.get(i)); + }else + { + m.put(paidByUserId, l2.get(i)); + } + + Map paidByUserGetBackMap = paidByUserBalance.getGetBack(); + + if(paidByUserGetBackMap.get(l1.get(i).getUseId()) != null) + { + paidByUserGetBackMap.put(l1.get(i).getUseId(), paidByUserGetBackMap.get(l1.get(i).getUseId()) + l2.get(i)); + + }else + { + paidByUserGetBackMap.put(l1.get(i).getUseId(), l2.get(i)); + } + + } + } +} diff --git a/splitwise/ExpenseType.java b/splitwise/ExpenseType.java new file mode 100644 index 0000000..d141a01 --- /dev/null +++ b/splitwise/ExpenseType.java @@ -0,0 +1,36 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package splitwise; + +import java.util.List; + +/** + * + * @author rajeshkumar.yadav + */ +public class ExpenseType { + + private int amount; + public void split(List l1, List l2, User paidByUser){ + + } + public void split(List l1, List l2){ + } + public void split(List l1){ + } + + public ExpenseType(int amount) { + this.amount = amount; + } + + public int getAmount() { + return amount; + } + + public void setAmount(int amount) { + this.amount = amount; + } +} diff --git a/splitwise/Percentage.java b/splitwise/Percentage.java new file mode 100644 index 0000000..fc14bda --- /dev/null +++ b/splitwise/Percentage.java @@ -0,0 +1,48 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package splitwise; + +import java.util.ArrayList; +import java.util.List; + +/** + * + * @author rajeshkumar.yadav + */ +public class Percentage extends ExpenseType{ + + public Percentage(int amount) { + super(amount); + } + + public void split(List l1, List l2){ + + int sum = 0; + for(int i=0; i list1 = new ArrayList(); + for(int i=0; i userSet; + Map uniqueConstarin; + + public SplitWiseApp() { + + userSet = new HashMap<>(); + uniqueConstarin = new HashMap<>(); + } + + public boolean isNumeric(String str) { + try { + Double.parseDouble(str); + return true; + } catch (NumberFormatException e) { + return false; + } + } + + public boolean isValidEmail(String mail) { + return true; + } + + public void createUser(int id, String name, String emailId, String mobNo) { + + if (uniqueConstarin.get(emailId) != null) { + System.err.println("This email Id is alredy registered with some other user, Plase select a diiferent one "); + return; + } + if (uniqueConstarin.get(mobNo) != null) { + System.err.println("This Mobile No is alredy registered with some other user, Plase select a diiferent one "); + return; + } + if (userSet.get(id) != null) { + System.err.println("This User Id is alredy registered with some other user, Plase select a diiferent one "); + return; + } + + if (!isNumeric(mobNo)) { + System.err.println("This mobile number is not valid "); + return; + } + + if (!isValidEmail(emailId)) { + System.err.println("This Email Id is not valid "); + return; + } + + User u1 = new User(id, name, emailId, mobNo); + userSet.put(id, u1); + + } + + public void processEaxctInput(int paidByUserId, int amount, List userIdList, List amountList) { + + ExpenseType expType = null; + expType = new Exact(); + List userList = new ArrayList(); + for (int i = 0; i < userIdList.size(); i++) { + userList.add(userSet.get(userIdList.get(i))); + } + expType.split(userList, amountList, userSet.get(paidByUserId)); + + } + + public void processPercentageInput(int paidByUserId, int amount, List userIdList, List amountList) { + + ExpenseType expType = null; + expType = new Percentage(amount); + List userList = new ArrayList(); + for (int i = 0; i < userIdList.size(); i++) { + userList.add(userSet.get(userIdList.get(i))); + } + expType.split(userList, amountList); + + } + + public void processEqualInput(int paidByUserId, int amount, List userIdList) { + + ExpenseType expType = null; + expType = new Equal(amount); + List userList = new ArrayList(); + for (int i = 0; i < userIdList.size(); i++) { + userList.add(userSet.get(userIdList.get(i))); + } + expType.split(userList); + + } + + public void show() + { + System.out.println("splitwise.SplitWiseApp.show() start "); + for(Integer i : userSet.keySet()) + { + User u = userSet.get(i); + Map payMap = u.getBalance().getPay(); + for(Integer j: payMap.keySet()) + { + System.out.println(u.getName() + " owe rs " + payMap.get(j) + " to user " + userSet.get(j).getName()); + } + } + //System.out.println(" "); + System.out.println(" "); + System.out.println(" "); + } + + public void show(int id) + { + System.out.println("splitwise.SplitWiseApp.show(id) start "); + User u = userSet.get(id); + Balance b = u.getBalance(); + Map payMap = b.getPay(); + for(Integer j: payMap.keySet()) + { + //System.err.println(" j =" + j); + System.out.println(u.getName() + " owe rs " + payMap.get(j) + " to user " + userSet.get(j).getName()); + } + Map getBackMap = b.getGetBack(); + for(Integer j: getBackMap.keySet()) + { + //System.err.println(" j =" + j); + System.out.println(u.getName() + " will get back rs" + getBackMap.get(j) + " from user " + userSet.get(j).getName()); + } + System.out.println(" "); + System.out.println(" "); + } +/* + + SHOW +SHOW u1 +EXPENSE u1 1250 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 + + */ + public static void main(String[] args) { + + SplitWiseApp app = new SplitWiseApp(); + app.createUser(1, "Rajesh", "Rajesh1@gmail.com", "9902543838"); + app.createUser(2, "Rajesh2", "Rajesh2@gmail.com", "9902543839"); + app.createUser(3, "Rajesh3", "Rajesh3@gmail.com", "9902543837"); + app.createUser(4, "Rajesh4", "Rajesh4@gmail.com", "9902543836"); + + app.show(); + app.show(1); + + List l1 = new ArrayList(); + List l2 = new ArrayList(); + List l3 = new ArrayList(); + l1.add(1); + l1.add(2); + l1.add(3); + l1.add(4); + app.processEqualInput(1, 1250, l1); + + app.show(4); + app.show(1); + + l1.clear(); + l1.add(2); + l1.add(3); + l2.add(370.0); + l2.add(880.0); + app.processEaxctInput(1, 1250, l1, l2); + l1.clear(); + + app.show(); + + l1.add(1); + l1.add(2); + l1.add(3); + l1.add(4); + l3.add(40);l3.add(20);l3.add(20);l3.add(20); + app.processPercentageInput(1, 1200, l1, l3); + + app.show(1); + app.show(); + + } + +} diff --git a/splitwise/User.java b/splitwise/User.java new file mode 100644 index 0000000..e2d210a --- /dev/null +++ b/splitwise/User.java @@ -0,0 +1,69 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package splitwise; + +/** + * + * @author rajeshkumar.yadav + */ + +public class User { + + private int useId; + private String name; + private String emailId; + private String mobNo; + public Balance balance; + + public User(int useId, String name, String emailId, String mobNo) { + this.useId = useId; + this.name = name; + this.emailId = emailId; + this.mobNo = mobNo; + balance = new Balance(); + } + + public Balance getBalance() { + return balance; + } + + public void setBalance(Balance balance) { + this.balance = balance; + } + + public int getUseId() { + return useId; + } + + public void setUseId(int useId) { + this.useId = useId; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmailId() { + return emailId; + } + + public void setEmailId(String emailId) { + this.emailId = emailId; + } + + public String getMobNo() { + return mobNo; + } + + public void setMobNo(String mobNo) { + this.mobNo = mobNo; + } + +}