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
10 changes: 10 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-13">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Split</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
14 changes: 14 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=13
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=13
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=13
Binary file added bin/Demo/SplitDemo.class
Binary file not shown.
Binary file added bin/Manager/TransactionManager.class
Binary file not shown.
Binary file added bin/Manager/UserManager.class
Binary file not shown.
Binary file added bin/Model/SplitType.class
Binary file not shown.
Binary file added bin/Model/User.class
Binary file not shown.
24 changes: 24 additions & 0 deletions src/Demo/SplitDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package Demo;

import java.util.Arrays;
import java.util.List;

import Manager.TransactionManager;
import Manager.UserManager;
import Model.SplitType;

public class SplitDemo {

public static void main(String[] args) {
UserManager userManager = new UserManager();
userManager.loadUsers();
TransactionManager transManager = new TransactionManager(userManager);
List<String> payee = Arrays.asList("Shika", "Anant", "Deepak");
transManager.splitBill("Deepak", 1500.0, payee, SplitType.EQUAL);
transManager.printAll();
payee = Arrays.asList("Deepak", "Anant");
transManager.splitBill("Shika", 1000.0, payee, SplitType.EQUAL);
transManager.printAll();
}

}
111 changes: 111 additions & 0 deletions src/Manager/TransactionManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package Manager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import Model.SplitType;

public class TransactionManager {
private HashMap<String, HashMap<String, Double>> transaction;

public TransactionManager(UserManager userManager) {
super();
transaction = new HashMap<>();
ArrayList<String> list = userManager.getAllUsers();
for(String user: list) {
HashMap<String, Double> map = new HashMap<>();
transaction.put(user, map);
}
}
public void splitBill(String payerId, Double amount, List<String> payeeId, SplitType type) {
assert(payeeId.size() > 0);
Double splitAmount = amount/(payeeId.size());
splitAmount = Math.round(splitAmount*100)/100.0;
HashMap<String, Double> map = transaction.get(payerId);
for(String payee: payeeId) {
if(payee.equalsIgnoreCase(payerId)) {
continue;
}
HashMap<String, Double> payeeMap = transaction.get(payee);
if(payeeMap.containsKey(payerId)) {
payeeMap.put(payerId, payeeMap.get(payerId)-splitAmount);
} else {
payeeMap.put(payerId, -splitAmount);
}
if(map.containsKey(payee)) {
map.put(payee, map.get(payee)+splitAmount);
} else {
map.put(payee, splitAmount);
}
}
}
public void splitBill(String payer, Double amount, List<String> payeeId, SplitType type,
List<Double> values) {
if(type == SplitType.EXACT) {
double sum = 0;
for(Double value: values) {
sum += value;
}
if(amount != sum || payeeId.size() != values.size()) {
System.out.println("Values don't sum up to total.");
return;
}
HashMap<String, Double> map = transaction.get(payer);
for(int i = 0; i < payeeId.size(); i++) {
String payee = payeeId.get(i);
Double amt = values.get(i);
if(payee.equalsIgnoreCase(payer)) {
continue;
}
HashMap<String, Double> payeeMap = transaction.get(payee);
if(payeeMap.containsKey(payer)) {
payeeMap.put(payer, payeeMap.get(payer)-amt);
} else {
payeeMap.put(payer, -amt);
}
if(map.containsKey(payee)) {
map.put(payee, map.get(payee)+amt);
} else {
map.put(payee, amt);
}
}
} else if(type == SplitType.PERCENTAGE) {
Double total = 0.0, amt = 0.0;
List<Double> owed = new ArrayList<>();
for(Double value: values) {
total += value;
amt = (amount*value)/100;
amt = Math.round(amt*100)/100.0;
owed.add(amt);
}
if(total != 100 || payeeId.size() != values.size()) {
System.out.println("Total doesn't add up.");
}
splitBill(payer, amount, payeeId, SplitType.EXACT, owed);
} else {
splitBill(payer, amount, payeeId, SplitType.EQUAL);
}
}

public void printAll() {
for(Map.Entry<String, HashMap<String, Double>> entrySet: transaction.entrySet()) {
String user = entrySet.getKey();
HashMap<String, Double> payer = entrySet.getValue();
System.out.println("Details of User: "+user);
for(Map.Entry<String, Double> entry: payer.entrySet()) {
Double amt = entry.getValue();
if(amt < 0) {
System.out.println(user+" owes "+-amt+" to "+entry.getKey());
} else if(amt > 0) {
System.out.println(user+" is owed "+amt+" by "+entry.getKey());
} else {
System.out.println("All settled between "+user+" and "+entry.getKey());
}
}
System.out.println();
}
System.out.println("\n\n");
}
}
38 changes: 38 additions & 0 deletions src/Manager/UserManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package Manager;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import Model.User;

public class UserManager {
private HashMap<String, User> userMap;

public UserManager() {
super();
userMap = new HashMap<>();
}

public void loadUsers() {
List<User> users = Arrays.asList(
new User("Shika", "Shika", "Shika@gmail", "76543"),
new User("Deepak", "Deepak", "Deepak@gmail", "76543"),
new User("Anant", "Anant", "Anant@gmail", "76543"),
new User("Nikhil", "Nikhil", "Nikhil@gmail", "76543"),
new User("Pankaj", "Pankaj", "Pankaj@gmail", "76543")
);
for(User user: users) {
userMap.put(user.getUserName(), user);
}
}

public ArrayList<String> getAllUsers() {
ArrayList<String> users = new ArrayList<>();
for(String user: userMap.keySet()) {
users.add(user);
}
return users;
}
}
5 changes: 5 additions & 0 deletions src/Model/SplitType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package Model;

public enum SplitType {
EQUAL, PERCENTAGE, EXACT
}
46 changes: 46 additions & 0 deletions src/Model/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package Model;

public class User {
private String userName, name, email, mobile;

public User(String userName, String name, String email, String mobile) {
super();
this.userName = userName;
this.name = name;
this.email = email;
this.mobile = mobile;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getMobile() {
return mobile;
}

public void setMobile(String mobile) {
this.mobile = mobile;
}

}