-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATM.java
More file actions
96 lines (84 loc) · 3.35 KB
/
Copy pathATM.java
File metadata and controls
96 lines (84 loc) · 3.35 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
import java.util.Scanner;
public class ATM {
private static double balance = 1000.00; // Initial balance
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("=== Welcome to Simple ATM ===");
while (true) {
printMenu();
int choice = getIntInput("Enter your choice (1-4): ");
switch (choice) {
case 1 -> checkBalance();
case 2 -> depositMoney();
case 3 -> withdrawMoney();
case 4 -> {
System.out.println("Thank you for using our ATM. Goodbye!");
scanner.close();
System.exit(0);
}
default -> System.out.println("Invalid choice. Please try again.");
}
}
}
private static void printMenu() {
System.out.println("\nMain Menu:");
System.out.println("1. Check Balance");
System.out.println("2. Deposit Money");
System.out.println("3. Withdraw Money");
System.out.println("4. Exit");
}
private static void checkBalance() {
System.out.printf("\nYour current balance: $%.2f%n", balance);
}
private static void depositMoney() {
System.out.println("\n=== Deposit Money ===");
double amount = getPositiveDoubleInput("Enter amount to deposit: $");
if (amount > 0) {
balance += amount;
System.out.printf("$%.2f deposited successfully.%n", amount);
System.out.printf("New balance: $%.2f%n", balance);
} else {
System.out.println("Invalid amount. Deposit canceled.");
}
}
private static void withdrawMoney() {
System.out.println("\n=== Withdraw Money ===");
double amount = getPositiveDoubleInput("Enter amount to withdraw: $");
if (amount <= 0) {
System.out.println("Invalid amount. Withdrawal canceled.");
} else if (amount > balance) {
System.out.println("Insufficient funds. Withdrawal canceled.");
} else {
balance -= amount;
System.out.printf("$%.2f withdrawn successfully.%n", amount);
System.out.printf("Remaining balance: $%.2f%n", balance);
}
}
// Helper methods
private static int getIntInput(String prompt) {
while (true) {
try {
System.out.print(prompt);
return scanner.nextInt();
} catch (Exception e) {
System.out.println("Invalid input. Please enter a number (1-4).");
scanner.nextLine(); // Clear invalid input
}
}
}
private static double getPositiveDoubleInput(String prompt) {
while (true) {
try {
System.out.print(prompt);
double value = scanner.nextDouble();
if (value >= 0) {
return value;
}
System.out.println("Amount must be positive.");
} catch (Exception e) {
System.out.println("Invalid input. Please enter a number.");
scanner.nextLine(); // Clear invalid input
}
}
}
}