编写简单的学生、教师管理程序,学生类、教师类有公共的父类:Person,请添加相关属性。写泛型类 Person Manager,实现以对了学生、教师进行管理。PersonManager有方法:add(T t) ; remove( T t) , findById( int id), update( T t) , findAll(). 根据需要添加其他方法。通过键盘选择是对学生进行管理,或者是对教师进行管理,所有必须的信息都通过键盘录入。录入的数据存储在List对象中。
Person 类
package test3.question4;public class Person {private String name;private String gender;private Integer age;public Person() {}public Person(String name, String gender, Integer age) {this.name = der = gender;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getGender() {return gender;}public void setGender(String gender) {der = gender;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}@Overridepublic String toString() {return "Person{" +"name='" + name + ''' +", gender='" + gender + ''' +", age=" + age +'}';}
}
student类
package test3.question4;public class Student extends Person{private Integer stuId;private Integer classId;private String profession;public Student() {}public Student(String name, String gender, Integer age, Integer stuId, Integer classId, String profession) {super(name, gender, age);this.stuId = stuId;this.classId = classId;this.profession = profession;}public Integer getStuId() {return stuId;}public void setStuId(Integer stuId) {this.stuId = stuId;}public Integer getClassId() {return classId;}public void setClassId(Integer classId) {this.classId = classId;}public String getProfession() {return profession;}public void setProfession(String profession) {this.profession = profession;}@Overridepublic String toString() {return "Student{" +"stuId=" + stuId +", classId=" + classId +", profession='" + profession + ''' +'}';}
}
Teacher类
package test3.question4;public class Teacher extends Person{private Integer teacherId;private String teachSubject;public Teacher() {}public Teacher(String name, String gender, Integer age, Integer teacherId, String teachSubject) {super(name, gender, age);acherId = achSubject = teachSubject;}public Integer getTeacherId() {return teacherId;}public void setTeacherId(Integer teacherId) {acherId = teacherId;}public String getTeachSubject() {return teachSubject;}public void setTeachSubject(String teachSubject) {achSubject = teachSubject;}@Overridepublic String toString() {return "Teacher{"+"teacherId=" + teacherId +", 所教的科目是:'" + teachSubject + ''' +'}';}
}
PersonMananger类
package test3.question4;import flect.Field;
import flect.InvocationTargetException;
import flect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;public class PersonManager<T> {private List<T> personList = new ArrayList<>();/**添加人员的操作*/public void add(T t) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {addAtrribute(t);personList.add(t);}/**删除人的操作*/public void remove(T t) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {Scanner input = new Scanner(System.in);Class clazz = t.getClass();
// System.out.SimpleName());String bigName = "";SimpleName().equals("Teacher")){System.out.print("请输入要删除的教师的教师号:");int id = Int();Teacher teacher = (Teacher) findById(id);if (teacher != null){ve(teacher);}else{System.out.println("查无此人");}}else SimpleName().equals("Student")){System.out.print("请输入要删除的学生的学号:");int id = Int();Student student = (Student) findById(id);if (student != null){ve(student);}else{System.out.println("查无此人");}}// if(isExist(t)){
// ve(t);
// }else{
//
// }}/**根据id找人*/public<T> T findById(int id) throws IllegalAccessException {for (int i = 0; i < personList.size(); i++) {T person = ((i);Class clazz = Class();List<Field> allFieldList = getAllField(clazz);for (Field field:allFieldList){field.setAccessible(true);((i)).equals(id)){return person;}}}return null;}/**更新*/public void update(T t) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {if(isExist(t)){ve(t);add(t);}else{System.out.println("查无此人");}}/** 查找所有*/public List<T> findAll(int x){List<T> list = new ArrayList<>();if(x == 1){for(int i = 0;i < personList.size();i++){if ((i).getClass().getSimpleName().equals("Teacher")) {list.(i));}}}if (x == 2){for(int i = 0;i < personList.size();i++){if ((i).getClass().getSimpleName().equals("Student")) {list.(i));}}}return list;}/**检验是否存在此人*/public boolean isExist(T t) throws IllegalAccessException {T person = null;Class().equals("Student")){Student student = (Student)t;person = StuId());}else{Teacher teacher = (Teacher)t;person = TeacherId());}if (person == null){return false;}else{return true;}}//获取所有属性的方法private List getAllField(Class clazz){/**获取所有的属性*/List<Field> fieldList = new ArrayList<>();while(clazz != null){Field[] fields = DeclaredFields();for(Field field:fields){fieldList.add(field);}clazz = Superclass();}return fieldList;}/** 添加属性方法*/private void addAtrribute(T t) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {Scanner input = new Scanner(System.in);Class clazz = t.getClass();
// System.out.SimpleName());String bigName = "";SimpleName().equals("Teacher")){bigName = "教师";}else SimpleName().equals("Student")){bigName = "学生";}/**获取所有的属性*/List<Field> fieldList = getAllField(clazz);for (Field field:fieldList) {String fieldName = Name();System.out.print("请输入此" + bigName + "的" + fieldName + ":");Object value = null;if (Type().equals(String.class)) {value = ();} else if (Type().equals(Integer.class)) {value = Int();} else if (Type().equals(Float.class)) {value = Float();} else if (Type().equals(Double.class)) {value = Double();} else if (Type().equals(Boolean.class)) {value = Boolean();}String oldFirstChar = String.valueOf(fieldName.charAt(0));String setterName = "set" + placeFirst(UpperCase());
// System.out.println(setterName);Method method = Method(Type());method.invoke(t,value);}}public List<T> getPersonList() {return personList;}public void setPersonList(List<T> personList) {this.personList = personList;}
}
测试类
package test3.question4;import flect.InvocationTargetException;
import java.util.List;
import java.util.Scanner;public class TestManager {public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {PersonManager<Person> personPersonManager = new PersonManager<>();Scanner input = new Scanner(System.in);while(true){System.out.println("1.对教师进行管理");System.out.println("2.对学生进行管理");System.out.println("3.退出管理界面");System.out.print("请输入您的选择:");int choice = Int();if (choice == 1){while(true){System.out.print("1.添加教师n" +"2.删除教师n" +"3.根据教师号查找老师n" +"4.查找所有教师n" +"5.退出n" + "请选择您要进行的操作:");int choose = Int();if (choose == 5){break;}switch (choose){case 1:Teacher teacher = new Teacher();personPersonManager.add(teacher);break;case 2:Teacher teacher1 = new Teacher();ve(teacher1);break;case 3:System.out.print("请输入教师号:");int id = Int();Teacher byId = (Teacher) personPersonManager.findById(id);System.out.println(byId);break;case 4:List<Person> list = personPersonManager.findAll(1);if (list.size() != 0){for (int i = 0; i < list.size(); i++) {Teacher teacher2 = ((i);System.out.String());}}else{System.out.println("没有教师存在!");}break;}}}if (choice == 2){while(true){System.out.print("1.添加学生n" +"2.删除学生n" +"3.根据学生号查找学生n" +"4.查找所有学生n" +"5.退出n" +"请选择您要进行的操作:");int choose = Int();if (choose == 5){break;}switch (choose){case 1:Student student = new Student();personPersonManager.add(student);break;case 2:Student student1 = new Student();ve(student1);break;case 3:System.out.print("请输入学号:");int id = Int();Student byId = (Student) personPersonManager.findById(id);System.out.println(byId);break;case 4:List<Person> list = personPersonManager.findAll(2);if (list.size() != 0){for (int i = 0; i < list.size(); i++) {Student student2 = (Student) (i);System.out.String());}}else{System.out.println("没有学生存在!");}break;}}}if (choice == 3){break;}}}}
本文发布于:2024-02-01 08:11:09,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170674627135120.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |