diff --git a/SplitWise Problem/input.txt b/SplitWise Problem/input.txt new file mode 100644 index 0000000..3a1cb8a --- /dev/null +++ b/SplitWise Problem/input.txt @@ -0,0 +1,10 @@ +SHOW +SHOW u1 +EXPENSE u1 1000 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 \ No newline at end of file diff --git a/SplitWise Problem/splitwise.java b/SplitWise Problem/splitwise.java new file mode 100644 index 0000000..312d439 --- /dev/null +++ b/SplitWise Problem/splitwise.java @@ -0,0 +1,220 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + + + + +public class Application { + //database that keeps track of user on the basis of their userId + static HashMap database = new HashMap<>(); + public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in)); + + User u1 = new User("u1", "User1", "ramsingh@gmail.com", "8753493733"); + u1.addToDatabase(database); + User u2 = new User("u2", "User2", "ankitsingh@gmail.com", "8676493733"); + u2.addToDatabase(database); + User u3 = new User("u3", "User3", "premsingh@gmail.com", "875343433"); + u3.addToDatabase(database); + User u4 = new User("u4", "User4", "shyamsingh@gmail.com", "87534456733"); + u4.addToDatabase(database); + + //variable that keeps track atleast one sharing took place. + boolean sharingStarted = false; + while(true) { + String[] s = br1.readLine().split(" "); + if(s[0].equalsIgnoreCase("SHOW")) { + if(!sharingStarted) { + System.out.println("No Balance"); + }else { + if(s.length==1) { + Iterator hmIterator = database.entrySet().iterator(); + while(hmIterator.hasNext()) { + Map.Entry mapElement = (Map.Entry)hmIterator.next(); + User u = (User)mapElement.getValue(); + u.showDebter(database); + } + }else if(s.length==2 && database.containsKey(s[1])) { + User u = (User)database.get(s[1]); + u.showDebter(database); + u.showBorrower(database); + }else { + System.out.println("Please Enter correct format."); + } + } + + }else if(s[0].equalsIgnoreCase("EXPENSE")) { + sharingStarted = true; + User u = (User)database.get(s[1]); + double amount = Double.parseDouble(s[2]); + int number =Integer.parseInt(s[3]); + String type = s[3+number+1]; + String[] debtUsers = new String[number]; + for(int i=0;i debt; + private HashMap borrow; + + public User(String userId,String name,String email,String mobileNo) { + this.userId = userId; + this.name = name; + this.email = email; + this.mobileNo = mobileNo; + debt = new HashMap<>(); + borrow = new HashMap<>(); + } + + public String getUserId() { + return userId; + } + + public String getName() { + return name; + } + + public String getEmail() { + return email; + } + + public String getMobileNo() { + return mobileNo; + } + + public HashMap getDebt() { + + return debt; + } + + public HashMap getBorrow() { + return borrow; + } + + //helper function that add all the user who had borrow money + public void addDebt(String debtUser,double amount) { + if(!debtUser.equals(this.userId)) { + if(this.debt.containsKey(debtUser)) { + double a = (double)this.debt.get(debtUser); + a +=amount; + this.debt.put(debtUser, a); + }else { + this.debt.put(debtUser, amount); + } + } + } + + //helper function that add the user whom current user has borrowed the money + public void addBorrow(String borrower,double amount) { + if(!borrower.equals(this.userId)) { + if(this.borrow.containsKey(borrower)) { + double a = (double)this.borrow.get(borrower); + a+=amount; + this.borrow.put(borrower, a); + }else { + this.borrow.put(borrower, amount); + } + } + } + + //helper function that add the user to database + public void addToDatabase(HashMap database) { + database.put(this.getUserId(),this); + } + + //helper function list all user who has taken from him + public void showDebter(HashMap database) { + Iterator debtIterator = debt.entrySet().iterator(); + while(debtIterator.hasNext()) { + Map.Entry mapElement = (Map.Entry)debtIterator.next(); + double value = (double)mapElement.getValue(); + String userId = (String)mapElement.getKey(); + String username = (String)database.get(userId).getName(); + if(value>0) { + System.out.println(String.format("%s owes %s : %.2f",username,this.name,value)); + } + + } + } + + //helper function list all user from whom this user has taken the money. + public void showBorrower(HashMap database) { + Iterator borrowerIterator = borrow.entrySet().iterator(); + while(borrowerIterator.hasNext()) { + Map.Entry mapElement = (Map.Entry)borrowerIterator.next(); + double value = (double)mapElement.getValue(); + String userId = (String)mapElement.getKey(); + String username = (String)database.get(userId).getName(); + if(value>0) { + System.out.println(String.format("%s owes %s : %.2f",this.name,username,value)); + } + } + } + +} \ No newline at end of file