-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankingSystem.java
More file actions
99 lines (87 loc) · 3.5 KB
/
Copy pathBankingSystem.java
File metadata and controls
99 lines (87 loc) · 3.5 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
import java.util.Scanner;
class BankAccount {
private String accountHolder;
private double balance;
// Constructor to initialize account with account holder's name
public BankAccount(String accountHolder) {
this.accountHolder = accountHolder;
this.balance = 0.0; // Initial balance set to zero
}
// Constructor to initialize account with account holder's name and initial deposit
public BankAccount(String accountHolder, double initialDeposit) {
this.accountHolder = accountHolder;
this.balance = initialDeposit; // Set the initial balance
}
// Method to check balance
public double checkBalance() {
return balance;
}
// Method to deposit money
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println("Deposited: $" + amount);
} else {
System.out.println("Deposit amount must be positive.");
}
}
// Method to withdraw money
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
System.out.println("Withdrawn: $" + amount);
} else {
System.out.println("Insufficient funds or invalid amount.");
}
}
// Method to display account information
public void displayAccountInfo() {
System.out.println("Account Holder: " + accountHolder);
System.out.println("Current Balance: $" + balance);
}
}
public class BankingSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Create a bank account with the account holder's name
System.out.print("Enter account holder's name: ");
String name = scanner.nextLine();
BankAccount account = new BankAccount(name);
// Menu for banking operations
int choice;
do {
System.out.println("\n--- Banking System ---");
System.out.println("1. Check Balance");
System.out.println("2. Deposit Money");
System.out.println("3. Withdraw Money");
System.out.println("4. Display Account Info");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("Current Balance: $" + account.checkBalance());
break;
case 2:
System.out.print("Enter amount to deposit: $");
double depositAmount = scanner.nextDouble();
account.deposit(depositAmount);
break;
case 3:
System.out.print("Enter amount to withdraw: $");
double withdrawAmount = scanner.nextDouble();
account.withdraw(withdrawAmount);
break;
case 4:
account.displayAccountInfo();
break;
case 5:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 5);
scanner.close();
}
}