2014-10-28 49 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