-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATMInterface.java
More file actions
165 lines (105 loc) · 4.12 KB
/
ATMInterface.java
File metadata and controls
165 lines (105 loc) · 4.12 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
import java.util.Scanner;
class GreaterWithdrawAmountException extends Exception {
public String toString() {
return "withdraw amount is greater then totalBalance therefore transactions is failure. Plase try again with leass amount";
}
}
class GreaterDepositAmountException extends Exception {
public String toString() {
return "Transactions is failure because amount for deposit is greater then 200000. Plase try again with leass amount";
}
}
abstract class ATMMachine {
abstract public void withdraw();
abstract public void deposit();
abstract public void checkBalance();
}
class BankAccount extends ATMMachine {
double totalBalance;
public BankAccount() {
}
public BankAccount(double amount) {
this.totalBalance = amount;
}
public void withdraw(double amount) throws GreaterWithdrawAmountException {
if ((totalBalance - 1500) >= amount) {
totalBalance = totalBalance - amount;
System.out.println(amount + " amount is withdrawed by you and transactions is successful.");
System.out.println();
}
else {
throw new GreaterWithdrawAmountException();
}
}
public void deposit(double amount) throws GreaterDepositAmountException {
if (amount <= 200000) {
totalBalance = totalBalance + amount;
System.out.println(amount + " amount is deposited by you and transactions is successful.");
System.out.println();
}
else {
throw new GreaterDepositAmountException();
}
}
public void checkBalance() {
System.out.println("Total balance of your Bank Account is : " + totalBalance);
System.out.println();
}
public void withdraw() {
throw new UnsupportedOperationException("Unimplemented method 'withdraw'");
}
public void deposit() {
throw new UnsupportedOperationException("Unimplemented method 'deposit'");
}
}
public class ATMInterface {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
BankAccount u1 = new BankAccount(5000);
while (true) {
System.out.println("---------------------------------------------");
System.out.println("1. Enter '1' for withdrawing money ");
System.out.println("2. Enter '2' for depositing money ");
System.out.println("3. Enter '3' for checking Balance");
System.out.println("4. Enter '0' for exit from program");
int userChoice = sc.nextInt();
System.out.println("------------------------------------------------");
System.out.println();
switch (userChoice) {
case 1:
try {
System.out.println("How much amount you want to withdraw : ");
double amountw = sc.nextDouble();
System.out.println();
u1.withdraw(amountw);
}
catch (GreaterWithdrawAmountException w) {
System.out.println(w);
System.out.println();
}
break;
case 2:
try {
System.out.println("How much amount you want to deposit : ");
double amountd = sc.nextDouble();
System.out.println();
u1.deposit(amountd);
}
catch (GreaterDepositAmountException d) {
System.out.println(d);
System.out.println();
}
break;
case 3:
u1.checkBalance();
break;
case 0:
System.exit(0);
default:
System.out.println(" Enter Invalide Input ! , plase enter 0, 1, 2 , 3 this !");
System.out.println();
break;
}
}
}
}