| |
"Exploring Enums"
Vol. 9, Issue 11, p. 49
Listing 1: Enumerated types - the old style
package simple;
public class FanOldStyle {
public static final int SwitchState_off = 0 ;
public static final int SwitchState_low = 1 ;
public static final int SwitchState_medium = 2 ;
public static final int SwitchState_high = 3 ;
private int currentState = SwitchState_off;
public void setState(int state){
// Out of range argument can result in illegal state.
currentState = state ;
}
}
Listing 2: Enumerated types - using new enums
package simple;
public class Fan {
public enum SwitchState { Off, Low, Medium, High } ;
public enum DurabilityRating { Low, Medium, High } ;
private SwitchState currentState ;
public void setState(SwitchState state){
currentState = state ;
}
public SwitchState getNextState(){
switch (currentState) {
case Off : return SwitchState.Low ;
case Low : return SwitchState.Medium ;
case Medium : return SwitchState.High ;
}
return null ;
}
public void printStates() {
for( SwitchState state : SwitchState.values())
System.out.println(state);
}
public static void main(String[] args) {
Fan newFan = new Fan();
newFan.setState( SwitchState.High);
System.out.println(newFan.getNextState());
newFan.printStates();
System.out.println(Enum.valueOf(SwitchState.class,"Low"));
System.out.println(Enum.valueOf(DurabilityRating.class,"High"));
System.out.println(Fan.SwitchState.Medium);
}
}
Listing 3: New switch construct that works with enums
public SwitchState getNextState(){
switch (currentState) {
case Off : return SwitchState.Low ;
case Low : return SwitchState.Medium ;
case Medium : return SwitchState.High ;
}
return null ;
}
Listing 4: New for-loop that works with enums
public void printStates() {
for( SwitchState state : SwitchState.values())
System.out.println(state);
}
Listing 5: Declaring standalone enums
package simple;
public enum Genre {
Comedy,
Family,
Action,
Drama,
Western,
Animation,
International,
Other
}
Listing 6
package simple;
public interface IAccountType {
public double getInterestRate() ;
public AccountType.AvgBalanceMethod getAvgBalanceMethod();
}
Listing 7
package simple;
public enum AccountType implements IAccountType {
Checking {
public String toString() {
return "Interest Checking Account" ;
}
},
Savings(3.5) {
public String toString() {
return "Savings Account" ;
}
},
Investment(5.3,25000) {
private double marginRate = 32.5 ;
// Overridden method
public AvgBalanceMethod getAvgBalanceMethod(){
return AvgBalanceMethod.Weekly;
}
public String toString() {
return "Advantage Investment Account" ;
}
};
// Instance variables shared by all declared constants.
private double interestRate = 1.5;
private double minBalance = 250;
// Enum within an enum!
public enum AvgBalanceMethod { Weekly, Monthly, Quarterly, Yearly };
// Class body for the enum type from here on...
// No-arg default constructor necessary "Checking" type
AccountType(){ }
AccountType(double interestRate){
this.interestRate = interestRate ;
}
AccountType(double interestRate, int minBalance ){
this.interestRate = interestRate ;
this.minBalance = minBalance ;
}
public AvgBalanceMethod getAvgBalanceMethod(){
return AvgBalanceMethod.Monthly ;
}
public double getInterestRate() {
return interestRate ;
}
public double getMinBalance() {
return minBalance;
}
}
Listing 8
package simple;
public class Account {
double accBalance ;
AccountType accType ;
String accNumber ;
boolean allowWithdrawal ;
public Account(String accNumber, AccountType type, double balance ) {
this.accNumber = accNumber;
this.accType = type ;
this.accBalance = balance ;
allowWithdrawal = (balance > accType.getMinBalance());
}
public Account(String accNumber, String type, double balance ) {
this.accNumber = accNumber;
try {
this.accType = Enum.valueOf(AccountType.class, type);
}
catch( IllegalArgumentException ex ) {
// Wrong argument passed in for type. Use default type.
this.accType = AccountType.Checking ;
}
this.accBalance = balance ;
allowWithdrawal = (balance > accType.getMinBalance());
}
private double getAvgBalance(AccountType.AvgBalanceMethod method) {
double balance = 0 ;
switch (accType.getAvgBalanceMethod()) {
case Weekly: // plug in logic here.
case Monthly:
case Quarterly:
case Yearly:
}
return balance ;
}
public double computeInterest() {
System.out.print("Computing interest for ");
System.out.print(accType + " Number : " + accNumber );
System.out.println(" using " + accType.getAvgBalanceMethod() + " method ") ;
double balance = getAvgBalance(accType.getAvgBalanceMethod());
double interest = balance * accType.getInterestRate() ;
return interest ;
}
}
Listing 9
package simple;
public class AccountTest {
public static void main(String[] args) {
Account savings1 = new Account("SAV1123", "Savings", 35890);
Account investment1 = new Account("DATEK331", AccountType.Investment, 25000);
savings1.computeInterest();
investment1.computeInterest();
}
}
|
|