-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATMSimulator.java
More file actions
106 lines (96 loc) · 3.51 KB
/
Copy pathATMSimulator.java
File metadata and controls
106 lines (96 loc) · 3.51 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;
private final String accountNumber;
private final String pin;
public BankAccount(String accountNumber, double initialBalance, String pin) {
this.accountNumber = accountNumber;
this.balance = initialBalance;
this.pin = pin;
}
public String getAccountNumber() {
return this.accountNumber;
}
public boolean verifyPIN(String enteredPIN) {
return this.pin.equals(enteredPIN);
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println("Successfully deposited:$" + amount);
} else {
System.out.println("Invalid deposit amount!");
}
}
public void withdraw(double amount) {
if (amount <= 0) {
System.out.println("Invalid withdrawal amount!");
} else if (amount > balance) {
System.out.println("Error: Insufficient funds");
} else {
balance -= amount;
System.out.println("Successfully withdrew: $" + amount);
}
}
}
public class ATMSimulator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
BankAccount myAccount = new BankAccount("123456789", 500.0, "4321");
System.out.println("=== Welcome to Java Bank ATM ===");
int attempts = 3;
boolean authenticated = false;
while (attempts > 0) {
System.out.print("Please enter your 4-digit PIN: ");
String enteredPIN = scanner.next();
if (myAccount.verifyPIN(enteredPIN)) {
System.out.println("\nPIN Verified. Access Granted!");
authenticated = true;
break;
} else {
attempts--;
System.out.println("Incorrect PIN. Attempts remaining: " + attempts);
}
}
if (!authenticated) {
System.out.println("\nToo many incorrect attempts. Your card has been blocked!");
scanner.close();
return;
}
boolean running = true;
while (running) {
System.out.println("\n--- MAIN MENU ---");
System.out.println("1. Check Balance");
System.out.println("2. Deposit Money");
System.out.println("3. Withdraw Money");
System.out.println("4. Exit");
System.out.print("Choose an option (1-4): ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("Your current balance is: $" + myAccount.getBalance());
break;
case 2:
System.out.print("Enter deposit amount: ");
double depAmount = scanner.nextDouble();
myAccount.deposit(depAmount);
break;
case 3:
System.out.print("Enter withdrawal amount: ");
double drawAmount = scanner.nextDouble();
myAccount.withdraw(drawAmount);
break;
case 4:
System.out.println("Thank you for using Java Bank ATM. Adios!");
running = false;
break;
default:
System.out.println("Invalid option. Please try again.");
}
}
scanner.close();
}
}