Class Enumerations in Java

I have one class that declares an enum type as:

public enum HOME_LOAN_TERMS {FIFTEEN_YEAR, THIRTY_YEAR};

Is this type used in another class? I'm basically trying to do my homework, where we have two types of loans and one loanManager class. When I try to use HOME_LOAN_TERMS.THIRTY_YEAR in my loanManager class, which does not extend or implement the loan class, I get the error message "I can not find the symbol HOME_LOAN_TERMS". Therefore, I did not know if my loanManager class should implement two different loan classes. Thank.

I am currently working on this, so I know that it is not complete, but here I tried to use it:

import java.util.ArrayList;

public class AcmeLoanManager 
{
    public void addLoan(Loan h)
    {
        loanArray.add(h);
    }
    /*
    public Loan[] getAllLoans()
    {
    }

    public Loan[] findLoans(Person p)
    {
    }

    public void removeLoan(int loanId)
    {
    }
    */
    private ArrayList<Loan> loanArray = new ArrayList<Loan>(5);

    public static void main(String[] args)
    {
        AcmeLoanManager aLoanManager = new AcmeLoanManager();
        Person aPerson = new Person("Crystal", "Twix", "619-111-1234", "ct@yahoo.com");
        HomeLoan aHomeLoan = new HomeLoan(aPerson, 400000, 5, HOME_LOAN_TERMS.THIRTY_YEAR);
        aLoanManager.addLoan(aHomeLoan);
    }
}
+3
source share
2 answers

, , .

Loan Loan.HOME_LOAN_TERMS.FIFTEEN_YEAR, .

+1

:

HOME_LOAN_TYPES type = HOME_LOAN_TYPES.FIFTEEN_YEAR;

, . , , :

public enum HomeLoanType {
  FIFTEEN YEAR,
  THIRTY_YEAR
}

, :

import static package.name.HomeLoanType.*;

...

HomeLoanType type = FIFTEEN_YEAR;

, Java - . :

public enum HomeLoanType {
  FIFTEEN YEAR(15),
  THIRTY_YEAR(30);

  private final int years;

  HomeLoanType(int years) {
    this.year = years;
  }

  public int getYears() {
    returns years;
  }
}
+7

Source: https://habr.com/ru/post/1742740/


All Articles