-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment1.java
More file actions
249 lines (219 loc) · 8.74 KB
/
Assignment1.java
File metadata and controls
249 lines (219 loc) · 8.74 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
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.Arrays;
class Account {
private int accountNumber;
private String accountHolderName;
private double balance;
private String email;
private String phoneNumber;
public Account(int accountNumber, String accountHolderName, double initialDeposit, String email, String phoneNumber) {
this.accountNumber = accountNumber;
this.accountHolderName = accountHolderName;
this.balance = initialDeposit;
this.email = email;
this.phoneNumber = phoneNumber;
}
public int getAccountNumber() {
return accountNumber;
}
public boolean deposit(double amount) {
if (amount <= 0) {
System.out.println("Error: Deposit amount must be positive.");
return false;
}
this.balance += amount;
System.out.println("Deposit successful. New balance: " + this.balance);
return true;
}
public boolean withdraw(double amount) {
if (amount <= 0) {
System.out.println("Error: Withdrawal amount must be positive.");
return false;
}
if (this.balance < amount) {
System.out.println("Error: Insufficient balance.");
return false;
}
this.balance -= amount;
System.out.println("Withdrawal successful. New balance: " + this.balance);
return true;
}
public void displayAccountDetails() {
System.out.println("\n--- Account Details ---");
System.out.println("Account Number: " + accountNumber);
System.out.println("Holder Name: " + accountHolderName);
System.out.println("Balance: " + balance);
System.out.println("Email: " + email);
System.out.println("Phone: " + phoneNumber);
}
public void updateContactDetails(String email, String phoneNumber) {
this.email = email;
this.phoneNumber = phoneNumber;
System.out.println("Contact details updated successfully.");
}
}
public class Main {
private Account[] accounts;
private int accountCount;
private Scanner scanner;
private static final int MAX_ACCOUNTS = 50;
private static int nextAccountNumber = 1001;
public Main() {
accounts = new Account[MAX_ACCOUNTS];
accountCount = 0;
scanner = new Scanner(System.in);
}
private Account findAccount(int accountNumber) {
for (int i = 0; i < accountCount; i++) {
if (accounts[i].getAccountNumber() == accountNumber) {
return accounts[i];
}
}
return null;
}
private int getIntInput(String prompt) throws InputMismatchException {
System.out.print(prompt);
return scanner.nextInt();
}
private double getDoubleInput(String prompt) throws InputMismatchException {
System.out.print(prompt);
return scanner.nextDouble();
}
public void createAccount() {
if (accountCount >= MAX_ACCOUNTS) {
System.out.println("Error: Maximum account limit reached.");
return;
}
try {
System.out.print("Enter account holder name: ");
String name = scanner.nextLine();
double initialDeposit = getDoubleInput("Enter initial deposit amount: ");
scanner.nextLine();
System.out.print("Enter email address: ");
String email = scanner.nextLine();
System.out.print("Enter phone number: ");
String phone = scanner.nextLine();
if (initialDeposit < 0) {
System.out.println("Error: Initial deposit cannot be negative.");
return;
}
Account newAccount = new Account(nextAccountNumber, name, initialDeposit, email, phone);
accounts[accountCount++] = newAccount;
System.out.println("Account created successfully with Account Number: " + nextAccountNumber);
nextAccountNumber++;
} catch (InputMismatchException e) {
System.out.println("Error: Invalid input. Amount must be a number.");
scanner.nextLine();
}
}
public void performDeposit() {
try {
int accountNumber = getIntInput("Enter account number: ");
double amount = getDoubleInput("Enter amount to deposit: ");
scanner.nextLine();
Account account = findAccount(accountNumber);
if (account != null) {
account.deposit(amount);
} else {
System.out.println("Error: Account not found.");
}
} catch (InputMismatchException e) {
System.out.println("Error: Invalid input. Account number and amount must be numbers.");
scanner.nextLine();
}
}
public void performWithdrawal() {
try {
int accountNumber = getIntInput("Enter account number: ");
double amount = getDoubleInput("Enter amount to withdraw: ");
scanner.nextLine();
Account account = findAccount(accountNumber);
if (account != null) {
account.withdraw(amount);
} else {
System.out.println("Error: Account not found.");
}
} catch (InputMismatchException e) {
System.out.println("Error: Invalid input. Account number and amount must be numbers.");
scanner.nextLine();
}
}
public void showAccountDetails() {
try {
int accountNumber = getIntInput("Enter account number: ");
scanner.nextLine();
Account account = findAccount(accountNumber);
if (account != null) {
account.displayAccountDetails();
} else {
System.out.println("Error: Account not found.");
}
} catch (InputMismatchException e) {
System.out.println("Error: Invalid input. Account number must be an integer.");
scanner.nextLine();
}
}
public void updateContact() {
try {
int accountNumber = getIntInput("Enter account number to update: ");
scanner.nextLine();
Account account = findAccount(accountNumber);
if (account != null) {
System.out.print("Enter new email address: ");
String newEmail = scanner.nextLine();
System.out.print("Enter new phone number: ");
String newPhone = scanner.nextLine();
account.updateContactDetails(newEmail, newPhone);
} else {
System.out.println("Error: Account not found.");
}
} catch (InputMismatchException e) {
System.out.println("Error: Invalid input. Account number must be an integer.");
scanner.nextLine();
}
}
public void mainMenu() {
int choice = 0;
do {
System.out.println("\nWelcome to the Banking Application!");
System.out.println("1. Create a new account");
System.out.println("2. Deposit money");
System.out.println("3. Withdraw money");
System.out.println("4. View account details");
System.out.println("5. Update contact details");
System.out.println("6. Exit");
System.out.print("Enter your choice: ");
try {
choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1: createAccount(); break;
case 2: performDeposit(); break;
case 3: performWithdrawal(); break;
case 4: showAccountDetails(); break;
case 5: updateContact(); break;
case 6:
System.out.println("Thank you for using the Banking Application. Goodbye!");
break;
default:
System.out.println("Invalid choice. Please enter a number from 1 to 6.");
}
} catch (InputMismatchException e) {
System.out.println("Error: Invalid input. Please enter a valid menu number.");
choice = 0;
scanner.nextLine();
}
} while (choice != 6);
}
public static void main(String[] args) {
Main app = new Main();
try {
app.mainMenu();
} finally {
if (app.scanner != null) {
app.scanner.close();
}
}
}
}