-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnewfile.java
More file actions
140 lines (119 loc) · 4.33 KB
/
Copy pathnewfile.java
File metadata and controls
140 lines (119 loc) · 4.33 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
import java.util.Scanner;
/**
* Complete ATM Banking System
* Includes bank account management with deposit, withdrawal, and balance checking
*/
public class ATMSystem {
// Bank account class
static class BankAccount {
private double balance;
public BankAccount(double initialBalance) {
this.balance = initialBalance;
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.printf("Deposited $%.2f successfully.%n", amount);
} else {
System.out.println("Invalid deposit amount.");
}
}
public boolean withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
System.out.printf("Withdrew $%.2f successfully.%n", amount);
return true;
}
System.out.println("Withdrawal failed. Check amount or balance.");
return false;
}
}
// ATM interface class
static class ATM {
private final BankAccount account;
private final Scanner scanner;
public ATM(BankAccount account) {
this.account = account;
this.scanner = new Scanner(System.in);
}
public void start() {
System.out.println("\nWelcome to JavaBank ATM");
System.out.println("-----------------------");
boolean running = true;
while (running) {
displayMenu();
int choice = getChoice();
switch (choice) {
case 1:
checkBalance();
break;
case 2:
processDeposit();
break;
case 3:
processWithdrawal();
break;
case 4:
running = false;
break;
default:
System.out.println("Invalid option. Try again.");
}
}
shutdown();
}
private void displayMenu() {
System.out.println("\nMain Menu:");
System.out.println("1. Check Balance");
System.out.println("2. Make Deposit");
System.out.println("3. Withdraw Cash");
System.out.println("4. Exit");
System.out.print("Enter your choice (1-4): ");
}
private int getChoice() {
while (!scanner.hasNextInt()) {
System.out.println("Please enter a number 1-4.");
scanner.next(); // Clear invalid input
displayMenu();
}
return scanner.nextInt();
}
private void checkBalance() {
System.out.printf("Current Balance: $%.2f%n", account.getBalance());
}
private void processDeposit() {
System.out.print("Enter deposit amount: $");
double amount = getAmount();
account.deposit(amount);
}
private void processWithdrawal() {
System.out.print("Enter withdrawal amount: $");
double amount = getAmount();
account.withdraw(amount);
}
private double getAmount() {
while (!scanner.hasNextDouble()) {
System.out.println("Please enter a valid amount.");
scanner.next(); // Clear invalid input
System.out.print("Enter amount: $");
}
return scanner.nextDouble();
}
private void shutdown() {
scanner.close();
System.out.println("\nThank you for using JavaBank ATM.");
System.out.println("Please take your card. Goodbye!");
}
}
// Main program
public static void main(String[] args) {
// Initialize bank account with $1000 balance
BankAccount userAccount = new BankAccount(1000.00);
// Create and start ATM
ATM atm = new ATM(userAccount);
atm.start();
}
}