-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathATM.java
82 lines (72 loc) · 1.7 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
import java.util.Scanner;
class ATM{
public static void main(String[] args){
UI ui = new UI();
while(true){
ui.showMenu();
ui.transaction(ui.getUserChoice());
}
}
}
class UI{
Scanner sc = new Scanner(System.in);
private Integer amount = 0;
public void showMenu(){
System.out.print("\n1.Deposit\n2.Withdraw\n3.Balance\n4.Exit");
}
public Integer getUserChoice(){
System.out.print("\nenter your choice: ");
return sc.nextInt();
}
public void transaction(Integer choice){
Transaction tr = new Transaction();
switch(choice){
case 1: amount = getAmount();
if(tr.deposit(amount)){
System.out.println("Amount deposited.");
}else{
System.out.println("Deposit failed.");
}
break;
case 2: amount = getAmount();
if(tr.withDraw(amount)){
System.out.println("Amount withdrawn.");
}else{
System.out.println("Withdrawl failed.");
}
break;
case 3: System.out.println("Balance is: " + tr.getBalance());
break;
case 4: System.out.println("Application is terminating....");
System.exit(0);
default: System.out.println("Invalid choice");
break;
}
}
private Integer getAmount(){
System.out.print("\nenter amount: ");
return sc.nextInt();
}
}
class Transaction{
private static Integer balance = 1000;
public Boolean deposit(Integer amount){
Boolean isDeposited = false;
if(amount > 0){
balance += amount;
isDeposited = true;
}
return isDeposited;
}
public Boolean withDraw(Integer amount){
Boolean isDeducted = false;
if(amount > 0 && getBalance() >= amount){
balance -= amount;
isDeducted = true;
}
return isDeducted;
}
public Integer getBalance(){
return balance;
}
}