简体   繁体   中英

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. These enums; WILMA, FRED, BETTY, & BARNEY will provide the data to calculate their retirement savings.

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM