Skip to content
This repository was archived by the owner on May 18, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .project
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>
91 changes: 91 additions & 0 deletions SplitWise/src/com/workattech/sw/driver/DriverClass.java
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;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

proper variable names?

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"))
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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);
}
}
}


}

}
14 changes: 14 additions & 0 deletions SplitWise/src/com/workattech/sw/models/Expense.java
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;
}

}
38 changes: 38 additions & 0 deletions SplitWise/src/com/workattech/sw/models/Money.java
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;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

??

}
}

}
37 changes: 37 additions & 0 deletions SplitWise/src/com/workattech/sw/models/User.java
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;
}



}
14 changes: 14 additions & 0 deletions SplitWise/src/com/workattech/sw/service/CalculateExpense.java
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;
}

}
67 changes: 67 additions & 0 deletions SplitWise/src/com/workattech/sw/service/PrintService.java
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 {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better name?


Map<String,ArrayList<Money>> listOfUserWhoOwesToUser;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why global variable?

public void printMoneyOwnedByParticularUser(String user,SplitWiseService splitWiseService)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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");
}
}

}
106 changes: 106 additions & 0 deletions SplitWise/src/com/workattech/sw/service/SplitWiseService.java
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()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CalculateMoneyEqual?
Should be calculateMoneyEqual

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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;
}
}

}

}



}