Reputation:
I'm messing around with enumerations and interfaces, however i cannot seem to add the 'moneyType' interface i had implemented to the enum, i've been unable to find the solution. Basically: the moneyType is red underlined, and i can not find out how i can get it right!
package Se.lucas.Main;
public enum moneyTypes implements moneyType {
DOLLAR(moneyType, 15, "Dollar"),
EURO(moneyType, 15, "Dollar"),
FRANK(moneyType, 15, "Dollar"),
MARK(moneyType, 15, "Dollar"),
POUND(moneyType, 15, "Dollar");
private moneyType type;
private int amount;
private String moneyName;
moneyTypes(moneyType type, int amount, String name) {
type = this.type;
amount = this.amount;
name = this.name();
}
@Override
public int getMoney() {
return this.amount;
}
@Override
public String getMoneyName() {
return this.name();
}
@Override
public String getMessage() {
return "got the message";
}
}
package Se.lucas.Main;
public interface moneyType {
public int getMoney();
public String getMoneyName();
public String getMessage();
}
I have not worked with enumeration and interfaces a lot yet.
Upvotes: 2
Views: 97
Reputation: 59699
You don't need to pass moneyType
in the enum list declaration, or have it in the constructor, just leave it out. The reason being each enum instance will be a moneyType
.
interface MoneyType {
public int getMoney();
public String getMoneyName();
public String getMessage();
}
public enum MoneyTypes implements MoneyType {
DOLLAR(15, "Dollar"), EURO(15, "Dollar"), FRANK(15, "Dollar"), MARK(15, "Dollar"), POUND(15, "Dollar");
private final int amount;
private final String moneyName;
MoneyTypes(int amount, String name) {
this.amount = amount;
this.moneyName = name;
}
@Override
public int getMoney() {
return this.amount;
}
@Override
public String getMoneyName() {
// moneyName is assigned the value of name
// You were returning name();
// Two different values, decide which you want to return.
return this.moneyName;
}
@Override
public String getMessage() {
return "got the message";
}
}
Now you can do:
MoneyType type = MoneyTypes.DOLLAR;
Note that I changed the names of the interface and the enum declaration to conform to Java naming standards, specifically beginning with an uppercase letter. I also modified the fields of the enum to be final
so they cannot be assigned after they are constructed.
Upvotes: 2
Reputation: 5353
This maybe ?
package Se.lucas.Main;
public enum moneyTypes implements moneyType {
DOLLAR(15, "Dollar"),
EURO(15, "Dollar"),
FRANK(15, "Dollar"),
MARK(15, "Dollar"),
POUND(15, "Dollar");
private int amount;
private String moneyName;
moneyTypes(int amount, String moneyName) {
this.amount = amount;
this.moneyName= moneyName;
}
@Override
public int getMoney() {
return this.amount;
}
@Override
public String getMoneyName() {
return this.moneyName;
}
@Override
public String getMessage() {
return "got the message";
}
Note :
this
were on the wrong sidethis.name()
which would return something like DOLLAR
in getMoneyName()
instead of (i think) this.moneyName
name
to moneyName
to not have conflict with enum name()
Final point : using an interface over an enum like this seems a bit too much, do you really need that interface ? You can add method to an enum without any interface.
Upvotes: 1