-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATMMain.java
More file actions
104 lines (87 loc) · 2.7 KB
/
Copy pathATMMain.java
File metadata and controls
104 lines (87 loc) · 2.7 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
import java.util.Scanner;
class ATM {
private BankAccount account;
public ATM(BankAccount account) {
this.account = account;
}
public void displayMenu() {
System.out.println("ATM Menu:");
System.out.println("1. Withdraw");
System.out.println("2. Deposit");
System.out.println("3. Check Balance");
System.out.println("4. Exit");
}
public void processChoice(int choice, Scanner scanner) {
switch (choice) {
case 1:
withdraw(scanner);
break;
case 2:
deposit(scanner);
break;
case 3:
checkBalance();
break;
case 4:
System.out.println("Thank you for using the ATM!");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
}
public void withdraw(Scanner scanner) {
System.out.print("Enter the amount to withdraw: ");
double amount = scanner.nextDouble();
if (account.withdraw(amount)) {
System.out.println("Withdrawal successful.");
} else {
System.out.println("Insufficient funds.");
}
}
public void deposit(Scanner scanner) {
System.out.print("Enter the amount to deposit: ");
double amount = scanner.nextDouble();
account.deposit(amount);
System.out.println("Deposit successful.");
}
public void checkBalance() {
System.out.println("Your current balance is: " + account.getBalance());
}
}
class BankAccount {
private double balance;
public BankAccount(double initialBalance) {
this.balance = initialBalance;
}
public double getBalance() {
return balance;
}
public boolean withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
return true;
} else {
return false;
}
}
public void deposit(double amount) {
balance += amount;
}
}
public class ATMMain {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
BankAccount account = new BankAccount(1000); // Initial balance of 1000
ATM atm = new ATM(account);
while (true) {
atm.displayMenu();
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
atm.processChoice(choice, scanner);
if (choice == 4) {
break;
}
}
scanner.close();
}
}