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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
<<<<<<< HEAD
# Advanced-Programming
A Repository for Programming Exercises and Assignments in Advanced Programming
=======
# Advanced_Programming
>>>>>>> 71439a9a807f9e5eaa72ba6c7eb117f4d09b7565
23 changes: 22 additions & 1 deletion src/Lecture4_interfaces_abstract_classes/BankAccount.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package Lecture4_interfaces_abstract_classes;

/**
* Represents a simple bank account with a balance.
* Starter code preserved; credit() and debit() helpers added
* so transaction classes don't need to call setBalance() directly.
*/
public class BankAccount {
private double balance;

public BankAccount(double balance) {
this.balance = balance;
}
Expand All @@ -13,4 +19,19 @@ public double getBalance() {
public void setBalance(double balance) {
this.balance = balance;
}
}

/** Adds amount to the balance (used by DepositTransaction). */
public void credit(double amount) {
this.balance += amount;
}

/** Subtracts amount from the balance (used by WithdrawalTransaction). */
public void debit(double amount) {
this.balance -= amount;
}

@Override
public String toString() {
return String.format("BankAccount[Balance=%.2f]", balance);
}
}
95 changes: 64 additions & 31 deletions src/Lecture4_interfaces_abstract_classes/BaseTransaction.java
Original file line number Diff line number Diff line change
@@ -1,51 +1,84 @@
package Lecture4_interfaces_abstract_classes;

import org.jetbrains.annotations.NotNull;

import java.util.Calendar;

