Skip to content
Open

Adts #36

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
2 changes: 1 addition & 1 deletion src/Lecture1_adt/Transaction1.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ public Transaction1(int amount, Calendar date) {
this.amount = amount;
this.date = (Calendar) date.clone();
}
}
}
4 changes: 2 additions & 2 deletions src/Lecture1_adt/Transaction2.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public Transaction2(int amount, @NotNull Calendar date) {
}

public int getAmount() {
return amount; // Because we are dealing with Value types we need not worry about what we return
return amount; // 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
}
}
}
2 changes: 1 addition & 1 deletion src/Lecture1_adt/Transaction3.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ 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
}
}
}
2 changes: 1 addition & 1 deletion src/Lecture1_adt/Transaction4.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ 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
}
}
}
2 changes: 1 addition & 1 deletion src/Lecture2_adt_specification/Transaction4.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ 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
}
}
}
55 changes: 54 additions & 1 deletion src/Lecture4_interfaces_abstract_classes/BankAccount.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,69 @@

package Lecture4_interfaces_abstract_classes;
/**
* BankAccount Class
* Description: Represents a simple bank account with a monetary balance.
* Provides methods to retrieve and update the account balance while enforcing non-negative values.

* Key Features:
* - Stores and manages account balance.
* - Allows controlled updates to ensure the integrity of the balance.

* @errorHandling:
* - Prevents negative balances during initialization or updates.


*/

public class BankAccount {
private double balance;
/**
* Constructor for BankAccount.
* Initializes a bank account with a specified initial balance.

* @param balance - The initial account balance (must be non-negative).
* @throws IllegalArgumentException if the initial balance is negative.
* @pre Initial balance must be non-negative.
* @post The account is created with the specified initial balance.

* Example:
* - Input: 1000.0
* - Output: Account created with a balance of 1000.0
*/

public BankAccount(double balance) {
this.balance = balance;
}
/**
* Retrieves the current account balance.

* @return double - The current account balance.
* @pre None.
* @post Returns the current balance without modifying it.

* Example:
* - Input: None
* - Output: 1000.0 (current account balance)
*/

public double getBalance() {
return balance;
}

/**
* Updates the account balance.
*
* @param balance - The new account balance (must be non-negative).
* @throws IllegalArgumentException if the new balance is negative.
* @pre New balance must be non-negative.
* @post The account balance is updated to the specified value.

* Example:
* - Input: 1500.0
* - Output: Account balance updated to 1500.0
*/

public void setBalance(double balance) {
this.balance = balance;
}
}
}
96 changes: 77 additions & 19 deletions src/Lecture4_interfaces_abstract_classes/BaseTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,107 @@
import org.jetbrains.annotations.NotNull;

import java.util.Calendar;
/**
* BaseTransaction Class
* Description: Represents an abstract base class for financial transactions.
* Implements the TransactionInterface to provide basic functionality such as retrieving transaction details
* (amount, date, and ID). This class serves as a foundation for specific transaction types like deposits and withdrawals.

* Key Features:
* - Provides common fields and methods for financial transactions.
* - Ensures that subclasses implement specific behavior for transaction application and details printing.

* Inherits:
* - TransactionInterface

* Abstract Methods:
* - printTransactionDetails(): Prints transaction details (to be implemented by subclasses).
* - apply(BankAccount ba): Applies the transaction to a bank account (to be implemented by subclasses).
*
* @errorHandling:
* - Defensive copying is used for date fields to ensure immutability.
* - Transaction ID is generated uniquely for each transaction.
*
*
*/

