-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankApplication.java
More file actions
106 lines (93 loc) · 3.62 KB
/
BankApplication.java
File metadata and controls
106 lines (93 loc) · 3.62 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 String accountNumber;
private double balance;
// Constructor to initialize a bank account with an account number
public BankAccount(String accountNumber) {
this.accountNumber = accountNumber;
this.balance = 0.0;
}
// This method allows the user to deposit funds into the account
public void deposit(double amount) {
if (amount > 0) {
balance += amount; // Add the deposited amount to the balance
System.out.println("Deposited $" + amount);
} else {
System.out.println("Invalid deposit amount.");
}
}
// This method allows the user to withdraw funds from the account
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount; // Subtract the withdrawn amount from the balance
System.out.println("Withdrawn $" + amount);
} else {
System.out.println("Invalid withdrawal amount or insufficient balance.");
}
}
// This method returns the current balance of the account
public double getBalance() {
return balance;
}
public void deposit(double amount) {
try {
if (amount > 0) {
balance += amount;
System.out.println("Deposited $" + amount);
} else {
System.out.println("Invalid deposit amount.");
}
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please enter a valid numeric amount.");
}
}
public void withdraw(double amount) {
try {
if (amount > 0 && amount <= balance) {
balance -= amount;
System.out.println("Withdrawn $" + amount);
} else {
System.out.println("Invalid withdrawal amount or insufficient balance.");
}
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please enter a valid numeric amount.");
}
}
}
public class BankApplication {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your account number: ");
String accountNumber = scanner.next();
BankAccount account = new BankAccount(accountNumber);
while (true) {
System.out.println("\nBank Account Menu:");
System.out.println("1. Deposit");
System.out.println("2. Withdraw");
System.out.println("3. Check Balance");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter the deposit amount: $");
double depositAmount = scanner.nextDouble();
account.deposit(depositAmount);
break;
case 2:
System.out.print("Enter the withdrawal amount: $");
double withdrawalAmount = scanner.nextDouble();
account.withdraw(withdrawalAmount);
break;
case 3:
System.out.println("Current Balance: $" + account.getBalance());
break;
case 4:
System.out.println("Thank you for using the bank application?.");
System.exit(0);
default:
System.out.println("Invalid choice. Please enter a valid option.");
}
}
}
}