Repository navigation
Expand file tree
/
Copy pathBankAccountManagement.java
More file actions
87 lines (78 loc) · 2.96 KB
/
Copy pathBankAccountManagement.java
File metadata and controls
87 lines (78 loc) · 2.96 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
import java.util.ArrayList;
import java.util.List;
// BankAccount class definition
class BankAccount {
private int accountNumber;
private String accountHolder;
private double balance;
private List<String> transactionHistory;
// Constructor to initialize bank account
public BankAccount(int accountNumber, String accountHolder, double initialBalance) {
this.accountNumber = accountNumber;
this.accountHolder = accountHolder;
this.balance = initialBalance;
this.transactionHistory = new ArrayList<>();
recordTransaction("Account created with initial balance: $" + initialBalance);
}
// Deposit method
public void deposit(double amount) {
if (amount <= 0) {
System.out.println("Deposit amount must be positive.");
return;
}
balance += amount;
recordTransaction("Deposited $" + amount);
System.out.println("Deposit successful. Current balance: $" + balance);
}
// Withdrawal method with overdraft handling
public void withdraw(double amount) {
if (amount <= 0) {
System.out.println("Withdrawal amount must be positive.");
return;
}
if (amount > balance) {
System.out.println("Error: Insufficient balance. Transaction denied.");
recordTransaction("Failed withdrawal of $" + amount + " due to insufficient funds");
} else {
balance -= amount;
recordTransaction("Withdrew $" + amount);
System.out.println("Withdrawal successful. Current balance: $" + balance);
}
}
// Method to check balance
public double checkBalance() {
System.out.println("Current balance: $" + balance);
return balance;
}
// Record transaction history
private void recordTransaction(String transaction) {
transactionHistory.add(transaction);
}
// Display transaction history
public void displayTransactionHistory() {
System.out.println("\nTransaction History for " + accountHolder + ":");
for (String transaction : transactionHistory) {
System.out.println(transaction);
}
System.out.println();
}
}
public class BankAccountManagement {
public static void main(String[] args) {
// Create multiple bank accounts
BankAccount account1 = new BankAccount(1001, "Mubashir", 500);
BankAccount account2 = new BankAccount(1002, "Fahad", 1000);
// Perform transactions for Mubashir
System.out.println("\nPerforming transactions for Mubashir:");
account1.deposit(200);
account1.withdraw(100);
account1.checkBalance();
account1.displayTransactionHistory();
// Perform transactions for Fahad
System.out.println("\nPerforming transactions for Fahad:");
account2.withdraw(500);
account2.deposit(300);
account2.checkBalance();
account2.displayTransactionHistory();
}
}