ATM

mail@pastecode.io avatar
unknown
java
5 months ago
4.7 kB
1
Indexable
 // Interface for defining transaction types
public interface Transaction {
    void execute();
    TransactionStatus getStatus();
}

// Interface for transaction status
public interface TransactionStatus {
    void setStatus(String status);
    String getStatus();
}

// Implementation of TransactionStatus
public class SimpleTransactionStatus implements TransactionStatus {
    private String status;

    @Override
    public void setStatus(String status) {
        this.status = status;
    }

    @Override
    public String getStatus() {
        return status;
    }
}

// Implementation of a Cash Withdrawal transaction
public class CashWithdrawal implements Transaction {
    private double amount;
    private TransactionStatus status;
    private ATM atm;

    public CashWithdrawal(double amount, TransactionStatus status, ATM atm) {
        this.amount = amount;
        this.status = status;
        this.atm = atm;
    }

    @Override
    public void execute() {
        if (amount > 0 && amount <= atm.getBalance()) {
            atm.setBalance(atm.getBalance() - amount);
            status.setStatus("Cash withdrawn: $" + amount);
        } else if (amount <= 0) {
            status.setStatus("Withdrawal amount must be positive.");
        } else {
            status.setStatus("Insufficient funds.");
        }
    }

    @Override
    public TransactionStatus getStatus() {
        return status;
    }
}

// Implementation of a Deposit transaction
public class DepositTransaction implements Transaction {
    private double amount;
    private TransactionStatus status;
    private ATM atm;

    public DepositTransaction(double amount, TransactionStatus status, ATM atm) {
        this.amount = amount;
        this.status = status;
        this.atm = atm;
    }

    @Override
    public void execute() {
        if (amount > 0) {
            atm.setBalance(atm.getBalance() + amount);
            status.setStatus("Deposited: $" + amount);
        } else {
            status.setStatus("Deposit amount must be positive.");
        }
    }

    @Override
    public TransactionStatus getStatus() {
        return status;
    }
}

// Implementation of a Balance Inquiry transaction
public class BalanceInquiry implements Transaction {
    private TransactionStatus status;
    private ATM atm;

    public BalanceInquiry(TransactionStatus status, ATM atm) {
        this.status = status;
        this.atm = atm;
    }

    @Override
    public void execute() {
        // Display the current balance
        status.setStatus("Current balance: $" + atm.getBalance());
    }

    @Override
    public TransactionStatus getStatus() {
        return status;
    }
}

// Concrete class for ATM
public class ATM {
    private double balance;
    private boolean authenticated;

    public ATM() {
        this.balance = 0.0;
        this.authenticated = false;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public void authenticate(String cardNumber, int pin) {
        // Dummy authentication
        if ("123456789".equals(cardNumber) && pin == 1234) {
            authenticated = true;
            System.out.println("Authenticated successfully.");
        } else {
            System.out.println("Authentication failed.");
        }
    }

    public void performTransaction(Transaction transaction) {
        if (authenticated) {
            transaction.execute();
            System.out.println("Transaction status: " + transaction.getStatus().getStatus());
        } else {
            System.out.println("Please authenticate first.");
        }
    }

    public static void main(String[] args) {
        ATM atm = new ATM();

        // Initial balance setup
        atm.setBalance(1000.00);

        // Authenticate user
        atm.authenticate("123456789", 1234);

        // Create transaction statuses
        TransactionStatus withdrawalStatus = new SimpleTransactionStatus();
        TransactionStatus balanceStatus = new SimpleTransactionStatus();
        TransactionStatus depositStatus = new SimpleTransactionStatus();

        // Perform transactions
        Transaction withdrawal = new CashWithdrawal(100.0, withdrawalStatus, atm);
        atm.performTransaction(withdrawal);

        Transaction balanceInquiry = new BalanceInquiry(balanceStatus, atm);
        atm.performTransaction(balanceInquiry);

        Transaction deposit = new DepositTransaction(200.0, depositStatus, atm);
        atm.performTransaction(deposit);

        // Perform another balance inquiry to reflect the changes
        atm.performTransaction(balanceInquiry);
    }
}
Leave a Comment