-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATMApp.java
More file actions
103 lines (89 loc) · 2.87 KB
/
ATMApp.java
File metadata and controls
103 lines (89 loc) · 2.87 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
import java.util.Scanner;
class BankAccount {
private double balance;
public BankAccount(double initialBalance) {
this.balance = initialBalance;
}
public boolean withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
return true;
} else {
return false;
}
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
public double getBalance() {
return balance;
}
}
class ATM {
private BankAccount account;
private Scanner scanner;
public ATM(BankAccount account) {
this.account = account;
this.scanner = new Scanner(System.in);
}
public void start() {
System.out.println("Welcome to the ATM!");
while (true) {
System.out.println("\nPlease choose an option:");
System.out.println("1.Check Balance");
System.out.println("2.Deposit");
System.out.println("3.Withdraw");
System.out.println("4.Exit");
System.out.print("Your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
checkBalance();
break;
case 2:
deposit();
break;
case 3:
withdraw();
break;
case 4:
System.out.println("Thank you for using the ATM. Goodbye!");
return;
default:
System.out.println("Invalid option. Please try again.");
}
}
}
private void checkBalance() {
System.out.printf("Your current balance is: Rs.%.2f\n", account.getBalance());
}
private void deposit() {
System.out.print("Enter amount to deposit: Rs.");
double amount = scanner.nextDouble();
if (amount > 0) {
account.deposit(amount);
System.out.printf("Rs.%.2f deposited successfully.\n", amount);
} else {
System.out.println("Invalid deposit amount.");
}
}
private void withdraw() {
System.out.print("Enter amount to withdraw: Rs.");
double amount = scanner.nextDouble();
if (account.withdraw(amount)) {
System.out.printf("Rs.%.2f withdrawn successfully.\n", amount);
} else {
System.out.println("Withdrawal failed. Insufficient balance or invalid amount.");
}
}
}
public class ATMApp {
public static void main(String[] args) {
BankAccount userAccount = new BankAccount(1000.00);
ATM atmMachine = new ATM(userAccount);
atmMachine.start();
}
}
}