2014-10-28 51 views
0

我不确定enum系列是否处于正确的位置,但是我在主类内部或外部得到相同的错误。这些枚举; WILMA,FRED,BETTY,& BARNEY将提供 数据来计算他们的退休储蓄。Java中的枚举构造函数的问题

public class Assignment5 
{ 

    enum Family 
    { 
     WILMA("Wilma", "Flintstone", 5000, 0.05, 10, 35), 
     FRED("Fred", "Flintstone", 15000, 0.075, 7, 30), //fred might swap with betty 
     BETTY("Betty", "Rubble", 7500, 0.0375, 10, 25), 
     BARNEY("Barney", "Rubble", 5000, 0.09, 10, 35), 
    } 

     //The properties’ “getters”/accessors should go here. 

     //instance fields 
     private final String firstName; //first names 
     private final String lastName; //last names 
     private final int annualDeposit; //annual deposit 
     private final double annualRate; //annual rate 
     private final int yearsDeposit; //number of years depositing 
     private final int yearsCalc; //number of years to compound 

     //enum family constructor 
     Assignment5(String firstName, String lastName, int annualDeposit, double annualRate, int yearsDeposit, int yearsCalc) 
     { 
      this.firstName = firstName; 
      this.lastName = lastName; 
      this.annualDeposit = annualDeposit; 
      this.annualRate = annualRate; 
      this.yearsDeposit = yearsDeposit; 
      this.yearsCalc = yearsCalc; 
     } 

     //getter for firstName 
     public String getFirstName() 
     { 
      return firstName; 
     } 

     //getter for lastName 
     public String getLastName() 
     { 
      return lastName; 
     } 

     //getter for annual deposit 
     public int getAnnualDeposit() 
     { 
      return annualDeposit; 
     } 

     //getter for annual rate 
     public double getAnnualRate() 
     { 
      return annualRate; 
     } 

     //getter for years deposit 
     public int getYearsDeposit() 
     { 
      return yearsDeposit; 
     } 

     //getter for years calc 
     public int getYearsCalc() 
     { 
      return yearsCalc; 
     } 

     //main method 
     //formulas for calculating retirement account and printing results will go here 
     public static void main(String[] args) 
     { 

     } 

} 
+1

什么是'Assignment5'?难道不是“家庭”吗? – Arkadiy 2014-10-28 18:18:55

+0

这样做会给我一个无效的方法声明,我以前试过。 Assignment5是我的主类 – greaterbeing 2014-10-28 18:21:43

+1

的名称请参阅[本教程](http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html),特别向下滚动到“Planet”示例。基本上,你在enum'花括号之外的东西需要放在里面。 – ajb 2014-10-28 18:25:10

回答

3

要创建带参数的enum,应使用合适的构造函数。在你的情况下更改枚举此:

enum Family 
{ 
    WILMA("Wilma", "Flintstone", 5000, 0.05, 10, 35), 
    FRED("Fred", "Flintstone", 15000, 0.075, 7, 30), //fred might swap with betty 
    BETTY("Betty", "Rubble", 7500, 0.0375, 10, 25), 
    BARNEY("Barney", "Rubble", 5000, 0.09, 10, 35); 
    //instance fields 
    private final String firstName; //first names 
    private final String lastName; //last names 
    private final int annualDeposit; //annual deposit 
    private final double annualRate; //annual rate 
    private final int yearsDeposit; //number of years depositing 
    private final int yearsCalc; //number of years to compound 

    Family(String firstName, String lastName, int annualDeposit, double annualRate, int yearsDeposit, int yearsCalc) 
    { 
     this.firstName = firstName; 
     this.lastName = lastName; 
     this.annualDeposit = annualDeposit; 
     this.annualRate = annualRate; 
     this.yearsDeposit = yearsDeposit; 
     this.yearsCalc = yearsCalc; 
    } 
} 
+0

应枚举家庭在我的课堂还是在它之前? – greaterbeing 2014-10-28 18:32:17

+0

在你的课堂内。 – 2014-10-28 18:33:50

0

在我看来,像它会更有意义,定义一个类“人”,而不是一个枚举的“家族”。枚举更适合于定义属于内聚包的常量,并且可能具有不可能改变的值。

因此,为每个人创建一个类实例更合乎逻辑,因为您可以设计程序与类类型而不是预定义的常量进行交互。为了让事情模块化等

Person类:

public class Person { 

    //instance fields 
    private final String firstName; //first names 
    private final String lastName; //last names 
    private final int annualDeposit; //annual deposit 
    private final double annualRate; //annual rate 
    private final int yearsDeposit; //number of years depositing 
    private final int yearsCalc; //number of years to compound 

    public Person(String firstName, String lastName, int annualDeposit, double annualRate, int yearsDeposit, int yearsCalc) 
    { 
     this.firstName = firstName; 
     this.lastName = lastName; 
     this.annualDeposit = annualDeposit; 
     this.annualRate = annualRate; 
     this.yearsDeposit = yearsDeposit; 
     this.yearsCalc = yearsCalc; 
    } 

    //getter for firstName 
    public String getFirstName() 
    { 
     return firstName; 
    } 

    //getter for lastName 
    public String getLastName() 
    { 
     return lastName; 
    } 

    //getter for annual deposit 
    public int getAnnualDeposit() 
    { 
     return annualDeposit; 
    } 

    //getter for annual rate 
    public double getAnnualRate() 
    { 
     return annualRate; 
    } 

    //getter for years deposit 
    public int getYearsDeposit() 
    { 
     return yearsDeposit; 
    } 

    //getter for years calc 
    public int getYearsCalc() 
    { 
     return yearsCalc; 
    } 
} 

然后在你的主要功能可以直接宣布某人,做您所需的计算等,而不需要程序来进行硬编码与您的枚举结构进行交互。

主要功能:

public class main { 

public static void main(String[] args) { 
     Person wilma = new Person("Wilma", "Flintstone", 5000, 0.05, 10, 35); 
    } 

} 
+0

感谢您的意见,不好意思尝试 – greaterbeing 2014-10-28 19:03:21