-
Notifications
You must be signed in to change notification settings - Fork 0
/
Task3_AtmInterface.java
209 lines (184 loc) · 6.98 KB
/
Task3_AtmInterface.java
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
package company.com;
import java.util.Scanner;
class BankAccount{
String name;
String userName;
String password;
String accountNo;
float balance = 10000f;
int transactions = 0;
String transactionHistory = "";
public void register() {
Scanner sc = new Scanner(System.in);
System.out.println("\nEnter your Name: ");
this.name = sc.nextLine();
System.out.println("\nEnter your Username: ");
this.userName = sc.nextLine();
System.out.println("\nEnter your Password: ");
this.password = sc.nextLine();
System.out.println("\nEnter your Account Number: ");
this.accountNo = sc.nextLine();
System.out.println("\nRegistration Successful. Please Log in to your Bank Account");
}
public boolean login() {
boolean isLogin = false;
Scanner sc=new Scanner(System.in);
while( !isLogin) {
System.out.println("\nEnter your username: ");
String Username = sc.nextLine();
if (Username.equals(userName)) {
while(!isLogin) {
System.out.println("\nEnter your password: ");
String Password = sc.nextLine();
if(Password.equals(password)) {
System.out.println("\nLogin Successful");
isLogin = true;
}
else {
System.out.println("\nIncorrect Password");
}
}
}else {
System.out.println("\nUsername not found");
}
}
return isLogin;
}
public void withdraw() {
System.out.println("\nEnter Amount to Withdraw: ");
Scanner sc=new Scanner(System.in);
float amount = sc.nextFloat();
try {
if(balance >= amount) {
transactions++;
balance -= amount;
System.out.println("\nWithdral Successful.");
String str = amount + "Rs Withdrawn\n";
transactionHistory = transactionHistory.concat(str);
}else {
System.out.println("\nInsufficient Balance.");
}
}catch(Exception e) {
}
}
public void deposit() {
System.out.println("\nEnter Amount to Deposit: ");
Scanner sc=new Scanner(System.in);
float amount = sc.nextFloat();
try {
if(amount <= 10000f) {
transactions++;
balance += amount;
System.out.println("\nDeposit Successful.");
String str = amount + "Rs deposited\n";
transactionHistory = transactionHistory.concat(str);
}else {
System.out.println("\nSorry. The limit is 10000.");
}
}catch(Exception e) {
}
}
public void transfer() {
Scanner sc=new Scanner(System.in);
System.out.println("\nEnter Receipent's Name: ");
String receipent = sc.nextLine();
System.out.println("\nEnter Amount to transfer: ");
float amount = sc.nextFloat();
try {
if(balance>= amount) {
if(amount <= 50000f) {
transactions++;
balance -= amount;
System.out.println("\nSuccesfully Transferred to "+ receipent);
String str = amount + "Rs transferred to " + receipent+"\n";
transactionHistory = transactionHistory.concat(str);
}else {
System.out.println("\nSorry. The limit is 50000.");
}
}else{
System.out.println("\nInsufficient Balance.");
}}catch(Exception e) {
}
}
public void checkBalance() {
System.out.println("\n"+balance+"Rs");
}
public void transHistory() {
if(transactions ==0) {
System.out.println("No TRansactions happened");
}else {
System.out.print("\n"+transactionHistory);
}
}
}
public class Task3_AtmInterface {
public static int takenIntegerInput(int limit) {
int input = 0;
boolean flag = false;
while(!flag) {
try {
Scanner sc = new Scanner(System.in);
input = sc.nextInt();
flag = true;
if(flag && input>limit || input<1) {
System.out.println("Choose the number between 1 to "+limit);
flag=false;
}
}catch(Exception e) {
System.out.println("Enter only integer value.");
flag=false;
}
}
return input;
}
public static void main(String[] args) {
System.out.println("\n*WELCOME TO SBI ATM INTERFACE");
System.out.println("\n1.Register \n2.Exit");
System.out.println("Choose one option: ");
int choose = takenIntegerInput(2);
if(choose == 1) {
BankAccount b= new BankAccount();
b.register();
while(true) {
System.out.println("\n1.Login \n2.Exit");
System.out.println("Enter your choice: ");
int ch = takenIntegerInput(2);
if(ch==1) {
if(b.login()) {
System.out.println("\n*WELCOME BACK"+b.name +"");
boolean isFinished = false;
while(!isFinished) {
System.out.println("\n1.withdraw \n2.Deposit \n3.Transfer \n4.check balance \n5.Transaction History \n6.Exit");
System.out.println("Enter your choice: ");
int c = takenIntegerInput(6);
switch(c) {
case 1:
b.withdraw();
break;
case 2:
b.deposit();
break;
case 3:
b.transfer();
break;
case 4:
b.checkBalance();
break;
case 5:
b.transHistory();
break;
case 6:
isFinished = true;
break;
}
}
}
}else {
System.exit(0);
}
}
}else {
System.exit(0);
}
}
}