This repository was archived by the owner on May 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
mock machine 2 submission #6
Open
shishiruniyal
wants to merge
1
commit into
workattech:master
Choose a base branch
from
shishiruniyal:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(" "); | ||
| 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; | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { | ||
|
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. 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; | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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.