This repository was archived by the owner on Jun 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathOnlineBankingSystemMain.java
More file actions
154 lines (139 loc) · 5.28 KB
/
OnlineBankingSystemMain.java
File metadata and controls
154 lines (139 loc) · 5.28 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
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
class BankAccount {
private String accountNumber;
private double balance;
private Map<String, Double> transactionHistory;
public BankAccount(String accountNumber, double initialBalance) {
this.accountNumber = accountNumber;
this.balance = initialBalance;
this.transactionHistory = new HashMap<>();
}
public void deposit(double amount) {
balance += amount;
transactionHistory.put("Deposit", amount);
}
public boolean withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
transactionHistory.put("Withdrawal", -amount);
return true;
}
return false;
}
public double getBalance() {
return balance;
}
public Map<String, Double> getTransactionHistory() {
return transactionHistory;
}
}
class OnlineBankingSystem {
private Map<String, BankAccount> accounts;
private Scanner scanner;
public OnlineBankingSystem() {
accounts = new HashMap<>();
scanner = new Scanner(System.in);
}
public void createAccount() {
System.out.println("Enter account number:");
String accountNumber = scanner.nextLine();
System.out.println("Enter initial balance:");
double initialBalance = scanner.nextDouble();
scanner.nextLine(); // Consume newline
BankAccount account = new BankAccount(accountNumber, initialBalance);
accounts.put(accountNumber, account);
System.out.println("Account created successfully.");
}
public void deposit() {
System.out.println("Enter account number:");
String accountNumber = scanner.nextLine();
BankAccount account = accounts.get(accountNumber);
if (account != null) {
System.out.println("Enter deposit amount:");
double amount = scanner.nextDouble();
scanner.nextLine(); // Consume newline
account.deposit(amount);
System.out.println("Deposit successful.");
} else {
System.out.println("Account not found.");
}
}
public void withdraw() {
System.out.println("Enter account number:");
String accountNumber = scanner.nextLine();
BankAccount account = accounts.get(accountNumber);
if (account != null) {
System.out.println("Enter withdrawal amount:");
double amount = scanner.nextDouble();
scanner.nextLine(); // Consume newline
if (account.withdraw(amount)) {
System.out.println("Withdrawal successful.");
} else {
System.out.println("Insufficient balance.");
}
} else {
System.out.println("Account not found.");
}
}
public void displayBalance() {
System.out.println("Enter account number:");
String accountNumber = scanner.nextLine();
BankAccount account = accounts.get(accountNumber);
if (account != null) {
System.out.println("Current balance: " + account.getBalance());
} else {
System.out.println("Account not found.");
}
}
public void displayTransactionHistory() {
System.out.println("Enter account number:");
String accountNumber = scanner.nextLine();
BankAccount account = accounts.get(accountNumber);
if (account != null) {
System.out.println("Transaction History:");
Map<String, Double> history = account.getTransactionHistory();
for (Map.Entry<String, Double> entry : history.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
} else {
System.out.println("Account not found.");
}
}
}
public class OnlineBankingSystemMain {
public static void main(String[] args) {
OnlineBankingSystem bankingSystem = new OnlineBankingSystem();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("\n1. Create Account\n2. Deposit\n3. Withdraw\n4. Display Balance\n5. Display Transaction History\n6. Exit\n");
System.out.println("Enter your choice:");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
bankingSystem.createAccount();
break;
case 2:
bankingSystem.deposit();
break;
case 3:
bankingSystem.withdraw();
break;
case 4:
bankingSystem.displayBalance();
break;
case 5:
bankingSystem.displayTransactionHistory();
break;
case 6:
System.out.println("Exiting...");
scanner.close();
System.exit(0);
default:
System.out.println("Invalid choice. Please enter a number from 1 to 6.");
}
}
}
}