관리 메뉴

gi_dor

Java enum λ³Έλ¬Έ

Language/Java

Java enum

기돌 2023. 12. 1. 12:21

 

 

πŸ”Έ enum (μ—΄κ±°ν˜•)

  • 미리 값이 μ •ν•΄μ Έ μžˆλŠ” ν•œμ •λœ λ°μ΄ν„°μ˜ 묢음
  • μ—΄κ±°ν˜•μœΌλ‘œ μƒμˆ˜λ₯Ό λŒ€μ²΄ ν•  수 μžˆλ‹€

β—» 고객의 λ“±κΈ‰ : Gold , Shilver , Bronze
β—» 학점 : A , B , C , D
β—» κ³„μ ˆ : λ΄„ , 여름 , 가을 , 겨울

πŸ“Œ μ—΄κ±°ν˜• μ •μ˜ν•˜κΈ°

public enum Grade {
	GOLD , SILVER , BRONZE;
    }
public enum Grade {
	A(4.0) , B(3.0) , C(2.0) , D (1.0) ,F (0.0);
    }

 

πŸ”Έ μƒμˆ˜ 

  • μƒμˆ˜λŠ” ν”„λ‘œκ·Έλž¨μ΄ μ‹€ν–‰λ˜λŠ” λ™μ•ˆ λ³€ν•˜μ§€ μ•ŠλŠ” κ°’ ( 미리 값이 정해진 데이터 ) 
  • public static final νƒ€μž… μƒμˆ˜μ΄λ¦„ = κ°’;
public class Constants{
    public static final int GOLD =1;
    public static final int SILVER = 2;
    public static final int BRONZE = 3;
    
    public static final double GOLD_POINT_DEPOSIT_RATE = 0.05; 
    public static final double SHILVER_POINT_DEPOSIT_RATE = 0.02; 
    public static final double BRONZE_POINT_DEPOSIT_RATE = 0.01; 
    
    public static final long MAX_UPLOAD_SIZE = 1024 * 1024 * 10 ;
}

 

πŸ”Ή μƒμˆ˜λ₯Ό μ •μ˜ν•˜λŠ” λͺ©μ 

  • 가독을 ν–₯상 μ‹œν‚¨λ‹€.
  • μœ μ§€λ³΄μˆ˜μ„±μ„ 높인닀.
  • μœ„μ— μ˜ˆμ‹œμΈ Constants μƒμˆ˜μ˜ 값을 λ³€κ²½ν•˜κΈ°λ§Œ ν•˜λ©΄ ν•΄λ‹Ή μƒμˆ˜ 값을 μ‚¬μš©ν•˜λŠ” λͺ¨λ“  ν΄λž˜μŠ€μ— λ³€κ²½λœ 값이 μ μš©λœλ‹€
  • μƒμˆ˜ 값듀간에 상관관계가 μžˆλŠ”κ²½μš° 2λ°° 3λ°° 1/2λ°°...λ“± μƒμˆ˜κ°’μ„ κ·Έ 상관 관계λ₯Ό ν‘œν˜„ν•˜λŠ” μ‹μœΌλ‘œ μž‘μ„±ν•œλ‹€
final double GOLD_DEPOSIT_RATE = 0.1;
final double SHILVER_DEPOSIT_RATE = GOLD_DEPOSIT_RATE/2;
final double BRONZE_DEPOSIT_RATE = GOLD_DEPOSIT_RATE/4;
final double GUEST_DEPOSIT_RATE = GOLD_DEPOSIT_RATE/8;

 

 

