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

Advanced Programming Assignment #41

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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

## compiled class files ##
*.class
151 changes: 151 additions & 0 deletions Transactions/Transactioninterface
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import java.util.Calendar;

public interface TransactionInterface {
double getAmount();
Calendar getDate();
String getTransactionID();
void printTransactionDetails();
void apply(BankAccount bankAccount);
}
// Basetransaction
import java.util.Calendar;

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

public BaseTransaction(double amount, Calendar date, String transactionID) {
this.amount = amount;
this.date = date;
this.transactionID = transactionID;
}

@Override
public double getAmount() {
return amount;
}

@Override
public Calendar getDate() {
return date;
}

@Override
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());
}

@Override
public abstract void apply(BankAccount bankAccount);
}
//Deposittransaction
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 bankAccount) {
bankAccount.deposit(getAmount());
System.out.println("Deposit successful. New balance: " + bankAccount.getBalance());
}
}
//Withdrawaltransaction
import java.util.Calendar;

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

@Override
public void apply(BankAccount bankAccount) throws InsufficientFundsException {
if (bankAccount.getBalance() >= getAmount()) {
bankAccount.withdraw(getAmount());
System.out.println("Withdrawal successful. New balance: " + bankAccount.getBalance());
} else {
throw new InsufficientFundsException("Insufficient funds for withdrawal!");
}
}

public boolean reverse(BankAccount bankAccount) {
bankAccount.deposit(getAmount());
System.out.println("Withdrawal reversed. New balance: " + bankAccount.getBalance());
return true;
}
}
//InsufficientFundsException
public class InsufficientFundsException extends Exception {
public InsufficientFundsException(String message) {
super(message);
}
}
//BankAccount
public class BankAccount {
private double balance;

public BankAccount(double initialBalance) {
this.balance = initialBalance;
}

public double getBalance() {
return balance;
}

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

public void withdraw(double amount) {
balance -= amount;
}
}
// Main
import java.util.Calendar;

public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount(1000);

// Deposit Transaction
Calendar depositDate = Calendar.getInstance();
DepositTransaction deposit = new DepositTransaction(500, depositDate, "D001");
deposit.apply(account);
deposit.printTransactionDetails();

// Withdrawal Transaction
Calendar withdrawalDate = Calendar.getInstance();
WithdrawalTransaction withdrawal = new WithdrawalTransaction(700, withdrawalDate, "W001");
try {
withdrawal.apply(account);
} catch (InsufficientFundsException e) {
System.out.println(e.getMessage());
}
withdrawal.printTransactionDetails();

// Attempt to reverse withdrawal
withdrawal.reverse(account);

// Testing insufficient funds
WithdrawalTransaction insufficientWithdrawal = new WithdrawalTransaction(2000, withdrawalDate, "W002");
try {
insufficientWithdrawal.apply(account);
} catch (InsufficientFundsException e) {
System.out.println(e.getMessage());
}
}
}




1 change: 1 addition & 0 deletions src/Lecture1_adt/Transaction1.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ public Transaction1(int amount, Calendar date) {
this.date = (Calendar) date.clone();
}
}
//transaction1.java
1 change: 1 addition & 0 deletions src/Lecture1_adt/Transaction2.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ public Calendar getDate() {
return (Calendar) date.clone(); // Defensive copying or Judicious Copying
}
}
//transaction2.java
1 change: 1 addition & 0 deletions src/Lecture1_adt/Transaction3.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ public Calendar getDate() {
return (Calendar) date.clone(); // Defensive copying or Judicious Copying
}
}
//transaction3.java
1 change: 1 addition & 0 deletions src/Lecture1_adt/Transaction4.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ public Calendar getDate() {
return (Calendar) date.clone(); // Defensive copying or Judicious Copying for produces Interfaces
}
}
//transaction4.java
57 changes: 57 additions & 0 deletions src/SCT 212-0718/2022 Fidelity kathure/BankAccount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
public class BankAccount {
private double balance;

/**
* Constructor to initialize the bank account with an initial balance.
* @param initialBalance The initial balance of the account.
* @throws IllegalArgumentException if initialBalance is negative.
* Precondition: initialBalance >= 0
* Postcondition: The balance of the account is set to initialBalance.
*/
public BankAccount(double initialBalance) {
if (initialBalance < 0) {
throw new IllegalArgumentException("Initial balance cannot be negative.");
}
this.balance = initialBalance;
}

/**
* Returns the current balance of the account.
* Precondition: None
* Postcondition: The current balance is returned, which should be >= 0.
* @return The current balance.
*/
public double getBalance() {
return balance;
}

/**
* @param amount The amount to deposit. Must be positive.
* Precondition: amount > 0
* Postcondition: The balance is increased by the deposit amount (balance = balance + amount).
* @throws IllegalArgumentException if the amount is not positive.
*/
public void deposit(double amount) {
if (amount <= 0) {
throw new IllegalArgumentException("Deposit amount must be positive.");
}
balance += amount;
}

/**
* @param amount The amount to withdraw. Must be positive and less than or equal to the current balance.
* Precondition: amount > 0 and amount <= balance
* Postcondition: The balance is decreased by the withdrawal amount (balance = balance - amount).
* @throws IllegalArgumentException if the amount is not positive or exceeds the current balance.
* @throws InsufficientFundsException if there are insufficient funds for the withdrawal.
*/
public void withdraw(double amount) {
if (amount <= 0) {
throw new IllegalArgumentException("Withdrawal amount must be positive.");
}
if (amount > balance) {
throw new IllegalArgumentException("Insufficient funds for withdrawal.");
}
balance -= amount;
}
}
74 changes: 74 additions & 0 deletions src/SCT 212-0718/2022 Fidelity kathure/BaseTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import java.util.Calendar;



