Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBank.java
More file actions
Latest commit
158 lines (134 loc) · 5.67 KB
/
Copy pathBank.java
File metadata and controls
158 lines (134 loc) · 5.67 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
importjava.util.Scanner;
publicclassBank {
// Data members
privateStringdepositorName;
privateStringdepositorAddress;
privateintaccountNumber;
privatedoublebalance;
// Static variable to generate unique account numbers
privatestaticintnextAccountNumber = 1001;
// Constructor
publicBank(Stringname, Stringaddress, doubleinitialBalance) {
this.depositorName = name;
this.depositorAddress = address;
this.accountNumber = nextAccountNumber++;
this.balance = initialBalance;
}
// Method to display depositor information and balance
publicvoiddisplayInfo() {
System.out.println("\nDepositor Information:");
System.out.println("Name: " + depositorName);
System.out.println("Address: " + depositorAddress);
System.out.println("Account Number: " + accountNumber);
System.out.println("Balance: $" + balance);
}
// Method to deposit amount
publicvoiddeposit(doubleamount) {
if (amount > 0) {
balance += amount;
System.out.println("$" + amount + " deposited successfully.");
} else {
System.out.println("Invalid amount. Deposit amount must be positive.");
}
}
// Method to withdraw amount
publicvoidwithdraw(doubleamount) {
if (amount > 0) {
if (amount <= balance) {
balance -= amount;
System.out.println("$" + amount + " withdrawn successfully.");
} else {
System.out.println("Insufficient balance.");
}
} else {
System.out.println("Invalid amount. Withdrawal amount must be positive.");
}
}
// Method to change address
publicvoidchangeAddress(StringnewAddress) {
this.depositorAddress = newAddress;
System.out.println("Address updated successfully.");
}
// Getter for account number
publicintgetAccountNumber() {
returnaccountNumber;
}
publicstaticvoidmain(String[] args) {
Scannerscanner = newScanner(System.in);
System.out.println("Bank Management System");
System.out.println("---------------------");
// Get number of depositors
System.out.print("Enter the number of depositors: ");
intnumDepositors = scanner.nextInt();
scanner.nextLine(); // Consume newline
// Create array to store Bank objects
Bank[] accounts = newBank[numDepositors];
// Input information for each depositor
for (inti = 0; i < numDepositors; i++) {
System.out.println("\nEnter details for depositor " + (i + 1) + ":");
System.out.print("Name: ");
Stringname = scanner.nextLine();
System.out.print("Address: ");
Stringaddress = scanner.nextLine();
System.out.print("Initial Balance: $");
doublebalance = scanner.nextDouble();
scanner.nextLine(); // Consume newline
accounts[i] = newBank(name, address, balance);
System.out.println("Account created successfully with Account Number: " + accounts[i].getAccountNumber());
}
// Menu-driven operations
intchoice = 0;
do {
System.out.println("\nOperations Menu:");
System.out.println("1. Display depositor information");
System.out.println("2. Deposit amount");
System.out.println("3. Withdraw amount");
System.out.println("4. Change address");
System.out.println("5. Exit");
System.out.print("Enter your choice (1-5): ");
choice = scanner.nextInt();
if (choice >= 1 && choice <= 4) {
System.out.print("Enter Account Number: ");
intaccNum = scanner.nextInt();
scanner.nextLine(); // Consume newline
// Find the account
Bankaccount = null;
for (Bankacc : accounts) {
if (acc.getAccountNumber() == accNum) {
account = acc;
break;
}
}
if (account == null) {
System.out.println("Account not found.");
continue;
}
switch (choice) {
case1:
account.displayInfo();
break;
case2:
System.out.print("Enter amount to deposit: $");
doubledepositAmount = scanner.nextDouble();
account.deposit(depositAmount);
account.displayInfo();
break;
case3:
System.out.print("Enter amount to withdraw: $");
doublewithdrawAmount = scanner.nextDouble();
account.withdraw(withdrawAmount);
account.displayInfo();
break;
case4:
System.out.print("Enter new address: ");
StringnewAddress = scanner.nextLine();
account.changeAddress(newAddress);
account.displayInfo();
break;
}
}
} while (choice != 5);
System.out.println("Thank you for using the Bank Management System.");
scanner.close();
}
}