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
34 changes: 34 additions & 0 deletions src/com/expense/EqualExpense.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.expense;

import java.util.ArrayList;
import java.util.List;

public class EqualExpense extends Expense{

@Override
public List<ExpenseLog> splitExpense(String expenseDetails) {
String[] details = expenseDetails.split(" ");
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 should not be part of your design. Should have created the expense object and passed here from the main method itself. Parsing the input should not be done here.

String paidByUserId = details[1];
double amount = Double.parseDouble(details[2]);
int totalUsersInvolved = Integer.parseInt(details[3]);
double eachContribution = Double.parseDouble(decimalFormat.format(amount/totalUsersInvolved));
double amountDistributed = 0.0;
List<ExpenseLog> expenseLogList = new ArrayList<ExpenseLog>();
for(int i = 0 ; i < totalUsersInvolved ; i++ ) {
String userId = details[4+i];
ExpenseLog expenseLog = new ExpenseLog(paidByUserId, userId, eachContribution);
expenseLogList.add(expenseLog);
amountDistributed += eachContribution;
}
// calculated offset and add it to first user
double contributionByFirstUser = expenseLogList.get(0).getAmount()+(amount - amountDistributed);
expenseLogList.get(0).setAmount(contributionByFirstUser);
return expenseLogList;
}

@Override
public boolean validateExpense(String expenseDetails) {
return true;
}

}
34 changes: 34 additions & 0 deletions src/com/expense/ExactExpense.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.expense;

import java.util.ArrayList;
import java.util.List;

public class ExactExpense extends Expense {

@Override
public List<ExpenseLog> splitExpense(String expenseDetails) {
String[] details = expenseDetails.split(" ");
String paidByUserId = details[1];
int totalUsersInvolved = Integer.parseInt(details[3]);
List<ExpenseLog> expenseLogList = new ArrayList<ExpenseLog>();
for(int i = 0 ; i < totalUsersInvolved ; i++ ) {
String userId = details[4+i];
double contibution = Double.parseDouble(details[4+totalUsersInvolved+i+1]);
ExpenseLog expenseLog = new ExpenseLog(paidByUserId, userId, contibution);
expenseLogList.add(expenseLog);
}
return expenseLogList;
}

@Override
public boolean validateExpense(String expenseDetails) {
String[] details = expenseDetails.split(" ");
double amount = Double.parseDouble(details[2]);
int totalUsersInvolved = Integer.parseInt(details[3]);
for(int i = 0 ; i < totalUsersInvolved; i++) {
amount -= Double.parseDouble(details[4+totalUsersInvolved+i+1]);
}
return (amount == 0.0)?true:false;
}

}
12 changes: 12 additions & 0 deletions src/com/expense/Expense.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.expense;

import java.text.DecimalFormat;
import java.util.List;

public abstract class Expense {
protected String pattern = "##.##";
protected DecimalFormat decimalFormat = new DecimalFormat(pattern);
public abstract boolean validateExpense(String expenseDetails);
public abstract List<ExpenseLog> splitExpense(String expenseDetails);

}
13 changes: 13 additions & 0 deletions src/com/expense/ExpenseFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.expense;

public class ExpenseFactory {
public static Expense getExpense(String expenseDetails) throws Exception {
if(expenseDetails.contains("EQUAL"))
return new EqualExpense();
else if(expenseDetails.contains("PERCENT"))
return new PercentExpense();
else if(expenseDetails.contains("EXACT"))
return new ExactExpense();
throw new Exception("Unspoorted expense");
}
}
68 changes: 68 additions & 0 deletions src/com/expense/ExpenseLog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.expense;