public class BaseTransaction implements TransactionInterface {
protected double amount; // The amount for this transaction
protected Calendar date; // The date this transaction occurred
protected String transactionID; // Unique identifier for the transaction

/**
* Constructs a new BaseTransaction object.
* @param amount the amount for the transaction, should be >= 0
* @param date the date of the transaction, must not be null
* @param transactionID the unique ID for the transaction, must not be null
*/
public BaseTransaction(double amount, Calendar date, String transactionID) {
if (amount < 0) {
throw new IllegalArgumentException("Amount must be greater than or equal to 0");
}
if (date == null || transactionID == null) {
throw new IllegalArgumentException("Date and transaction ID must not be null");
}
this.amount = amount;
this.date = date;
this.transactionID = transactionID;
}

/**
* Returns the amount of the transaction.
* @return the amount of the transaction
*/
@Override
public double getAmount() {
return amount;
}

/**
* Returns the date of the transaction.
* @return the date of the transaction as a Calendar object
*/
@Override
public Calendar getDate() {
return (Calendar) date.clone();
}

/**
* Returns the unique transaction ID.
* @return the transaction ID as a String
*/
@Override
public String getTransactionID() {
return transactionID;
}


public void printTransactionDetails() {
System.out.println("Transaction ID: " + transactionID);
System.out.println("Amount: " + amount);
System.out.println("Date: " + date.getTime());
}

/**
* Applies the transaction to the specified bank account.
* - The bank account balance should be updated according to the transaction (e.g., deposit or withdrawal).
*
* @param ba the BankAccount to apply the transaction to
*/
public void apply(BankAccount ba) {
if (ba == null) {
throw new IllegalArgumentException("BankAccount must not be null");
}
System.out.println("Applying transaction...");
}
}
42 changes: 42 additions & 0 deletions src/SCT 212-0718/2022 Fidelity kathure/DepositTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import java.util.Calendar;




public class DepositTransaction extends BaseTransaction {

/**
* Constructs a new DepositTransaction object.
* @param amount the amount to be deposited, must be > 0
* @param date the date of the deposit, must not be null
* @param transactionID the unique ID for the transaction, must not be null
*/
public DepositTransaction(double amount, Calendar date, String transactionID) {
super(amount, date, transactionID);
if (amount <= 0) {
throw new IllegalArgumentException("Deposit amount must be greater than 0");
}
}

/**
* Applies the deposit transaction to the specified bank account by increasing the balance.
*
* Preconditions:
* - The BankAccount provided must not be null.
* - The amount to be deposited must be greater than 0 (checked in the constructor).
*
* Postcondition:
* - The bank account balance is increased by the amount of the deposit.
* - A message is printed confirming the deposit and the amount.
*
* @param ba the BankAccount to apply the deposit to
*/
@Override
public void apply(BankAccount ba) {
if (ba == null) {
throw new IllegalArgumentException("BankAccount must not be null");
}
ba.deposit(amount);
System.out.println("Deposit successful: " + amount);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class InsufficientFundsException extends Exception {
public InsufficientFundsException(String message) {
super(message);
}
}

23 changes: 23 additions & 0 deletions src/SCT 212-0718/2022 Fidelity kathure/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import java.util.Calendar;

public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount(1000.0);
Calendar date = Calendar.getInstance();

// Create DepositTransaction
DepositTransaction deposit = new DepositTransaction(500.0, date, "D123");
deposit.apply(account);
deposit.printTransactionDetails();

// Create WithdrawalTransaction
WithdrawalTransaction withdrawal = new WithdrawalTransaction(300.0, date, "W456");
withdrawal.apply(account);
withdrawal.printTransactionDetails();

// Test reversal of withdrawal
withdrawal.reverse(account);

System.out.println("Final Balance: " + account.getBalance());
}
}
Loading