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
3 changes: 3 additions & 0 deletions .idea/.gitignore

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

16 changes: 16 additions & 0 deletions .idea/checkstyle-idea.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/misc.xml

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

9 changes: 9 additions & 0 deletions .idea/mock-machine-coding-2.iml

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

10 changes: 10 additions & 0 deletions .idea/modules.xml

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

Empty file.
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.

6 changes: 3 additions & 3 deletions docs/problem-statement.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ User3 owes User4: 240 (0+240)


## Requirements
- User: Each user should have a userId, name, email, mobile number.
- Expense: Could either be EQUAL, EXACT or PERCENT
- entity.User: Each user should have a userId, name, email, mobile number.
- entity.SplitType: Could either be EQUAL, EXACT or PERCENT
- Users can add any amount, select any type of expense and split with any of the available users.
- The percent and amount provided could have decimals upto two decimal places.
- In case of percent, you need to verify if the total sum of percentage shares is 100 or not.
Expand All @@ -73,7 +73,7 @@ User3 owes User4: 240 (0+240)

- You can create a few users in your main method. No need to take it as input.
- There will be 3 types of input:
- Expense in the format: ```EXPENSE <user-id-of-person-who-paid> <no-of-users> <space-separated-list-of-users> <EQUAL/EXACT/PERCENT> <space-separated-values-in-case-of-non-equal>```
- entity.SplitType in the format: ```EXPENSE <user-id-of-person-who-paid> <no-of-users> <space-separated-list-of-users> <EQUAL/EXACT/PERCENT> <space-separated-values-in-case-of-non-equal>```
- Show balances for all: ```SHOW```
- Show balances for a single user: ```SHOW <user-id>```

Expand Down
Binary file added out/production/snake-ladder/Driver.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added out/production/snake-ladder/SnakeLadderService.class
Binary file not shown.
Binary file added out/production/snake-ladder/models/User.class
Binary file not shown.
Binary file added out/production/snake-ladder/models/object/Dice.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added out/production/splitwise/CommandManager.class
Binary file not shown.
Binary file added out/production/splitwise/SplitwiseMain.class
Binary file not shown.
Binary file added out/production/splitwise/entity/EqualSplit.class
Binary file not shown.
Binary file added out/production/splitwise/entity/ExactSplit.class
Binary file not shown.
Binary file not shown.
Binary file added out/production/splitwise/entity/PercentSplit.class
Binary file not shown.
Binary file added out/production/splitwise/entity/Split.class
Binary file not shown.
Binary file not shown.
Binary file added out/production/splitwise/entity/SplitType.class
Binary file not shown.
Binary file added out/production/splitwise/entity/User.class
Binary file not shown.
10 changes: 10 additions & 0 deletions out/production/splitwise/input.txt
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions snake-ladder/resources/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
9
62 5
33 6
49 9
88 16
41 20
56 53
98 64
93 73
95 75
8
2 37
27 46
10 32
51 68
61 79
65 84
71 91
81 100
2
Gaurav
Sagar
16 changes: 16 additions & 0 deletions snake-ladder/snake-ladder.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="CheckStyle-IDEA-Module">
<option name="configuration">
<map />
</option>
</component>
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="jbr-11" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
34 changes: 34 additions & 0 deletions snake-ladder/src/Driver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Driver {
public static void main(String[] args) throws FileNotFoundException {
SnakeLadderManager snakeLadderManager = new SnakeLadderManager();
File file = new File("/Users/varsha.lalwani/personal/mock-machine-coding-2/snake-ladder/resources/input.txt");
Scanner scanner = new Scanner(file);
String command = scanner.nextLine();
Integer numOfSnakes = Integer.valueOf(command);
for (int i=0; i<numOfSnakes; i++) {
command = scanner.nextLine();
String[] commands = command.split(" ");
snakeLadderManager.addSnake(Integer.valueOf(commands[0]),
Integer.valueOf(commands[1]));
}
command = scanner.nextLine();
Integer numOfLadder = Integer.valueOf(command);
for (int i=0; i<numOfLadder; i++) {
command = scanner.nextLine();
String[] commands = command.split(" ");
snakeLadderManager.addLadder(Integer.valueOf(commands[0]),
Integer.valueOf(commands[1]));
}
command = scanner.nextLine();
Integer numOfUsers = Integer.valueOf(command);
for (int i=0; i<numOfUsers; i++) {
command = scanner.nextLine();
snakeLadderManager.addUser(command);
}
snakeLadderManager.playGame();
}
}
71 changes: 71 additions & 0 deletions snake-ladder/src/SnakeLadderManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import java.util.HashMap;
import java.util.Map;

import models.User;
import models.object.Dice;
import models.object.ObjectType;

public class SnakeLadderManager {

private Map<Integer, Integer> objectsPosMap;

private Map<User, Integer> userPosMap;

SnakeLadderManager() {
objectsPosMap = new HashMap<>();
userPosMap = new HashMap<>();
}

public void addSnake(Integer head, Integer tail) {
SnakeLadderService.addObject(ObjectType.SNAKE, head, tail);
objectsPosMap.put(head, tail);
}

public void addLadder(Integer head, Integer tail) {
SnakeLadderService.addObject(ObjectType.LADDER, head, tail);
objectsPosMap.put(head, tail);
}

public void addUser(String name) {
User user = new User(name);
userPosMap.put(user, 0);
}

public void playGame() {
while (true) {
boolean winnerDeclared = false;
for (User user : userPosMap.keySet()) {
Integer currVal = Dice.getDiceValue(1, 6);
Integer currPos = userPosMap.get(user);
if (currPos + currVal == 100) {
printMove(user, currVal, currPos, currPos+currVal);
printWinner(user);
winnerDeclared = true;
break;
}
if (currPos + currVal > 100) {
printMove(user, currVal, currPos, currPos);
continue;
}
Integer newPos = currPos + currVal;
if (objectsPosMap.containsKey(newPos)) {
newPos = objectsPosMap.get(newPos);
}
userPosMap.put(user, newPos);
printMove(user, currVal, currPos, newPos);
}
if (winnerDeclared) {
break;
}
}
}

public void printMove(User user, Integer diceMove, Integer initialPos, Integer newPos) {
System.out.printf("%s rolled a %s and moved from %s to %s %n", user.getName(), diceMove,
initialPos, newPos);
}
public void printWinner(User user) {
System.out.printf("%s wins the game %n", user.getName());
}

}
19 changes: 19 additions & 0 deletions snake-ladder/src/SnakeLadderService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import models.object.GameObject;
import models.object.Ladder;
import models.object.ObjectType;
import models.object.Snake;

public class SnakeLadderService {

public static GameObject addObject(ObjectType objectType, Integer head, Integer tail) {
switch (objectType) {
case SNAKE:
return new Snake(head, tail);
case LADDER:
return new Ladder(head, tail);
default:
return null;
}
}

}
Loading