public class ExpenseLog {
private String piadby;
private String owedBy;
private double amount;
public ExpenseLog(String piadby, String owedBy, double amount) {
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.

typo

this.piadby = piadby;
this.owedBy = owedBy;
this.amount = amount;
}

public String getPiadby() {
return piadby;
}
public void setPiadby(String piadby) {
this.piadby = piadby;
}
public String getOwedBy() {
return owedBy;
}

public void setOwedBy(String owedBy) {
this.owedBy = owedBy;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(amount);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + ((owedBy == null) ? 0 : owedBy.hashCode());
result = prime * result + ((piadby == null) ? 0 : piadby.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ExpenseLog other = (ExpenseLog) obj;
if (Double.doubleToLongBits(amount) != Double.doubleToLongBits(other.amount))
return false;
if (owedBy == null) {
if (other.owedBy != null)
return false;
} else if (!owedBy.equals(other.owedBy))
return false;
if (piadby == null) {
if (other.piadby != null)
return false;
} else if (!piadby.equals(other.piadby))
return false;
return true;
}

}
42 changes: 42 additions & 0 deletions src/com/expense/PercentExpense.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.expense;

import java.util.ArrayList;
import java.util.List;

public class PercentExpense extends Expense {

@Override
public List<ExpenseLog> splitExpense(String expenseDetails) {
String[] details = expenseDetails.split(" ");
String paidByUserId = details[1];
double amount = Double.parseDouble(details[2]);
int totalUsersInvolved = Integer.parseInt(details[3]);
List<ExpenseLog> expenseLogList = new ArrayList<ExpenseLog>();
double amountDistributed = 0.0;
for(int i = 0 ; i < totalUsersInvolved ; i++ ) {
String userId = details[4+i];
double percentContibution = Double.parseDouble(details[4+totalUsersInvolved+i+1]);
double userContribution = Double.parseDouble(decimalFormat.format((amount*percentContibution)/100));
amountDistributed += userContribution;
ExpenseLog expenseLog = new ExpenseLog(paidByUserId, userId, userContribution);
expenseLogList.add(expenseLog);
}
// calculated offset and add it to first user
double contributionByFirstUser = expenseLogList.get(0).getAmount()+(amount - amountDistributed);
expenseLogList.get(0).setAmount(contributionByFirstUser);
return expenseLogList;
}

@Override
public boolean validateExpense(String expenseDetails) {
String[] details = expenseDetails.split(" ");
int totalUsersInvolved = Integer.parseInt(details[3]);
double totalPercent = 100;
for(int i = 0 ; i < totalUsersInvolved ; i++ ) {
double percentContibution = Double.parseDouble(details[4+totalUsersInvolved+i+1]);
totalPercent -=percentContibution;
}
return (totalPercent == 0.0)?true:false;
}

}
85 changes: 85 additions & 0 deletions src/com/expenseSharing/model/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.expenseSharing.model;

public class User {
private String userName;
private String userId;
private String email;
private long mobileNumber;
private Wallet wallet;
public User(String userName, String userId, String email, long mobileNumber) {
this.wallet = new Wallet();
this.userName = userName;
this.userId = userId;
this.email = email;
this.mobileNumber = mobileNumber;
}
public Wallet getWallet() {
return wallet;
}
public void setWallet(Wallet wallet) {
this.wallet = wallet;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public long getMobileNumber() {
return mobileNumber;
}
public void setMobileNumber(long mobileNumber) {
this.mobileNumber = mobileNumber;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((email == null) ? 0 : email.hashCode());
result = prime * result + (int) (mobileNumber ^ (mobileNumber >>> 32));
result = prime * result + ((userId == null) ? 0 : userId.hashCode());
result = prime * result + ((userName == null) ? 0 : userName.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (email == null) {
if (other.email != null)
return false;
} else if (!email.equals(other.email))
return false;
if (mobileNumber != other.mobileNumber)
return false;
if (userId == null) {
if (other.userId != null)
return false;
} else if (!userId.equals(other.userId))
return false;
if (userName == null) {
if (other.userName != null)
return false;
} else if (!userName.equals(other.userName))
return false;
return true;
}

}
26 changes: 26 additions & 0 deletions src/com/expenseSharing/model/Wallet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.expenseSharing.model;

import java.util.HashMap;
import java.util.Map;

public class Wallet {
private final Map<String,Double> ledger;

public Wallet() {
this.ledger = new HashMap<String, Double>();
}
public Map<String, Double> getLedger() {
return ledger;
}

public void addOwedTo(String userId, double amount) {
if(ledger.get(userId) == null)
ledger.put(userId, 0.0);
ledger.put(userId, ledger.get(userId)+amount);
}
public void addOwedFrom(String userId, double amount) {
if(ledger.get(userId) == null)
ledger.put(userId, 0.0);
ledger.put(userId, ledger.get(userId)-amount);
}
}
Loading