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
213 changes: 213 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,215 @@
# Advanced-Programming
A Repository for Programming Exercises and Assignments in Advanced Programming
# Advanced Programming Assignment 1

**Course:** object oriented programming 2
**Name:** phillip kimonyi
regno: SCT212-0057/2024
**Semester:** Sept–Dec 2024

This project implements a simple **Bank Transaction System** in Java to demonstrate key **Advanced Object-Oriented Programming** concepts including:
- ADT (Abstract Data Type) Design
- Interfaces
- Abstract Classes
- Inheritance
- Polymorphism
- Method Overriding
- Exception Handling
- Defensive Programming

The system processes financial transactions such as:

- Deposits
- Withdrawals
- Reversible Withdrawals

---
## Folder Structure
src/
├── Lecture1_adt/
│ Transaction1.java
│ Transaction2.java
│ Transaction3.java
│ Transaction4.java
├── Lecture2_adt_specification/
│ Transaction4.java
├── Lecture4_interfaces_abstract_classes/
│ TransactionInterface.java
│ BaseTransaction.java
│ BankAccount.java
│ DepositTrasaction.java
│ WithdrawalTransaction.java
│ InsufficientFundsException.java
└── Main.java

## Concepts Implemented
### 1. Interface Implementation

`TransactionInterface` defines the transaction contract.

Required methods:
java
getAmount()
getDate()
getTransactionID()

### 2. Abstract Class Design

`BaseTransaction`:
- Implements `TransactionInterface`
- Stores common transaction fields:
- amount
- date
- transactionID
- Defines shared transaction behaviour.
### 3. Inheritance and Method Overriding

Derived classes:

- `DepositTrasaction`
- `WithdrawalTransaction`

Override:

```java
apply()
printTransactionDetails()

to provide specialized behavior.

### 4. Polymorphism

Demonstrated using superclass references:

```java
BaseTransaction bt =
new DepositTrasaction(250,date);
```

Late binding ensures the correct subclass implementation of `apply()` executes.

---

### 5. Exception Handling

Custom exception class:

```java
InsufficientFundsException
```

Features implemented:

- `throws`
- `try{ } catch{ } finally{ }`

Used to safely handle withdrawal failures.

---

### 6. Withdrawal Reversal

Unlike deposits, withdrawals are reversible.

Implemented through:

```java
reverse()
```

The method restores the original bank account balance.

---

### 7. Partial Withdrawal Mode

Implemented through overloaded method:

```java
apply(BankAccount ba,
boolean availableBalanceMode)
```

Behavior:

If:

```plaintext
0 < balance < withdrawal amount
```

the system:

- withdraws all available funds
- stores remaining unpaid amount
- records `amountNotWithdrawn`

---

## Running the Project

Compile:

```bash
javac Main.java
```

Run:

```bash
java Main
```

---

## Sample Output

```plaintext
Initial Balance: 1000

Deposit Transaction
Balance After Deposit: 1500

Withdrawal Transaction
Balance After Withdrawal: 1200

Withdrawal Reversed: true
Balance After Reversal: 1500

Exception Test:
Insufficient account balance.

Partial withdrawal completed.
Amount not withdrawn: 500
```

---

## Assignment Requirements Covered

✔ Interface implementation

✔ Abstract class inheritance

✔ Method overriding

✔ Polymorphism

✔ Exception handling

✔ Reversible withdrawals

✔ Overloaded methods

✔ Client code testing

---

## Author

Student Assignment Submission
Advanced Programming — Assignment One
2 changes: 1 addition & 1 deletion src/Lecture1_adt/Transaction1.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public class Transaction1 {
public int amount;
public Calendar date;

//we need to introduce a method to and make the fields private to prevent external modification
public Transaction1(int amount, Calendar date) {
this.amount = amount;
this.date = (Calendar) date.clone();
Expand Down
Binary file not shown.
10 changes: 6 additions & 4 deletions src/Lecture4_interfaces_abstract_classes/BaseTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public abstract class BaseTransaction implements TransactionInterface {
public BaseTransaction(int amount, @NotNull Calendar date) {
this.amount = amount;
this.date = (Calendar) date.clone();
int uniq = (int) Math.random()*10000;
int uniq = (int)(Math.random()*10000);
transactionID = date.toString()+uniq;
}

Expand All @@ -37,15 +37,17 @@ public double getAmount() {
* @return Calendar Object
*/
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 or Judicious Copying
}

// Method to get a unique identifier for the transaction
public String getTransactionID(){
return transactionID;
}
// Method to print a transaction receipt or details
// Abstract method to be implemented by subclasses for specific transaction details
public abstract void printTransactionDetails();
public abstract void apply(BankAccount ba);
// Abstract method to apply the transaction to a BankAccount, to be implemented by subclasses
public abstract void apply(BankAccount ba)
throws InsufficientFundsException;
}
43 changes: 43 additions & 0 deletions src/Lecture4_interfaces_abstract_classes/DepositTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package Lecture4_interfaces_abstract_classes;

import org.jetbrains.annotations.NotNull;

import java.util.Calendar;

public class DepositTransaction extends BaseTransaction {
public DepositTransaction(int amount, @NotNull Calendar date){
super(amount, date);
}
private boolean checkDepositAmount(int amt){
if (amt < 0){
return false;
}
else{
return true;
}
}

// Method to print a transaction receipt or details
// public void printTransactionDetails(){
// System.out.println("Deposit Trasaction: "+this.toString());
// }
//impoved version of printTransactionDetails
public void printTransactionDetails() {

System.out.println("\nDeposit Transaction");
System.out.println("Transaction ID: "
+ getTransactionID());

System.out.println("Amount: "
+ getAmount());

System.out.println("Date: "
+ getDate().getTime());
}

public void apply(BankAccount ba){
double curr_balance = ba.getBalance();
double new_balance = curr_balance + getAmount();
ba.setBalance(new_balance);
}
}
30 changes: 0 additions & 30 deletions src/Lecture4_interfaces_abstract_classes/DepositTrasaction.java

This file was deleted.

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package Lecture4_interfaces_abstract_classes;

public class InsufficientFundsException
extends Exception {
// Constructor that accepts a message to describe the exception
public InsufficientFundsException(String message) {
super(message);// Call the constructor of the superclass (Exception) with the provided message
}
}
Binary file not shown.
Loading