-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathATM.java
140 lines (126 loc) · 5.18 KB
/
ATM.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
import java.util.ArrayList;
import java.util.Scanner;
public class ATM {
public static Scanner sc = new Scanner(System.in);
public static ArrayList<String> list = new ArrayList<>();
// "transac" function displays the transaction history for the user
public static void transac(int balance) {
for (String i : list) {
System.out.println(i);
}
ask(balance);
}
// "login" function checks if the given username and pin are valid
public static void login(String username, int pin) {
if (username.contains("Jack") && pin == 1010) {
list.add("Logged in");
ask(6000);
} else {
System.out.println("Enter valid info!");
System.out.print("Username followed by pin seperated with a space: ");
login(sc.next(), sc.nextInt());
}
}
// "withdraw" function allows the user to withdraw a specified amount from their
// account
public static void withdraw(int balance, int amount) {
if (amount <= balance && amount >= 0) {
balance -= amount;
System.out.println("Transaction successful, Current balance is " + balance);
list.add("Rupee/'s " + amount + " withdrawn.");
ask(balance);
} else {
System.out.print("Enter a valid amount(greater than equal to 0): ");
withdraw(balance, sc.nextInt());
}
}
// "deposit" to deposit a specified amount into their account
public static void deposit(int balance, int amount) {
if (amount >= 0) {
balance += amount;
System.out.println("Transaction successful, Current balance is " + balance);
list.add(amount + " ruppee/'s deposited!");
ask(balance);
} else {
System.out.print("Enter a valid amount(greater than equal to 0): ");
deposit(balance, sc.nextInt());
}
}
// "transfer" function allows the user to transfer a specified amount to another
// user's account
public static void transfer(int balance, int amount) {
if ((amount <= balance) && (amount >= 0)) {
System.out.print("Enter reciver's username: ");
String accname = sc.next();
balance -= amount;
System.out.println(amount + " rupee/s sent to " + accname);
System.out.println("Transaction successful, Current balance is " + balance);
list.add(amount + " transfered to account " + accname);
ask(balance);
} else {
System.out.print("Enter a valid amount(greater than equal to 0): ");
transfer(balance, sc.nextInt());
}
}
// "quit" to exit the program
public static void quit(int balance, char qresponse) {
if (qresponse == 'y' || qresponse == 'Y') {
System.out.println("---------------Logged out---------------");
System.out.println();
} else
ask(balance);
}
// "ask" function presents the user with a list of options and allows them to
// choose one of the actions
public static void ask(int bal) {
System.out.println("----------------------------------------");
System.out.println();
System.out.println("1) Transaction History");
System.out.println("2) Withdraw");
System.out.println("3) Deposit");
System.out.println("4) Transfer");
System.out.println("5) Quit");
System.out.print("Choose one of the following actions: ");
int opt = sc.nextInt();
System.out.println();
if (1 > opt || opt > 5) {
System.out.println("Enter valid input!");
ask(bal);
} else {
switch (opt) {
case 1:
System.out.println("----------Transaction history!----------");
transac(bal);
break;
case 2:
System.out.print("Enter amount to be withdrawn: ");
withdraw(bal, sc.nextInt());
break;
case 3:
System.out.print("Enter amount to be deposited: ");
deposit(bal, sc.nextInt());
break;
case 4:
System.out.print("Enter amount to be transfered: ");
transfer(bal, sc.nextInt());
break;
case 5:
System.out.print("Do you want to exit?(Yes/No): ");
char qresponse = sc.next().charAt(0);
quit(bal, qresponse);
break;
}
}
}
public static void main(String[] args) {
System.out.println();
// Use username as Given'Jack'
System.out.println("Use pin as '1010 for testing!");
System.out.println("---------------**Login!**---------------");
System.out.print("Enter user name: ");
String username = sc.next();
System.out.print("Enter pin: ");
int pin = sc.nextInt();
login(username, pin);
}
}