-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
54 lines (44 loc) · 1.81 KB
/
Main.java
File metadata and controls
54 lines (44 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<User> roommates = new ArrayList<>();
ExpenseManager manager = new ExpenseManager();
System.out.println("=== VIT Hostel Expense Splitter ===");
System.out.print("How many roommates? ");
int count = sc.nextInt();
for (int i = 0; i < count; i++) {
System.out.print("Enter name of roommate " + (i + 1) + ": ");
roommates.add(new User(sc.next()));
}
System.out.println("\n--- Record an Expense ---");
System.out.print("Enter total amount: ");
double amount = sc.nextDouble();
System.out.print("Who paid this? (Enter Name): ");
String payerName = sc.next();
// Find the user object for the payer
User payer = null;
for (User u : roommates) {
if (u.getName().equalsIgnoreCase(payerName)) {
payer = u;
break;
}
}
if (payer != null) {
manager.splitExpense(amount, payer, roommates);
System.out.println("\n--- Final Settlement Report ---");
for (User u : roommates) {
if (u.getBalance() < 0) {
System.out.printf("%s owes: ₹%.2f\n", u.getName(), Math.abs(u.getBalance()));
} else if (u.getBalance() > 0) {
System.out.printf("%s is owed: ₹%.2f\n", u.getName(), u.getBalance());
} else {
System.out.println(u.getName() + " is all settled up!");
}
}
} else {
System.out.println("Error: Payer name not found in roommate list.");
}
sc.close();
}
}