0% found this document useful (0 votes)
4 views

Template Coding Quer Stion Java

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Template Coding Quer Stion Java

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

13/05/2023, 23:48 download.

svg

Instructions to run the Application


To Build the code run the command

./mvnw clean install -DskipTests

To run the code, follow below instructions

• Navigate to the task/ directory.


• Run the command
• ./mvnw clean install -DskipTests
• Once the build is compiled, run the following
• java -cp target/ExpensesSharingApplication-1.0-SNAPSHOT.jar
com.expense.share.Main
• Once the service starts, input can be provided in the terminal as described in the problem statement.

Expense Sharing Application

1st Task:
Create an expense sharing application.
An expense sharing application is where you can add your expenses and split it among different people.
The app keeps balances between people as in who owes how much to whom.

Explanation
You live with 3 other friends.
You: User1 (id: u1)
Friends: User2 (u2), User3 (u3), User4 (u4)

So you will add all the 4 friends to the app like


Input:
`USER u1 User1Name`
`USER u2 User2Name`
`USER u3 User3Name`
`USER u4 User4Name`

---
This month's Water bill was Rs. 1000.
Now you can just go to the app and add that you paid 1000, select all the 4 people and then select split
equally.

Input

`EXPENSE u1 1000 WaterBill 4 u1 u2 u3 u4 EQUAL`

For this transaction, everyone owes 250 to User1.


The app should update the balances in each of the profiles accordingly.

User2 owes User1: 250 (0+250)


User3 owes User1: 250 (0+250)
User4 owes User1: 250 (0+250)
file:///Users/lovish/Desktop/download.svg 1/5
13/05/2023, 23:48 download.svg
User4 owes User1: 250 (0+250)

Now, It is the BBD sale on Flipkart and there is an offer on your card.
You buy a few stuffs for User2 and User3 as they asked you to.
The total amount for each person is different.

Input

`EXPENSE u1 3500 Flipkart 2 u2 u3 EXACT 1600 1900`

For this transaction, User2 owes 1600 to User1 and User3 owes 1900 to User1.
The app should update the balances in each of the profiles accordingly on below command.

`SHOW`

User2 owes User1: 620 (250+1600)


User3 owes User1: 1130 (250+1900)
User4 owes User1: 250 (250+0)

---

Requirements
User: Each user should have a userId, userName
Expense: Could either be EQUAL, EXACT or PERCENT

• EQUAL - All users have equal amount of contribution in the expense


• EXACT - Each user has different amount of contribution in the expense
• PERCENT - Each user has different percentage of contribution in the expense

• 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.
• The application should have a capability to show expenses for a single user as well as balances for
everyone.
• When asked to show balances, the application should show balances of a user with all the users
where there is a non-zero balance.
• The amount should be rounded off to two decimal places. Say if User1 paid 100 and amount is split
equally among 3 people. Assign 33.34 to first person and 33.33 to others.
• There can be a maximum of 500 Users in the application.

Implement 4 methods in ExpensiveService class

1. `addUser(String input)` -> This method adds a user and is called for inputs starting with USER. Eg->
USER u1 User1Name
2. `addExpense(String input)` -> This method add expenses and is called for inputs starting with EXPENSE.
Eg-> EXPENSE u1 3500 Flipkart 2 u2 u3 EXACT 1600 1900
3. `showBalanceForUser(String input)` -> This method returns an ArrayList of all the expenses and is called
for inputs like "SHOW u1".
4. `showAllBalances()` -> This method returns an array consisting of all the balances for each users and is
called for input "SHOW.

Input

There will be 4 types of input:


file:///Users/lovish/Desktop/download.svg 2/5
13/05/2023, 23:48 download.svg
yp p
1. Add User in the format: USER <user-id> <user-name>
2. Add Expense in the format: EXPENSE <user-id-of-person-who-paid> <amount-paid-by-the-user> <one
word description of the type of transaction> <no-of-users> <space-separated-list-of-users>
<EQUAL/EXACT/PERCENT> <space-separated-values-in-case-of-non-equal>
3. Show balances for all: SHOW
4. Show balances for a single user: SHOW <user-id>

Output
1) Incase of SHOW <user-id>
When asked to show balance for a single user. Show all the balances that user is part of:
Format: <user-name-of-x> owes <amount> to <user-name-of-y>
If there are no balances for the input, then print "No balances for <user-name>"

2) When asked to show balance for all i.e SHOW, for each user print the balance in below format
Format: <user-id-of-x> owes <user-id-of-y>: <amount>
If there are no balances for any user, then print "No balances for <user-name>"

Examples:
1. Input

USER u1 Rajat
USER u2 Shyam
USER u3 Ankit
EXPENSE u1 5000.26 FOOD 3 u1 u2 u3 EQUAL
SHOW

Output

Ankit owes 1666.75 to Rajat


Shyam owes 1666.75 to Rajat

2. Input:

USER u1 Rajat
USER u2 Shyam
USER u3 Ankit
EXPENSE u1 2400.60 FOOD 2 u2 u3 EXACT 1500.25 900.35
SHOW u1
SHOW

Output:

Ankit owes 900.35 to Rajat


Shyam owes 1500.25 to Rajat //Till this point result is for SHOW u1
Ankit owes Rajat: 900.35
Shyam owes Rajat: 1500.25 //Till this point result is for SHOW and
the user is u1
Shyam owes Rajat: 1500.25 //This is for SHOW when the user is u2
Ankit owes Rajat: 900.35 //This is for SHOW when the user is u3

3. Input:

USER u1 Rajat
USER u2 Shyam
USER u3 Ankit
EXPENSE u1 5000 FOOD 3 u1 u2 u3 EQUAL
EXPENSE u1 5000.25 FOOD 3 u1 u2 u3 PERCENT 25.50 40.65 33.85
SHOW u1
file:///Users/lovish/Desktop/download.svg 3/5
13/05/2023, 23:48 download.svg
SHOW u1
SHOW u2

Output:

Ankit owes 3359.25 to Rajat


Shyam owes 3699.27 to Rajat //Till this point result is for SHOW u1
Shyam owes 3699.27 to Rajat //This is for SHOW u2

## 2nd Task:
Show balances of the user once added.

SHOW u1

## 3rd Task:
In `UserDeleteService` Class code is written for the Expense with Dummy Data.This class has a method
`deleteUser(String userId)` which deletes the all the user balances with other users and also deletes the
user itself from the app.
There is a bug implanted in `deleteUser` method due to which code is not working properly. Your task is to
understand the code, identify the bug and fix it.

file:///Users/lovish/Desktop/download.svg 4/5
13/05/2023, 23:48 download.svg

file:///Users/lovish/Desktop/download.svg 5/5

You might also like