Skip to content

Instantly share code, notes, and snippets.

@dtauer
Created January 12, 2026 18:37
Show Gist options
  • Select an option

  • Save dtauer/90ae0ad72f6cb669013b677547b745c4 to your computer and use it in GitHub Desktop.

Select an option

Save dtauer/90ae0ad72f6cb669013b677547b745c4 to your computer and use it in GitHub Desktop.
java-question
// ============================================
// 1. PAYMENT METHOD STRATEGIES (Strategy Pattern)
// ============================================
// Payment Strategy Interface
interface PaymentStrategy {
void processPayment(double amount, String transactionType);
}
// Concrete Payment Strategies
class CashPayment implements PaymentStrategy {
@Override
public void processPayment(double amount, String transactionType) {
System.out.println(transactionType + " of ₹" + amount + " via Cash (Physical Submission)");
}
}
class RTGSPayment implements PaymentStrategy {
@Override
public void processPayment(double amount, String transactionType) {
System.out.println(transactionType + " of ₹" + amount + " via RTGS");
}
}
class OnlinePayment implements PaymentStrategy {
@Override
public void processPayment(double amount, String transactionType) {
System.out.println(transactionType + " of ₹" + amount + " via Online Banking");
}
}
class GooglePayPayment implements PaymentStrategy {
@Override
public void processPayment(double amount, String transactionType) {
System.out.println(transactionType + " of ₹" + amount + " via Google Pay");
}
}
// ============================================
// 2. PAYMENT STRATEGY FACTORY (Factory Pattern)
// ============================================
class PaymentStrategyFactory {
private PaymentStrategyFactory() {} // Singleton-like behavior
public static PaymentStrategy createDepositStrategy(String method) {
switch (method.toUpperCase()) {
case "CASH": return new CashPayment();
case "RTGS": return new RTGSPayment();
case "ONLINE": return new OnlinePayment();
case "GOOGLEPAY": return new GooglePayPayment();
default: throw new IllegalArgumentException("Invalid deposit method: " + method);
}
}
public static PaymentStrategy createWithdrawStrategy(String method) {
switch (method.toUpperCase()) {
case "ONLINE": return new OnlinePayment();
case "GOOGLEPAY": return new GooglePayPayment();
default: throw new IllegalArgumentException("Invalid withdrawal method: " + method);
}
}
}
// ============================================
// 3. ACCOUNT STATE INTERFACE (State Pattern)
// ============================================
interface AccountState {
void deposit(double amount, PaymentStrategy strategy);
void withdraw(double amount, PaymentStrategy strategy);
String getStateName();
}
// ============================================
// 4. CONCRETE STATES (State Pattern + Null Object Pattern)
// ============================================
class OpenedState implements AccountState {
@Override
public void deposit(double amount, PaymentStrategy strategy) {
if (amount <= 0) {
System.out.println("Invalid amount for deposit");
return;
}
System.out.println("[OPENED ACCOUNT - DEPOSIT]");
strategy.processPayment(amount, "Deposit");
System.out.println("Deposit successful!\n");
}
@Override
public void withdraw(double amount, PaymentStrategy strategy) {
if (amount <= 0) {
System.out.println("Invalid amount for withdrawal");
return;
}
System.out.println("[OPENED ACCOUNT - WITHDRAWAL]");
strategy.processPayment(amount, "Withdrawal");
System.out.println("Withdrawal successful!\n");
}
@Override
public String getStateName() {
return "OPENED";
}
}
class ClosedState implements AccountState {
@Override
public void deposit(double amount, PaymentStrategy strategy) {
System.out.println("[CLOSED ACCOUNT]");
System.out.println("❌ Deposit operation not available - Account is closed\n");
}
@Override
public void withdraw(double amount, PaymentStrategy strategy) {
System.out.println("[CLOSED ACCOUNT]");
System.out.println("❌ Withdrawal operation not available - Account is closed\n");
}
@Override
public String getStateName() {
return "CLOSED";
}
}
// ============================================
// 5. ACCOUNT TYPE ABSTRACTION
// ============================================
abstract class BankAccount {
protected String accountNumber;
protected AccountState state;
protected double balance;
public BankAccount(String accountNumber) {
this.accountNumber = accountNumber;
this.state = new OpenedState(); // Default state
this.balance = 0.0;
}
public void setState(AccountState state) {
this.state = state;
System.out.println("Account state changed to: " + state.getStateName());
}
public void deposit(double amount, String paymentMethod) {
PaymentStrategy strategy = PaymentStrategyFactory.createDepositStrategy(paymentMethod);
state.deposit(amount, strategy);
if (state instanceof OpenedState) {
balance += amount;
}
}
public void withdraw(double amount, String paymentMethod) {
PaymentStrategy strategy = PaymentStrategyFactory.createWithdrawStrategy(paymentMethod);
state.withdraw(amount, strategy);
if (state instanceof OpenedState && balance >= amount) {
balance -= amount;
}
}
public abstract void displayAccountType();
public double getBalance() {
return balance;
}
}
// ============================================
// 6. CONCRETE ACCOUNT TYPES
// ============================================
class SavingsAccount extends BankAccount {
public SavingsAccount(String accountNumber) {
super(accountNumber);
}
@Override
public void displayAccountType() {
System.out.println("Account Type: SAVINGS");
System.out.println("Account Number: " + accountNumber);
System.out.println("Current Balance: ₹" + balance);
System.out.println("State: " + state.getStateName());
}
}
class CurrentAccount extends BankAccount {
public CurrentAccount(String accountNumber) {
super(accountNumber);
}
@Override
public void displayAccountType() {
System.out.println("Account Type: CURRENT");
System.out.println("Account Number: " + accountNumber);
System.out.println("Current Balance: ₹" + balance);
System.out.println("State: " + state.getStateName());
}
}
// ============================================
// 7. ACCOUNT FACTORY (Factory Pattern)
// ============================================
class BankAccountFactory {
public static BankAccount createAccount(String type, String accountNumber) {
switch (type.toUpperCase()) {
case "SAVINGS":
return new SavingsAccount(accountNumber);
case "CURRENT":
return new CurrentAccount(accountNumber);
default:
throw new IllegalArgumentException("Invalid account type: " + type);
}
}
}
// ============================================
// 8. MAIN CLASS - DEMONSTRATION
// ============================================
public class BankingSystemDemo {
public static void main(String[] args) {
System.out.println("========== BANKING SYSTEM DEMO ==========\n");
// Create a Savings Account using Factory
BankAccount savingsAccount = BankAccountFactory.createAccount("SAVINGS", "SAV123456");
savingsAccount.displayAccountType();
System.out.println();
// Deposit operations with different payment methods (Opened State)
System.out.println("--- DEPOSIT OPERATIONS ---");
savingsAccount.deposit(5000, "CASH");
savingsAccount.deposit(3000, "RTGS");
savingsAccount.deposit(2000, "ONLINE");
savingsAccount.deposit(1000, "GOOGLEPAY");
// Withdraw operations with different payment methods
System.out.println("--- WITHDRAWAL OPERATIONS ---");
savingsAccount.withdraw(1500, "ONLINE");
savingsAccount.withdraw(500, "GOOGLEPAY");
System.out.println("Current Balance: ₹" + savingsAccount.getBalance());
System.out.println();
// Close the account
System.out.println("--- CLOSING ACCOUNT ---");
savingsAccount.setState(new ClosedState());
System.out.println();
// Try operations on closed account
System.out.println("--- ATTEMPTING OPERATIONS ON CLOSED ACCOUNT ---");
savingsAccount.deposit(1000, "CASH");
savingsAccount.withdraw(500, "ONLINE");
// Reopen account
System.out.println("--- REOPENING ACCOUNT ---");
savingsAccount.setState(new OpenedState());
savingsAccount.deposit(2000, "ONLINE");
System.out.println("\nFinal Balance: ₹" + savingsAccount.getBalance());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment