Repository navigation
Expand file tree
/
Copy pathBankAccount.java
More file actions
106 lines (91 loc) · 3.05 KB
/
Copy pathBankAccount.java
File metadata and controls
106 lines (91 loc) · 3.05 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
import java.util.Scanner;
class BankAccount {
private double balance;
public BankAccount(double balance) {
this.balance = balance;
}
public double getBalance() {
return balance;
}
public boolean deposit(double amount) {
if (amount > 0) {
balance += amount;
return true;
} else {
System.out.println("Invalid amount for deposit.");
return false;
}
}
public boolean withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
return true;
} else {
System.out.println("Insufficient funds or invalid amount for withdrawal.");
return false;
}
}
}
class ATM {
private BankAccount bankAccount;
public ATM(BankAccount bankAccount) {
this.bankAccount = bankAccount;
}
public void displayMenu() {
System.out.println("\nMenu:");
System.out.println("1. Withdraw");
System.out.println("2. Deposit");
System.out.println("3. Check Balance");
System.out.println("4. Exit");
}
public void withdraw(double amount) {
if (bankAccount.withdraw(amount)) {
System.out.println("Withdrawal successful. Remaining balance: " + bankAccount.getBalance());
}
}
public void deposit(double amount) {
if (bankAccount.deposit(amount)) {
System.out.println("Deposit successful. Current balance: " + bankAccount.getBalance());
}
}
public void checkBalance() {
System.out.println("Your balance: " + bankAccount.getBalance());
}
public void run() {
Scanner scanner = new Scanner(System.in);
while (true) {
displayMenu();
System.out.print("Enter your choice: ");
String choice = scanner.nextLine();
switch (choice) {
case "1":
System.out.print("Enter amount to withdraw: ");
double withdrawAmount = scanner.nextDouble();
withdraw(withdrawAmount);
break;
case "2":
System.out.print("Enter amount to deposit: ");
double depositAmount = scanner.nextDouble();
deposit(depositAmount);
break;
case "3":
checkBalance();
break;
case "4":
System.out.println("Thank you for using the ATM. Goodbye!");
scanner.close();
return;
default:
System.out.println("Invalid choice. Please try again.");
break;
}
}
}
}
public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount(1000); // Initial balance
ATM atm = new ATM(account);
atm.run();
}
}