[英]Trouble with enum constructor in Java
I am unsure if the enum Family is in the correct position, however I get the same errors weather it is inside or outside my main class. 我不确定枚举Family是否处于正确的位置,但是无论在主类内部还是外部,我都会遇到相同的错误。 These enums;
这些枚举; WILMA, FRED, BETTY, & BARNEY will provide the data to calculate their retirement savings.
WILMA,FRED,BETTY和BARNEY将提供数据以计算其退休储蓄。
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)
{
}
}
To create an enum
with parameters you should use a suitable constructor. 要使用参数创建
enum
,应使用合适的构造函数。 In your case change your enum to this: 在您的情况下,将您的枚举更改为:
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;
}
}
Seems to me like it would make more sense to define a class 'person' rather than an enum 'family'. 在我看来,定义一个类“人”而不是一个枚举“家庭”似乎更有意义。 Enums are better suited to define constants that belong in a cohesive package and may have values that are unlikely to change.
枚举更适合定义内聚包中的常数,并且可能具有不大可能改变的值。
Therefore creating a class instance for each individual is more logical because you can design your program to interact with the class type rather than predefined constants. 因此,为每个人创建一个类实例更合乎逻辑,因为您可以将程序设计为与类类型进行交互,而不是与预定义常量进行交互。 To keep things modular etc.
使事物保持模块化等
Person class: 人类:
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;
}
}
Then in your main function you can directly declare a person and do your desired calculations etc., without the program needing to be hard-coded to interact with your enum structure. 然后,在您的主要函数中,您可以直接声明一个人并进行所需的计算等,而无需对该程序进行硬编码以与您的枚举结构进行交互。
Main function: 主功能:
public class main {
public static void main(String[] args) {
Person wilma = new Person("Wilma", "Flintstone", 5000, 0.05, 10, 35);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.