From ee01b6b311ef685a8fa72719fe337413864169b6 Mon Sep 17 00:00:00 2001 From: App-hie Date: Mon, 8 Jun 2026 20:38:03 +0300 Subject: [PATCH] Add files via upload --- Q1.java | 160 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Q2.java | 52 ++++++++++++++++++ Q3.java | 69 ++++++++++++++++++++++++ Q4.java | 39 ++++++++++++++ 4 files changed, 320 insertions(+) create mode 100644 Q1.java create mode 100644 Q2.java create mode 100644 Q3.java create mode 100644 Q4.java diff --git a/Q1.java b/Q1.java new file mode 100644 index 0000000..7edcd8f --- /dev/null +++ b/Q1.java @@ -0,0 +1,160 @@ +import java.util.Calendar; +import java.util.UUID; + +// Class for Insufficient Funds +class InsufficientFundsException extends Exception { + public InsufficientFundsException(String message) { + super(message); + } +} + +class BankAccount { + private String accountNumber; + private double balance; + + public BankAccount(String accountNumber, double initialBalance) { + this.accountNumber = accountNumber; + this.balance = initialBalance; + } + + public double getBalance() { return balance; } + public String getAccountNumber() { return accountNumber; } + + public void deposit(double amount) { + balance += amount; + } + + public void withdraw(double amount) throws InsufficientFundsException { + if (amount > balance) { + // Throwing custom exception when funds are low + throw new InsufficientFundsException("Insufficient funds! Available balance is $" + balance); + } + balance -= amount; + } +} + +// Transaction Interface defining the required contract +interface TransactionInterface { + double getAmount(); + Calendar getDate(); + String getTransactionID(); + void printTransactionDetails(); + void apply(BankAccount ba) throws InsufficientFundsException; +} + +// Base Class implementing the Interface +class BaseTransaction implements TransactionInterface { + protected double amount; + protected Calendar date; + protected String transactionID; + + public BaseTransaction(double amount) { + this.amount = amount; + this.date = Calendar.getInstance(); + this.transactionID = UUID.randomUUID().toString(); // Generates a unique string ID + } + + @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("Date: " + date.getTime()); + System.out.println("Amount: $" + amount); + } + + @Override + public void apply(BankAccount ba) throws InsufficientFundsException { + System.out.println("System Log: BaseTransaction invoked on Account " + ba.getAccountNumber() + ". No balance modified."); + } +} + +// Derived Class for Deposits +class DepositTransaction extends BaseTransaction { + + public DepositTransaction(double amount) { + super(amount); + } + + @Override + public void printTransactionDetails() { + System.out.println("\n--- Deposit Transaction ---"); + super.printTransactionDetails(); + } + + // Overriding apply() to perform a specific credit logic + @Override + public void apply(BankAccount ba) { + ba.deposit(this.amount); + System.out.println("Deposit of $" + this.amount + " applied. New Balance: $" + ba.getBalance()); + } +} + +// Derived Class for Withdrawals +class WithdrawalTransaction extends BaseTransaction { + + public WithdrawalTransaction(double amount) { + super(amount); + } + + @Override + public void printTransactionDetails() { + System.out.println("\n--- Withdrawal Transaction ---"); + super.printTransactionDetails(); + } + + @Override + public void apply(BankAccount ba) throws InsufficientFundsException { + ba.withdraw(this.amount); + System.out.println("Withdrawal of $" + this.amount + " applied. New Balance: $" + ba.getBalance()); + } +} + +public class Main { + public static void main(String[] args) { + // Initialize a test account + BankAccount account = new BankAccount("ACC-987654321", 500.00); + System.out.println("Initial Account Balance: $" + account.getBalance()); + + TransactionInterface genericTx = new BaseTransaction(0.0); + TransactionInterface depositTx = new DepositTransaction(250.00); + TransactionInterface validWithdrawalTx = new WithdrawalTransaction(150.00); + TransactionInterface invalidWithdrawalTx = new WithdrawalTransaction(1000.00); + + try { + System.out.println("\n[Testing Base Transaction]"); + genericTx.printTransactionDetails(); + genericTx.apply(account); + + System.out.println("\n[Testing Deposit]"); + depositTx.printTransactionDetails(); + depositTx.apply(account); + + System.out.println("\n[Testing Valid Withdrawal]"); + validWithdrawalTx.printTransactionDetails(); + validWithdrawalTx.apply(account); + + System.out.println("\n[Testing Invalid Withdrawal]"); + invalidWithdrawalTx.printTransactionDetails(); + invalidWithdrawalTx.apply(account); // This line throws the exception + + } catch (InsufficientFundsException e) { + System.err.println("TRANSACTION FAILED: " + e.getMessage()); + } + + System.out.println("\nFinal Account Balance: $" + account.getBalance()); + } +} \ No newline at end of file diff --git a/Q2.java b/Q2.java new file mode 100644 index 0000000..a258ec0 --- /dev/null +++ b/Q2.java @@ -0,0 +1,52 @@ +class DepositTransaction extends BaseTransaction { + + public DepositTransaction(double amount) { + super(amount); + } + + @Override + public void printTransactionDetails() { + System.out.println("Deposit Transaction"); + super.printTransactionDetails(); + } + + @Override + public void apply(BankAccount ba) { + ba.deposit(this.amount); + System.out.println("Deposit of $" + this.amount + " applied. New Balance: $" + ba.getBalance()); + } +} + +class WithdrawalTransaction extends BaseTransaction { + + private BankAccount account; + private boolean reversed; + + public WithdrawalTransaction(double amount) { + super(amount); + this.reversed = false; + } + + @Override + public void printTransactionDetails() { + System.out.println("Withdrawal Transaction"); + super.printTransactionDetails(); + } + + @Override + public void apply(BankAccount ba) throws InsufficientFundsException { + ba.withdraw(this.amount); + this.account = ba; + System.out.println("Withdrawal of $" + this.amount + " applied. New Balance: $" + ba.getBalance()); + } + + public boolean reverse() { + if (this.account != null && !this.reversed) { + this.account.deposit(this.amount); + this.reversed = true; + System.out.println("Withdrawal reversed. Balance restored to: $" + this.account.getBalance()); + return true; + } + return false; + } +} \ No newline at end of file diff --git a/Q3.java b/Q3.java new file mode 100644 index 0000000..2cc5726 --- /dev/null +++ b/Q3.java @@ -0,0 +1,69 @@ +class InsufficientFundsException extends Exception { + public InsufficientFundsException(String message) { + super(message); + } +} + +class WithdrawalTransaction extends BaseTransaction { + + private BankAccount account; + private boolean reversed; + private double amountNotWithdrawn; + + public WithdrawalTransaction(double amount) { + super(amount); + this.reversed = false; + this.amountNotWithdrawn = 0.0; + } + + public void printTransactionDetails() { + System.out.println("Withdrawal Transaction"); + System.out.println("Transaction ID: " + getTransactionID()); + System.out.println("Date: " + getDate().getTime()); + System.out.println("Amount Requested: $" + getAmount()); + System.out.println("Amount Not Withdrawn: $" + amountNotWithdrawn); + } + + public void apply(BankAccount ba) throws InsufficientFundsException { + if (this.amount > ba.getBalance()) { + throw new InsufficientFundsException("Insufficient funds available."); + } + ba.withdraw(this.amount); + this.account = ba; + System.out.println("Withdrawal of $" + this.amount + " applied. New Balance: $" + ba.getBalance()); + } + + public void apply(BankAccount ba, boolean allowPartial) { + try { + if (ba.getBalance() <= 0) { + throw new InsufficientFundsException("Balance is zero or negative. No funds to withdraw."); + } + + if (ba.getBalance() > 0 && ba.getBalance() < this.amount) { + this.amountNotWithdrawn = this.amount - ba.getBalance(); + double availableFunds = ba.getBalance(); + ba.withdraw(availableFunds); + this.account = ba; + System.out.println("Partial withdrawal of $" + availableFunds + " applied. Shortfall: $" + this.amountNotWithdrawn); + } else { + ba.withdraw(this.amount); + this.account = ba; + System.out.println("Full withdrawal of $" + this.amount + " applied. New Balance: $" + ba.getBalance()); + } + } catch (InsufficientFundsException e) { + System.out.println("Transaction Error: " + e.getMessage()); + } finally { + System.out.println("Overloaded withdrawal attempt concluded. Final balance is $" + ba.getBalance()); + } + } + + public boolean reverse() { + if (this.account != null && !this.reversed) { + this.account.deposit(this.amount - this.amountNotWithdrawn); + this.reversed = true; + System.out.println("Withdrawal reversed. Balance restored to: $" + this.account.getBalance()); + return true; + } + return false; + } +} \ No newline at end of file diff --git a/Q4.java b/Q4.java new file mode 100644 index 0000000..b12ab30 --- /dev/null +++ b/Q4.java @@ -0,0 +1,39 @@ +public class Main { + public static void main(String[] args) { + BankAccount account = new BankAccount("123456789", 1000.00); + + DepositTransaction deposit = new DepositTransaction(500.00); + deposit.printTransactionDetails(); + deposit.apply(account); + + WithdrawalTransaction withdrawal = new WithdrawalTransaction(200.00); + withdrawal.printTransactionDetails(); + + try { + withdrawal.apply(account); + } catch (InsufficientFundsException e) { + System.out.println(e.getMessage()); + } + + withdrawal.reverse(); + + WithdrawalTransaction partialWithdrawal = new WithdrawalTransaction(2000.00); + partialWithdrawal.apply(account, true); + + BaseTransaction baseTx1 = (BaseTransaction) deposit; + baseTx1.printTransactionDetails(); + try { + baseTx1.apply(account); + } catch (InsufficientFundsException e) { + System.out.println(e.getMessage()); + } + + BaseTransaction baseTx2 = (BaseTransaction) withdrawal; + baseTx2.printTransactionDetails(); + try { + baseTx2.apply(account); + } catch (InsufficientFundsException e) { + System.out.println(e.getMessage()); + } + } +}