-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathATM_interface.java
196 lines (170 loc) · 6.61 KB
/
ATM_interface.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
package Firstproject;
import java.util.Scanner;
class BankAccount {
int balance;
int prevTransaction;
String customerName;
String customerId;
int flag = 0;
BankAccount(String cName, String cId) {
customerName = cName;
customerId = cId;
}
public final void clrscr() {
try {
try {
final String os = System.getProperty("os.name");
if (os.contains("Windows")) {
Runtime.getRuntime().exec("cls");
} else {
Runtime.getRuntime().exec("clear");
}
} catch (final Exception e) {
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
}
} catch (final Exception es) {
// System.out.println("nothing here!");
}
}
void checkId() {
clrscr();
System.out.println("Welcome " + customerName);
System.out.println();
System.out.print("Please enter the Customer ID or PIN: ");
Scanner id = new Scanner(System.in);
String chk = id.nextLine();
if (chk.equals(customerId)) {
clrscr();
showMenu();
} else {
System.out.println("=================================");
System.out.println("Wrong Login!!");
System.out.println("=================================");
if (flag < 3) {
flag++;
checkId();
}
}
}
void deposit(int amount) {
if (amount != 0) {
balance = balance + amount;
prevTransaction = amount;
}
}
void withdraw(int amount) {
if (this.balance > amount) {
balance = balance - amount;
prevTransaction = -amount;
} else {
clrscr();
System.out.println("=================================");
System.out.println("Sufficient Balance not available for the withdrawl!");
System.out.println("=================================");
}
}
void getPrevTransaction() {
if (prevTransaction > 0) {
System.out.println("Deposited: " + prevTransaction);
} else if (prevTransaction < 0) {
System.out.println("Withdraw: " + Math.abs(prevTransaction));
} else {
System.out.println("No Transaction Occured ");
}
}
public void transfer(double amount, BankAccount acc) {
if (this.balance < amount) {
clrscr();
System.out.println("=================================");
System.out.println("Transfer Fails due to insufficient balance!");
System.out.println("=================================");
} else {
this.balance -= amount;
acc.balance += amount;
System.out.println("Account of " + this.customerName + " becomes $" + this.balance);
System.out.println("Account of " + acc.customerName + " becomes $" + acc.balance);
System.out.println("\n");
}
}
private void showMenu() {
char option;
Scanner sc = new Scanner(System.in);
System.out.println("Welcome " + customerName);
System.out.println("Your ID is " + customerId);
do {
System.out.println("\n");
System.out.println("\n");
System.out.println("A. Check Balance");
System.out.println("B. Deposit");
System.out.println("C. Withdraw");
System.out.println("D. Previous Transaction");
System.out.println("E. Transfer");
System.out.println("F. Exit");
System.out.println("=================================");
System.out.println("Enter the option");
System.out.println("=================================");
option = sc.next().charAt(0);
option = Character.toUpperCase(option);
System.out.println("\n");
switch (option) {
case 'A':
System.out.println("================");
System.out.println("Balance " + balance);
System.out.println("================");
System.out.println("\n");
break;
case 'B':
clrscr();
System.out.println("================");
System.out.println("Enter the amount to deposit");
System.out.println("================");
int amount = sc.nextInt();
deposit(amount);
System.out.println("\n");
break;
case 'C':
clrscr();
System.out.println("================");
System.out.println("Enter the amount to withdraw");
System.out.println("================");
int amount2 = sc.nextInt();
withdraw(amount2);
System.out.println("\n");
break;
case 'D':
clrscr();
System.out.println("================");
getPrevTransaction();
System.out.println("================");
System.out.println("\n");
break;
case 'E':
clrscr();
System.out.println("*****");
System.out.println("To whom");
BankAccount bb = new BankAccount("Raj", "1002");
System.out.println(bb.customerName);
System.out.println("*****");
System.out.println("Amount to Transfer");
double am = sc.nextDouble();
System.out.println("*****");
transfer(am, bb);
break;
case 'F':
clrscr();
System.out.println("*****");
break;
default:
clrscr();
System.out.println("Invalid Option!!! Please Enter Again");
}
} while (option != 'F');
System.out.println("ThankYou For using our services");
}
}
class ATMinterface {
public static void main(String[] args) {
BankAccount ba = new BankAccount("Damini", "2004");
ba.checkId();
}
}