and then i created a class responsible for displaying the history of the accounts
that was displayhistoryofaccounts.execute("number of the account","number of the account") but you said that's not the way to go
But why do you want to implement a Command class for all those different
actions? Those different actions all take a different number of parameters,
even different types of parameters.
Why hide that fact by using a varargs String ... parameter list. It doesn't buy
you anything here. If I want to withdraw money from an account I know what
to do: tell the Account to withdraw some money; same with the deposit action.
If you really want to hide those details you should encapsulate those details
in the Command implementing classes; it'll be quite a bit of overhead:
-
public class Withdrawer implements Command {
-
private Account a;
-
private double amount;
-
public Withdrawer(Account a, double amount) {
-
this.a= a;
-
this.amount= amount;
-
}
-
// interface implementation
-
public void execute() {
-
a.withdraw(amount);
-
}
-
}
-
public class Depositer implements Command {
-
private Account a;
-
private double amount;
-
public Depositer(Account a, double amount) {
-
this.a= a;
-
this.amount= amount;
-
}
-
// interface implementation
-
public void execute() {
-
a.deposit(amount);
-
}
-
}
-
public interface Command {
-
public void execute();
-
}
Only then you have abstracted away what actually has been done to accounts
but at quite a high cost, i.e. for every account action you have to create a new
object with the Account and possibly some money value.
Listing all accounts does fit too in that scenario. Possibly a static method in the
Account class can do the real job, delegated by another Command class.
kind regards,
Jos