forked from mulangonando/Advanced-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ1.java
More file actions
160 lines (130 loc) · 5.08 KB
/
Copy pathQ1.java
File metadata and controls
160 lines (130 loc) · 5.08 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import java.util.Calendar;
import java.util.UUID;
// Class for Insufficient Funds
class InsufficientFundsException extends Exception {
public InsufficientFundsException(String message) {
super(message);
}
}
class BankAccount {
private String accountNumber;
private double balance;
public BankAccount(String accountNumber, double initialBalance) {
this.accountNumber = accountNumber;
this.balance = initialBalance;
}
public double getBalance() { return balance; }
public String getAccountNumber() { return accountNumber; }
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) throws InsufficientFundsException {
if (amount > balance) {
// Throwing custom exception when funds are low
throw new InsufficientFundsException("Insufficient funds! Available balance is $" + balance);
}
balance -= amount;
}
}
// Transaction Interface defining the required contract
interface TransactionInterface {
double getAmount();
Calendar getDate();
String getTransactionID();
void printTransactionDetails();
void apply(BankAccount ba) throws InsufficientFundsException;
}
// Base Class implementing the Interface
class BaseTransaction implements TransactionInterface {
protected double amount;
protected Calendar date;
protected String transactionID;
public BaseTransaction(double amount) {
this.amount = amount;
this.date = Calendar.getInstance();
this.transactionID = UUID.randomUUID().toString(); // Generates a unique string ID
}
@Override
public double getAmount() {
return amount;
}
@Override
public Calendar getDate() {
return date;
}
@Override
public String getTransactionID() {
return transactionID;
}
@Override
public void printTransactionDetails() {
System.out.println("Transaction ID: " + transactionID);
System.out.println("Date: " + date.getTime());
System.out.println("Amount: $" + amount);
}
@Override
public void apply(BankAccount ba) throws InsufficientFundsException {
System.out.println("System Log: BaseTransaction invoked on Account " + ba.getAccountNumber() + ". No balance modified.");
}
}
// Derived Class for Deposits
class DepositTransaction extends BaseTransaction {
public DepositTransaction(double amount) {
super(amount);
}
@Override
public void printTransactionDetails() {
System.out.println("\n--- Deposit Transaction ---");
super.printTransactionDetails();
}
// Overriding apply() to perform a specific credit logic
@Override
public void apply(BankAccount ba) {
ba.deposit(this.amount);
System.out.println("Deposit of $" + this.amount + " applied. New Balance: $" + ba.getBalance());
}
}
// Derived Class for Withdrawals
class WithdrawalTransaction extends BaseTransaction {
public WithdrawalTransaction(double amount) {
super(amount);
}
@Override
public void printTransactionDetails() {
System.out.println("\n--- Withdrawal Transaction ---");
super.printTransactionDetails();
}
@Override
public void apply(BankAccount ba) throws InsufficientFundsException {
ba.withdraw(this.amount);
System.out.println("Withdrawal of $" + this.amount + " applied. New Balance: $" + ba.getBalance());
}
}
public class Main {
public static void main(String[] args) {
// Initialize a test account
BankAccount account = new BankAccount("ACC-987654321", 500.00);
System.out.println("Initial Account Balance: $" + account.getBalance());
TransactionInterface genericTx = new BaseTransaction(0.0);
TransactionInterface depositTx = new DepositTransaction(250.00);
TransactionInterface validWithdrawalTx = new WithdrawalTransaction(150.00);
TransactionInterface invalidWithdrawalTx = new WithdrawalTransaction(1000.00);
try {
System.out.println("\n[Testing Base Transaction]");
genericTx.printTransactionDetails();
genericTx.apply(account);
System.out.println("\n[Testing Deposit]");
depositTx.printTransactionDetails();
depositTx.apply(account);
System.out.println("\n[Testing Valid Withdrawal]");
validWithdrawalTx.printTransactionDetails();
validWithdrawalTx.apply(account);
System.out.println("\n[Testing Invalid Withdrawal]");
invalidWithdrawalTx.printTransactionDetails();
invalidWithdrawalTx.apply(account); // This line throws the exception
} catch (InsufficientFundsException e) {
System.err.println("TRANSACTION FAILED: " + e.getMessage());
}
System.out.println("\nFinal Account Balance: $" + account.getBalance());
}
}