-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
269 lines (230 loc) · 8.33 KB
/
Copy pathMain.java
File metadata and controls
269 lines (230 loc) · 8.33 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class Main {
static class Transaction {
String type;
double amount;
double balanceAfter;
LocalDateTime timestamp;
Transaction(String type, double amount, double balanceAfter) {
this.type = type;
this.amount = amount;
this.balanceAfter = balanceAfter;
this.timestamp = LocalDateTime.now();
}
@Override
public String toString() {
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return String.format("[%s] %-8s $%-10.2f Balance after: $%.2f",
timestamp.format(fmt), type, amount, balanceAfter);
}
}
static abstract class Account {
String accountNumber;
String ownerName;
double balance;
List<Transaction> history = new ArrayList<>();
Account(String accountNumber, String ownerName, double balance) {
this.accountNumber = accountNumber;
this.ownerName = ownerName;
this.balance = balance;
}
abstract String getAccountType();
abstract double getMinimumBalance();
void deposit(double amount) {
balance += amount;
history.add(new Transaction("DEPOSIT", amount, balance));
}
void withdraw(double amount) {
balance -= amount;
history.add(new Transaction("WITHDRAW", amount, balance));
}
}
static class SavingsAccount extends Account {
SavingsAccount(String accountNumber, String ownerName, double balance) {
super(accountNumber, ownerName, balance);
}
@Override
String getAccountType() {
return "Savings";
}
@Override
double getMinimumBalance() {
return 100;
}
}
static class CheckingAccount extends Account {
CheckingAccount(String accountNumber, String ownerName, double balance) {
super(accountNumber, ownerName, balance);
}
@Override
String getAccountType() {
return "Checking";
}
@Override
double getMinimumBalance() {
return -200;
}
}
static Map<String, Account> accounts = new HashMap<>();
static Scanner scanner = new Scanner(System.in);
static int nextAccountNumber = 1001;
public static void main(String[] args) {
boolean running = true;
while (running) {
printMenu();
String choice = scanner.nextLine().trim();
switch (choice) {
case "1":
createAccount();
break;
case "2":
viewAccountDetails();
break;
case "3":
deposit();
break;
case "4":
withdraw();
break;
case "5":
viewTransactionHistory();
break;
case "6":
running = false;
System.out.println("Goodbye!");
break;
default:
System.out.println("Invalid option. Please try again.");
}
}
scanner.close();
}
static void printMenu() {
System.out.println("\n===== Console Bank =====");
System.out.println("1. Create account");
System.out.println("2. View account details");
System.out.println("3. Deposit money");
System.out.println("4. Withdraw money");
System.out.println("5. View transaction history");
System.out.println("6. Exit");
System.out.print("Choose an option: ");
}
static void createAccount() {
System.out.print("Enter owner name: ");
String name = scanner.nextLine().trim();
if (name.isEmpty()) {
System.out.println("Name cannot be empty.");
return;
}
String type;
while (true) {
System.out.print("Enter account type (checking/savings): ");
type = scanner.nextLine().trim().toLowerCase();
if (type.equals("checking") || type.equals("savings")) {
break;
}
System.out.println("Please enter 'checking' or 'savings'.");
}
double initialDeposit = 0;
while (true) {
System.out.print("Enter initial deposit amount (0 or more): ");
try {
initialDeposit = Double.parseDouble(scanner.nextLine().trim());
if (initialDeposit < 0) {
System.out.println("Amount cannot be negative.");
continue;
}
break;
} catch (NumberFormatException e) {
System.out.println("Please enter a valid number.");
}
}
String accountNumber = String.valueOf(nextAccountNumber++);
Account account = type.equals("savings")
? new SavingsAccount(accountNumber, name, 0)
: new CheckingAccount(accountNumber, name, 0);
if (initialDeposit > 0) {
account.deposit(initialDeposit);
}
accounts.put(accountNumber, account);
System.out.println("Account created successfully!");
System.out.println("Your account number is: " + accountNumber);
}
static Account findAccount() {
System.out.print("Enter account number: ");
String accountNumber = scanner.nextLine().trim();
Account account = accounts.get(accountNumber);
if (account == null) {
System.out.println("Account not found.");
}
return account;
}
static void viewAccountDetails() {
Account account = findAccount();
if (account == null) return;
System.out.println("\n--- Account Details ---");
System.out.println("Account Number: " + account.accountNumber);
System.out.println("Owner Name: " + account.ownerName);
System.out.println("Account Type: " + account.getAccountType());
System.out.printf("Balance: $%.2f%n", account.balance);
}
static void deposit() {
Account account = findAccount();
if (account == null) return;
System.out.print("Enter amount to deposit: ");
double amount;
try {
amount = Double.parseDouble(scanner.nextLine().trim());
} catch (NumberFormatException e) {
System.out.println("Please enter a valid number.");
return;
}
if (amount <= 0) {
System.out.println("Deposit amount must be positive.");
return;
}
account.deposit(amount);
System.out.printf("Deposit successful. New balance: $%.2f%n", account.balance);
}
static void withdraw() {
Account account = findAccount();
if (account == null) return;
System.out.print("Enter amount to withdraw: ");
double amount;
try {
amount = Double.parseDouble(scanner.nextLine().trim());
} catch (NumberFormatException e) {
System.out.println("Please enter a valid number.");
return;
}
if (amount <= 0) {
System.out.println("Withdrawal amount must be positive.");
return;
}
if (account.balance - amount < account.getMinimumBalance()) {
System.out.printf("Insufficient funds. %s accounts cannot go below $%.2f.%n",
account.getAccountType(), account.getMinimumBalance());
return;
}
account.withdraw(amount);
System.out.printf("Withdrawal successful. New balance: $%.2f%n", account.balance);
}
static void viewTransactionHistory() {
Account account = findAccount();
if (account == null) return;
if (account.history.isEmpty()) {
System.out.println("No transactions yet.");
return;
}
System.out.println("\n--- Transaction History for " + account.accountNumber + " ---");
for (Transaction t : account.history) {
System.out.println(t);
}
}
}