From a082b2090c415e6e0484b2119d589973a030b07c Mon Sep 17 00:00:00 2001 From: Gideon9697 Date: Thu, 11 Jun 2026 11:21:05 +0300 Subject: [PATCH] Add files via upload --- src/App.java | 5 + src/BankAccount.java | 19 +++ src/BaseTransaction.java | 39 ++++++ src/DepositTransaction.java | 12 ++ src/InsufficientFundsException.java | 5 + src/Main.java | 197 ++++++++-------------------- 6 files changed, 132 insertions(+), 145 deletions(-) create mode 100644 src/App.java create mode 100644 src/BankAccount.java create mode 100644 src/BaseTransaction.java create mode 100644 src/DepositTransaction.java create mode 100644 src/InsufficientFundsException.java diff --git a/src/App.java b/src/App.java new file mode 100644 index 0000000..0a839f9 --- /dev/null +++ b/src/App.java @@ -0,0 +1,5 @@ +public class App { + public static void main(String[] args) throws Exception { + System.out.println("Hello, World!"); + } +} diff --git a/src/BankAccount.java b/src/BankAccount.java new file mode 100644 index 0000000..a6af086 --- /dev/null +++ b/src/BankAccount.java @@ -0,0 +1,19 @@ +public class BankAccount { + private double balance; + + public BankAccount(double initialBalance) { + this.balance = initialBalance; + } + + public double getBalance() { + return balance; + } + + public void deposit(double amount) { + balance += amount; + } + + public void withdraw(double amount) { + balance -= amount; + } +} \ No newline at end of file diff --git a/src/BaseTransaction.java b/src/BaseTransaction.java new file mode 100644 index 0000000..5cf2273 --- /dev/null +++ b/src/BaseTransaction.java @@ -0,0 +1,39 @@ +import java.util.Calendar; + +public class BaseTransaction implements TransactionInterface { + protected double amount; + protected Calendar date; + protected String transactionID; + + // Constructor + public BaseTransaction(double amount, String transactionID) { + this.amount = amount; + this.date = Calendar.getInstance(); // Captures the current date/time + this.transactionID = transactionID; + } + + @Override + public double getAmount() { + return amount; // [cite: 25] + } + + @Override + public Calendar getDate() { + return date; // [cite: 26] + } + + @Override + public String getTransactionID() { + return transactionID; // [cite: 27] + } + + public void printTransactionDetails() { // [cite: 29] + System.out.println("Transaction ID: " + transactionID); + System.out.println("Date: " + date.getTime()); + System.out.println("Amount: $" + amount); + } + + public void apply(BankAccount ba) throws InsufficientFundsException { // [cite: 30] + System.out.println("Base transaction applied. No changes made to balance."); + } +} \ No newline at end of file diff --git a/src/DepositTransaction.java b/src/DepositTransaction.java new file mode 100644 index 0000000..de0b475 --- /dev/null +++ b/src/DepositTransaction.java @@ -0,0 +1,12 @@ +public class DepositTransaction extends BaseTransaction { + + public DepositTransaction(double amount, String transactionID) { + super(amount, transactionID); + } + + @Override + public void apply(BankAccount ba) throws InsufficientFundsException { // + ba.deposit(amount); + System.out.println("Deposit of $" + amount + " applied. New balance: $" + ba.getBalance()); + } +} \ No newline at end of file diff --git a/src/InsufficientFundsException.java b/src/InsufficientFundsException.java new file mode 100644 index 0000000..814e463 --- /dev/null +++ b/src/InsufficientFundsException.java @@ -0,0 +1,5 @@ +public class InsufficientFundsException extends Exception { + public InsufficientFundsException(String message) { + super(message); + } +} \ No newline at end of file diff --git a/src/Main.java b/src/Main.java index 584a048..2cab3df 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,156 +1,63 @@ -import Lecture1_adt.*; // Import all classes from Lecture1_adt package to be used in this client code - -import java.util.Calendar; -import java.util.GregorianCalendar; -import java.util.ArrayList; -import java.util.List; - -//TIP To Run code, press or -// click the icon in the gutter. -/* -* Client Code for accessing the Lecture1_adt.TransactionInterface.java module - */ public class Main { - - public static void testTransaction1() { - Calendar d1 = new GregorianCalendar(); // d1 is an Object [Objects are Reference types] - Lecture1_adt.Transaction1 t1 = new Lecture1_adt.Transaction1(1000, d1); // amount and d1 are arguments - - System.out.println(t1.toString()); - System.out.println("Lecture1_adt.TransactionInterface Amount: \t " + t1.amount); - System.out.println("Lecture1_adt.TransactionInterface Date: \t " + t1.date); - - // Please note that the Client Codes can access the data in the class directly through the dot operator - // This kind of exposure is a threat to both the Representation Independence and Preservation of Invariants - } - - - /** @return a transaction of same amount as t, one month later - * This is a PRODUCER of the class Lecture1_adt.Transaction2 - * This code will help demostrate the Design exposures still present in transaction2 class - * */ - - public static Transaction2 makeNextPayment(Transaction2 t) { - Calendar d = t.getDate(); - d.add(Calendar.MONTH, 1); - return new Transaction2(t.getAmount(), d); - } - - /* - Testing Transaction2 class - */ - public static void testTransaction2() { - - Calendar d1 = new GregorianCalendar(); - - Lecture1_adt.Transaction2 t = new Lecture1_adt.Transaction2(1000, d1); - - Lecture1_adt.Transaction2 modified_t = makeNextPayment(t); - - System.out.println("\n\nState of the Object T1 After Client Code Tried to Change the Amount"); - System.out.println("Lecture1_adt.TransactionInterface Amount: \t "+modified_t.getAmount()); - System.out.println("Lecture1_adt.TransactionInterface Date: \t "+modified_t.getDate().getTime()); - - System.out.println("\n\nHow does T2 Look Like?????"); - System.out.println("Lecture1_adt.TransactionInterface Amount: \t "+modified_t.getAmount()); - System.out.println("Lecture1_adt.TransactionInterface Date: \t "+modified_t.getDate().getTime()); - - /* Please note that Although we have solved the problem of Transaction1 - * And client code can no longer use the dot (.) operator to directly access the data - * There is still some exposure especially if we pass an object of a previous Transaction2 to create a new Transaction2 object - */ - - } - - - /** @return a list of 12 monthly payments of identical amounts - * This code will help demostrate the Design exposures still present in transaction3 class - * */ - public static List makeYearOfPayments (int amount) throws NullPointerException { - - List listOfTransaction3s = new ArrayList(); - Calendar date = new GregorianCalendar(2024, Calendar.JANUARY, 3); - - - for (int i = 0; i < 12; i++) { - listOfTransaction3s.add(new Transaction3(amount, date)); - date.add(Calendar.MONTH, 1); + public static void main(String[] args) { + System.out.println("========== STARTING TRANSACTION SYSTEM TESTS =========="); + + // 1. Setup Bank Account + BankAccount account = new BankAccount(500.0); + System.out.println("Initial Balance: $" + account.getBalance()); + + // 2. Test DepositTransaction + System.out.println("\n--- 1. Testing Standard Deposit ---"); + DepositTransaction deposit = new DepositTransaction(200.0, "TXN-DEP-001"); + deposit.printTransactionDetails(); + try { + deposit.apply(account); + } catch (InsufficientFundsException e) { + System.out.println("Error: " + e.getMessage()); } - return listOfTransaction3s; - } - - /* - Testing Transaction3 class - */ - public static void testTransaction3() { - - List allPaymentsIn2024 = makeYearOfPayments(1000); - - for (Transaction3 t3 : allPaymentsIn2024) { - // Display all the 12 Transactions - for (Transaction3 transact : allPaymentsIn2024) { - System.out.println("\n\n ::::::::::::::::::::::::::::::::::::::::::::\n"); - System.out.println("Lecture1_adt.TransactionInterface Amount: \t "+transact.getAmount()); - System.out.println("Lecture1_adt.TransactionInterface Date: \t "+transact.getDate().getTime()); - } + // 3. Test Standard Withdrawal (Success) + System.out.println("\n--- 2. Testing Standard Withdrawal ---"); + WithdrawalTransaction withdrawal1 = new WithdrawalTransaction(100.0, "TXN-WIT-001"); + withdrawal1.printTransactionDetails(); + try { + withdrawal1.apply(account); + } catch (InsufficientFundsException e) { + System.out.println("Error: " + e.getMessage()); } - /* Please Check all the 12 transactions displayed and hwo their dates look like - * Note that Although Transaction3 class resolves to an extent the exposure in Transaction2 class - * There is still some exposure especially if we pass an object of a previous Transaction3 to create a - * new Transaction3 object - */ - } - - - /** @return a list of 12 monthly payments of identical amounts - * This code Show that by judicious copying and defensive programming we eliminate the exposure in Transaction3 - * As defined in the constructor of Transaction4 class - * */ - - public static List makeYearOfPaymentsFinal (int amount) throws NullPointerException { - - List listOfTransaction4s = new ArrayList(); - Calendar date = new GregorianCalendar(2024, Calendar.JANUARY, 3); - - - for (int i = 0; i < 12; i++) { - listOfTransaction4s.add(new Transaction4(amount, date)); - date.add(Calendar.MONTH, 1); + // 4. Test Reversal (From Phase 2) + System.out.println("\n--- 3. Testing Transaction Reversal ---"); + withdrawal1.reverse(); + + // 5. Test Exception Handling (From Phase 3) + System.out.println("\n--- 4. Testing Insufficient Funds Exception ---"); + WithdrawalTransaction withdrawal2 = new WithdrawalTransaction(1000.0, "TXN-WIT-002"); + try { + withdrawal2.apply(account); + } catch (InsufficientFundsException e) { + System.out.println("EXPECTED EXCEPTION CAUGHT: " + e.getMessage()); } - return listOfTransaction4s; - } - - /* - Testing Transaction3 class - */ - public static void testTransaction4() { - - /* - * Call the function to make all the Twelve transaction in a year of our business - */ - - List transactionsIn2024 = makeYearOfPaymentsFinal(1200); - // Display all the 12 Transactions - for (Transaction4 transact : transactionsIn2024) { - System.out.println("\n\n ::::::::::::::::::::::::::::::::::::::::::::\n"); - System.out.println("Lecture1_adt.TransactionInterface Amount: \t "+transact.getAmount()); - System.out.println("Lecture1_adt.TransactionInterface Date: \t "+transact.getDate().getTime()); + // 6. Test Partial Withdrawal (From Phase 3 Overloaded Method) + System.out.println("\n--- 5. Testing Partial Withdrawal ---"); + WithdrawalTransaction withdrawal3 = new WithdrawalTransaction(800.0, "TXN-WIT-003"); + withdrawal3.apply(account, true); + + // 7. Test Polymorphism & Type Casting (Crucial Q4 Requirement) + System.out.println("\n--- 6. Testing Polymorphism & Type Casting ---"); + // Creating a subtype object (DepositTransaction) but referencing it as the Base type + BaseTransaction polyTransaction = new DepositTransaction(50.0, "TXN-POLY-001"); + polyTransaction.printTransactionDetails(); + + try { + // Because of late binding (polymorphism), this will run the DepositTransaction's apply() + // method, NOT the BaseTransaction's generic apply() method. + polyTransaction.apply(account); + } catch (InsufficientFundsException e) { + System.out.println("Error: " + e.getMessage()); } - // Please Take a look at all the 12 transaction now and compare with the outputs of the Transaction3 class - } - - - public static void main(String[] args) { - // This is the client code - // Uncomment the following lines to test the class which you would like to test - - // testTransaction1() - // testTransaction2() - // testTransaction3() - // testTransaction4() + System.out.println("\n========== TESTS COMPLETED =========="); } } \ No newline at end of file