使用Java中的枚举构造函数时出现问题

时间:2014-10-28 18:16:06

标签: java enums

我不确定枚举家族是否处于正确的位置,但是我在主要课堂内外都遇到同样的错误。这些枚举; WILMA,FRED,BETTY,&巴尼将提供 数据来计算他们的退休储蓄。

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)
        {

        }

}

2 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分:0)

在我看来,定义一个班级的人更有意义。而不是一个enum'家庭'。枚举更适合定义属于内聚包的常量,并且可能具有不太可能更改的值。

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

人员类:

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);
    }

}