-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBank.java
More file actions
158 lines (134 loc) · 5.67 KB
/
Bank.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
import java.util.Scanner;
public class Bank {
// Data members
private String depositorName;
private String depositorAddress;
private int accountNumber;
private double balance;
// Static variable to generate unique account numbers
private static int nextAccountNumber = 1001;
// Constructor
public Bank(String name, String address, double initialBalance) {
this.depositorName = name;
this.depositorAddress = address;
this.accountNumber = nextAccountNumber++;
this.balance = initialBalance;
}
// Method to display depositor information and balance
public void displayInfo() {
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
public void deposit(double amount) {
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
public void withdraw(double amount) {
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
public void changeAddress(String newAddress) {
this.depositorAddress = newAddress;
System.out.println("Address updated successfully.");
}
// Getter for account number
public int getAccountNumber() {
return accountNumber;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Bank Management System");
System.out.println("---------------------");
// Get number of depositors
System.out.print("Enter the number of depositors: ");
int numDepositors = scanner.nextInt();
scanner.nextLine(); // Consume newline
// Create array to store Bank objects
Bank[] accounts = new Bank[numDepositors];
// Input information for each depositor
for (int i = 0; i < numDepositors; i++) {
System.out.println("\nEnter details for depositor " + (i + 1) + ":");
System.out.print("Name: ");
String name = scanner.nextLine();
System.out.print("Address: ");
String address = scanner.nextLine();
System.out.print("Initial Balance: $");
double balance = scanner.nextDouble();
scanner.nextLine(); // Consume newline
accounts[i] = new Bank(name, address, balance);
System.out.println("Account created successfully with Account Number: " + accounts[i].getAccountNumber());
}
// Menu-driven operations
int choice = 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: ");
int accNum = scanner.nextInt();
scanner.nextLine(); // Consume newline
// Find the account
Bank account = null;
for (Bank acc : accounts) {
if (acc.getAccountNumber() == accNum) {
account = acc;
break;
}
}
if (account == null) {
System.out.println("Account not found.");
continue;
}
switch (choice) {
case 1:
account.displayInfo();
break;
case 2:
System.out.print("Enter amount to deposit: $");
double depositAmount = scanner.nextDouble();
account.deposit(depositAmount);
account.displayInfo();
break;
case 3:
System.out.print("Enter amount to withdraw: $");
double withdrawAmount = scanner.nextDouble();
account.withdraw(withdrawAmount);
account.displayInfo();
break;
case 4:
System.out.print("Enter new address: ");
String newAddress = scanner.nextLine();
account.changeAddress(newAddress);
account.displayInfo();
break;
}
}
} while (choice != 5);
System.out.println("Thank you for using the Bank Management System.");
scanner.close();
}
}