diff --git a/.gitignore b/.gitignore index f68d109..1638c37 100644 --- a/.gitignore +++ b/.gitignore @@ -26,4 +26,7 @@ bin/ .vscode/ ### Mac OS ### -.DS_Store \ No newline at end of file +.DS_Store + +## compiled class files ## +*.class \ No newline at end of file diff --git a/Transactions/Transactioninterface b/Transactions/Transactioninterface new file mode 100644 index 0000000..57c46a2 --- /dev/null +++ b/Transactions/Transactioninterface @@ -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()); + } + } +} + + + + diff --git a/src/Lecture1_adt/Transaction1.java b/src/Lecture1_adt/Transaction1.java index 8a46bd5..27e2e47 100644 --- a/src/Lecture1_adt/Transaction1.java +++ b/src/Lecture1_adt/Transaction1.java @@ -16,3 +16,4 @@ public Transaction1(int amount, Calendar date) { this.date = (Calendar) date.clone(); } } +//transaction1.java \ No newline at end of file diff --git a/src/Lecture1_adt/Transaction2.java b/src/Lecture1_adt/Transaction2.java index 6c7a010..006cd50 100644 --- a/src/Lecture1_adt/Transaction2.java +++ b/src/Lecture1_adt/Transaction2.java @@ -34,3 +34,4 @@ public Calendar getDate() { return (Calendar) date.clone(); // Defensive copying or Judicious Copying } } +//transaction2.java diff --git a/src/Lecture1_adt/Transaction3.java b/src/Lecture1_adt/Transaction3.java index 48781e7..9bbd8d5 100644 --- a/src/Lecture1_adt/Transaction3.java +++ b/src/Lecture1_adt/Transaction3.java @@ -29,3 +29,4 @@ public Calendar getDate() { return (Calendar) date.clone(); // Defensive copying or Judicious Copying } } +//transaction3.java diff --git a/src/Lecture1_adt/Transaction4.java b/src/Lecture1_adt/Transaction4.java index f09b0d8..fc9d182 100644 --- a/src/Lecture1_adt/Transaction4.java +++ b/src/Lecture1_adt/Transaction4.java @@ -30,3 +30,4 @@ public Calendar getDate() { return (Calendar) date.clone(); // Defensive copying or Judicious Copying for produces Interfaces } } +//transaction4.java \ No newline at end of file diff --git a/src/SCT 212-0718/2022 Fidelity kathure/BankAccount.java b/src/SCT 212-0718/2022 Fidelity kathure/BankAccount.java new file mode 100644 index 0000000..d208d11 --- /dev/null +++ b/src/SCT 212-0718/2022 Fidelity kathure/BankAccount.java @@ -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; + } +} diff --git a/src/SCT 212-0718/2022 Fidelity kathure/BaseTransaction.java b/src/SCT 212-0718/2022 Fidelity kathure/BaseTransaction.java new file mode 100644 index 0000000..ccea74a --- /dev/null +++ b/src/SCT 212-0718/2022 Fidelity kathure/BaseTransaction.java @@ -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..."); + } +} diff --git a/src/SCT 212-0718/2022 Fidelity kathure/DepositTransaction.java b/src/SCT 212-0718/2022 Fidelity kathure/DepositTransaction.java new file mode 100644 index 0000000..9ba3956 --- /dev/null +++ b/src/SCT 212-0718/2022 Fidelity kathure/DepositTransaction.java @@ -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); + } +} diff --git a/src/SCT 212-0718/2022 Fidelity kathure/InsufficientFundsException.java b/src/SCT 212-0718/2022 Fidelity kathure/InsufficientFundsException.java new file mode 100644 index 0000000..845512f --- /dev/null +++ b/src/SCT 212-0718/2022 Fidelity kathure/InsufficientFundsException.java @@ -0,0 +1,6 @@ +public class InsufficientFundsException extends Exception { + public InsufficientFundsException(String message) { + super(message); + } +} + diff --git a/src/SCT 212-0718/2022 Fidelity kathure/Main.java b/src/SCT 212-0718/2022 Fidelity kathure/Main.java new file mode 100644 index 0000000..b70e413 --- /dev/null +++ b/src/SCT 212-0718/2022 Fidelity kathure/Main.java @@ -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()); + } +} diff --git a/src/SCT 212-0718/2022 Fidelity kathure/TransactionInterface.java b/src/SCT 212-0718/2022 Fidelity kathure/TransactionInterface.java new file mode 100644 index 0000000..376b299 --- /dev/null +++ b/src/SCT 212-0718/2022 Fidelity kathure/TransactionInterface.java @@ -0,0 +1,10 @@ +import java.util.Calendar; + +public interface TransactionInterface { + double getAmount(); + Calendar getDate(); + String getTransactionID(); + void printTransactionDetails(); + void apply(BankAccount ba); + boolean reverse(BankAccount ba); +} diff --git a/src/SCT 212-0718/2022 Fidelity kathure/WithdrawalTransaction.java b/src/SCT 212-0718/2022 Fidelity kathure/WithdrawalTransaction.java new file mode 100644 index 0000000..358e444 --- /dev/null +++ b/src/SCT 212-0718/2022 Fidelity kathure/WithdrawalTransaction.java @@ -0,0 +1,57 @@ +import java.util.Calendar; + +public class WithdrawalTransaction extends BaseTransaction { + + /** + * Constructs a new WithdrawalTransaction object. + * @param amount the amount to be withdrawn, must be > 0 + * @param date the date of the withdrawal, must not be null + * @param transactionID the unique ID for the transaction, must not be null + */ + public WithdrawalTransaction(double amount, Calendar date, String transactionID) { + super(amount, date, transactionID); + if (amount <= 0) { + throw new IllegalArgumentException("Withdrawal amount must be greater than 0"); + } + } + + /** + * Applies the withdrawal transaction to the specified bank account by decreasing the balance. + * + * Preconditions: + * - The BankAccount provided must not be null. + * - The amount to be withdrawn must be greater than 0 (checked in the constructor). + * - The account must have sufficient funds for the withdrawal. + * @param ba the BankAccount to apply the withdrawal to + */ + @Override + public void apply(BankAccount ba) { + if (ba == null) { + throw new IllegalArgumentException("BankAccount must not be null"); + } + try { + if (ba.getBalance() < amount) { + throw new InsufficientFundsException("Insufficient funds for withdrawal."); + } + ba.withdraw(amount); + System.out.println("Withdrawal successful: " + amount); + } catch (InsufficientFundsException e) { + System.out.println(e.getMessage()); + } + } + + /** + * Reverses the withdrawal by depositing the same amount back to the bank account. + * @param ba the BankAccount to reverse the withdrawal on + * @return true if the reversal is successful + */ + @Override + public boolean reverse(BankAccount ba) { + if (ba == null) { + throw new IllegalArgumentException("BankAccount must not be null"); + } + ba.deposit(amount); + System.out.println("Withdrawal reversed: " + amount); + return true; + } +}