主函数
package cs.itcast.Student; import java.util.*; import static cs.itcast.Student.studentutils.*; public class StudentTest { public static void main(String[] args) { List<student> list = new ArrayList<>(); Scanner sc = new Scanner(System.in); //初始化班级,本来想用random类但是想了一下感觉数据太离谱不切合实际 startclass(list); while(true){ System.out.println("----------------"); System.out.println("1.添加学生"); System.out.println("2.删除学生"); System.out.println("3.修改学生"); System.out.println("4.查找学生"); System.out.println("5.展示所有学生"); int choice = sc.nextInt(); switch (choice) { case 1: addstudent(list);break; case 2:deletestudent(list);break; case 3:updatestudent(list);break; case 4: selectstudent(list);break; case 5: showstudent(list);break; } } } }
student类
package cs.itcast.Student; import java.util.Objects; public class student { private String name; private String gender; private double weight; private double height; private String hobby; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public String getHobby() { return hobby; } public void setHobby(String hobby) { this.hobby = hobby; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; student student = (student) o; return Double.compare(student.weight, weight) == 0 && Double.compare(student.height, height) == 0 && Objects.equals(name, student.name) && Objects.equals(gender, student.gender) && Objects.equals(hobby, student.hobby); } @Override public int hashCode() { return Objects.hash(name, gender, weight, height, hobby); } @Override public String toString() { return "student{" + "name='" + name + '\'' + ", gender='" + gender + '\'' + ", weight=" + weight + ", height=" + height + ", hobby='" + hobby + '\'' + '}'; } }
studentutils
package cs.itcast.Student; import java.util.List; import java.util.Random; import java.util.Scanner; /* 申明一下方法 1.startclass初始化班级 2.addstudent添加一个学生 3.showstudent展示所有学生 4.selectstudent查询学生 5.deletestudent删除学生 6.uodatestudent修改学生 */ public class studentutils { public static List<student> startclass(List list) { for (int i = 0; i < 10; i++) { student s = new student(); Random r = new Random(); s.setName("张" + i); s.setGender(i > 5 ? "男" : "女"); double weight = 70; s.setWeight(weight); double height = 180; s.setHeight(height); s.setHobby(i > 5 ? "java" : "c++"); list.add(s); } return list; } public static List<student> addstudent(List list){ Scanner sc = new Scanner(System.in); student stu01 = new student(); System.out.println("请输入姓名"); String name = sc.next(); stu01.setName(name); System.out.println("请输入性别"); String gender = sc.next(); stu01.setGender(gender); System.out.println("请输入体重"); double weight = sc.nextDouble(); stu01.setWeight(weight); System.out.println("请输入身高"); Double height = sc.nextDouble(); stu01.setHeight(height); System.out.println("请输入爱好"); String hobby = sc.next(); stu01.setHobby(hobby); list.add(stu01); return list; } public static void showstudent (List list){ if (list.size() == 0) { System.out.print("表内还没有学生"); } else { System.out.println("展示班级所有学生"); for (int count = 0; count < list.size(); count++) { System.out.println(list.get(count)); } } } public static List<student> selectstudent(List<student> list){ Scanner sc = new Scanner(System.in); int count =0; if (list.size() == 0) { System.out.print("表内还没有学生"); } student stu02 = new student(); System.out.println("请输入需要查询的学生姓名"); String name = sc.next(); stu02.setName(name); System.out.println("查询到的学生是"); for( count =0;count< list.size();count++){ if(stu02.getName().equals(list.get(count).getName())) System.out.println(list.get(count)); } if(count==10) System.out.println("查询失败"); return list; } public static void deletestudent(List<student> list){ Scanner sc = new Scanner(System.in); if (list.size() == 0) { System.out.print("表内还没有学生"); } student stu02 = new student(); System.out.println("请输入需要删除的学生姓名"); String name = sc.next(); stu02.setName(name); for( int count =0;count< list.size();count++){ if(stu02.getName().equals(list.get(count).getName())) list.remove(list.get(count)); } System.out.println("删除成功!"); showstudent(list); } public static void updatestudent(List<student> list){ Scanner sc = new Scanner(System.in); int count =0; if (list.size() == 0) { System.out.print("表内还没有学生"); } student stu02 = new student(); System.out.println("请输入需要修改信息的学生姓名"); String name = sc.next(); stu02.setName(name); for( count =0;count< list.size();count++){ if(stu02.getName().equals(list.get(count).getName())){ System.out.println("请选择想要修改的信息"); System.out.println("----------------"); System.out.println("1.name"); System.out.println("2.gender"); System.out.println("3.weight"); System.out.println("4.height"); System.out.println("5.hobby"); int choice =sc.nextInt(); switch (choice){ case 1:System.out.println("请输入");String name01 = sc.next();list.get(count).setName(name01);break; case 2:System.out.println("请输入");String gender01 = sc.next();list.get(count).setGender(gender01);break; case 3:System.out.println("请输入");double weight01 = sc.nextDouble();list.get(count).setWeight(weight01);break; case 4:System.out.println("请输入");double height01 = sc.nextDouble();list.get(count).setHeight(height01);break; case 5:System.out.println("请输入");String hobby01 = sc.next();list.get(count).setHobby(hobby01);break; } } if(count==10) System.out.println("查找失败"); // else // System.out.println("查询不到学生,无法进行修改"); } showstudent(list); } }