public abstract class BaseTransaction implements TransactionInterface {
/**
* Q1 - Concrete class that implements TransactionInterface.
*
* The starter code made this abstract; the assignment asks us to make it
* a CONCRETE class that fully implements the interface, while ensuring
* its apply() behaves differently from the subclasses.
*
* Fields match the starter code exactly (int amount, Calendar date, String transactionID).
*/
public class BaseTransaction implements TransactionInterface {

// --- Fields (preserved from starter code) ---
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 - mirrors the starter code signature.
*
* @param amount integer transaction amount
* @param date must not be null; defensively copied to preserve invariants
*/
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;
public BaseTransaction(int amount, @NotNull Calendar date) {
this.amount = amount;
this.date = (Calendar) date.clone(); // defensive copy (from lecture)
int uniq = (int)(Math.random() * 10000);
this.transactionID = date.toString() + uniq;
}

/**
* getAmount()
* @return integer
*/
// ---------------------------------------------------------------
// Q1 – Getter implementations (from TransactionInterface)
// ---------------------------------------------------------------

@Override
public double getAmount() {
return amount; // Because we are dealing with Value types we need not worry about what we return
return amount; // int → double widening; value type, safe to return directly
}

/**
* 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 / judicious copy (from lecture)
}

// Method to get a unique identifier for the transaction
public String getTransactionID(){
return transactionID;
@Override
public String getTransactionID() {
return transactionID;
}

// ---------------------------------------------------------------
// Q1 – printTransactionDetails()
// ---------------------------------------------------------------

@Override
public void printTransactionDetails() {
System.out.println("=== Base Transaction Details ===");
System.out.println(" Transaction ID : " + transactionID);
System.out.printf (" Amount : %.2f%n", (double) amount);
System.out.printf (" Date : %tF%n", date);
System.out.println(" Type : BaseTransaction");
}

// ---------------------------------------------------------------
// Q1 – apply()
//
// BaseTransaction.apply() is intentionally DIFFERENT from the
// subclass overrides: it is a neutral / informational application
// that logs the call but does NOT modify the account balance.
// This makes the base behaviour clearly distinct from Deposit
// (which credits) and Withdrawal (which debits).
// ---------------------------------------------------------------

@Override
public void apply(BankAccount ba) {
System.out.printf(
"[BaseTransaction] apply() called — Amount: %.2f | Balance unchanged at: %.2f%n",
(double) amount, ba.getBalance()
);
}
// Method to print a transaction receipt or details
public abstract void printTransactionDetails();
public abstract void apply(BankAccount ba);
}
}
58 changes: 41 additions & 17 deletions src/Lecture4_interfaces_abstract_classes/DepositTrasaction.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,54 @@
package Lecture4_interfaces_abstract_classes;

import org.jetbrains.annotations.NotNull;

import java.util.Calendar;

/**
* Q1 & Q2 - Concrete class for deposit transactions.
*
* Deposits are IRREVERSIBLE by design (assignment spec, Q2).
* Overrides apply() to credit the BankAccount.
*
* Note: class name intentionally matches starter code spelling ("Trasaction").
*/
public class DepositTrasaction extends BaseTransaction {
public DepositTrasaction(int amount, @NotNull Calendar date){

// --- Constructor ---
public DepositTrasaction(int amount, @NotNull Calendar date) {
super(amount, date);
}
private boolean checkDepositAmount(int amt){
if (amt < 0){
return false;
}
else{
return true;
}

// Helper preserved from starter code
private boolean checkDepositAmount(int amt) {
return amt >= 0;
}

// Method to print a transaction receipt or details
public void printTransactionDetails(){
System.out.println("Deposit Trasaction: "+this.toString());
// ---------------------------------------------------------------
// Q1 – Override apply()
// Credits the account; clearly different from BaseTransaction.apply()
// which makes no balance change, and from WithdrawalTransaction
// which debits.
// ---------------------------------------------------------------

@Override
public void apply(BankAccount ba) {
ba.credit(getAmount());
System.out.printf(
"[DepositTransaction] Credited %.2f → New balance: %.2f%n",
getAmount(), ba.getBalance()
);
}

public void apply(BankAccount ba){
double curr_balance = ba.getBalance();
double new_balance = curr_balance + getAmount();
ba.setBalance(new_balance);
// ---------------------------------------------------------------
// Q1 – Override printTransactionDetails()
// ---------------------------------------------------------------

@Override
public void printTransactionDetails() {
System.out.println("=== Deposit Transaction Details ===");
System.out.println(" Transaction ID : " + getTransactionID());
System.out.printf (" Amount : +%.2f%n", getAmount());
System.out.printf (" Date : %tF%n", getDate());
System.out.println(" Type : Deposit (irreversible)");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package Lecture4_interfaces_abstract_classes;

/**
* Q3 - Custom checked exception for insufficient funds.
*
* Extends Exception (not RuntimeException) so it is a proper checked exception
* and callers are forced to handle or declare it — demonstrating Java's
* exception hierarchy through inheritance.
*/
public class InsufficientFundsException extends Exception {

private final double amountRequested;
private final double amountAvailable;

public InsufficientFundsException(double amountRequested, double amountAvailable) {
super(String.format(
"Insufficient funds: requested %.2f but only %.2f is available.",
amountRequested, amountAvailable
));
this.amountRequested = amountRequested;
this.amountAvailable = amountAvailable;
}

public double getAmountRequested() { return amountRequested; }
public double getAmountAvailable() { return amountAvailable; }

/** Convenience: how much short the account is. */
public double getShortfall() { return amountRequested - amountAvailable; }
}
14 changes: 10 additions & 4 deletions src/Lecture4_interfaces_abstract_classes/TransactionInterface.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package Lecture4_interfaces_abstract_classes;

import java.util.Calendar;

/**
* Interface for Transactions
* Any class that defines a transaction is expected to implement this Interface
* Interface for Transactions.
* Any class that defines a transaction is expected to implement this interface.
*
* Q1: Extended with printTransactionDetails() and apply() as required by the assignment.
*/
public interface TransactionInterface {

Expand All @@ -16,6 +19,9 @@ public interface TransactionInterface {
// Method to get a unique identifier for the transaction
String getTransactionID();

}

// Method to print transaction details (Q1)
void printTransactionDetails();

// Method to apply the transaction to a BankAccount (Q1)
void apply(BankAccount ba);
}
Loading