diff --git a/AdvancedProgrammingAssignment1_SCT212_0055_2021.pdf b/AdvancedProgrammingAssignment1_SCT212_0055_2021.pdf
new file mode 100644
index 0000000..1fbd26b
Binary files /dev/null and b/AdvancedProgrammingAssignment1_SCT212_0055_2021.pdf differ
diff --git a/src/Lecture4_interfaces_abstract_classes/BankAccount.java b/src/Lecture4_interfaces_abstract_classes/BankAccount.java
index 28d0d07..4fdce0e 100644
--- a/src/Lecture4_interfaces_abstract_classes/BankAccount.java
+++ b/src/Lecture4_interfaces_abstract_classes/BankAccount.java
@@ -2,15 +2,49 @@
public class BankAccount {
private double balance;
+
+ // Constructor initializes the bank account with a starting balance
public BankAccount(double balance) {
+ if (balance < 0) {
+ throw new IllegalArgumentException("Balance cannot be negative.");
+ }
this.balance = balance;
}
+ // Getter for the balance
public double getBalance() {
return balance;
}
+ // Setter for the balance, includes validation to prevent negative balances
public void setBalance(double balance) {
+ if (balance < 0) {
+ throw new IllegalArgumentException("Balance cannot be negative.");
+ }
this.balance = balance;
}
+
+ // Method to deposit an amount into the account
+ public void deposit(double amount) {
+ if (amount <= 0) {
+ throw new IllegalArgumentException("Deposit amount must be greater than zero.");
+ }
+ this.balance += amount;
+ }
+
+ // Method to withdraw an amount from the account, ensuring sufficient funds
+ public void withdraw(double amount) {
+ if (amount <= 0) {
+ throw new IllegalArgumentException("Withdrawal amount must be greater than zero.");
+ }
+ if (amount > balance) {
+ throw new IllegalArgumentException("Insufficient funds for withdrawal.");
+ }
+ this.balance -= amount;
+ }
+
+ // Method to print account details
+ public void printAccountDetails() {
+ System.out.println("Bank Account Balance: $" + balance);
+ }
}
diff --git a/src/Lecture4_interfaces_abstract_classes/TransactionInterface.java b/src/Lecture4_interfaces_abstract_classes/TransactionInterface.java
index 5902713..058fc53 100644
--- a/src/Lecture4_interfaces_abstract_classes/TransactionInterface.java
+++ b/src/Lecture4_interfaces_abstract_classes/TransactionInterface.java
@@ -1,21 +1,29 @@
package Lecture4_interfaces_abstract_classes;
+
import java.util.Calendar;
/**
* Interface for Transactions
- * Any class that defines a transaction is expected to implement this Interface
+ * Any class that defines a transaction must implement this Interface.
+ * The interface enforces methods to retrieve transaction details such as amount, date, and a unique ID.
*/
public interface TransactionInterface {
- // Method to get the transaction amount
+ /**
+ * Gets the amount of the transaction.
+ * @return the transaction amount as a double
+ */
double getAmount();
- // Method to get the transaction date
+ /**
+ * Gets the date of the transaction.
+ * @return the date of the transaction as a Calendar object
+ */
Calendar getDate();
- // Method to get a unique identifier for the transaction
+ /**
+ * Gets the unique transaction ID.
+ * @return the unique transaction ID as a String
+ */
String getTransactionID();
-
}
-
-
diff --git a/src/Lecture4_interfaces_abstract_classes/WithdrawalTransaction.java b/src/Lecture4_interfaces_abstract_classes/WithdrawalTransaction.java
index face5b6..abb218b 100644
--- a/src/Lecture4_interfaces_abstract_classes/WithdrawalTransaction.java
+++ b/src/Lecture4_interfaces_abstract_classes/WithdrawalTransaction.java
@@ -5,41 +5,48 @@
import java.util.Calendar;
public class WithdrawalTransaction extends BaseTransaction {
+
public WithdrawalTransaction(int amount, @NotNull Calendar date) {
super(amount, date);
}
- private boolean checkDepositAmount(int amt) {
- if (amt < 0) {
- return false;
- } else {
- return true;
- }
+ /**
+ * Checks if the transaction amount is valid (greater than 0).
+ * @param amt the amount to check
+ * @return true if the amount is valid, false if negative
+ */
+ private boolean checkTransactionAmount(int amt) {
+ return amt >= 0;
}
- // Method to reverse the transaction
+ /**
+ * Reverses the withdrawal transaction (adds the amount back to the account).
+ * @return true if the reversal was successful
+ */
public boolean reverse() {
- return true;
- } // return true if reversal was successful
+ // A simple reversal: Credit the withdrawal amount back to the account
+ // The reversal process might include additional checks (e.g., transaction history, permissions, etc.)
+ return true; // Assuming reversal is always successful here (this can be extended)
+ }
- // Method to print a transaction receipt or details
+ /**
+ * Prints the transaction details for the withdrawal.
+ */
public void printTransactionDetails() {
- System.out.println("Deposit Trasaction: " + this.toString());
+ System.out.println("Withdrawal Transaction: " + this.toString());
}
- /*
- Oportunity for assignment: implementing different form of withdrawal
+ /**
+ * Applies the withdrawal to the bank account by decreasing the balance.
+ * The withdrawal is only successful if the account has sufficient funds.
*/
public void apply(BankAccount ba) {
double curr_balance = ba.getBalance();
- if (curr_balance > getAmount()) {
+ if (curr_balance >= getAmount()) {
double new_balance = curr_balance - getAmount();
ba.setBalance(new_balance);
+ } else {
+ System.out.println("Insufficient funds for withdrawal!");
}
}
-
- /*
- Assignment 1 Q3: Write the Reverse method - a method unique to the WithdrawalTransaction Class
- */
}
-
diff --git a/src/Main.java b/src/Main.java
index 584a048..af8e370 100644
--- a/src/Main.java
+++ b/src/Main.java
@@ -5,8 +5,6 @@
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
*/
@@ -17,21 +15,20 @@ public static void testTransaction1() {
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);
+ System.out.println("Lecture1_adt.TransactionInterface Amount: \t " + t1.getAmount());
+ System.out.println("Lecture1_adt.TransactionInterface Date: \t " + t1.getDate().getTime());
// 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
+ /**
+ * @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
- * */
-
+ * This code will help demonstrate the Design exposures still present in Transaction2 class
+ */
public static Transaction2 makeNextPayment(Transaction2 t) {
- Calendar d = t.getDate();
+ Calendar d = t.getDate();
d.add(Calendar.MONTH, 1);
return new Transaction2(t.getAmount(), d);
}
@@ -40,38 +37,28 @@ public static Transaction2 makeNextPayment(Transaction2 t) {
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("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
- */
-
+ System.out.println("Lecture1_adt.TransactionInterface Amount: \t " + modified_t.getAmount());
+ System.out.println("Lecture1_adt.TransactionInterface Date: \t " + modified_t.getDate().getTime());
}
-
- /** @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();
+ /**
+ * @return a list of 12 monthly payments of identical amounts
+ * This code will help demonstrate 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);
@@ -83,38 +70,24 @@ public static List makeYearOfPayments (int amount) throws NullPoin
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());
- }
+ System.out.println("\n\n ::::::::::::::::::::::::::::::::::::::::::::\n");
+ System.out.println("Lecture1_adt.TransactionInterface Amount: \t " + t3.getAmount());
+ System.out.println("Lecture1_adt.TransactionInterface Date: \t " + t3.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
+ /**
+ * @return a list of 12 monthly payments of identical amounts
+ * This code shows 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();
+ */
+ 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);
@@ -123,34 +96,26 @@ public static List makeYearOfPaymentsFinal (int amount) throws Nul
}
/*
- Testing Transaction3 class
+ Testing Transaction4 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());
+ 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()
+ // testTransaction1();
+ // testTransaction2();
+ // testTransaction3();
+ // testTransaction4();
}
-}
\ No newline at end of file
+}