1 package collection.map; 2 3 4 public class Student { 5 private String name; 6 private String classNo; 7 private double score; 8 9 10 public Student(String name, String classNo, double score) { 11 super(); 12 this.name = name; 13 this.classNo = classNo; 14 this.score = score; 15 } 16 public String getName() { 17 return name; 18 } 19 public void setName(String name) { 20 this.name = name; 21 } 22 public String getClassNo() { 23 return classNo; 24 } 25 public void setClassNo(String classNo) { 26 this.classNo = classNo; 27 } 28 public double getScore() { 29 return score; 30 } 31 public void setScore(double score) { 32 this.score = score; 33 } 34 35 36 }
package collection.map; import java.util.ArrayList; import java.util.List; public class ClassRoom { private String roomNo; private List<Student> stus; //学生列表 private double total; //总分 public ClassRoom() { stus = new ArrayList<Student>(); } public ClassRoom(String roomNo) { this(); this.roomNo = roomNo; } public String getRoomNo() { return roomNo; } public void setRoomNo(String roomNo) { this.roomNo = roomNo; } public List<Student> getStus() { return stus; } public void setStus(List<Student> stus) { this.stus = stus; } public double getTotal() { return total; } public void setTotal(double total) { this.total = total; } }
1 package collection.map; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.Iterator; 6 import java.util.List; 7 import java.util.Map; 8 import java.util.Set; 9 10 11 /* 12 * 定义一个Student类,属性:name 姓名,classNumber 班号,score 成绩 13 * 现在将若干Student对象放入List,请统计出每个班级的总分和平均分,分别打印出来 14 * 以面向对象的思维解决 15 * 16 */ 17 public class MapDemo02 { 18 19 //将若干Student放入List 20 public static void exam(List<Student> list) 21 { 22 list.add(new Student("AAA", "301", 88)); 23 list.add(new Student("BBB", "301", 84)); 24 list.add(new Student("CCC", "302", 85)); 25 list.add(new Student("DDD", "302", 86)); 26 list.add(new Student("EEE", "201", 46)); 27 list.add(new Student("FFF", "201", 68)); 28 list.add(new Student("AAA", "301", 88)); 29 } 30 31 //统计分数 32 public static void count(Map<String, ClassRoom> rooms,List<Student> list) 33 { 34 for(Student stu:list) 35 { 36 String no = stu.getClassNo(); 37 double score = stu.getScore(); 38 //根据班级编号 查看 Map是否存在该班级 分拣思路 39 ClassRoom room = rooms.get(no); 40 if(null == room) 41 { 42 room = new ClassRoom(no); 43 rooms.put(no, room); 44 } 45 //存储总分 46 room.setTotal(room.getTotal() + score); 47 room.getStus().add(stu); 48 } 49 } 50 51 //打印 总分与平均分 52 public static void printScoreAndAvg(Map<String, ClassRoom> rooms) 53 { 54 Set<Map.Entry<String, ClassRoom>> entrySet = rooms.entrySet(); 55 Iterator<Map.Entry<String, ClassRoom>> iter = entrySet.iterator(); 56 while (iter.hasNext()) { 57 Map.Entry<String, ClassRoom> entry = iter.next(); 58 ClassRoom room = entry.getValue(); 59 double avg = Math.round(room.getTotal() / room.getStus().size()); 60 System.out.println("班号:"+room.getRoomNo()+",总分:"+room.getTotal()+"平均分:"+avg); 61 } 62 } 63 64 public static void main(String[] args) { 65 List<Student> list = new ArrayList<Student>(); 66 exam(list); 67 68 //统计 69 Map<String, ClassRoom> rooms = new HashMap<String, ClassRoom>(); 70 count(rooms, list); 71 printScoreAndAvg(rooms); 72 73 } 74 }
时间: 2024-10-05 08:34:11