-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject-system-Banak.java
More file actions
209 lines (191 loc) · 8.4 KB
/
Project-system-Banak.java
File metadata and controls
209 lines (191 loc) · 8.4 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
import javax.swing.JOptionPane;
public class Main {
static final int MAX = 100; // max accounts
static String[] accountNumbers = new String[MAX];
static String[] accountNames = new String[MAX];
static double[] balances = new double[MAX];
static int accountCount = 0; // number of created accounts
public static void main(String[] args) {
while (true) {
String choiceStr = JOptionPane.showInputDialog(
null,
"Simple Banking Menu\n\n" +
"1 - Create account\n" +
"2 - Deposit\n" +
"3 - Withdraw\n" +
"4 - Check balance\n" +
"0 - Exit\n\n" +
"Enter choice (0-4):",
"Menu",
JOptionPane.QUESTION_MESSAGE);
if (choiceStr == null) break; // user closed/cancelled -> exit
int choice = parseIntSafe(choiceStr, -1);
switch (choice) {
case 1: createAccount(); break;
case 2: deposit(); break;
case 3: withdraw(); break;
case 4: checkBalance(); break;
case 0:
JOptionPane.showMessageDialog(null, "Goodbye!");
System.exit(0);
default:
JOptionPane.showMessageDialog(null, "Invalid choice. Enter 0-4.");
}
}
}
// Create account and store data in parallel arrays (same index = same account)
public static void createAccount() {
if (accountCount >= MAX) {
JOptionPane.showMessageDialog(null, "System full. Cannot create more accounts.");
return;
}
String name = JOptionPane.showInputDialog(null, "Enter account holder name:");
if (name == null) return;
name = name.trim();
if (name.isEmpty()) {
JOptionPane.showMessageDialog(null, "Name cannot be empty.");
return;
}
String depositStr = JOptionPane.showInputDialog(null, "Enter initial deposit (0 or more):");
if (depositStr == null) return;
double dep = parseDoubleSafe(depositStr, -1);
if (dep < 0) {
JOptionPane.showMessageDialog(null, "Invalid deposit amount.");
return;
}
String accNum = String.format("ACC%03d", accountCount + 1); // ACC001 ...
accountNumbers[accountCount] = accNum;
accountNames[accountCount] = name;
balances[accountCount] = dep;
accountCount++;
JOptionPane.showMessageDialog(null,
"Account created!\nAccount Number: " + accNum +
"\nName: " + name + "\nBalance: $" + String.format("%.2f", dep));
}
// Deposit money to an account (lookup is flexible)
public static void deposit() {
if (accountCount == 0) {
JOptionPane.showMessageDialog(null, "No accounts yet. Create one first.");
return;
}
String key = JOptionPane.showInputDialog(null,
"Enter account number (ACC001 / a1 / 1) or account holder name:");
if (key == null) return;
int idx = findAccountIndexFlexible(key.trim());
if (idx == -1) {
JOptionPane.showMessageDialog(null, "Account not found.");
return;
}
String amtStr = JOptionPane.showInputDialog(null,
"Account: " + accountNumbers[idx] + "\nName: " + accountNames[idx] +
"\nCurrent Balance: $" + String.format("%.2f", balances[idx]) +
"\nEnter deposit amount:");
if (amtStr == null) return;
double amt = parseDoubleSafe(amtStr, -1);
if (amt <= 0) {
JOptionPane.showMessageDialog(null, "Deposit must be > 0.");
return;
}
balances[idx] += amt;
JOptionPane.showMessageDialog(null,
"Deposit successful.\nNew Balance: $" + String.format("%.2f", balances[idx]));
}
// Withdraw money with sufficient funds check
public static void withdraw() {
if (accountCount == 0) {
JOptionPane.showMessageDialog(null, "No accounts yet. Create one first.");
return;
}
String key = JOptionPane.showInputDialog(null,
"Enter account number (ACC001 / a1 / 1) or account holder name:");
if (key == null) return;
int idx = findAccountIndexFlexible(key.trim());
if (idx == -1) {
JOptionPane.showMessageDialog(null, "Account not found.");
return;
}
String amtStr = JOptionPane.showInputDialog(null,
"Account: " + accountNumbers[idx] + "\nName: " + accountNames[idx] +
"\nAvailable: $" + String.format("%.2f", balances[idx]) +
"\nEnter withdrawal amount:");
if (amtStr == null) return;
double amt = parseDoubleSafe(amtStr, -1);
if (amt <= 0) {
JOptionPane.showMessageDialog(null, "Amount must be > 0.");
return;
}
if (amt > balances[idx]) {
JOptionPane.showMessageDialog(null, "Insufficient funds.");
return;
}
balances[idx] -= amt;
JOptionPane.showMessageDialog(null,
"Withdrawal successful.\nNew Balance: $" + String.format("%.2f", balances[idx]));
}
// Check balance/details
public static void checkBalance() {
if (accountCount == 0) {
JOptionPane.showMessageDialog(null, "No accounts yet.");
return;
}
String key = JOptionPane.showInputDialog(null,
"Enter account number (ACC001 / a1 / 1) or account holder name:");
if (key == null) return;
int idx = findAccountIndexFlexible(key.trim());
if (idx == -1) {
JOptionPane.showMessageDialog(null, "Account not found.");
return;
}
JOptionPane.showMessageDialog(null,
"Account: " + accountNumbers[idx] +
"\nName: " + accountNames[idx] +
"\nBalance: $" + String.format("%.2f", balances[idx]));
}
// Flexible account lookup:
// - If input like "ACC005" -> match accountNumbers
// - If input like "a5" or "A5" or "5" -> treat as account number index 5 (ACC005)
// - Else try exact name match (case-insensitive), then substring match
public static int findAccountIndexFlexible(String key) {
if (key.isEmpty()) return -1;
String k = key.trim();
// 1) direct account number match
for (int i = 0; i < accountCount; i++) {
if (accountNumbers[i].equalsIgnoreCase(k)) return i;
}
// 2) patterns like "A5", "a5" -> number after letter
if (k.length() >= 2 && Character.toUpperCase(k.charAt(0)) == 'A') {
String rest = k.substring(1).trim();
int n = parseIntSafe(rest, -1);
if (n > 0) {
int idx = n - 1;
if (idx >= 0 && idx < accountCount) return idx;
}
}
// 3) pure number like "5" -> treat as index
int n = parseIntSafe(k, -1);
if (n > 0) {
int idx = n - 1;
if (idx >= 0 && idx < accountCount) return idx;
}
// 4) search by exact name (case-insensitive)
for (int i = 0; i < accountCount; i++) {
if (accountNames[i].equalsIgnoreCase(k)) return i;
}
// 5) search by name contains (case-insensitive)
String kl = k.toLowerCase();
for (int i = 0; i < accountCount; i++) {
if (accountNames[i].toLowerCase().contains(kl)) return i;
}
return -1; // not found
}
// Helper: parse int safely, return defaultVal on error
public static int parseIntSafe(String s, int defaultVal) {
try { return Integer.parseInt(s.trim()); }
catch (Exception e) { return defaultVal; }
}
// Helper: parse double safely, return defaultVal on error
public static double parseDoubleSafe(String s, double defaultVal) {
try { return Double.parseDouble(s.trim()); }
catch (Exception e) { return defaultVal; }
}
}