-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATMInterface.java
More file actions
171 lines (155 loc) · 6 KB
/
ATMInterface.java
File metadata and controls
171 lines (155 loc) · 6 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
// Class to represent the user's bank account
class BankAccount {
private double balance;
private String accountNumber;
public BankAccount(String accountNumber, double initialBalance) {
this.accountNumber = accountNumber;
this.balance = initialBalance >= 0 ? initialBalance : 0; // Ensure non-negative initial balance
}
public double getBalance() {
return balance;
}
public String getAccountNumber() {
return accountNumber;
}
public boolean withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
return true;
}
return false;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
}
// Class to represent the ATM Machine
class ATMMachine {
private BankAccount account;
private Scanner scanner;
private List<String> transactionHistory;
public ATMMachine(BankAccount account) {
this.account = account;
this.scanner = new Scanner(System.in);
this.transactionHistory = new ArrayList<>();
}
// Display the ATM menu
private void displayMenu() {
System.out.printf("\n=== ATM Menu (Account: %s, Balance: $%.2f) ===%n",
account.getAccountNumber(), account.getBalance());
System.out.println("1. Check Balance");
System.out.println("2. Deposit");
System.out.println("3. Withdraw");
System.out.println("4. Transaction History");
System.out.println("5. Exit");
System.out.print("Choose an option (1-5): ");
}
// Main method to run the ATM interface
public void start() {
while (true) {
displayMenu();
String choice = scanner.nextLine().trim();
switch (choice) {
case "1":
checkBalance();
break;
case "2":
deposit();
break;
case "3":
withdraw();
break;
case "4":
showTransactionHistory();
break;
case "5":
System.out.println("Thank you for using the ATM. ComeAgain!");
scanner.close();
return;
default:
System.out.println("Invalid option. Please choose 1-5.");
}
}
}
// Check balance method
private void checkBalance() {
double balance = account.getBalance();
System.out.printf("Current balance: $%.2f%n", balance);
transactionHistory.add(String.format("Checked balance: $%.2f", balance));
}
// Deposit method
private void deposit() {
System.out.print("Enter amount to deposit: $");
try {
double amount = Double.parseDouble(scanner.nextLine().trim());
if (amount <= 0) {
System.out.println("Invalid amount. Please enter a positive value.");
transactionHistory.add("Failed deposit: Invalid amount (non-positive)");
return;
}
account.deposit(amount);
System.out.printf("Successfully deposited $%.2f. New balance: $%.2f%n",
amount, account.getBalance());
transactionHistory.add(String.format("Deposited: $%.2f, New balance: $%.2f",
amount, account.getBalance()));
} catch (NumberFormatException e) {
System.out.println("Invalid input. Please enter a valid number.");
transactionHistory.add("Failed deposit: Invalid input (non-numeric)");
}
}
// Withdraw method
private void withdraw() {
System.out.printf("Current balance: $%.2f%n", account.getBalance());
System.out.print("Enter amount to withdraw: $");
try {
double amount = Double.parseDouble(scanner.nextLine().trim());
if (amount <= 0) {
System.out.println("Invalid amount. Please enter a positive value.");
transactionHistory.add("Failed withdrawal: Invalid amount (non-positive)");
return;
}
if (account.withdraw(amount)) {
System.out.printf("Successfully withdrew $%.2f. New balance: $%.2f%n",
amount, account.getBalance());
transactionHistory.add(String.format("Withdrew: $%.2f, New balance: $%.2f",
amount, account.getBalance()));
} else {
System.out.printf("Cannot withdraw $%.2f. Insufficient balance or invalid amount.%n",
amount);
transactionHistory.add(String.format("Failed withdrawal: $%.2f (Insufficient funds, Balance: $%.2f)",
amount, account.getBalance()));
}
} catch (NumberFormatException e) {
System.out.println("Invalid input. Please enter a valid number.");
transactionHistory.add("Failed withdrawal: Invalid input (non-numeric)");
}
}
// Show transaction history using enhanced for loop
private void showTransactionHistory() {
if (transactionHistory.isEmpty()) {
System.out.println("No transactions yet.");
} else {
System.out.println("\n=== Transaction History ===");
int index = 1;
for (String transaction : transactionHistory) {
System.out.printf("%d. %s%n", index++, transaction);
}
}
}
}
// Main class to test the ATM system
public class ATMInterface {
public static void main(String[] args) {
// Create a bank account with initial balance of $0 for testing
BankAccount account = new BankAccount("123456789", 0.00);
// Create ATM instance and start
ATMMachine atm = new ATMMachine(account);
System.out.println("Welcome to the ATM!");
atm.start();
}
}