public abstract class BaseTransaction implements TransactionInterface {
private final int 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
* Constructor for BaseTransaction.
* Initializes a transaction with a specified amount and date, generating a unique transaction ID.
*
* @param amount - The transaction amount (must be positive).
* @param date - The transaction date (must be a valid, non-null Calendar object).
* @throws IllegalArgumentException if the amount is negative or the date is null.
* @pre Amount must be positive, and date must be a valid Calendar object.
* @post The transaction is initialized with the specified amount, date, and a unique ID.
*/
public BaseTransaction(int amount, @NotNull Calendar date) {

public BaseTransaction(int amount, @NotNull Calendar date) {
this.amount = amount;
this.date = (Calendar) date.clone();
int uniq = (int) Math.random()*10000;
transactionID = date.toString()+uniq;

// Generate a shortened transaction ID using a simple format
String baseID = String.valueOf(date.getTimeInMillis()); // Use the time in milliseconds for simplicity
int uniq = (int) (Math.random() * 10000); // Random value for uniqueness
transactionID = baseID.substring(0, 8) + "-" + uniq; // Shortened to the first 8 digits of the time and added random part
}

/**
* getAmount()
* @return integer
* Retrieves the transaction amount.
*
* @return double - The transaction amount.
* @pre None.
* @post Returns the transaction amount as a positive integer.

* Example:
* - Input: None
* - Output: 500 (transaction amount)
*/

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

/**
* getDate()
* @return Calendar Object
* Retrieves the transaction date.
*
* @return Calendar - A defensive copy of the transaction date.
* @pre None.
* @post Returns a copy of the transaction date to ensure immutability.

* Example:
* - Input: None
* - Output: Calendar instance with 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();
}

// Method to get a unique identifier for the transaction
/*
* Retrieves the unique transaction identifier.

* @return String - The unique transaction ID.
* @pre None.
* @post Returns a unique string identifier for the transaction.

* Example:
* - Input: None
* - Output: TX-12345678-9876 (unique transaction ID)
*/

public String getTransactionID(){
return transactionID;
}
// Method to print a transaction receipt or details


public abstract void printTransactionDetails();
public abstract void apply(BankAccount ba);
}


public abstract void apply(BankAccount ba) throws InsufficientFundsException;
}
108 changes: 108 additions & 0 deletions src/Lecture4_interfaces_abstract_classes/DepositTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package Lecture4_interfaces_abstract_classes;

import org.jetbrains.annotations.NotNull;

import java.util.Calendar;

/**
* DepositTransaction Class
* Description: Represents a deposit transaction in the banking system.
* It extends the BaseTransaction class to provide functionality for handling monetary deposits.

* Key Features:
* - Tracks and applies deposit transactions to bank accounts.
* - Validates deposit amounts to ensure only positive values are processed.
* - Provides detailed transaction information.

* Inherits:
* - BaseTransaction
*
* @errorHandling:
* - Prevents processing of invalid deposit amounts (zero or negative).
* - Provides feedback on successful or failed transactions.
*


*/

public class DepositTransaction extends BaseTransaction {
/**
* Constructor for DepositTransaction.
* Initializes a deposit transaction with a specified amount and date.
*
* @param amount - The amount to deposit (must be positive).
* @param date - The date of the transaction (must be a valid, non-null Calendar object).
* @throws IllegalArgumentException if amount is negative or date is null.
* @pre Amount must be a positive integer. Date must be a valid Calendar object.
* @post The transaction is initialized with the specified amount and date.
*/

public DepositTrasaction(int amount, @NotNull Calendar date){
super(amount, date);

}

/**
* Validates the deposit transaction amount.
*
* @param amt - The deposit amount to validate.
* @return boolean - Returns true if the amount is strictly greater than 0, false otherwise.
* @pre None.
* @post Returns true for valid deposit amounts and false for invalid amounts.

* Example:
* - Input: 500
* - Output: true
*/

private boolean checkDepositAmount(double amt){
return amt > 0;
}

/**
* Prints the details of the deposit transaction.
*
* @pre None.
* @post Prints transaction details to the console, including amount, date, and transaction ID.

* Example:
* - Input: None
* - Output:
* Deposit Transaction Details:
* Amount: 500
* Date: Mon Jan 01 12:00:00 EAT 2024
* Transaction ID: TX-12345678
*/

@Override
public void printTransactionDetails(){

System.out.println("Deposit Transaction Details: ");
System.out.println("Amount: " + getAmount());
System.out.println("Date: " + getDate().getTime());
System.out.println("Transaction ID: " + getTransactionID());
}
/**
* Applies the deposit transaction to a specified bank account.
*
* @param ba - The BankAccount object to apply the deposit to.
* @pre The bank account must be valid, and the deposit amount must be positive.
* @post The bank account balance is increased if the deposit is valid. Otherwise, no changes are made.

* Example:
* - Input: BankAccount with balance 1000, deposit amount 500.
* - Output:
* Deposit of 500 applied.New Balance: 1500
*/
@Override
public void apply(BankAccount ba){
if (checkDepositAmount(getAmount())) {
double curr_balance = ba.getBalance();
double new_balance = curr_balance + getAmount();
ba.setBalance(new_balance);
System.out.println("Deposit of " + getAmount() + " applied. New Balance: " + ba.getBalance());
} else {
System.out.println("Invalid deposit amount: " + getAmount());
}
}
}
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,8 @@
package Lecture4_interfaces_abstract_classes;

public class InsufficientFundsException extends Exception {
// Constructor to pass the error message
public InsufficientFundsException(String message) {
super(message);
}
}
Loading