-
Notifications
You must be signed in to change notification settings - Fork 0
/
ATM_Interface.java
107 lines (96 loc) · 3.36 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package AtmInterface;
import java.util.InputMismatchException;
import java.util.Scanner;
class BankAccount{
private int balance;
public BankAccount(int initialAmount){
this.balance = initialAmount;
}
public int balance(){
return balance;
}
public void deposit(int amount){
if (amount > 0){
balance += amount;
System.out.println("Deposit of " + amount + "Rs was successfull");
} else {
System.out.println("Invalid entry!. Please choose a number above 0");
}
}
public void withdraw(int amount){
if (amount > 0 && amount <= balance){
System.out.println("Withdraw successfull of "+amount+"Rs");
balance -= amount;
} else {
System.out.println("Invaid entry!!.Insuffucient balance or negative value entered");
}
}
}
class ATM{
private BankAccount account;
public ATM(BankAccount account) {
this.account = account;
}
public void display(){
System.out.println("Select Options : ");
System.out.println("1, Deposit");
System.out.println("2, Withdraw");
System.out.println("3, Check Balance");
System.out.println("4, Leave");
}
public void run(){
Scanner sc = new Scanner(System.in);
int option;
try{
do{
display();
System.out.println("Pick an option");
option = sc.nextInt();
switch (option){
case 1:
System.out.println("Enter amount to deposit");
int depositAmount = sc.nextInt();
account.deposit(depositAmount);
break;
case 2:
System.out.println("Enter amount to withdraw");
int withdrawAmount = sc.nextInt();
account.withdraw(withdrawAmount);
break;
case 3:
System.out.println("Current balance " + account.balance());
break;
case 4:
System.out.println("Thanks for working with State Bank");
break;
default:
System.out.println("Invalid entry!!. Enter only the given options");
}
}while (option != 4);
sc.close();
}
catch(InputMismatchException ex){
System.out.println("Only up to Rs 50,000 can be transacted in a day.");
ex.printStackTrace();
}
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
BankAccount userAccount = new BankAccount(30000);
ATM atm = new ATM(userAccount);
System.out.println("Enter Your PIN : ");
int pin = sc.nextInt();
if (pin == 1234) {
atm.run();
} else {
System.out.println("Wrong pin");
}
}
}