-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
143 lines (130 loc) · 5.7 KB
/
Main.java
File metadata and controls
143 lines (130 loc) · 5.7 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
private static List<User> users = new ArrayList<>();
private static final int MAX_USERS = 10;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// User interface
System.out.println("Welcome to My Finance App!");
while (true) {
System.out.println("\nMain Menu:");
System.out.println("1. Login");
System.out.println("2. Register");
System.out.println("3. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
User user = login(scanner);
if (user != null) {
FinanceManager financeManager = new FinanceManager(user);
userMenu(scanner, user, financeManager);
}
break;
case 2:
if (users.size() < MAX_USERS) {
User newUser = register(scanner);
if (newUser != null) {
users.add(newUser);
}
} else {
System.out.println("User registration limit reached.");
}
break;
case 3:
System.out.println("Exiting My Finance App. Goodbye!");
System.exit(0);
break;
default:
System.out.println("Wrong choice. Try again.");
}
}
}
private static void userMenu(Scanner scanner, User user, FinanceManager financeManager) {
boolean exit = false;
while (!exit) {
System.out.println("\nUser Menu:");
System.out.println("1. Show financial condition");
System.out.println("2. Add new income");
System.out.println("3. Deduct expenses");
System.out.println("4. Calculate future expenses and remaining balance");
System.out.println("5. Logout");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
System.out.println("\nFinancial Condition:");
System.out.println("Total Income: $" + user.getTotalIncome());
System.out.println("Total Expenses: $" + user.getTotalExpenses());
System.out.println("Remaining Balance: $" + financeManager.calculateRemainingBalance());
break;
case 2:
System.out.print("\nEnter amount of new income: $");
double income = scanner.nextDouble();
financeManager.addIncome(income);
System.out.println("Income added successfully.");
break;
case 3:
System.out.print("\nEnter amount of expenses to deduct: $");
double expenses = scanner.nextDouble();
financeManager.deductExpenses(expenses);
System.out.println("Expenses deducted successfully.");
break;
case 4:
System.out.print("\nEnter amount of future expenses: $");
double futureExpenses = scanner.nextDouble();
double remainingBalance = financeManager.calculateFutureExpensesAndBalance(futureExpenses);
System.out.println("After deducting future expenses, remaining balance will be: $" + remainingBalance);
break;
case 5:
exit = true;
System.out.println("\nLogged out successfully.");
break;
default:
System.out.println("Wrong choice. Try again.");
}
}
}
// Method to handle user registration
private static User register(Scanner scanner) {
System.out.println("\nUser Registration:");
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.print("Enter desired username: ");
String username = scanner.nextLine();
System.out.print("Enter password: ");
String password = scanner.nextLine();
System.out.print("Enter total income: $");
double totalIncome = scanner.nextDouble();
System.out.print("Enter total expenses: $");
double totalExpenses = scanner.nextDouble();
scanner.nextLine(); // Consume newline
// Check if the username is already taken
for (User user : users) {
if (user.getUsername().equals(username)) {
System.out.println("Username already taken. Please try a different one.");
return null;
}
}
return new User(name, username, password, totalIncome, totalExpenses);
}
// Method to handle user login
private static User login(Scanner scanner) {
System.out.println("\nUser Login:");
System.out.print("Username: ");
String username = scanner.nextLine();
System.out.print("Password: ");
String password = scanner.nextLine();
for (User user : users) {
if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
return user;
}
}
System.out.println("Invalid username or password. Please try again.");
return null;
}
}