Question

I am working on an assignment for a class which basically simulates a banking system (we are working with concurrency in the class so we need to make the backbone for the rest of this semester). The problem I am having is creating a abstract class called Transaction which implements a interface of a bunch of different methods relating to different types of transactions. Then we have to make subclasses of the Transaction class for each type of transaction (i.e. AccountNumberInquiry, AccountBalanceInquiry, AccountWithdrawal, etc.).

My questions is am I on the right track or am I understanding this all wrong?

Transaction.java

public abstract class Transaction implements TransactionInterface {
    private Account account;
    public Transaction(Account account) {
        this.account = account;
    }
}

TransactionInterface.java

public interface TransactionInterface {
    public int getAccountNumber();
    public void makeTransfer();
    public boolean makeWithdrawal();
    public double getAccountBalance();
    public void makeDeposit();
}

AccountTransfer.java (just an example subclass of Transaction)

public class AccountTransfer extends Transaction {

    private Account account;

    public AccountTransfer(Account account) {
        super(account);
        this.account = account;
    }

    public void makeTransfer(Account toAccount, double amount) {
        AccountWithdrawal withdrawal = new AccountWithdrawal(account);
        if(withdrawal.makeWithdrawl(amount) == true) {
            // transfer to other account
        } else {
            // prompt error
        }  
    }

}

I need to @Override the interface methods in my AccountTransfer class correct? There's just a whole bunch of stuff confusing me.

Was it helpful?

Solution

Remove the initialization of account from AccountTransfer.java, as you can pass a created account object to the class. Also, as stated, protected is going to be important when you implement this as MVC as you won't be able to communicate between the model, view, and controller if you have it as private.

Cheers Matt

OTHER TIPS

I was going to suggest you ask your instructor some uncomfortable questions about the Interface Segregation Principle. In particular, the getAccountNumber() and getAccountBalance() methods strike me as orthogonal to the Transaction interface, but the principle states that no client should be forced to depend on methods which it does not use--and I suspect that the Transaction interface as shown may not be focused well enough to prevent this from happening.

If your parent class is abstract, why do you need to use an interface? Any abstract methods declared in the parent class will have to be defined in each of the child classes.

You could simply make all those functions you put in the interface in the class and make those functions abstract. That should do it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top