-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATMinterface.java
More file actions
83 lines (71 loc) · 2.82 KB
/
ATMinterface.java
File metadata and controls
83 lines (71 loc) · 2.82 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
import java.util.HashMap;
import java.util.Scanner;
public class ATMinterface {
private HashMap<String, Double> accounts;
public ATMinterface() {
accounts = new HashMap<>();
// Sample account data (accountNumber, balance)
accounts.put("123456789", 1000.00);
accounts.put("987654321", 500.00);
}
public void run() {
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to the ATM!");
System.out.print("Enter your account number: ");
String accountNumber = scanner.nextLine();
if (validateAccount(accountNumber)) {
while (true) {
System.out.println("\nMenu:");
System.out.println("1. Check Balance");
System.out.println("2. Withdraw Money");
System.out.println("3. Exit");
System.out.print("Enter your choice (1/2/3): ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
checkBalance(accountNumber);
break;
case 2:
withdrawMoney(accountNumber);
break;
case 3:
System.out.println("Thank you for using the ATM!");
return;
default:
System.out.println("Invalid choice. Try again.");
}
}
} else {
System.out.println("Invalid account number. Exiting...");
}
}
private boolean validateAccount(String accountNumber) {
// Simple account validation check
return accounts.containsKey(accountNumber);
}
private void checkBalance(String accountNumber) {
double balance = accounts.get(accountNumber);
System.out.println("Your account balance is: $" + balance);
}
private void withdrawMoney(String accountNumber) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the amount to withdraw: ");
double amount = scanner.nextDouble();
if (amount <= 0) {
System.out.println("Invalid amount. Withdrawal failed.");
} else {
double balance = accounts.get(accountNumber);
if (balance >= amount) {
balance -= amount;
accounts.put(accountNumber, balance);
System.out.println("Withdrawal successful. Remaining balance: $" + balance);
} else {
System.out.println("Insufficient funds. Withdrawal failed.");
}
}
}
public static void main(String[] args) {
ATMinterface atm = new ATMinterface();
atm.run();
}
}