Given the UML, I am stuck on how to properly implement the interfaces with the "comparable" keyword. Below is a rough implementation of my code. (Abstract class should be correct but interface isn't)
interface Bank{
public void institution();
}
abstract Account implements Bank {
public static int account;
public void deposit(double amount) {
}
}
CodePudding user response:
Given that your Account
is an abstract class you don't really need to implement the methods in the Interfaces, they must only be implemented by any concrete class.
Nevertheless, if you want to provide a default implementation for both institution()
and compareTo(Bank o)
methods you can do the following:
abstract class Account implements Bank {
public static int account;
public void deposit(double amount) {
}
@Override
public void institution() {
// Your logic here
}
@Override
public int compareTo(Bank o) {
// Your comparison logic here
}
}
Your Bank
interface needs to extend java.lang.Comparable
:
interface Bank extends Comparable<Bank>{
void institution();
}
CodePudding user response:
The following is clear from both Bank (three parts, a class) and Comparable (two parts, and interface). The arrows do not conform to full UML.
public class Bank implements Comparable<Bank>
Bank must implement compareTo
so a class is logical.
That an Account is in any way a Bank does not seem logical (two Accounts being two Banks).
public abstract class Account {
public static int globalAccount; // 'account' in diagram.
public final Bank bank;
public final int account;
protected Account(Bank bank) {
this.bank = bank;
globalAccount; // New account number.
account = globalAccount;
}
public abstract void deposit(double amount);
}
That there should be just one global account
value for all I interprete here as a globalAccount
for giving a new number to every account.
The Bank arrow I interprete as field. Not really UML.
Bank islandBank = new Bank();
Account maryAccount = new IslandAccount();
Account joeAccount = new IslandAccount();
assert(maryAccount.bank == islandBank);
assert(maryAccount.account != joeAccount.account);
maryAccount.deposit(-100);
joeAccount.deposit(100);