-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAtmInterface.java
More file actions
110 lines (90 loc) · 3.03 KB
/
AtmInterface.java
File metadata and controls
110 lines (90 loc) · 3.03 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
import java.util.Scanner;
class BankAccount {
private double balance;
public BankAccount(double balance) {
this.balance = balance;
}
public boolean deposit(double amount) {
if (amount > 0) {
this.balance += amount;
return true;
} else {
System.out.println("Invalid amount for deposit.");
return false;
}
}
public boolean withdraw(double amount) {
if (amount > 0 && amount <= this.balance) {
this.balance -= amount;
return true;
} else {
System.out.println("Invalid amount for withdrawal or insufficient funds.");
return false;
}
}
public double checkBalance() {
return this.balance;
}
}
class ATM {
private BankAccount userAccount;
public ATM(BankAccount userAccount) {
this.userAccount = userAccount;
}
public void displayOptions() {
System.out.println("1. Withdraw");
System.out.println("2. Deposit");
System.out.println("3. Check Balance");
System.out.println("4. Quit");
}
public void performTransaction(int option) {
Scanner scanner = new Scanner(System.in);
switch (option) {
case 1:
System.out.print("Enter withdrawal amount: ");
double withdrawAmount = scanner.nextDouble();
if (userAccount.withdraw(withdrawAmount)) {
System.out.println("Withdrawal successful. Remaining balance: " + userAccount.checkBalance());
} else {
System.out.println("Withdrawal failed.");
}
break;
case 2:
System.out.print("Enter deposit amount: ");
double depositAmount = scanner.nextDouble();
if (userAccount.deposit(depositAmount)) {
System.out.println("Deposit successful. New balance: " + userAccount.checkBalance());
} else {
System.out.println("Deposit failed.");
}
break;
case 3:
System.out.println("Current balance: " + userAccount.checkBalance());
break;
case 4:
System.out.println("Exiting ATM. Thank you!");
break;
default:
System.out.println("Invalid option. Please select a valid option.");
}
}
public void runATM() {
Scanner scanner = new Scanner(System.in);
while (true) {
displayOptions();
System.out.print("Enter your choice (1-4): ");
int option = scanner.nextInt();
if (option == 4) {
break;
}
performTransaction(option);
}
}
}
public class AtmInterface {
public static void main(String[] args) {
BankAccount userAccount = new BankAccount(1000); // Initial balance is 1000
ATM atm = new ATM(userAccount);
atm.runATM();
}
}