Skip to content

Commit

Permalink
added splitwise machine coding code: test pending
Browse files Browse the repository at this point in the history
  • Loading branch information
Vishal M Yadav committed Jun 4, 2024
0 parents commit 936268d
Show file tree
Hide file tree
Showing 21 changed files with 644 additions and 0 deletions.
38 changes: 38 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions .idea/checkstyle-idea.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.gatomalvado</groupId>
<artifactId>MachineCoding</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
</dependencies>

</project>
8 changes: 8 additions & 0 deletions src/main/java/com/gatomalvado/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.gatomalvado;

public class Main {

public static void main(String[] args) {
System.out.println("Hello world!");
}
}
104 changes: 104 additions & 0 deletions src/main/java/com/gatomalvado/splitwise/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.gatomalvado.splitwise;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import java.util.stream.Collectors;

import com.gatomalvado.splitwise.model.Expense;
import com.gatomalvado.splitwise.model.ExpenseType;
import com.gatomalvado.splitwise.model.User;
import com.gatomalvado.splitwise.orchestrator.ExpenseManager;

public class Main {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ExpenseManager expenseManager = new ExpenseManager();

// create users
User user1 = User.builder().userId("User1").build();
User user2 = User.builder().userId("User2").build();
User user3 = User.builder().userId("User3").build();
User user4 = User.builder().userId("User4").build();

// add user to the expense manager
expenseManager.addUser(user1);
expenseManager.addUser(user2);
expenseManager.addUser(user3);
expenseManager.addUser(user4);

while(true){
String command = scanner.nextLine();
String[] commands = command.split(" ");
String commandType = commands[0];

switch(commandType){
case "SHOW":
if(commands.length == 2){
expenseManager.showBalanceForUser(commands[1]);
} else {
expenseManager.showBalance();
}
case "EXPENSE":
String userWhoPaid = commands[1];
int numberOfUsers = Integer.parseInt(commands[2]);
String expenseType = commands[3 + numberOfUsers];
List<String> userInvolved = new ArrayList<>();
for(int i=3; i< 3+numberOfUsers; i++){
userInvolved.add(commands[i]);
}
Double amount = Double.parseDouble(commands[4+numberOfUsers]);
Expense expense = null;
switch(expenseType) {
case "EQUAL":
expense = Expense.builder()
.expenseType(ExpenseType.EQUAL)
.paidBy(userWhoPaid)
.paidFor(userInvolved)
.amount(amount)
.build();
expenseManager.addExpense(expense);
case "EXACT":
List<Double> amounts = new ArrayList<>();
for(int i=5+numberOfUsers; i<commands.length; i++){
amounts.add(Double.parseDouble(commands[i]));
}
expense = Expense.builder()
.expenseType(ExpenseType.EXACT)
.paidBy(userWhoPaid)
.paidFor(userInvolved)
.amount(amount)
.exactAmount(amounts)
.build();
expenseManager.addExpense(expense);
case "PERCENT":
List<Double> percentages = new ArrayList<>();
for(int i=5+numberOfUsers; i<commands.length; i++){
percentages.add(Double.parseDouble(commands[i]));
}
expense = Expense.builder()
.expenseType(ExpenseType.PERCENT)
.paidBy(userWhoPaid)
.paidFor(userInvolved)
.amount(amount)
.percentage(percentages)
.build();
expenseManager.addExpense(expense);
default:
// throw exception
System.out.println("Command not supported ");
break;
}
default:
break;
}
}
}



}
Loading

0 comments on commit 936268d

Please sign in to comment.