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 #20

Open
wants to merge 7 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
18 changes: 0 additions & 18 deletions src/Lecture1_adt/Transaction1.java

This file was deleted.

36 changes: 0 additions & 36 deletions src/Lecture1_adt/Transaction2.java

This file was deleted.

31 changes: 0 additions & 31 deletions src/Lecture1_adt/Transaction3.java

This file was deleted.

32 changes: 0 additions & 32 deletions src/Lecture1_adt/Transaction4.java

This file was deleted.

47 changes: 0 additions & 47 deletions src/Lecture2_adt_specification/Transaction4.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package Lecture4_interfaces_abstract_classes.ASSIGNMENT;

/**
* Represents a basic bank account with functionalities for deposits, withdrawals,
* and balance management
*/
public class BankAccount {
private double balance ;


/**
* Creates a new bank account with the specified initial balance
*
* @param initialbalance the initial balance for the account (must not be negative)
* @throws IllegalArgumentException if the initial balance is negative
*
*/

public BankAccount(double initialbalance) {
if (initialbalance < 0) {
throw new IllegalArgumentException("Initial balance must not be negative");
}
this.balance = initialbalance;
}
/**
* gets the current balance of the account
* @return the current balance
*/
public double getBalance() {
return balance;
}


/**
* deposits the specified amont in to the bank account
* preconditions
* -the deposit must be greater than 0
* postconditions
* - The specified amount is added to the current balance
*
* @param amt the amount to be deposited
* @throws IllegalArgumentExemption if the deposit is less than or equal to zero
*/
public void deposit(double amount){
if (amount <= 0){
throw new IllegalArgumentException("Deposit amount must be greater than zero.");

}
balance += amount;
System.out.println("Deposited successfully: " + amount + "|New balance: " + balance);
}

/**
* withdraws the specified amount from the bank account
* preconditions
* - amount must be greater than zero
* - amount must be less than or equal to the current balance
* postconditions
* - the specified amount is subtracted from the current balance
* @param amount the amount to withdraw from the bank account
* @throws IllegalArgumentException if the amount is less than zero or greater than the current balance
* @throws InsufficientResourceException if the amount is greater than the current balance
*/
public void withdraw(double amount){
if (amount <= 0){
throw new IllegalArgumentException("Withdrawal amount must be greater than zero.");
}
if (amount > 0){
throw new InsufficientResourceException("Insufficient funds for withdrawal.");
}
balance -= amount;
System.out.println("Withdrawn successfully: " + amount + "|New balance: " + balance);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package Lecture4_interfaces_abstract_classes.ASSIGNMENT;
import java.util.Calendar;


import Lecture4_interfaces_abstract_classes.BankAccount;

import Lecture4_interfaces_abstract_classes.TransactionInterface;

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

/**
@param amount It should be a positive number
@param date the date cannot be null
@param transactionID it cannot be null or empty
@throws illegalArgumentException
*/
public BaseTransaction(double amount, Calendar date, String transactionID) {
if (amount <=0){
throw new IllegalArgumentException("Amount must be positive");

}
if (date ==null){
throw new IllegalArgumentException("Date must not be null");

}
if (transactionID ==null){
throw new IllegalArgumentException("Transaction ID must not be null");
}
this.amount = amount;
this.date = date;
this.transactionID = transactionID;
}

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

/**
* retrives the date of the transaction
* @return date of the transaction
*/

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

/**
It retrives the unique identifier for the transaction
@return unique identifier for the transaction

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


/**
* @param ba the bankAccount object to which this transaction will be applied
* @throws IllegalArgumentException if the bankAccount is null
*
*/


@Override
public void apply(BankAccount ba){
if (ba == null){
throw new IllegalArgumentException("BankAccount must not be null");
}
System.out.println("BaseTransaction applied ");
}

}


Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package Lecture4_interfaces_abstract_classes.ASSIGNMENT;
import java.util.Calendar;

import Lecture4_interfaces_abstract_classes.BankAccount;

/**
* Represents a depositTransaction that credits a specified amount to a bank account
* extends the BaseTransaction class
*/

public class DepositTransaction extends BaseTransaction {

/**
* Constructor
* @param amount
* @param transactionID
* @param date
*/
public DepositTransaction(double amount, String transactionID, Calendar date) {
super(amount, date , transactionID );
}

/**
* Apply the deposit transaction to the bank account.
* preconditions
* - The amount to deposit must be greater than 0
* - The bank account must not be null
*
* postconditions
* - The amount is added to the bank account
* - A success message is then printed
*
* @param ba the bank account to deposit the amount to
*/

@Override
public void apply(BankAccount ba) {
if (ba == null) {
throw new IllegalArgumentException("Bank account is null.");
}
if (getAmount() <= 0) {
throw new IllegalArgumentException("Amount to deposit must be greater than 0.");
}

ba.deposit(getAmount());
System.out.println("Deposit Applied successfully:" + getAmount());

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package Lecture4_interfaces_abstract_classes.ASSIGNMENT ;

public class InsufficientResourceException extends RuntimeException {
public InsufficientResourceException(String message) {
super(message);
}

}
Loading