forked from CSEdgeOfficial/Java-Programming-Internship
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBank.java
More file actions
115 lines (101 loc) · 3.69 KB
/
Bank.java
File metadata and controls
115 lines (101 loc) · 3.69 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
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
class Account {
private String accountNumber;
private String holderName;
private double balance;
public Account(String accountNumber, String holderName, double balance) {
this.accountNumber = accountNumber;
this.holderName = holderName;
this.balance = balance;
}
public String getAccountNumber() {
return accountNumber;
}
public String getHolderName() {
return holderName;
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
} else {
System.out.println("Insufficient funds!");
}
}
}
public class OnlineBankingSystem {
private static Map<String, Account> accounts = new HashMap<>();
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
while (true) {
System.out.println("1. Create Account");
System.out.println("2. Deposit");
System.out.println("3. Withdraw");
System.out.println("4. Exit");
System.out.print("Choose an option: ");
int option = scanner.nextInt();
scanner.nextLine(); // Consume newline character
switch (option) {
case 1:
createAccount();
break;
case 2:
deposit();
break;
case 3:
withdraw();
break;
case 4:
System.out.println("Exiting...");
System.exit(0);
default:
System.out.println("Invalid option!");
}
}
}
private static void createAccount() {
System.out.print("Enter account number: ");
String accountNumber = scanner.nextLine();
System.out.print("Enter holder name: ");
String holderName = scanner.nextLine();
System.out.print("Enter initial balance: ");
double balance = scanner.nextDouble();
scanner.nextLine(); // Consume newline character
Account account = new Account(accountNumber, holderName, balance);
accounts.put(accountNumber, account);
System.out.println("Account created successfully!");
}
private static void deposit() {
System.out.print("Enter account number: ");
String accountNumber = scanner.nextLine();
Account account = accounts.get(accountNumber);
if (account != null) {
System.out.print("Enter deposit amount: ");
double amount = scanner.nextDouble();
account.deposit(amount);
System.out.println("Deposit successful. New balance: " + account.getBalance());
} else {
System.out.println("Account not found!");
}
}
private static void withdraw() {
System.out.print("Enter account number: ");
String accountNumber = scanner.nextLine();
Account account = accounts.get(accountNumber);
if (account != null) {
System.out.print("Enter withdrawal amount: ");
double amount = scanner.nextDouble();
account.withdraw(amount);
System.out.println("Withdrawal successful. New balance: " + account.getBalance());
} else {
System.out.println("Account not found!");
}
}
}