敏捷开发学习之三_EnumMap用法

/**
 *
 */
package test.student;

import static org.junit.Assert.*;

import org.junit.Test;

import student.ReportCard;
import student.Student;


/**
 * @author
 *
 */
public class ReportCardTest {
    @Test
    public void message(){
        ReportCard reportCard=new ReportCard();
        assertEquals(ReportCard.A_MESSAGE,reportCard.getMessage(Student.Grade.A));
    }
}


package student;

import java.util.EnumMap;
import java.util.Map;

import student.Student.Grade;

public class ReportCard {

    public static final String A_MESSAGE = "Excellent";
    public static final String B_MESSAGE="Very good";
    public static final String C_MESSAGE="Hmmm...";
    public static final String D_MESSAGE="You're not trying";
    public static final String E_MESSAGE="Loser";
   
    private Map<Student.Grade,String> message=null;

    public Map<Student.Grade, String> getMessage() {
        if(message==null)
            loadMessage();
        return message;
    }
   
    private void loadMessage(){
        message=new EnumMap<Student.Grade,String>(Student.Grade.class);
        //当创建EnumMap实例时,必须以一个Student.Grade.class作为参数
        message.put(Student.Grade.A, A_MESSAGE);
        message.put(Student.Grade.B, B_MESSAGE);
        message.put(Student.Grade.C, C_MESSAGE);
        message.put(Student.Grade.D, D_MESSAGE);
        message.put(Student.Grade.E, E_MESSAGE);
       
    }

    public String getMessage(Grade a) {
        // TODO Auto-generated method stub
        return this.getMessage().get(a);
    }



}
 
import java.util.ArrayList;

public class Student {
//在某个枚举类型定义中包含实例变量、构造函数、以及方法,就你类定义一样。主要限制:枚举不以能extend.
//其中,枚举值作为参数传入Grade的构造函数,并保存在私有变量points中,最后通过方法getPoints来获取//points
    public enum Grade {
        A(4),
        B(3),
        C(2),
        D(1),
        E(0);
        private int points;
        Grade(int points){
            this.points=points;
        }
        int getPoints(){
            return points;
        }
    }
    
    private String name;
    private int credits;
    private static int TWELVE_GREAT=12;
    private ArrayList <Grade>  list =new ArrayList <Grade> ();
    
    private GradingStrategy  gradingStragety =new RegularGradingStrategy();
    
    public GradingStrategy getGradingStragety() {
        return gradingStragety;
    }

    public void setGradingStragety(GradingStrategy gradingStragety) {
        this.gradingStragety = gradingStragety;
    }

    public Student(String name) {
        this.name=name;
        this.credits=0;
    }

    public String getName() {
        // TODO Auto-generated method stub
        return this.name;
    }

    public boolean isFullTime() {
        // TODO Auto-generated method stub
        return credits>TWELVE_GREAT;
    }

    public void addCredits(int i) {
        // TODO Auto-generated method stub
        credits+=i;
    }

    public int getCredits() {
        // TODO Auto-generated method stub
        return credits;
    }

    public double getGpa() {
        // TODO Auto-generated method stub
        if(list.isEmpty())
            return 0.0;
        double total=0.0;
        for(Grade grade: list){
            total+=gradingStragety.getGradePointsFor(grade);
        }
        return total/list.size();
    }
    
    public void addGrade(Grade grade) {
        // TODO Auto-generated method stub
        list.add(grade);
    }

}

你可能感兴趣的:(list,String,敏捷开发,null,Class,import)