Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
34 changes: 34 additions & 0 deletions src/Lecture4_interfaces_abstract_classes/BankAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
22 changes: 15 additions & 7 deletions src/Lecture4_interfaces_abstract_classes/TransactionInterface.java
Original file line number Diff line number Diff line change
@@ -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();

}


45 changes: 26 additions & 19 deletions src/Lecture4_interfaces_abstract_classes/WithdrawalTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
}

103 changes: 34 additions & 69 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import java.util.ArrayList;
import java.util.List;

//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
/*
* Client Code for accessing the Lecture1_adt.TransactionInterface.java module
*/
Expand All @@ -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);
}
Expand All @@ -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<Transaction3> makeYearOfPayments (int amount) throws NullPointerException {

List<Transaction3> listOfTransaction3s = new ArrayList<Transaction3>();
/**
* @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<Transaction3> makeYearOfPayments(int amount) throws NullPointerException {
List<Transaction3> 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);
Expand All @@ -83,38 +70,24 @@ public static List<Transaction3> makeYearOfPayments (int amount) throws NullPoin
Testing Transaction3 class
*/
public static void testTransaction3() {

List<Transaction3> 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<Transaction4> makeYearOfPaymentsFinal (int amount) throws NullPointerException {

List<Transaction4> listOfTransaction4s = new ArrayList<Transaction4>();
*/
public static List<Transaction4> makeYearOfPaymentsFinal(int amount) throws NullPointerException {
List<Transaction4> 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);
Expand All @@ -123,34 +96,26 @@ public static List<Transaction4> 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<Transaction4> 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();
}
}
}