-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankAccount.java
More file actions
192 lines (149 loc) · 6.02 KB
/
BankAccount.java
File metadata and controls
192 lines (149 loc) · 6.02 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
import java.util.HashMap;
import java.util.Scanner;
public class BankAccount {
private String accountNumber;
private String holderName;
private double balance;
Scanner sc = new Scanner(System.in);
HashMap<String, BankAccount> map = new HashMap<>();
public BankAccount() {
}
public BankAccount(String accountNumber, String holderName, double balance) {
this.accountNumber = accountNumber;
this.holderName = holderName;
this.balance = balance;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
public void setHolderName(String holderName) {
this.holderName = holderName;
}
public void setBalance(double balance) {
this.balance = balance;
}
public String getAccountNumber() {
return accountNumber;
}
public String getHolderName() {
return holderName;
}
public double getBalance() {
return balance;
}
void createAccount() {
System.out.println("Enter account Number");
String accountNumber = sc.nextLine();
System.out.println("Enter Your Name");
String holderName = sc.nextLine();
double balance;
do {
System.out.println("Deposit Minimum 1000 atleast to open account");
balance = sc.nextDouble();
if (balance < 1000) {
System.out.println(" Insufficient Amount ");
} else {
break;
}
} while (balance < 1000);
map.put(accountNumber, new BankAccount(accountNumber, holderName, balance));
System.out.println(" Account Created Successfully!");
System.out.println("\nEnter ~ to Open Transaction Menu Again");
sc.skip("\\R");
sc.nextLine();
}
void depositeMoney(String accountNumber) {
if (map.containsKey(accountNumber)) {
System.out.println("Enter Amount to Deposit");
double amount = sc.nextDouble();
BankAccount accountDetail = map.get(accountNumber);
String name=accountDetail.getHolderName();
double balance = amount + accountDetail.getBalance();
map.put(accountNumber, new BankAccount(accountNumber, name, balance));
System.out.println(" Amount credited Successfully!");
} else {
System.out.println("No Such Account Exist");
System.out.println("'Please Enter Valid Account Number!'");
}
System.out.println("\nEnter ~ to Open Transaction Menu Again");
sc.skip("\\R");
sc.nextLine();
}
void withdrawl(String accountNumber) {
if (map.containsKey(accountNumber)) {
System.out.println("Enter Amount to Withdraw");
double amount = sc.nextDouble();
BankAccount accountDetail = map.get(accountNumber);
if (accountDetail.getBalance() < amount) {
System.out.println("Insufficient Account Balance ");
} else {
double balance = accountDetail.getBalance() - amount;
map.put(accountNumber, new BankAccount(accountNumber, holderName, balance));
System.out.println("Amount Withdrawal Successfully!");
}
} else {
System.out.println("No Such Account Exist");
System.out.println("'Please Enter Valid Account Number!'");
}
System.out.println("\nEnter ~ to Open Transaction Menu Again");
sc.skip("\\R");
sc.nextLine();
}
void checkBalance(String accountNumber) {
if (map.containsKey(accountNumber)) {
BankAccount bankDetail = map.get(accountNumber);
System.out.println("Account Holder Name = " + bankDetail.getHolderName());
System.out.println("Account Balance = " + bankDetail.getBalance());
} else {
System.out.println("No Such Account Exist");
System.out.println("'Please Enter Valid Account Number!'");
}
System.out.println("\nEnter ~ to Open Transaction Menu Again");
sc.nextLine();
}
public static void main(String[] args) {
String task;
Scanner input = new Scanner(System.in);
BankAccount bankAccount = new BankAccount();
do {
System.out.println(" Transact Option :");
System.out.println("1. Create account");
System.out.println("2. Deposit Money");
System.out.println("3. Withdrawl");
System.out.println("4. Check Balance");
System.out.println("5. Exit");
System.out.println("Choose any : ");
task = input.nextLine();
switch (task) {
case "1":
// Create Account
bankAccount.createAccount();
break;
case "2":
// Deposit Money
System.out.println("Enter Account Number");
bankAccount.depositeMoney(input.nextLine());
break;
case "3":
// Withdrawl
System.out.println("Enter Account Number");
bankAccount.withdrawl(input.nextLine());
break;
case "4":
// Check Balance
System.out.println("Enter Account Number");
bankAccount.checkBalance(input.nextLine());
break;
case "5":
// Exit
break;
default:
System.out.println("Please Enter valid option ");
System.out.println("\nEnter ~ to Open Transaction Menu Again");
input.nextLine();
break;
}
} while (task != "5");
input.close();
}
}