-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavalab10.java
More file actions
191 lines (172 loc) · 5.49 KB
/
Copy pathjavalab10.java
File metadata and controls
191 lines (172 loc) · 5.49 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
import java.util.Scanner;
class Bank
{
private static int accNo = 1001;
private String address, name;
private double balance;
private int accountNum;
Bank(String address, String name, double balance)
{
this.address = address;
this.name = name;
this.balance = balance;
this.accountNum = accNo++; // Assign current accNo, then increment
}
// Deposit method
void deposit(double amt)
{
if (amt < 0)
{
System.out.println("Amount can't be negative!!");
}
else
{
balance += amt;
System.out.println("Amount deposited successfully!");
}
}
// Withdraw method
void withdraw(double amt)
{
if (amt < 0)
{
System.out.println("You can't withdraw a negative amount!");
}
else if (amt > balance)
{
System.out.println("You can't withdraw more than the balance amount!!");
}
else
{
balance -= amt;
System.out.println("Withdrawal successful!");
}
}
// Change address
void changeAddress(String newAddress)
{
this.address = newAddress;
System.out.println("Address updated successfully!");
}
// Display account details
void show()
{
System.out.println("\nAccount Number: " + accountNum);
System.out.println("Name: " + name);
System.out.println("Address: " + address);
System.out.println("Balance: $" + balance);
}
// Get account number
int getAccountNum()
{
return accountNum;
}
}
public class javalab10
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of bank accounts: ");
int n = sc.nextInt();
Bank[] accounts = new Bank[n];
// Create accounts
for (int i = 0; i < n; i++)
{
sc.nextLine();
System.out.println("\nAccount " + (i + 1));
System.out.print("Name: ");
String name = sc.nextLine();
System.out.print("Address: ");
String address = sc.nextLine();
System.out.print("Initial Balance: ");
double balance = sc.nextDouble();
accounts[i] = new Bank(address, name, balance);
}
int choice;
do
{
System.out.println("\nMenu:");
System.out.println("1. Deposit");
System.out.println("2. Withdraw");
System.out.println("3. Change Address");
System.out.println("4. Show All Accounts");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
choice = sc.nextInt();
switch (choice)
{
case 1: // Deposit
System.out.print("Enter Account Number: ");
int accNum = sc.nextInt();
Bank account = findAccount(accounts, accNum);
if (account != null)
{
System.out.print("Enter amount to deposit: ");
double amt = sc.nextDouble();
account.deposit(amt);
}
else
{
System.out.println("Account not found!");
}
break;
case 2: // Withdraw
System.out.print("Enter Account Number: ");
accNum = sc.nextInt();
account = findAccount(accounts, accNum);
if (account != null)
{
System.out.print("Enter amount to withdraw: ");
double amt = sc.nextDouble();
account.withdraw(amt);
}
else
{
System.out.println("Account not found!");
}
break;
case 3: // Change Address
System.out.print("Enter Account Number: ");
accNum = sc.nextInt();
sc.nextLine();
account = findAccount(accounts, accNum);
if (account != null)
{
System.out.print("Enter new address: ");
String newAddress = sc.nextLine();
account.changeAddress(newAddress);
}
else
{
System.out.println("Account not found!");
}
break;
case 4: // Show all accounts
for (Bank acc : accounts)
{
acc.show();
}
break;
case 5:
System.out.println("Exiting program...");
break;
default:
System.out.println("Enter a valid choice!!");
}
} while (choice != 5);
sc.close();
}
// Method to find account by account number
private static Bank findAccount(Bank[] accounts, int accNum)
{
for (Bank acc : accounts)
{
if (acc.getAccountNum() == accNum)
{
return acc;
}
}
return null;
}
}