Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated codes ap #39

Closed
wants to merge 17 commits into from
Closed
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 .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Lecture4_interfaces_abstract_classes/BankAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ public double getBalance() {
public void setBalance(double balance) {
this.balance = balance;
}
}
}
62 changes: 27 additions & 35 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;
import java.util.UUID;

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
*/
public BaseTransaction(int amount, @NotNull Calendar date) {
private double amount; // The transaction amount
private Calendar date; // The transaction date
private String transactionID; // The unique identifier for the transaction

// Constructor to initialize the Transaction object
public BaseTransaction(double amount, Calendar date) {
this.amount = amount;
this.date = (Calendar) date.clone();
int uniq = (int) Math.random()*10000;
transactionID = date.toString()+uniq;
this.date = date;
this.transactionID = generateTransactionID();
}

/**
* getAmount()
* @return integer
*/
// Method to get the transaction amount
@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
*/
// Method to get the transaction date
@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 date;
}

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

// Abstract method to apply the transaction to a BankAccount
public abstract void apply(BankAccount account);

// Helper method to generate a unique identifier
private String generateTransactionID() {
return UUID.randomUUID().toString();
}
// Method to print a transaction receipt or details
public abstract void printTransactionDetails();
public abstract void apply(BankAccount ba);
}
}
48 changes: 48 additions & 0 deletions src/Lecture4_interfaces_abstract_classes/DepositTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package Lecture4_interfaces_abstract_classes;

import org.jetbrains.annotations.NotNull;

import java.util.Calendar;

public class DepositTransaction extends BaseTransaction {

// Constructor
public DepositTransaction(double amount, @NotNull Calendar date) {
super(amount, date);
}

// Validate the deposit amount
private boolean isDepositAmountValid(double amount) {
return amount > 0; // Deposit must be a positive value
}

// Print transaction details
public void printTransactionDetails() {
System.out.println("Deposit Transaction Details:");
System.out.println("Transaction ID: " + getTransactionID());
System.out.println("Transaction Date: " + getDate().getTime());
System.out.println("Amount Deposited: " + getAmount());
}

// Override the apply method to process the deposit for a BankAccount
@Override
public void apply(@NotNull BankAccount account) {
// Validate the deposit amount
if (!isDepositAmountValid(getAmount())) {
throw new IllegalArgumentException("Invalid deposit amount. Must be greater than zero.");
}

// Update the balance in the bank account
double currentBalance = account.getBalance();
double newBalance = currentBalance + getAmount();
account.setBalance(newBalance);

// Print success message
System.out.println("Deposit of " + getAmount() + " applied successfully. New balance: " + newBalance);
}

// Revert method to enforce irreversibility
public void revert(@NotNull BankAccount account) {
throw new UnsupportedOperationException("Deposits are irreversible and cannot be reverted.");
}
}
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,4 @@
package Lecture4_interfaces_abstract_classes;

public class InsufficientFundsException {
}
37 changes: 37 additions & 0 deletions src/Lecture4_interfaces_abstract_classes/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package Lecture4_interfaces_abstract_classes;

import java.util.Calendar;

public class Main {
public static void main(String[] args) {
// Create a bank account with an initial balance
BankAccount account = new BankAccount(500.0);

// Create a withdrawal transaction
Calendar date = Calendar.getInstance();
WithdrawalTransaction withdrawal = new WithdrawalTransaction(100.0, date);

// Print transaction details
withdrawal.printTransactionDetails();

// Apply the withdrawal transaction
try {
withdrawal.apply(account);
} catch (IllegalStateException | IllegalArgumentException e) {
System.out.println("Transaction Failed: " + e.getMessage());
}

// Print the updated balance
System.out.println("Account Balance After Withdrawal: " + account.getBalance());

// Reverse the withdrawal
try {
withdrawal.reverse(account);
} catch (Exception e) {
System.out.println("Reversal Failed: " + e.getMessage());
}

// Print the final balance
System.out.println("Final Account Balance: " + account.getBalance());
}
}
12 changes: 2 additions & 10 deletions src/Lecture4_interfaces_abstract_classes/TransactionInterface.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package Lecture4_interfaces_abstract_classes;
import java.util.Calendar;

/**
* Interface for Transactions
* Any class that defines a transaction is expected to implement this Interface
*/
import java.util.Calendar;
public interface TransactionInterface {

// Method to get the transaction amount
double getAmount();

Expand All @@ -15,7 +10,4 @@ public interface TransactionInterface {

// Method to get a unique identifier for the transaction
String getTransactionID();

}


}
66 changes: 40 additions & 26 deletions src/Lecture4_interfaces_abstract_classes/WithdrawalTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,55 @@
import java.util.Calendar;

public class WithdrawalTransaction extends BaseTransaction {
public WithdrawalTransaction(int amount, @NotNull Calendar date) {

// Constructor
public WithdrawalTransaction(double amount, @NotNull Calendar date) {
super(amount, date);
}

private boolean checkDepositAmount(int amt) {
if (amt < 0) {
return false;
} else {
return true;
}
// Check if the withdrawal amount is valid
private boolean isWithdrawalAmountValid(double amount) {
return amount > 0; // Withdrawal must be positive
}

// Method to reverse the transaction
public boolean reverse() {
return true;
} // return true if reversal was successful

// Method to print a transaction receipt or details
// Print transaction details
public void printTransactionDetails() {
System.out.println("Deposit Trasaction: " + this.toString());
System.out.println("Withdrawal Transaction Details:");
System.out.println("Transaction ID: " + getTransactionID());
System.out.println("Transaction Date: " + getDate().getTime());
System.out.println("Amount Withdrawn: " + getAmount());
}

/*
Oportunity for assignment: implementing different form of withdrawal
*/
public void apply(BankAccount ba) {
double curr_balance = ba.getBalance();
if (curr_balance > getAmount()) {
double new_balance = curr_balance - getAmount();
ba.setBalance(new_balance);
// Override the apply method to process the withdrawal for a BankAccount
@Override
public void apply(@NotNull BankAccount account) {
// Validate the withdrawal amount
if (!isWithdrawalAmountValid(getAmount())) {
throw new IllegalArgumentException("Invalid withdrawal amount. Must be greater than zero.");
}

// Check if there are sufficient funds in the account
double currentBalance = account.getBalance();
if (currentBalance >= getAmount()) {
// Deduct the amount from the balance
double newBalance = currentBalance - getAmount();
account.setBalance(newBalance);

// Print success message
System.out.println("Withdrawal of " + getAmount() + " applied successfully. New balance: " + newBalance);
} else {
// Insufficient funds
throw new IllegalStateException("Insufficient funds for withdrawal.");
}
}

/*
Assignment 1 Q3: Write the Reverse method - a method unique to the WithdrawalTransaction Class
*/
}
// Method to reverse the withdrawal
public void reverse(@NotNull BankAccount account) {
// Add the withdrawn amount back to the account balance
double newBalance = account.getBalance() + getAmount();
account.setBalance(newBalance);

// Print success message
System.out.println("Withdrawal of " + getAmount() + " reversed successfully. New balance: " + newBalance);
}
}
Loading