From 9c7d38e98425f0cc39046f0e0e8ee33248c54fab Mon Sep 17 00:00:00 2001 From: Nihznyweizer Date: Mon, 15 Jun 2026 17:28:08 +0300 Subject: [PATCH] Add files via upload --- .gitignore | 30 +---------- BankAccount.java | 27 ++++++++++ BaseTransaction.java | 41 +++++++++++++++ DepositTransaction.java | 15 ++++++ InsufficientFundsException.java | 6 +++ TransactionInterface.java | 14 +++++ WithdrawalTransaction.java | 93 +++++++++++++++++++++++++++++++++ 7 files changed, 197 insertions(+), 29 deletions(-) create mode 100644 BankAccount.java create mode 100644 BaseTransaction.java create mode 100644 DepositTransaction.java create mode 100644 InsufficientFundsException.java create mode 100644 TransactionInterface.java create mode 100644 WithdrawalTransaction.java diff --git a/.gitignore b/.gitignore index f68d109..5241a72 100644 --- a/.gitignore +++ b/.gitignore @@ -1,29 +1 @@ -### IntelliJ IDEA ### -out/ -!**/src/main/**/out/ -!**/src/test/**/out/ - -### Eclipse ### -.apt_generated -.classpath -.factorypath -.project -.settings -.springBeans -.sts4-cache -bin/ -!**/src/main/**/bin/ -!**/src/test/**/bin/ - -### NetBeans ### -/nbproject/private/ -/nbbuild/ -/dist/ -/nbdist/ -/.nb-gradle/ - -### VS Code ### -.vscode/ - -### Mac OS ### -.DS_Store \ No newline at end of file +*.class \ No newline at end of file diff --git a/BankAccount.java b/BankAccount.java new file mode 100644 index 0000000..8c53d30 --- /dev/null +++ b/BankAccount.java @@ -0,0 +1,27 @@ +public class BankAccount { + + private double balance; + + public BankAccount(double balance) { + this.balance = balance; + } + + public double getBalance() { + return balance; + } + + public void deposit(double amount) { + balance += amount; + } + + public void withdraw(double amount) + throws InsufficientFundsException { + + if (amount > balance) { + throw new InsufficientFundsException( + "Insufficient Funds."); + } + + balance -= amount; + } +} \ No newline at end of file diff --git a/BaseTransaction.java b/BaseTransaction.java new file mode 100644 index 0000000..e894639 --- /dev/null +++ b/BaseTransaction.java @@ -0,0 +1,41 @@ +import java.util.Calendar; + +public class BaseTransaction implements TransactionInterface { + + protected double amount; + protected Calendar date; + protected String transactionID; + + public BaseTransaction(double amount, String transactionID) { + this.amount = amount; + this.transactionID = transactionID; + this.date = Calendar.getInstance(); + } + + @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 void apply(BankAccount ba) { + System.out.println("Generic transaction applied."); + } +} \ No newline at end of file diff --git a/DepositTransaction.java b/DepositTransaction.java new file mode 100644 index 0000000..06c00ba --- /dev/null +++ b/DepositTransaction.java @@ -0,0 +1,15 @@ +public class DepositTransaction extends BaseTransaction { + + public DepositTransaction(double amount, + String transactionID) { + super(amount, transactionID); + } + + @Override + public void apply(BankAccount ba) { + ba.deposit(amount); + System.out.println( + "Deposit of " + amount + + " completed."); + } +} \ No newline at end of file diff --git a/InsufficientFundsException.java b/InsufficientFundsException.java new file mode 100644 index 0000000..30b1a1d --- /dev/null +++ b/InsufficientFundsException.java @@ -0,0 +1,6 @@ +public class InsufficientFundsException extends Exception { + + public InsufficientFundsException(String message) { + super(message); + } +} \ No newline at end of file diff --git a/TransactionInterface.java b/TransactionInterface.java new file mode 100644 index 0000000..695aafd --- /dev/null +++ b/TransactionInterface.java @@ -0,0 +1,14 @@ +import java.util.Calendar; + +public interface TransactionInterface { + + double getAmount(); + + Calendar getDate(); + + String getTransactionID(); + + void printTransactionDetails(); + + void apply(BankAccount ba); +} \ No newline at end of file diff --git a/WithdrawalTransaction.java b/WithdrawalTransaction.java new file mode 100644 index 0000000..24de988 --- /dev/null +++ b/WithdrawalTransaction.java @@ -0,0 +1,93 @@ +public class WithdrawalTransaction extends BaseTransaction { + + private BankAccount accountUsed; + private boolean reversed = false; + private double amountNotWithdrawn; + + public WithdrawalTransaction(double amount, + String transactionID) { + super(amount, transactionID); + } + + @Override + public void apply(BankAccount ba) { + + try { + + ba.withdraw(amount); + accountUsed = ba; + + System.out.println( + "Withdrawal of " + + amount + + " completed."); + + } catch (InsufficientFundsException e) { + + System.out.println( + e.getMessage()); + } + } + + public void apply(BankAccount ba, + boolean useAvailableBalance) { + + try { + + double balance = + ba.getBalance(); + + if (balance <= 0) { + + throw new + InsufficientFundsException( + "Account balance is zero."); + } + + if (balance < amount) { + + amountNotWithdrawn = + amount - balance; + + ba.withdraw(balance); + + System.out.println( + "Partial withdrawal completed."); + } else { + + ba.withdraw(amount); + + amountNotWithdrawn = 0; + } + + } catch (InsufficientFundsException e) { + + System.out.println( + e.getMessage()); + + } finally { + + System.out.println( + "Transaction processing finished."); + } + } + + public boolean reverse() { + + if (accountUsed != null && + !reversed) { + + accountUsed.deposit(amount); + + reversed = true; + + return true; + } + + return false; + } + + public double getAmountNotWithdrawn() { + return amountNotWithdrawn; + } +} \ No newline at end of file