Skip to content
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
160 changes: 160 additions & 0 deletions Q1.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
52 changes: 52 additions & 0 deletions Q2.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
69 changes: 69 additions & 0 deletions Q3.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
39 changes: 39 additions & 0 deletions Q4.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
}