-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATM Simulation
More file actions
269 lines (235 loc) · 6.42 KB
/
Copy pathATM Simulation
File metadata and controls
269 lines (235 loc) · 6.42 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import java.util.*;
class Client{
private int clientId;
private String name;
private String nationality;
private String occupation;
private String address;
private int age;
private String gender;
private int pin;
private ArrayList<Account> accounts;
Client(int clientId, String name, String nationality, String occupation, String address, int age, String gender, int pin){
this.clientId = clientId;
this.name = name;
this.nationality = nationality;
this.occupation = occupation;
this.address = address;
this.age = age;
this.gender = gender;
this.pin = pin;
this.accounts = new ArrayList<Account>();
}
public int getPin() {
return this.pin;
}
public String getName() {
return this.name;
}
public int getClientId() {
return this.clientId;
}
public void addAccount(Account account) {
this.accounts.add(account);
}
public ArrayList<Account> getAccounts(){
return accounts;
}
}
abstract class Account{
private int accountId;
private static String currency;
private String branch;
protected double balance;
Account(int accountId, String currency, String branch){
this.accountId = accountId;
this.currency = currency;
this.branch = branch;
}
public int getAccountId() {
return this.accountId;
}
public static String getCurrency() {
return currency;
}
public double getBalance() {
return this.balance;
}
public double deposit(double amount) {
balance += amount;
return balance;
}
public double withdraw(double amount) {
if (amount <= this.balance) {
balance -= amount;
}
else {
System.out.println("Insufficient Balance.");
}
return this.balance;
}
}
class SavingsAccount extends Account{
double interestRate;
SavingsAccount(int accountId, String currency, String branch, double interestRate){
super(accountId, currency, branch);
this.interestRate = interestRate;
}
public double addInterest() {
balance += (this.interestRate*0.01*this.balance);
return this.balance;
}
}
class CurrentAccount extends Account{
CurrentAccount(int accountId, String currency, String branch){
super(accountId, currency, branch);
}
}
class Loan{
private double amount;
private double interestRate;
private double duration;
private String paymentMethod;
private Account account;
Loan(double amount, double interestRate, double duration, String paymentMethod, Account account) {
this.amount = amount;
this.interestRate = interestRate;
this.duration = duration;
this.paymentMethod = paymentMethod;
this.account = account;
}
}
class Transaction{
private int transactionId;
private int accountId;
private String date;
private String status;
Transaction(int transactionId, int accountId, String date, String status) {
this.transactionId = transactionId;
this.accountId = accountId;
this.date = date;
this.status = status;
}
public int getTransactionId() {
return transactionId;
}
public int getAccountId() {
return accountId;
}
public String getDate() {
return date;
}
public String getStatus() {
return status;
}
}
class ATM{
private ArrayList<Client> clients = Bank.getClients();
ATM(){
System.out.println("Welcome!");
}
public void operateATM() {
Scanner eye = new Scanner(System.in);
//requesting client's ID
System.out.print("Enter Your ClientId: ");
int clientId = eye.nextInt();
//catch the client with above clientID
Client user = null;
for (Client client:clients) {
if (client.getClientId()==clientId) {
user = client;
break;
}
}
//Ask PIN number for security verification.
System.out.print("Enter Your PIN: ");
int pin = eye.nextInt();
//correct pin number.
if (user.getPin()==pin) {
//account selection process.
for (Account account: user.getAccounts()) {
System.out.println("Account ID: "+account.getAccountId());
}
System.out.print("Enter Account ID: ");
int accID = eye.nextInt();
Account accountInProcess;
//picking the required account
for (Account account: user.getAccounts()) {
if (account.getAccountId()== accID) {
accountInProcess = account;
//operation selection.
System.out.println("1. View Balance.\n"+ "2. Withdraw money.\n"+ "3. Deposit money.\n"+ "4. Exit.");
System.out.print("Enter your selection: ");
int option = eye.nextInt();
if (option == 1) {
System.out.println("CurrentBalance is: "+accountInProcess.getBalance());
}
else if (option==2 || option==3) {
System.out.print("Enter the amount: ");
double amount = eye.nextDouble();
if (option==2) {
account.withdraw(amount);
System.out.println("Current Balance is: "+ account.getBalance());
}
else {
account.deposit(amount);
System.out.println("Current Balance is: "+ account.getBalance());
}
}
else {
System.out.println("Thank You for Banking With Us.");
return;
}
break;
}
}
}
//wrong pin number.
else {
System.out.println("Invalid Pin!");
return;
}
}
}
class Bank{
//this array list can be connected with databases during next level developments.
private static ArrayList<Client> clients;
Bank(){
clients = new ArrayList<>();
}
public static ArrayList<Client> getClients() {
return clients;
}
public static void addClients(Client client) {
clients.add(client);
}
}
public class ATMTest {
public static void main(String args[]) {
//initializing required elements.
//initialize accounts
SavingsAccount mySavAcc1 = new SavingsAccount(10000, "LKR", "Colombo",7.56);
CurrentAccount myCurrAcc1 = new CurrentAccount(10001, "LKR", "Colombo");
//initialize client 1
Client client1 = new Client(1000, "Vinton", "SriLankan", "Computer Scientist", "Colombo", 56, "Male", 129845);
//adding account details
client1.addAccount(mySavAcc1);
client1.addAccount(myCurrAcc1);
//initializing bank accounts for client2
SavingsAccount mySavAcc2 = new SavingsAccount(10010, "LKR", "Galle",12.45);
CurrentAccount myCurrAcc2 = new CurrentAccount(10011, "LKR", "Galle");
//initializing client2
Client client2 = new Client(1001, "Khan", "SriLankan", "Teacher", "Galle", 25, "Male", 129855);
//adding account details
client2.addAccount(mySavAcc2);
client2.addAccount(myCurrAcc2);
//initialize bank objects
Bank ABC_Bank = new Bank();
//adding clients to the bank
ABC_Bank.addClients(client1);
ABC_Bank.addClients(client2);
//this is the checking step of this code.
ATM abcATM = new ATM();
abcATM.operateATM();
}
}