Repository navigation
Expand file tree
/
Copy pathBankSystem.java
More file actions
165 lines (129 loc) · 5.79 KB
/
Copy pathBankSystem.java
File metadata and controls
165 lines (129 loc) · 5.79 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
import java.util.Scanner;
import java.util.ArrayList;
public class BankSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<String> accounts = new ArrayList<>();
ArrayList<String> passwords = new ArrayList<>();
ArrayList<Double> balances = new ArrayList<>();
while(true){
System.out.println("==== Bank =====");
System.out.println("1) Open Account");
System.out.println("2) Deposit");
System.out.println("3) Withdraw");
System.out.println("4) View one account");
System.out.println("5) View all accounts");
System.out.println("6) Exit");
System.out.print("Take your pick: ");
int choice = scanner.nextInt();
scanner.nextLine();
if(choice == 1){
System.out.print("Enter account name: ");
String account = scanner.nextLine();
System.out.print("Enter password: ");
String password = scanner.nextLine();
accounts.add(account);
passwords.add(password);
balances.add(0.0);
System.out.println("Account saved.");
} else if(choice == 2) {
if (accounts.size() == 0) {
System.out.println("No accounts available.");
continue;
}
System.out.println("==== Pick the account you would like to deposit in ====");
for (int i = 0; i < accounts.size(); i++) {
System.out.println((i+1) + ") " + accounts.get(i));
}
System.out.print("Enter account number: ");
int index = scanner.nextInt() - 1;
scanner.nextLine();
if (index < 0 || index >= accounts.size()) {
System.out.println("Invalid account number.");
continue;
}
System.out.print("Enter password: ");
String pass = scanner.nextLine();
if (!passwords.get(index).equals(pass)) {
System.out.println("Incorrect password.");
continue;
}
System.out.print("Enter amount to deposit: ");
double amount = scanner.nextDouble();
scanner.nextLine();
if (amount <= 0) {
System.out.println("Amount must be greater than 0.");
} else {
balances.set(index, balances.get(index) + amount);
System.out.println("Deposit successful. New balance: $" + balances.get(index));
}
} else if(choice == 3) {
if (accounts.size() == 0) {
System.out.println("No accounts available.");
continue;
}
System.out.println("==== Pick the account you would like to withdraw from ====");
for (int i = 0; i < accounts.size(); i++){
System.out.println((i+1) + ") " + accounts.get(i));
}
System.out.print("Enter account number: ");
int index = scanner.nextInt() - 1;
scanner.nextLine();
if (index < 0 || index >= accounts.size()) {
System.out.println("Invalid account number.");
continue;
}
System.out.print("Enter password: ");
String pass = scanner.nextLine();
if (!passwords.get(index).equals(pass)) {
System.out.println("Incorrect password.");
continue;
}
System.out.print("Enter amount to withdraw: ");
double amount = scanner.nextDouble();
scanner.nextLine();
if (amount <= 0) {
System.out.println("Amount must be greater than 0.");
} else if (amount > balances.get(index)) {
System.out.println("Not enough money in account.");
} else {
balances.set(index, balances.get(index) - amount);
System.out.println("Withdraw successful. New balance: $" + balances.get(index));
}
} else if(choice == 4) {
if (accounts.size() == 0) {
System.out.println("No accounts available.");
continue;
}
System.out.print("Enter account name: ");
String name = scanner.nextLine();
boolean found = false;
for (int i = 0; i < accounts.size(); i++) {
if (accounts.get(i).equals(name)) {
System.out.println("Account: " + accounts.get(i));
System.out.println("Balance: $" + balances.get(i));
found = true;
break;
}
}
if (!found) {
System.out.println("Account not found.");
}
} else if(choice == 5) {
if (accounts.size() == 0) {
System.out.println("No accounts available.");
continue;
}
System.out.println("==== All Accounts ====");
for (int i = 0; i < accounts.size(); i++) {
System.out.println(accounts.get(i) + " - $" + balances.get(i));
}
} else if(choice == 6) {
System.out.println("Have a great day!");
break;
} else {
System.out.println("Invalid choice.");
}
}
}
}