// μƒμˆ˜ μ‚¬μš©μ•ˆν•¨
public int getDeposiPoint(int customerGrade , int amount) {
	if(customerGrade == 1 ) {
        return (int) (amount * 0.05);
        } else if ( customerGrade == 2) {
        return (int) (amount * 0.02 ) ;
        } else if (customerGrade == 3 {
        return (int) (amount * 0.01);
        } 
        return 0;
	
}


// μƒμˆ˜μ‚¬μš©
public int getDeposiPoint(int customerGrade , int amount) {
	if(customerGrade == constants.GOLD ) {
        return (int) (amount * 0.05);
        } else if ( customerGrade == constants.SHILVER) {
        return (int) (amount * 0.02 ) ;
        } else if (customerGrade == constants.BRONZE {
        return (int) (amount * 0.01);
        } 
        return 0;

}

 

πŸ”Ή enum (μ—΄κ±°ν˜•)
        μƒμˆ˜ κ°’ (μƒμˆ˜λ°μ΄ν„°)의 집합이 μ—΄κ±°ν˜•μ΄λ‹€

 

πŸ”Ή ν˜•μ‹

   enum (μ—΄κ±°ν˜•μ΄λ¦„) {
        μƒμˆ˜ , μƒμˆ˜ , μƒμˆ˜;
    }
  • μ—΄κ±°ν˜• 이름 : 클래슀 μ΄λ¦„μ²˜λŸΌ 첫번째 κΈ€μžλ₯Ό λŒ€λ¬Έμžλ‘œ ν•˜κ³  λ‚˜λ¨Έμ§€λŠ” μ†Œλ¬Έμžλ‘œ μž‘μ„±
  • μƒμˆ˜ : μ „λΆ€ λŒ€λ¬Έμžλ‘œ μž‘μ„± , 두단어 이상 쑰합일 λ–„ ' _ ' 둜 μ—°κ²°

 

πŸ”Ή enum의 μž₯점

  • ν—ˆμš© κ°€λŠ₯ν•œ 값듀을 μ œν•œ ν•œλ‹€
     • μ—΄κ±°ν˜• μƒμˆ˜κ°€ νŠΉμ • μ—΄κ±°νƒ€μž…μ— ν¬ν•¨λ˜μ–΄ 있기 λ•Œλ¬Έμ— μ—΄κ±°ν˜• λ³€μˆ˜μ— λŒ€μž… κ°€λŠ₯ν•œ 값은
       ν•΄λ‹Ή μ—΄κ±°ν˜•μ˜ μ—΄κ±°μƒμˆ˜λ“€ 뿐이닀
    static final int GOLD = 1;
    static final int SILVER = 2;
    int customerGrade = GOLD;
    int customerGrade = 100;    // 고객등급과 μƒκ΄€μ—†λŠ” 값이 λŒ€μž…λ˜μ–΄λ„ μ—λŸ¬κ°€ λ°œμƒ μ•ˆν•¨
   public enum Grade{
    GOLD , SILVER , BRONZE;
    }
    
    Grade grade = Grade.GOLD;
    Grade grade = 100;  // 컴파일 였λ₯˜ λ°œμƒ

 

  •  κ°œλ°œλ„κ΅¬μ˜ μžλ™μ™„μ„± , λ¬Έλ²•μ˜€λ₯˜ 체크등을 μ§€μ›λ°›μ„μˆ˜ μžˆλ‹€
  • μ‘°κ±΄μ‹μ—μ„œ == 으둜 비ꡐ κ°€λŠ₯ν•˜κ³  , switchλ¬Έμ—μ„œλ„ λ°”λ‘œ μ‚¬μš©μ΄ κ°€λŠ₯ν•˜λ‹€

 πŸ”Ή enum μ£Όμš” λ‚΄μž₯ λ©”μ„œλ“œ

 String name()
        열거객체가 가지고 μžˆλŠ” λ¬Έμžμ—΄μ„ λ°˜ν™˜ , λ¬Έμžμ—΄μ€ μ—΄κ±°μƒμˆ˜μ˜ 이름과 λ™μΌν•˜λ‹€
    
    int ordinal()
    	μ—΄κ±°κ°μ²΄μ˜ μˆœλ²ˆμ„(0λ²ˆλΆ€ν„° μ‹œμž‘ν•˜λŠ”) λ°˜ν™˜ν•œλ‹€ 
    
    int compareTo(enum other)
    	μ§€μ •λœ 열거객체와 μˆœλ²ˆμ„ λΉ„κ΅ν•΄μ„œ μ •μˆ˜κ°’μ„ λ°˜ν™˜ν•œλ‹€
        전달받은 λ‹€λ₯Έ μ—΄κ±°κ°μ²΄μ˜ μˆœλ²ˆλ³΄λ‹€ λΉ λ₯΄λ©΄ 음수
        전달받은 λ‹€λ₯Έ μ—΄κ±°κ°μ²΄μ˜ μˆœλ²ˆλ³΄λ‹€ 늦으면 μ–‘μˆ˜ 
        
    enum valueOf(String name)
    	λ§€κ°œλ³€μˆ˜λ‘œ 전달받은 λ¬Έμžμ—΄κ³Ό λ™μΌν•œ 이름을 κ°€μ§€λŠ” 열거객체λ₯Ό μƒμ„±ν•΄μ„œ λ°˜ν™˜
    
    enum[] values()
    	ν•΄λ‹Ή μ—΄κ±°ν˜•μ˜ λͺ¨λ“  μ—΄κ±°μƒμˆ˜λ“€μ— λŒ€ν•œ 열거객체λ₯Ό λ°°μ—΄λ‘œ λ°˜ν™˜
    
    열거객체 		---->			λ¬Έμžμ—΄
    Grade.GOLD   	 name()			"GOLD"
    
    λ¬Έμžμ—΄			---->			열거객체
    "GOLD"		 Grade.valueOf("GOLD")  	Grade.GOLD

πŸ“Œ λ”°λ‘œ new μ—°μ‚°μžλ‘œ 객체λ₯Ό μƒμ„±ν•˜μ§€ μ•Šκ³  ν•΄λ‹Ή μ½”λ“œ 처럼 ν•˜λ©΄  Grade.GOLDκ°€ μ—΄κ±° κ°μ²΄κ°€ λœλ‹€
  

Grade grade = Grade.GOLD;

 

 


 

πŸ”Ή  enumκ³Ό μƒμˆ˜ κ°’ λ§€ν•‘ν•˜κΈ°

 enum ScoreGrade{
    public static final ScoreGrade 수 = new ScroreGrade(90);
    public static final ScoreGrade 우 = new ScroreGrade(80);
    public static final ScoreGrade λ―Έ = new ScroreGrade(70);

public int score;

private ScoreGrade (int score) {
this.score = score;
}

public int getScore() {
    return score;
}

 


 

728x90

'Language > Java' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

Java ν”„λ‘œμ„ΈμŠ€μ™€ μŠ€λ ˆλ“œ  (1) 2023.11.29
Java I / O  (0) 2023.11.23
Java Exception  (4) 2023.11.22
Java μ œλ„€λ¦­, μ»¬λ ‰μ…˜ , Set , List , Map , Stack  (1) 2023.11.20
Java Iterator , Score CRUD  (1) 2023.11.17