From 8f2a137c3db575153cd3c98c908fd9faeecda96f Mon Sep 17 00:00:00 2001 From: charleschyna Date: Fri, 29 Nov 2024 05:05:08 +0300 Subject: [PATCH 1/6] fixed errrors --- src/Lecture4_interfaces_abstract_classes/BankAccount.java | 6 ++++++ .../TransactionInterface.java | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/src/Lecture4_interfaces_abstract_classes/BankAccount.java b/src/Lecture4_interfaces_abstract_classes/BankAccount.java index 28d0d07..cb6633f 100644 --- a/src/Lecture4_interfaces_abstract_classes/BankAccount.java +++ b/src/Lecture4_interfaces_abstract_classes/BankAccount.java @@ -13,4 +13,10 @@ public double getBalance() { public void setBalance(double balance) { this.balance = balance; } + public void deposit(double amount){ + balance += amount; + } + public void withdraw(double amount){ + balance -= amount; + } } diff --git a/src/Lecture4_interfaces_abstract_classes/TransactionInterface.java b/src/Lecture4_interfaces_abstract_classes/TransactionInterface.java index 5902713..d5a35aa 100644 --- a/src/Lecture4_interfaces_abstract_classes/TransactionInterface.java +++ b/src/Lecture4_interfaces_abstract_classes/TransactionInterface.java @@ -16,6 +16,11 @@ public interface TransactionInterface { // Method to get a unique identifier for the transaction String getTransactionID(); + + void printTransactionDetails(); + + void apply(BankAccount ba); + } From 826088e5e192510fcf06ab8ee63fe6b6c3200974 Mon Sep 17 00:00:00 2001 From: charleschyna <124861413+charleschyna@users.noreply.github.com> Date: Fri, 29 Nov 2024 06:03:57 +0300 Subject: [PATCH 2/6] Add files via upload These is the assignment --- BankAccount.java | 18 +++++++++++ BaseTransaction.java | 47 +++++++++++++++++++++++++++ DepositTransaction.java | 16 +++++++++ InsufficientResourceException.java | 8 +++++ Main.java | 52 ++++++++++++++++++++++++++++++ TransactionInterface.JAVA | 9 ++++++ WithdrawalTransaction.java | 32 ++++++++++++++++++ 7 files changed, 182 insertions(+) create mode 100644 BankAccount.java create mode 100644 BaseTransaction.java create mode 100644 DepositTransaction.java create mode 100644 InsufficientResourceException.java create mode 100644 Main.java create mode 100644 TransactionInterface.JAVA create mode 100644 WithdrawalTransaction.java diff --git a/BankAccount.java b/BankAccount.java new file mode 100644 index 0000000..79b9215 --- /dev/null +++ b/BankAccount.java @@ -0,0 +1,18 @@ +package ASSIGNMENT; +public class BankAccount { + private double balance ; + + public BankAccount(double balance) { + this.balance = balance; + +} + public double getBalance() { + return balance; + } + public void deposit(double amount){ + balance += amount; + } + public void withdraw(double amount){ + balance -= amount; + } +} diff --git a/BaseTransaction.java b/BaseTransaction.java new file mode 100644 index 0000000..bfd1362 --- /dev/null +++ b/BaseTransaction.java @@ -0,0 +1,47 @@ +package ASSIGNMENT; +import java.util.Calendar; + + +import Lecture4_interfaces_abstract_classes.BankAccount; + +import Lecture4_interfaces_abstract_classes.TransactionInterface; + +public abstract class BaseTransaction implements TransactionInterface { +private double amount; + private Calendar date; + private String transactionID; + + public BaseTransaction(double amount, Calendar date, String transactionID) { + this.amount = amount; + this.date = date; + this.transactionID = transactionID; + } + + @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("Amount: " + amount); + System.out.println("Date: " + date.getTime()); + } + + + + @Override + public abstract void apply(BankAccount ba); +} + diff --git a/DepositTransaction.java b/DepositTransaction.java new file mode 100644 index 0000000..d6ac808 --- /dev/null +++ b/DepositTransaction.java @@ -0,0 +1,16 @@ +package ASSIGNMENT; +import java.util.Calendar; + +import Lecture4_interfaces_abstract_classes.BankAccount; + + +public class DepositTransaction extends BaseTransaction { + public DepositTransaction(double amount, String transactionID, Calendar date) { + super(amount, date , transactionID ); + } + @Override + public void apply(BankAccount ba) { + ba.deposit(getAmount()); + System.out.println("Deposit Applied successfully:" + getAmount()); + +} diff --git a/InsufficientResourceException.java b/InsufficientResourceException.java new file mode 100644 index 0000000..ab5a4e1 --- /dev/null +++ b/InsufficientResourceException.java @@ -0,0 +1,8 @@ +package ASSIGNMENT ; + +public class InsufficientResourceException extends RuntimeException { + public InsufficientResourceException(String message) { + super(message); + } + +} diff --git a/Main.java b/Main.java new file mode 100644 index 0000000..898d1e6 --- /dev/null +++ b/Main.java @@ -0,0 +1,52 @@ +package ASSIGNMENT; + +import java.util.Calendar; +import Lecture4_interfaces_abstract_classes.BankAccount; + +public class Main { + public static void main(String[] args) { + // Create a BankAccount object + BankAccount myAccount = new BankAccount("18548", 1000 .00); + + // Printing initial account details + System.out.println("Initial Account Details:"); + System.out.println(myAccount); + + // Creating a DepositTransaction object + Calendar depositDate = Calendar.getInstance(); + DepositTransaction deposit = new DepositTransaction(500.00, "TXN001", depositDate); + + // Applying the deposit transaction + System.out.println("\nApplying Deposit Transaction:"); + deposit.apply(myAccount); + + // Printing updated account details + System.out.println("Updated Account Details:"); + System.out.println(myAccount); + + // Creating a WithdrawalTransaction object + Calendar withdrawalDate = Calendar.getInstance(); + WithdrawalTransaction withdrawal = new WithdrawalTransaction(300.00, withdrawalDate, "TXN002"); + + // Applying the withdrawal transaction + System.out.println("\nApplying Withdrawal Transaction:"); + try { + withdrawal.apply(myAccount); + } catch (InsufficientResourceException e) { + System.out.println("Error: " + e.getMessage()); + } + + // Printing updated account details + System.out.println("Updated Account Details:"); + System.out.println(myAccount); + + // Testing reversal of withdrawal + System.out.println("\nReversing Withdrawal Transaction:"); + boolean reversed = withdrawal.reverse(myAccount); + System.out.println("Reversal Successful: " + reversed); + + // Printing final account details + System.out.println("Final Account Details:"); + System.out.println(myAccount); + } +} diff --git a/TransactionInterface.JAVA b/TransactionInterface.JAVA new file mode 100644 index 0000000..38e7d8a --- /dev/null +++ b/TransactionInterface.JAVA @@ -0,0 +1,9 @@ + + +public interface TransactionInterface { + double getAmount(); + java.util.Calendar getDate(); + String getTransactionID(); + void apply(BankAccount ba); // Applies the transaction to a bank account + void printTransactionDetails(); +} diff --git a/WithdrawalTransaction.java b/WithdrawalTransaction.java new file mode 100644 index 0000000..8731356 --- /dev/null +++ b/WithdrawalTransaction.java @@ -0,0 +1,32 @@ +package ASSIGNMENT ; +import java.util.Calendar; + +import Lecture4_interfaces_abstract_classes.BankAccount; +public class WithdrawalTransaction extends BaseTransaction { + public WithdrawalTransaction(double amount, Calendar date , String transactionID) { + super(amount,date, transactionID); + + } + + @Override + + + public void apply(BankAccount ba) throws InsufficientResourceException { + if (ba == null) { + throw new InsufficientResourceException("Bank account is null."); + } + if (ba.getBalance() < getAmount()) { + throw new InsufficientResourceException("Insufficient funds for withdrawal."); + } + ba.withdraw(getAmount()); + System.out.println("Withdrawal applied: " + getAmount()); + } + + public boolean reverse(BankAccount ba) { + ba.deposit(getAmount()); + System.out.println("Withdrawal reversed: " + getAmount()); + return true; + +} + +} From a55a017c3e2e7d37ae85b70b0a3832387cf7589e Mon Sep 17 00:00:00 2001 From: charleschyna Date: Wed, 4 Dec 2024 15:20:08 +0300 Subject: [PATCH 3/6] updated --- BankAccount.java | 18 ----------- BaseTransaction.java | 47 --------------------------- DepositTransaction.java | 16 --------- InsufficientResourceException.java | 8 ----- Main.java | 52 ------------------------------ TransactionInterface.JAVA | 9 ------ WithdrawalTransaction.java | 32 ------------------ 7 files changed, 182 deletions(-) delete mode 100644 BankAccount.java delete mode 100644 BaseTransaction.java delete mode 100644 DepositTransaction.java delete mode 100644 InsufficientResourceException.java delete mode 100644 Main.java delete mode 100644 TransactionInterface.JAVA delete mode 100644 WithdrawalTransaction.java diff --git a/BankAccount.java b/BankAccount.java deleted file mode 100644 index 79b9215..0000000 --- a/BankAccount.java +++ /dev/null @@ -1,18 +0,0 @@ -package ASSIGNMENT; -public class BankAccount { - private double balance ; - - public BankAccount(double balance) { - this.balance = balance; - -} - public double getBalance() { - return balance; - } - public void deposit(double amount){ - balance += amount; - } - public void withdraw(double amount){ - balance -= amount; - } -} diff --git a/BaseTransaction.java b/BaseTransaction.java deleted file mode 100644 index bfd1362..0000000 --- a/BaseTransaction.java +++ /dev/null @@ -1,47 +0,0 @@ -package ASSIGNMENT; -import java.util.Calendar; - - -import Lecture4_interfaces_abstract_classes.BankAccount; - -import Lecture4_interfaces_abstract_classes.TransactionInterface; - -public abstract class BaseTransaction implements TransactionInterface { -private double amount; - private Calendar date; - private String transactionID; - - public BaseTransaction(double amount, Calendar date, String transactionID) { - this.amount = amount; - this.date = date; - this.transactionID = transactionID; - } - - @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("Amount: " + amount); - System.out.println("Date: " + date.getTime()); - } - - - - @Override - public abstract void apply(BankAccount ba); -} - diff --git a/DepositTransaction.java b/DepositTransaction.java deleted file mode 100644 index d6ac808..0000000 --- a/DepositTransaction.java +++ /dev/null @@ -1,16 +0,0 @@ -package ASSIGNMENT; -import java.util.Calendar; - -import Lecture4_interfaces_abstract_classes.BankAccount; - - -public class DepositTransaction extends BaseTransaction { - public DepositTransaction(double amount, String transactionID, Calendar date) { - super(amount, date , transactionID ); - } - @Override - public void apply(BankAccount ba) { - ba.deposit(getAmount()); - System.out.println("Deposit Applied successfully:" + getAmount()); - -} diff --git a/InsufficientResourceException.java b/InsufficientResourceException.java deleted file mode 100644 index ab5a4e1..0000000 --- a/InsufficientResourceException.java +++ /dev/null @@ -1,8 +0,0 @@ -package ASSIGNMENT ; - -public class InsufficientResourceException extends RuntimeException { - public InsufficientResourceException(String message) { - super(message); - } - -} diff --git a/Main.java b/Main.java deleted file mode 100644 index 898d1e6..0000000 --- a/Main.java +++ /dev/null @@ -1,52 +0,0 @@ -package ASSIGNMENT; - -import java.util.Calendar; -import Lecture4_interfaces_abstract_classes.BankAccount; - -public class Main { - public static void main(String[] args) { - // Create a BankAccount object - BankAccount myAccount = new BankAccount("18548", 1000 .00); - - // Printing initial account details - System.out.println("Initial Account Details:"); - System.out.println(myAccount); - - // Creating a DepositTransaction object - Calendar depositDate = Calendar.getInstance(); - DepositTransaction deposit = new DepositTransaction(500.00, "TXN001", depositDate); - - // Applying the deposit transaction - System.out.println("\nApplying Deposit Transaction:"); - deposit.apply(myAccount); - - // Printing updated account details - System.out.println("Updated Account Details:"); - System.out.println(myAccount); - - // Creating a WithdrawalTransaction object - Calendar withdrawalDate = Calendar.getInstance(); - WithdrawalTransaction withdrawal = new WithdrawalTransaction(300.00, withdrawalDate, "TXN002"); - - // Applying the withdrawal transaction - System.out.println("\nApplying Withdrawal Transaction:"); - try { - withdrawal.apply(myAccount); - } catch (InsufficientResourceException e) { - System.out.println("Error: " + e.getMessage()); - } - - // Printing updated account details - System.out.println("Updated Account Details:"); - System.out.println(myAccount); - - // Testing reversal of withdrawal - System.out.println("\nReversing Withdrawal Transaction:"); - boolean reversed = withdrawal.reverse(myAccount); - System.out.println("Reversal Successful: " + reversed); - - // Printing final account details - System.out.println("Final Account Details:"); - System.out.println(myAccount); - } -} diff --git a/TransactionInterface.JAVA b/TransactionInterface.JAVA deleted file mode 100644 index 38e7d8a..0000000 --- a/TransactionInterface.JAVA +++ /dev/null @@ -1,9 +0,0 @@ - - -public interface TransactionInterface { - double getAmount(); - java.util.Calendar getDate(); - String getTransactionID(); - void apply(BankAccount ba); // Applies the transaction to a bank account - void printTransactionDetails(); -} diff --git a/WithdrawalTransaction.java b/WithdrawalTransaction.java deleted file mode 100644 index 8731356..0000000 --- a/WithdrawalTransaction.java +++ /dev/null @@ -1,32 +0,0 @@ -package ASSIGNMENT ; -import java.util.Calendar; - -import Lecture4_interfaces_abstract_classes.BankAccount; -public class WithdrawalTransaction extends BaseTransaction { - public WithdrawalTransaction(double amount, Calendar date , String transactionID) { - super(amount,date, transactionID); - - } - - @Override - - - public void apply(BankAccount ba) throws InsufficientResourceException { - if (ba == null) { - throw new InsufficientResourceException("Bank account is null."); - } - if (ba.getBalance() < getAmount()) { - throw new InsufficientResourceException("Insufficient funds for withdrawal."); - } - ba.withdraw(getAmount()); - System.out.println("Withdrawal applied: " + getAmount()); - } - - public boolean reverse(BankAccount ba) { - ba.deposit(getAmount()); - System.out.println("Withdrawal reversed: " + getAmount()); - return true; - -} - -} From cfc504a902f0575e4ccf31624a44a8585cc0ae7d Mon Sep 17 00:00:00 2001 From: charleschyna Date: Wed, 4 Dec 2024 15:48:29 +0300 Subject: [PATCH 4/6] updated == --- src/Lecture1_adt/Transaction1.java | 18 ------- src/Lecture1_adt/Transaction2.java | 36 -------------- src/Lecture1_adt/Transaction3.java | 31 ------------ src/Lecture1_adt/Transaction4.java | 32 ------------- .../Transaction4.java | 47 ------------------- 5 files changed, 164 deletions(-) delete mode 100644 src/Lecture1_adt/Transaction1.java delete mode 100644 src/Lecture1_adt/Transaction2.java delete mode 100644 src/Lecture1_adt/Transaction3.java delete mode 100644 src/Lecture1_adt/Transaction4.java delete mode 100644 src/Lecture2_adt_specification/Transaction4.java diff --git a/src/Lecture1_adt/Transaction1.java b/src/Lecture1_adt/Transaction1.java deleted file mode 100644 index 8a46bd5..0000000 --- a/src/Lecture1_adt/Transaction1.java +++ /dev/null @@ -1,18 +0,0 @@ -package Lecture1_adt; - -import java.util.Calendar; - -/** - * This Lecture1_adt.TransactionInterface Class violates several ADT design principles: - * 1. Representation Independence: --- Changes in representation of the data may require external code to alter access - * 2. Preservation of Invariants: --- Any external client code can alter the internal values - */ -public class Transaction1 { - public int amount; - public Calendar date; - - public Transaction1(int amount, Calendar date) { - this.amount = amount; - this.date = (Calendar) date.clone(); - } -} diff --git a/src/Lecture1_adt/Transaction2.java b/src/Lecture1_adt/Transaction2.java deleted file mode 100644 index 6c7a010..0000000 --- a/src/Lecture1_adt/Transaction2.java +++ /dev/null @@ -1,36 +0,0 @@ -package Lecture1_adt; - -import org.jetbrains.annotations.NotNull; - -import java.util.Calendar; -//import java.util.Date; - -/** - * This Lecture1_adt.Transaction2 Class Takes the first step to resolves the ADT design issues of Transaction1: - * 1. Representation Independence: --- Encapsulation - Providing access methods to the internal data. - * External client code only access via allowable operations - * --- Changes to internal representation can still be accessed via same methods defined - * - * 2. Preservation of Invariants: --- Access Modifies private final makes the data Unchangeable - * - * Lecture1_adt - */ - -public class Transaction2 { - private final int amount; - private final Calendar date; - - public Transaction2(int amount, @NotNull Calendar date) { - this.amount = amount; - this.date = date; - } - - public int getAmount() { - return amount; // Because we are dealing with Value types we need not worry about what we return - } - - public Calendar getDate() { -// return date; // Because we are dealing with Reference types we need to judiciously copy what our getters return - return (Calendar) date.clone(); // Defensive copying or Judicious Copying - } -} diff --git a/src/Lecture1_adt/Transaction3.java b/src/Lecture1_adt/Transaction3.java deleted file mode 100644 index 48781e7..0000000 --- a/src/Lecture1_adt/Transaction3.java +++ /dev/null @@ -1,31 +0,0 @@ -package Lecture1_adt; - -import org.jetbrains.annotations.NotNull; - -import java.util.Calendar; - -/** - * There is still Exposure seen in Transaction2 I that if we make next payment the date of first payment is also altered - * This Class Transaction3:Adds Code to correct this exposure: - * Intentional review of any methods that receive produces (returns) - * If produces interface of a method deals with objects or any reference types, - * there is need to perform defensive copying to enhance Invariant preservation - */ -public class Transaction3 { - private final int amount; - private final Calendar date; - - public Transaction3(int amount, @NotNull Calendar date) { - this.amount = amount; - this.date = date; - } - - public int getAmount() { - return amount; // Because we are dealing with Value types we need not worry about what we return - } - - public Calendar getDate() { -// return date; // Because we are dealing with Reference types we need to judiciously copy what our getters return - return (Calendar) date.clone(); // Defensive copying or Judicious Copying - } -} diff --git a/src/Lecture1_adt/Transaction4.java b/src/Lecture1_adt/Transaction4.java deleted file mode 100644 index f09b0d8..0000000 --- a/src/Lecture1_adt/Transaction4.java +++ /dev/null @@ -1,32 +0,0 @@ -package Lecture1_adt; - -import org.jetbrains.annotations.NotNull; - -import java.util.Calendar; -/** - * There is still Exposure seen in Transaction3 in that if we make a list of 12 payments, - * the date of all payments remain the same. - * This Class Transaction4:Adds Code to correct this exposure: - * In Making th List of 12 Transactions, we are constantly using the constructor which takes a Date Object - * Intentional review of any methods that receives or requires (i.e. takes in as parameters) - * If requires interface of a method deals with objects or any reference types, - * there is need to perform defensive copying to enhance Invariant preservation - */ -public class Transaction4 { - private final int amount; - private final Calendar date; - - public Transaction4(int amount, @NotNull Calendar date) { - this.amount = amount; - this.date = (Calendar) date.clone(); // Defensive copying or Judicious Copying for Requires interfaces - } - - public int getAmount() { - return amount; // Because we are dealing with Value types we need not worry about what we return - } - - public Calendar getDate() { -// return date; // Because we are dealing with Reference types we need to judiciously copy what our getters return - return (Calendar) date.clone(); // Defensive copying or Judicious Copying for produces Interfaces - } -} diff --git a/src/Lecture2_adt_specification/Transaction4.java b/src/Lecture2_adt_specification/Transaction4.java deleted file mode 100644 index be8a1eb..0000000 --- a/src/Lecture2_adt_specification/Transaction4.java +++ /dev/null @@ -1,47 +0,0 @@ -package Lecture2_adt_specification; - -import org.jetbrains.annotations.NotNull; - -import java.util.Calendar; -//import java.util.Date; - -/** - * In Addition to the Design considerations in Transaction4 class: - * This class adds Specifications defining the Requires and Produces interfaces - */ - -public class Transaction4 { - private final int amount; - private final Calendar date; - - /** - * Lecture1_adt.TransactionInterface Constructor - * @param amount in an integer - * @param date: Not null, and must be a Calendar object - * @return void - * Instialises the field, attributes of a transaction - * Creates a object of this - */ - public Transaction4(int amount, @NotNull Calendar date) { - this.amount = amount; - this.date = (Calendar) date.clone(); - } - - - /** - * getAmount() - * @return integer - */ - public int getAmount() { - return amount; // Because we are dealing with Value types we need not worry about what we return - } - - /** - * getDate() - * @return Calendar Object - */ - public Calendar getDate() { -// return date; // Because we are dealing with Reference types we need to judiciously copy what our getters return - return (Calendar) date.clone(); // Defensive copying or Judicious Copying - } -} From 571bec54b84ea6e62586c975a0332bf4786a45ac Mon Sep 17 00:00:00 2001 From: charleschyna Date: Wed, 4 Dec 2024 16:02:05 +0300 Subject: [PATCH 5/6] new --- src/Main.java | 156 -------------------------------------------------- 1 file changed, 156 deletions(-) delete mode 100644 src/Main.java diff --git a/src/Main.java b/src/Main.java deleted file mode 100644 index 584a048..0000000 --- a/src/Main.java +++ /dev/null @@ -1,156 +0,0 @@ -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); - } - 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()); - } - } - - /* 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); - } - 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()); - } - - // 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() - } -} \ No newline at end of file From 0499d61011081ba9b36f1b5c0fa2831c4aa12c8d Mon Sep 17 00:00:00 2001 From: charleschyna Date: Wed, 4 Dec 2024 16:03:50 +0300 Subject: [PATCH 6/6] new --- .../ASSIGNMENT/BankAccount.java | 74 +++++++++++++++ .../ASSIGNMENT/BaseTransaction.java | 91 +++++++++++++++++++ .../ASSIGNMENT/DepositTransaction.java | 49 ++++++++++ .../InsufficientResourceException.java | 8 ++ .../ASSIGNMENT/Main.java | 41 +++++++++ .../ASSIGNMENT/TransactionInterface.JAVA | 9 ++ .../ASSIGNMENT/WithdrawalTransaction.java | 61 +++++++++++++ 7 files changed, 333 insertions(+) create mode 100644 src/Lecture4_interfaces_abstract_classes/ASSIGNMENT/BankAccount.java create mode 100644 src/Lecture4_interfaces_abstract_classes/ASSIGNMENT/BaseTransaction.java create mode 100644 src/Lecture4_interfaces_abstract_classes/ASSIGNMENT/DepositTransaction.java create mode 100644 src/Lecture4_interfaces_abstract_classes/ASSIGNMENT/InsufficientResourceException.java create mode 100644 src/Lecture4_interfaces_abstract_classes/ASSIGNMENT/Main.java create mode 100644 src/Lecture4_interfaces_abstract_classes/ASSIGNMENT/TransactionInterface.JAVA create mode 100644 src/Lecture4_interfaces_abstract_classes/ASSIGNMENT/WithdrawalTransaction.java diff --git a/src/Lecture4_interfaces_abstract_classes/ASSIGNMENT/BankAccount.java b/src/Lecture4_interfaces_abstract_classes/ASSIGNMENT/BankAccount.java new file mode 100644 index 0000000..6c69766 --- /dev/null +++ b/src/Lecture4_interfaces_abstract_classes/ASSIGNMENT/BankAccount.java @@ -0,0 +1,74 @@ +package Lecture4_interfaces_abstract_classes.ASSIGNMENT; + +/** + * Represents a basic bank account with functionalities for deposits, withdrawals, + * and balance management + */ +public class BankAccount { + private double balance ; + + + /** + * Creates a new bank account with the specified initial balance + * + * @param initialbalance the initial balance for the account (must not be negative) + * @throws IllegalArgumentException if the initial balance is negative + * + */ + + public BankAccount(double initialbalance) { + if (initialbalance < 0) { + throw new IllegalArgumentException("Initial balance must not be negative"); + } + this.balance = initialbalance; +} +/** + * gets the current balance of the account + * @return the current balance + */ + public double getBalance() { + return balance; + } + + + /** + * deposits the specified amont in to the bank account + * preconditions + * -the deposit must be greater than 0 + * postconditions + * - The specified amount is added to the current balance + * + * @param amt the amount to be deposited + * @throws IllegalArgumentExemption if the deposit is less than or equal to zero + */ + public void deposit(double amount){ + if (amount <= 0){ + throw new IllegalArgumentException("Deposit amount must be greater than zero."); + + } + balance += amount; + System.out.println("Deposited successfully: " + amount + "|New balance: " + balance); +} + +/** + * withdraws the specified amount from the bank account + * preconditions + * - amount must be greater than zero + * - amount must be less than or equal to the current balance + * postconditions + * - the specified amount is subtracted from the current balance + * @param amount the amount to withdraw from the bank account + * @throws IllegalArgumentException if the amount is less than zero or greater than the current balance + * @throws InsufficientResourceException if the amount is greater than the current balance + */ + public void withdraw(double amount){ + if (amount <= 0){ + throw new IllegalArgumentException("Withdrawal amount must be greater than zero."); + } + if (amount > 0){ + throw new InsufficientResourceException("Insufficient funds for withdrawal."); + } + balance -= amount; + System.out.println("Withdrawn successfully: " + amount + "|New balance: " + balance); + } +} diff --git a/src/Lecture4_interfaces_abstract_classes/ASSIGNMENT/BaseTransaction.java b/src/Lecture4_interfaces_abstract_classes/ASSIGNMENT/BaseTransaction.java new file mode 100644 index 0000000..f062db2 --- /dev/null +++ b/src/Lecture4_interfaces_abstract_classes/ASSIGNMENT/BaseTransaction.java @@ -0,0 +1,91 @@ +package Lecture4_interfaces_abstract_classes.ASSIGNMENT; +import java.util.Calendar; + + +import Lecture4_interfaces_abstract_classes.BankAccount; + +import Lecture4_interfaces_abstract_classes.TransactionInterface; + +public class BaseTransaction implements TransactionInterface { +private double amount; + private Calendar date; + private String transactionID; + + /** + @param amount It should be a positive number + @param date the date cannot be null + @param transactionID it cannot be null or empty + @throws illegalArgumentException + */ + public BaseTransaction(double amount, Calendar date, String transactionID) { + if (amount <=0){ + throw new IllegalArgumentException("Amount must be positive"); + + } + if (date ==null){ + throw new IllegalArgumentException("Date must not be null"); + + } + if (transactionID ==null){ + throw new IllegalArgumentException("Transaction ID must not be null"); + } + this.amount = amount; + this.date = date; + this.transactionID = transactionID; + } + + /** + * returns the amount + * @return amount of the transaction + */ + @Override + public double getAmount() { + return amount; + } + + /** + * retrives the date of the transaction + * @return date of the transaction + */ + + @Override + public Calendar getDate() { + return date; + } + + /** + It retrives the unique identifier for the transaction + @return unique identifier for the transaction + + */ + @Override + public String getTransactionID() { + return transactionID; + } + + @Override + public void printTransactionDetails() { + System.out.println("Transaction ID: " + transactionID); + System.out.println("Amount: " + amount); + System.out.println("Date: " + date.getTime()); + } + + + /** + * @param ba the bankAccount object to which this transaction will be applied + * @throws IllegalArgumentException if the bankAccount is null + * + */ + + + @Override + public void apply(BankAccount ba){ + if (ba == null){ + throw new IllegalArgumentException("BankAccount must not be null"); + } + System.out.println("BaseTransaction applied "); +} + +} + + diff --git a/src/Lecture4_interfaces_abstract_classes/ASSIGNMENT/DepositTransaction.java b/src/Lecture4_interfaces_abstract_classes/ASSIGNMENT/DepositTransaction.java new file mode 100644 index 0000000..dc83730 --- /dev/null +++ b/src/Lecture4_interfaces_abstract_classes/ASSIGNMENT/DepositTransaction.java @@ -0,0 +1,49 @@ +package Lecture4_interfaces_abstract_classes.ASSIGNMENT; +import java.util.Calendar; + +import Lecture4_interfaces_abstract_classes.BankAccount; + +/** + * Represents a depositTransaction that credits a specified amount to a bank account + * extends the BaseTransaction class + */ + +public class DepositTransaction extends BaseTransaction { + + /** + * Constructor + * @param amount + * @param transactionID + * @param date + */ + public DepositTransaction(double amount, String transactionID, Calendar date) { + super(amount, date , transactionID ); + } + + /** + * Apply the deposit transaction to the bank account. + * preconditions + * - The amount to deposit must be greater than 0 + * - The bank account must not be null + * + * postconditions + * - The amount is added to the bank account + * - A success message is then printed + * + * @param ba the bank account to deposit the amount to + */ + + @Override + public void apply(BankAccount ba) { + if (ba == null) { + throw new IllegalArgumentException("Bank account is null."); + } + if (getAmount() <= 0) { + throw new IllegalArgumentException("Amount to deposit must be greater than 0."); + } + + ba.deposit(getAmount()); + System.out.println("Deposit Applied successfully:" + getAmount()); + +} +} diff --git a/src/Lecture4_interfaces_abstract_classes/ASSIGNMENT/InsufficientResourceException.java b/src/Lecture4_interfaces_abstract_classes/ASSIGNMENT/InsufficientResourceException.java new file mode 100644 index 0000000..bb37776 --- /dev/null +++ b/src/Lecture4_interfaces_abstract_classes/ASSIGNMENT/InsufficientResourceException.java @@ -0,0 +1,8 @@ +package Lecture4_interfaces_abstract_classes.ASSIGNMENT ; + +public class InsufficientResourceException extends RuntimeException { + public InsufficientResourceException(String message) { + super(message); + } + +} diff --git a/src/Lecture4_interfaces_abstract_classes/ASSIGNMENT/Main.java b/src/Lecture4_interfaces_abstract_classes/ASSIGNMENT/Main.java new file mode 100644 index 0000000..15c0175 --- /dev/null +++ b/src/Lecture4_interfaces_abstract_classes/ASSIGNMENT/Main.java @@ -0,0 +1,41 @@ +package Lecture4_interfaces_abstract_classes.ASSIGNMENT; + +import java.util.Calendar; + +public class Main { + public static void main(String[] args) { + + BankAccount myAccount = new BankAccount(1000.00); + System.out.println("=== Initial Account Details ==="); + System.out.println(myAccount); + + Calendar depositDate = Calendar.getInstance(); + DepositTransaction deposit = new DepositTransaction(500.00, "TXN001", depositDate); + + System.out.println("\n=== Applying Deposit Transaction ==="); + deposit.printTransactionDetails(); + deposit.apply(myAccount); + + System.out.println("\n=== Updated Account Details After Deposit ==="); + System.out.println(myAccount); + + Calendar withdrawalDate = Calendar.getInstance(); + WithdrawalTransaction withdrawal = new WithdrawalTransaction(300.00, withdrawalDate, "TXN002"); + + System.out.println("\n=== Applying Withdrawal Transaction ==="); + withdrawal.printTransactionDetails(); + try { + withdrawal.apply(myAccount); + } catch (IllegalStateException e) { + System.out.println("Error during withdrawal: " + e.getMessage()); + } + + System.out.println("\n=== Updated Account Details After Withdrawal ==="); + System.out.println(myAccount); + + + + System.out.println("\n=== Final Account Details ==="); + System.out.println(myAccount); + } +} diff --git a/src/Lecture4_interfaces_abstract_classes/ASSIGNMENT/TransactionInterface.JAVA b/src/Lecture4_interfaces_abstract_classes/ASSIGNMENT/TransactionInterface.JAVA new file mode 100644 index 0000000..035ce7d --- /dev/null +++ b/src/Lecture4_interfaces_abstract_classes/ASSIGNMENT/TransactionInterface.JAVA @@ -0,0 +1,9 @@ + + +public interface TransactionInterface { + double getAmount(); + java.util.Calendar getDate(); + String getTransactionID(); + void apply(BankAccount ba); // Applies the transaction to a bank account + void printTransactionDetails(); +} diff --git a/src/Lecture4_interfaces_abstract_classes/ASSIGNMENT/WithdrawalTransaction.java b/src/Lecture4_interfaces_abstract_classes/ASSIGNMENT/WithdrawalTransaction.java new file mode 100644 index 0000000..771d5be --- /dev/null +++ b/src/Lecture4_interfaces_abstract_classes/ASSIGNMENT/WithdrawalTransaction.java @@ -0,0 +1,61 @@ +package Lecture4_interfaces_abstract_classes.ASSIGNMENT ; +import java.util.Calendar; + +import Lecture4_interfaces_abstract_classes.BankAccount; + +/** + * Represents a WithdrawalTransaction that debits a specified amount from a bank account. + * Extends the BaseTransaction class. + */ +public class WithdrawalTransaction extends BaseTransaction { + /** + * constructs a withdrwalTransaction that debits a specifid amount to a bank account + * @param amount + * @param date + * @param transactionID + */ + public WithdrawalTransaction(double amount, Calendar date , String transactionID) { + super(amount,date, transactionID); + + } + + + + @Override + + + public void apply(BankAccount ba) throws InsufficientResourceException { + if (ba == null) { + throw new InsufficientResourceException("Bank account is null."); + } + if (ba.getBalance() < getAmount()) { + throw new InsufficientResourceException("Insufficient funds for withdrawal."); + } + ba.withdraw(getAmount()); + System.out.println("Withdrawal applied: " + getAmount()); + } + + /** + * preconditions + - The bank account must not be null + - The bank account must have sufficient funds to complete the withdrawal transaction + + * postconditions + - The specified withdrawal amount is addd back to the bank account balance + - A confirmation message is shown + + @param ba the bank account + @return true if the withdrawal transaction is reversed, false otherwise + */ + + public boolean reverse(BankAccount ba) { + if ( ba == null){ + throw new IllegalArgumentException("Bank account cannot be null for reversal."); + } + ba.deposit(getAmount()); + System.out.println("Withdrawal reversed: " + getAmount()); + return true; + +} + +}