149 - 教師類-2
阿新 • • 發佈:2018-11-07
149 - 教師類-2
Time Limit: 1000 Memory Limit: 65535
Submit: 740 Solved: 270
Description
修改題目143 1. 修改教師類,使得由多個Teacher物件所形成的陣列可以排序(編號由低到高排序),並在main函式中使用Arrays.sort(Object[] a)方法排序 2. 定義一個類TeacherManagement,包含教師陣列,提供方法add(Teacher[]),使其可以新增教師,提供過載方法search,方法可以在一組給定的教師中,根據姓名或年齡返回等於指定姓名或年齡的教師的字串資訊,資訊格式為:“no: **, name:**, age: **, seminary: **”。如果沒有滿足條件的教師,則返回“no such teacher”。
Input
教師個數
教師資訊
待查詢教師的姓名
待查詢教師的年齡
Output
排序後的資訊
按姓名查詢的老師資訊
按年齡查詢的老師資訊
Sample Input
4
3 Linda 38 SoftwareEngineering
1 Mindy 27 ComputerScience
4 Cindy 28 SoftwareEngineering
2 Melody 27 ComputerScience
Cindy
27
Sample Output
no: 1, name: Mindy, age: 27, seminary: ComputerScience no: 2, name: Melody, age: 27, seminary: ComputerScience no: 3, name: Linda, age: 38, seminary: SoftwareEngineering no: 4, name: Cindy, age: 28, seminary: SoftwareEngineering search by name: no: 4, name: Cindy, age: 28, seminary: SoftwareEngineering search by age: no: 1, name: Mindy, age: 27, seminary: ComputerScience no: 2, name: Melody, age: 27, seminary: ComputerScience
import java.util.*; import java.lang.reflect.Array; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int i; int num = scan.nextInt(); TeacherManagement tm = new TeacherManagement(num); Teacher[] t = new Teacher[num]; for(i=0; i<num; i++){ int no = scan.nextInt(); String name = scan.next(); int age = scan.nextInt(); String sem = scan.next(); t[i] = new Teacher(no,name,age,sem); } tm.add(t); Arrays.sort(t); for(i=0;i<t.length;i++){ System.out.println(t[i].toString()); } String s1 = scan.next(); tm.search(s1); int s2 = scan.nextInt(); tm.search(s2); } } class Teacher implements Comparable<Teacher>{ int no; String name; int age; String seminary; Teacher(int no, String name, int age, String seminary){ setNo(no); setName(name); setSeminary(seminary); setAge(age); } public void setNo(int no) { this.no = no; } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public void setSeminary(String seminary) { this.seminary = seminary; } public String getName() { return name; } public int getNo() { return no; } public int getAge() { return age; } public String getSeminary() { return seminary; } public boolean equals(Object o){ if(o == null) return false; else{ boolean res = false; if(o instanceof Teacher){ Teacher t = (Teacher)o; if(this.no == t.no){ res = true; } } return res; } } public String toString(){ return "no: "+getNo()+", name: "+getName()+", age: "+getAge()+", seminary: "+getSeminary(); } public int compareTo(Teacher t){ if(this.no == t.no) return 0; else if(this.no>t.no) return 1; else return -1; } } class TeacherManagement{ Teacher[] t; int num; TeacherManagement(int num){ this.num = num; this.t = new Teacher[num]; } public void add(Teacher[] t){ this.t = t; } // 過載 public void search(int age){ int flag = 0; System.out.println("search by age:"); for (Teacher aT : t) { if (aT.age == age) { flag = 1; System.out.println(aT.toString()); } } if(flag==0){ System.out.println("no such teacher"); } } public void search(String name){ int flag = 0; System.out.println("search by name:"); for (Teacher aT : t) { if (aT.name.equals(name)) { flag = 1; System.out.println(aT.toString()); } } if(flag==0){ System.out.println("no such teacher"); } } }