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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ bin/
.vscode/

### Mac OS ###
.DS_Store
.DS_Store

# Ignore all .class files
*.class
23 changes: 8 additions & 15 deletions src/Lecture1_adt/Transaction2.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,29 @@
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:
* This Lecture1_adt.Transaction2 Class takes the first step to resolve 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
* External client code only accesses via allowable operations
* --- Changes to internal representation can still be accessed via the same methods defined
*
* Lecture1_adt
* 2. Preservation of Invariants: --- Access modifies private final fields to make the data unchangeable
*/

public class Transaction2 {
private final int amount;
private final Calendar date;

public Transaction2(int amount, @NotNull Calendar date) {
public Transaction2(int amount, Calendar date) {
this.amount = amount;
this.date = date;
this.date = date; // Defensive copying not required for constructor arguments in this class
}

public int getAmount() {
return amount; // Because we are dealing with Value types we need not worry about what we return
return amount; // Value types like integers can be returned directly
}

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
return (Calendar) date.clone(); // Defensive copying ensures reference types are not altered externally
}
}
18 changes: 6 additions & 12 deletions src/Lecture1_adt/Transaction3.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,25 @@
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
* This Lecture1_adt.Transaction3 class adds code to correct potential exposure issues in Transaction2.
* Methods that return objects of reference types use defensive copying to enhance invariant preservation.
*/
public class Transaction3 {
private final int amount;
private final Calendar date;

public Transaction3(int amount, @NotNull Calendar date) {
public Transaction3(int amount, Calendar date) {
this.amount = amount;
this.date = date;
this.date = date; // Defensive copying not required for constructor arguments in this class
}

public int getAmount() {
return amount; // Because we are dealing with Value types we need not worry about what we return
return amount; // Value types like integers can be returned directly
}

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
return (Calendar) date.clone(); // Defensive copying ensures reference types are not altered externally
}
}
21 changes: 7 additions & 14 deletions src/Lecture1_adt/Transaction4.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,25 @@
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
* This Lecture1_adt.Transaction4 class ensures that both constructor arguments and method returns use defensive copying.
* This prevents unintended side effects when dealing with reference types.
*/
public class Transaction4 {
private final int amount;
private final Calendar date;

public Transaction4(int amount, @NotNull Calendar date) {
public Transaction4(int amount, Calendar date) {
this.amount = amount;
this.date = (Calendar) date.clone(); // Defensive copying or Judicious Copying for Requires interfaces
this.date = (Calendar) date.clone(); // Defensive copying for constructor arguments
}

public int getAmount() {
return amount; // Because we are dealing with Value types we need not worry about what we return
return amount; // Value types like integers can be returned directly
}

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
return (Calendar) date.clone(); // Defensive copying ensures reference types are not altered externally
}
}
35 changes: 13 additions & 22 deletions src/Lecture2_adt_specification/Transaction4.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,38 @@
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
* This Lecture2_adt_specification.Transaction4 class extends the design considerations of the previous Transaction4 class.
* It adds specifications for 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
* Constructor for Transaction4
* @param amount the transaction amount (must be an integer)
* @param date the transaction date (must be a Calendar object and not null)
*/
public Transaction4(int amount, @NotNull Calendar date) {
public Transaction4(int amount, Calendar date) {
this.amount = amount;
this.date = (Calendar) date.clone();
this.date = (Calendar) date.clone(); // Defensive copying ensures input reference is not shared
}


/**
* getAmount()
* @return integer
* Retrieves the transaction amount.
* @return an integer representing the amount
*/
public int getAmount() {
return amount; // Because we are dealing with Value types we need not worry about what we return
return amount; // Value types like integers can be returned directly
}

/**
* getDate()
* @return Calendar Object
* Retrieves the transaction date.
* @return a Calendar object representing the transaction date
*/
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
return (Calendar) date.clone(); // Defensive copying ensures reference types are not altered externally
}
}
13 changes: 11 additions & 2 deletions src/Lecture4_interfaces_abstract_classes/BankAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

public class BankAccount {
private double balance;

public BankAccount(double balance) {
this.balance = balance;
}
Expand All @@ -10,7 +11,15 @@ public double getBalance() {
return balance;
}

public void setBalance(double balance) {
this.balance = balance;
public void deposit(double amount) {
balance += amount;
}

public void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
} else {
System.out.println("Insufficient balance for withdrawal.");
}
}
}
54 changes: 23 additions & 31 deletions src/Lecture4_interfaces_abstract_classes/BaseTransaction.java
Original file line number Diff line number Diff line change
@@ -1,51 +1,43 @@
package Lecture4_interfaces_abstract_classes;

import org.jetbrains.annotations.NotNull;

import java.util.Calendar;

public abstract class BaseTransaction implements TransactionInterface {
private final int amount;
private final double amount;
private final Calendar date;
private final String transactionID;

/**
* 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 BaseTransaction(int amount, @NotNull Calendar date) {
public BaseTransaction(double amount, Calendar date, String transactionID) {
this.amount = amount;
this.date = (Calendar) date.clone();
int uniq = (int) Math.random()*10000;
transactionID = date.toString()+uniq;
this.date = (Calendar) date.clone(); // Defensive copying
this.transactionID = transactionID;
}

/**
* getAmount()
* @return integer
*/
@Override
public double getAmount() {
return amount; // Because we are dealing with Value types we need not worry about what we return
return amount;
}

/**
* getDate()
* @return Calendar Object
*/
@Override
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
return (Calendar) date.clone(); // Defensive copying
}

@Override
public String getTransactionID() {
return transactionID;
}

// Method to get a unique identifier for the transaction
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());
}
// Method to print a transaction receipt or details
public abstract void printTransactionDetails();

@Override
public abstract void apply(BankAccount ba);

@Override
public abstract boolean reverse(BankAccount ba);
}
27 changes: 27 additions & 0 deletions src/Lecture4_interfaces_abstract_classes/DepositTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package Lecture4_interfaces_abstract_classes;

import java.util.Calendar;

public class DepositTransaction extends BaseTransaction {
public DepositTransaction(double amount, Calendar date, String transactionID) {
super(amount, date, transactionID);
}

@Override
public void apply(BankAccount ba) {
ba.deposit(getAmount());
System.out.println("Deposited: " + getAmount());
}

@Override
public boolean reverse(BankAccount ba) {
if (ba.getBalance() >= getAmount()) {
ba.withdraw(getAmount());
System.out.println("Deposit reversed: " + getAmount());
return true;
} else {
System.out.println("Unable to reverse deposit: insufficient balance.");
return false;
}
}
}
30 changes: 0 additions & 30 deletions src/Lecture4_interfaces_abstract_classes/DepositTrasaction.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package Lecture4_interfaces_abstract_classes;
public class InsufficientFundsException extends Exception {
public InsufficientFundsException(String message) {
super(message);
}
}
Loading