-
Notifications
You must be signed in to change notification settings - Fork 83
SplitWise #4
base: master
Are you sure you want to change the base?
SplitWise #4
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <projectDescription> | ||
| <name>mock-machine-coding-2</name> | ||
| <comment></comment> | ||
| <projects> | ||
| </projects> | ||
| <buildSpec> | ||
| </buildSpec> | ||
| <natures> | ||
| </natures> | ||
| </projectDescription> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| package com.workattech.sw.driver; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Scanner; | ||
|
|
||
| import com.workattech.sw.models.User; | ||
| import com.workattech.sw.service.PrintService; | ||
| import com.workattech.sw.service.SplitWiseService; | ||
|
|
||
| public class DriverClass { | ||
| public static void main(String[] args) | ||
| { | ||
| Scanner sc=new Scanner(System.in); | ||
| User u1=new User("u1","u1.gmail.com","3879872789"); | ||
| User u2=new User("u2","u2.gmail.com","3877872789"); | ||
| User u3=new User("u3","u3.gmail.com","3879872889"); | ||
| User u4=new User("u4","u4.gmail.com","3877872989"); | ||
|
|
||
| PrintService printService=new PrintService(); | ||
| SplitWiseService splitWiseService=new SplitWiseService(); | ||
| while(true) | ||
| { | ||
| String input=sc.nextLine(); | ||
| String[] inputarray=input.split("\\s+"); | ||
| System.out.println("length "+inputarray.length); | ||
| if(inputarray[0].equals("SHOW")) | ||
| { | ||
| if(inputarray.length==1) | ||
| { | ||
| printService.printMoneyOwnedByAll(); | ||
| } | ||
| else | ||
| { | ||
| printService.printMoneyOwnedByParticularUser(inputarray[1],splitWiseService); | ||
| } | ||
| } | ||
| else if(inputarray[0].equals("EXPENSE")) | ||
| { | ||
| System.out.println("inside expense"); | ||
| String lender=inputarray[1]; | ||
| int amount=Integer.parseInt(inputarray[2]); | ||
| int noOfborrowers=Integer.parseInt(inputarray[3]); | ||
| ArrayList<String> borrowerList=new ArrayList<String>(); | ||
| int i=4; | ||
| int k=0; | ||
| while(k<noOfborrowers) | ||
| { | ||
| if(!inputarray[i].equals(lender)) { | ||
| borrowerList.add(inputarray[i]); | ||
| } | ||
| k++; | ||
| i++; | ||
| } | ||
| System.out.println("i "+i); | ||
| String Expensetype=inputarray[i]; | ||
|
|
||
| if(Expensetype.equals("EQUAL")) | ||
| { | ||
| System.out.println("inside equal"); | ||
| splitWiseService.distributeMoneyIfEqual(borrowerList,lender,amount); | ||
| } | ||
| else if(Expensetype.equals("PERCENT")) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use switch case for better readability of code. |
||
| { | ||
| ArrayList<Integer> percentList=new ArrayList<>(); | ||
| int m=i+2; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. proper variable names? |
||
| while(m<i+noOfborrowers) | ||
| { | ||
| percentList.add(Integer.parseInt(inputarray[m])); | ||
| m++; | ||
| } | ||
| splitWiseService.distributeMoneyIfPercent(borrowerList, lender, amount, percentList); | ||
| } | ||
| else if(Expensetype.equals("EXACT")) | ||
| { | ||
|
|
||
| ArrayList<Integer> exactList=new ArrayList<>(); | ||
| int m=i+1; | ||
| while(m<=i+noOfborrowers) | ||
| { | ||
| exactList.add(Integer.parseInt(inputarray[m])); | ||
| m++; | ||
| } | ||
| splitWiseService.distributeMoneyIfExact(borrowerList, lender, exactList); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package com.workattech.sw.models; | ||
|
|
||
| public class Expense { | ||
| private String expenseType; | ||
| public Expense(String expenseType) | ||
| { | ||
| this.expenseType=expenseType; | ||
| } | ||
| public String getExpenseType() | ||
| { | ||
| return this.expenseType; | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package com.workattech.sw.models; | ||
|
|
||
| public class Money { | ||
| String personA; | ||
| String personB; | ||
| double amount; | ||
| public Money(String moneyGivenBy,String moneyTakenBy,double amount) | ||
| { | ||
| this.personA=moneyGivenBy; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Money should not know about who paid to whom. A better name would have been expense |
||
| this.personB=moneyTakenBy; | ||
| this.amount=amount; | ||
| } | ||
| public String getPersonA() | ||
| { | ||
| return this.personA; | ||
| } | ||
| public String getPersonB() | ||
| { | ||
| return this.personB; | ||
| } | ||
| public double getAmount() | ||
| { | ||
| return this.amount; | ||
| } | ||
| public void addAmountGivenByPersonAToPersonB(double amountGivenByPersonA) | ||
| { | ||
| this.amount+=amountGivenByPersonA; | ||
| } | ||
| public void substractAmountTakenByPersonAFromPersonB(double amountTakenBypersonA) | ||
| { | ||
| this.amount-=amountTakenBypersonA; | ||
| if(this.amount<0) | ||
| { | ||
| this.amount=0; //No money is owned by personA from personB | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ?? |
||
| } | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package com.workattech.sw.models; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| public class User { | ||
| private String userId; | ||
| private String userName; | ||
| private String email; | ||
| private String mobileNumber; | ||
| public User(String userName,String email,String mobileNumber) | ||
| { | ||
| this.userId=UUID.randomUUID().toString(); | ||
| this.userName=userName; | ||
| this.email=email; | ||
| this.mobileNumber=mobileNumber; | ||
|
|
||
| } | ||
| public String getUserName() | ||
| { | ||
| return this.userName; | ||
| } | ||
| public String getUserId() | ||
| { | ||
| return this.userId; | ||
| } | ||
| public String getEmail() | ||
| { | ||
| return this.email; | ||
| } | ||
| public String getMobileNumber() | ||
| { | ||
| return this.mobileNumber; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package com.workattech.sw.service; | ||
|
|
||
| public class CalculateExpense { | ||
| public double CalculateMoneyEqual(int amount,int number) | ||
| { | ||
| return amount/number; | ||
| } | ||
| public double CalculateMoneyPercent(int amount,int percent) | ||
| { | ||
| double share=percent/100.0; | ||
| return amount*share; | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package com.workattech.sw.service; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Map; | ||
| import java.util.Map.Entry; | ||
|
|
||
| import com.workattech.sw.models.Money; | ||
| import com.workattech.sw.models.User; | ||
|
|
||
| public class PrintService { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. better name? |
||
|
|
||
| Map<String,ArrayList<Money>> listOfUserWhoOwesToUser; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why global variable? |
||
| public void printMoneyOwnedByParticularUser(String user,SplitWiseService splitWiseService) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method is not very readable |
||
| { | ||
| listOfUserWhoOwesToUser=splitWiseService.getlistOfUserWhoOwesToUser(); | ||
| System.out.println("inside print user"); | ||
| boolean borrowerListPrinted=false; | ||
| System.out.println("lender"+user); | ||
| for(Entry<String, ArrayList<Money>> userList:listOfUserWhoOwesToUser.entrySet()) { | ||
| System.out.println("user"+userList.getKey()); | ||
| } | ||
| if(listOfUserWhoOwesToUser.containsKey(user)) { | ||
| ArrayList<Money> borrowerList=listOfUserWhoOwesToUser.get(user); | ||
| System.out.println("borrowerList "+borrowerList); | ||
| for(int i=0;i<borrowerList.size();i++) | ||
| { | ||
| if(borrowerList.get(i).getAmount()!=0) | ||
| { | ||
| System.out.println(borrowerList.get(i).getPersonB()+" ownes "+user+" "+borrowerList.get(i).getAmount()); | ||
| borrowerListPrinted=true; | ||
| } | ||
|
|
||
|
|
||
| } | ||
|
|
||
| } | ||
| if(!borrowerListPrinted) | ||
| { | ||
| System.out.println("No expenses"); | ||
| } | ||
| } | ||
| public void printMoneyOwnedByAll() | ||
| { | ||
|
|
||
| boolean borrowerListPrinted=false; | ||
| for(Entry<String, ArrayList<Money>> userList:listOfUserWhoOwesToUser.entrySet()) { | ||
| String user=userList.getKey(); | ||
| ArrayList<Money> borrowerList=userList.getValue(); | ||
| for(int i=0;i<borrowerList.size();i++) | ||
| { | ||
| if(borrowerList.get(i).getAmount()!=0) | ||
| { | ||
| System.out.println(borrowerList.get(i).getPersonB()+" ownes "+user+" "+borrowerList.get(i).getAmount()); | ||
| borrowerListPrinted=true; | ||
| } | ||
|
|
||
|
|
||
| } | ||
|
|
||
| } | ||
| if(!borrowerListPrinted) | ||
| { | ||
| System.out.println("No expenses"); | ||
| } | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| package com.workattech.sw.service; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| import com.workattech.sw.models.Expense; | ||
| import com.workattech.sw.models.Money; | ||
| import com.workattech.sw.models.User; | ||
|
|
||
| public class SplitWiseService { | ||
| ArrayList<User> userList=new ArrayList<User>(); | ||
| CalculateExpense calculateExpense=new CalculateExpense(); | ||
| public Map<String,ArrayList<Money>> listOfUserWhoOwesToUser=new HashMap<>(); | ||
| public Map<String,ArrayList<Money>> getlistOfUserWhoOwesToUser() | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should use proper camelCase |
||
| { | ||
|
|
||
| return this.listOfUserWhoOwesToUser; | ||
| } | ||
| public void setUserList(ArrayList<User> userList) | ||
| { | ||
| this.userList=userList; | ||
| } | ||
| public void distributeMoneyIfEqual(ArrayList<String> borrowers,String lender,int amount) | ||
| { | ||
|
|
||
| double amountToBeDistributed=calculateExpense.CalculateMoneyEqual(amount,borrowers.size()+1); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CalculateMoneyEqual?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same comment for other methods |
||
| for(int i=0;i<borrowers.size();i++) { | ||
| addMoneyToLender(borrowers.get(i),lender,amountToBeDistributed); | ||
| subtractMoneyFromBorrowers(borrowers.get(i),lender,amountToBeDistributed); | ||
| } | ||
| } | ||
| public void distributeMoneyIfExact(ArrayList<String> borrowers,String lender,ArrayList<Integer> exactShare) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of exposing multiple methods from here. You could have created a single method like addExpense and taken an expense object as param. And deciding which distribution to choose could be done here |
||
| { | ||
| System.out.println("inside exact"); | ||
| for(int i=0;i<borrowers.size();i++) { | ||
| addMoneyToLender(borrowers.get(i),lender,exactShare.get(i)); | ||
|
|
||
| subtractMoneyFromBorrowers(borrowers.get(i),lender,exactShare.get(i)); | ||
| } | ||
| } | ||
| public void distributeMoneyIfPercent(ArrayList<String> borrowers,String lender,int amount,ArrayList<Integer> PercentShare) | ||
| { | ||
| for(int i=0;i<borrowers.size();i++) { | ||
| double amountToBeDistributed=calculateExpense.CalculateMoneyPercent(amount,PercentShare.get(i)); | ||
| System.out.println("amountToBeDistributed"+amountToBeDistributed); | ||
| addMoneyToLender(borrowers.get(i),lender,amountToBeDistributed); | ||
|
|
||
| subtractMoneyFromBorrowers(borrowers.get(i),lender,amountToBeDistributed); | ||
| } | ||
| } | ||
|
|
||
| public void addMoneyToLender(String borrower,String lender,double amountToBeDistributed) | ||
| { | ||
| System.out.println("lender"+lender); | ||
| if(listOfUserWhoOwesToUser.containsKey(lender)) | ||
| { | ||
| System.out.println("inside addMoneyToLender1 "); | ||
| ArrayList<Money> moneyBorrowers=listOfUserWhoOwesToUser.get(lender); | ||
| boolean balanceAddedToBorrower=false; | ||
| for(int i=0;i<moneyBorrowers.size();i++) | ||
| { | ||
| if(moneyBorrowers.get(i).getPersonB().equals(borrower)) | ||
| { | ||
| moneyBorrowers.get(i).addAmountGivenByPersonAToPersonB(amountToBeDistributed); | ||
| listOfUserWhoOwesToUser.put(lender, moneyBorrowers); | ||
| balanceAddedToBorrower=true; | ||
| } | ||
| } | ||
| if(!balanceAddedToBorrower) | ||
| { | ||
| moneyBorrowers.add(new Money(lender,borrower,amountToBeDistributed)); | ||
| listOfUserWhoOwesToUser.put(lender,moneyBorrowers); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| System.out.println("inside addMoneyToLender2 "); | ||
| ArrayList<Money> moneyBorrowers=new ArrayList<Money>(); | ||
| moneyBorrowers.add(new Money(lender,borrower,amountToBeDistributed)); | ||
| listOfUserWhoOwesToUser.put(lender,moneyBorrowers); | ||
| } | ||
| } | ||
| public void subtractMoneyFromBorrowers(String borrower,String lender,double amountToBeDistributed) | ||
| { | ||
| if(listOfUserWhoOwesToUser.containsKey(borrower)) | ||
| { | ||
| ArrayList<Money> moneyBorrowers=listOfUserWhoOwesToUser.get(borrower); | ||
| boolean balanceAddedToBorrower=false; | ||
| for(int i=0;i<moneyBorrowers.size();i++) | ||
| { | ||
| if(moneyBorrowers.get(i).getPersonB().equals(lender)) | ||
| { | ||
| moneyBorrowers.get(i).substractAmountTakenByPersonAFromPersonB(amountToBeDistributed); | ||
| listOfUserWhoOwesToUser.put(borrower, moneyBorrowers); | ||
| balanceAddedToBorrower=true; | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
proper variable names?