日期:2014-05-20 浏览次数:20738 次
Student.java package info; import java.util.ArrayList; import java.util.List; public class Student { private int sno; private String name; private int age; private List<Score> scores = new ArrayList<Score>(); public Student() { } public Student(int sno, String name, int age) { this.sno = sno; this.name = name; this.age = age; } public int getSno() { return sno; } public void setSno(int sno) { this.sno = sno; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } /** * 指定课程成绩是否已经存在 * * @param cno * @return * @author sunstar * @date 2012-7-26 下午3:03:27 */ public boolean isExists(int cno) { for (int i = scores.size() - 1; i > -1; i--) { if (this.scores.get(i).getCno() == cno) { return true; } } return false; } public void addScore(Score score) { //如果课程还没有成绩则增加,否则不添加,防止添加多次相同课程的成绩 if (!isExists(score.getCno())) { this.scores.add(score); } } public void delScore(int cno) { for (int i = scores.size() - 1; i > -1; i--) { if (this.scores.get(i).getCno() == cno) { scores.remove(i); break; } } } @Override public String toString() { return "Student [sno=" + sno + ", name=" + name + ", age=" + age + ", scores=" + scores + "]"; } /** * 显示学生 的所有学习的课程成绩 * * @author sunstar * @date 2012-7-26 下午2:58:20 */ public void showScore() { System.out.println("Student [sno=" + sno + ", name=" + name + ", age=" + age + "] 的详细成绩如下:"); for (int i = 0; i < scores.size(); i++) { System.out.println(scores.get(i).toString()); } } /** * 根据课程ID cno获取成绩 * * @param cno * @return * @author sunstar * @date 2012-7-26 下午2:57:58 */ public Score getScore(int cno) { Score score = null; for (int i = 0; i < scores.size(); i++) { if (scores.get(i).getCno() == cno) { score = scores.get(i); break; } } return score; } /** * 显示指定 课程ID的成绩 * * @param cno * @author sunstar * @date 2012-7-26 下午2:57:33 */ public void showScore(int cno) { Score score = getScore(cno); if (score == null) { System.out.println("Student [sno=" + sno + ", name=" + name + ", age=" + age + "] 没有学习 ID 为" + cno + "的课程."); } else { System.out.println("Student [sno=" + sno + ", name=" + name + ", age=" + age + "] 的 【" + score.getCourseName() + "】 成绩如下:"); System.out.println(score.toString()); } } } Score.java package info; import java.util.ArrayList; import java.util.List; public class Score { private List<Double> daily = new ArrayList<Double>() ;//日常成绩 private double mid ;//期中 private double practice ;//实习 private double end ; //期末 private int cno ;//课程号 private String courseName ; //课程名 public String getCourseName() { return courseName; } public void setCourseName(String courseName) { this.courseName = courseName; } public Score(){} public Score(double mid, double practice, double end, int cno,String courseName) { this.mid = mid; this.practice = practice; this.end = end; this.cno = cno; this.courseName = courseName ; } public Score(List<Double> daily, double mid, double practice, double end, int cno, String courseName ) { this.daily = daily; this.mid = mid; this.practice = practice; this.end = end; this.cno = cno; this.courseName = courseName ; } public List<Double> getDaily() { return daily; } public void setDaily(List<Double> daily) { this.daily = daily; } public double getMid() { return mid; } public void setMid(double mid) { this.mid = mid; } public double getPractice() { return practice; } public void setPractice(double practice) { this.practice = practice; } public double getEnd() { return end; } public void setEnd(double end) { this.end = end; } public int getCno() { return cno; } public void setCno(int cno) { this.cno = cno; } /** * 得到日常成绩的平均值 * @return * @author sunstar * @date 2012-7-26 下午2:11:07 */ public double getSumDaily(){ double sum = 0.0 ; for (int i = 0; i < daily.size(); i++){ sum += daily.get(i) ; } return sum ; } //某个课程的总评成绩 public double getZhongPing(){ double sum = this.getSumDaily() + this.mid + this.practice + this.end ; return sum / 4 ; } @Override public String toString() { return "Score [cno=" + cno + ", courseName=" + courseName +" , daily=" + this.getSumDaily() + ", mid=" + mid + ", practice=" + practice + ", end=" + end + "]"; } } School.java package info; import java.util.ArrayList; import java.util.List; public class School { public static List<Student> stuList = new ArrayList<Student>() ; //学生 public static void showZhongPing(int cno){ Score score = null ; Student stu = null ; for (int i = 0; i < stuList.size(); i++) { stu = stuList.get(i) ; score = stu.getScore(cno) ; if (score == null){ System.out.println(stu.getName() + " 没有学习 ID 为 " + cno + "的课程."); }else{ System.out.println(stu.getName() + " 的 【 " + score.getCourseName() + " 】课程总评成绩为:"+ score.getZhongPing()); } } } /** * 0-60 , 61-80, 81 -100; * @return * @author sunstar * @date 2012-7-26 下午2:55:47 */ public static int[] total(){ int iArrs[] = new int[3] ; //.iArrs......iArrs. return iArrs ; } /** * @param args * @author sunstar * @date 2012-7-26 下午2:20:05 */ public static void main(String[] args) { List<Double> daily = null ; Student stu = new Student(1, "张三", 22) ; daily = new ArrayList<Double>() ; daily.add(90.5) ; daily.add(88.5) ; daily.add(85.0) ; daily.add(50.0) ; Score score = new Score(daily, 80.0, 60.0, 70.0,1, "A课程" ) ; stu.addScore(score) ; daily = new ArrayList<Double>() ; daily.add(50.5) ; daily.add(38.5) ; score = new Score(daily, 40.0, 50.0, 30.0,2, "B课程" ) ; stu.addScore(score) ; stuList.add( stu ) ; stu = new Student(2, "李斯", 22) ; daily = new ArrayList<Double>() ; daily.add(10.5) ; daily.add(18.5) ; daily.add(15.0) ; daily.add(10.0) ; score = new Score(daily, 10.0, 10.0, 10.0,1, "A课程" ) ; stu.addScore(score) ; daily = new ArrayList<Double>() ; daily.add(50.5) ; daily.add(38.5) ; score = new Score(daily, 40.0, 50.0, 30.0,2, "B课程" ) ; stu.addScore(score) ; stuList.add( stu ) ; stu = new Student(3, "Jone", 19) ; stuList.add( stu ) ; stu = new Student(4, "王五", 24) ; stuList.add( stu ) ; stu = new Student(5, "X六", 23) ; stuList.add( stu ) ; stuList.get(0).showScore() ; stuList.get(0).showScore(1) ; stuList.get(0).showScore(2) ; showZhongPing(1) ; } }