Main方法
public class Main {
public static void main(String[] args) {
Manager manager = new Manager();
manager.Student();
}
}
创建学生类
public class Student {
private String name;
private String sex;
private int age;
private int sid;
public Student() {
}
public Student(String name, String sex, int age, int sid) {
this.name = name;
this.sex = sex;
this.age = age;
this.sid = sid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String toString() {
return "Student{name = " + name + ", sex = " + sex + ", age = " + age + ", sid = " + sid + "}";
}
}
创建目录
public class Manager {
public static void Student() {
List<Student> sm = new ArrayList<>();
max:
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、退出");
Scanner sc = new Scanner(System.in);
System.out.println("选择功能:");
int i = sc.nextInt();
switch (i) {
case 1:
ManagerStudent.add(sm);
break;
case 2:
ManagerStudent.printAll(sm);
break;
case 3:
ManagerStudent.revise(sm);
break;
case 4:
ManagerStudent.del(sm);
break;
case 5:
break max;
}
}
}
}
创建功能实现
public class ManagerStudent {
public static void add(List<Student> sm) {
Scanner sc = new Scanner(System.in);
Student stu = new Student();
System.out.println("----添加学生----");
System.out.println("姓名");
stu.setName(sc.next());
System.out.println("性别");
stu.setSex(sc.next());
System.out.println("年龄");
stu.setAge(sc.nextInt());
System.out.println("学号");
stu.setSid(sc.nextInt());
sm.add(stu);
System.out.println("保存完成");
System.out.println();
System.out.println();
}
public static void printAll(List<Student> sm) {
System.out.println("----查询所有----");
for (int i = 0; i < sm.size(); i++) {
System.out.print(sm.get(i).getName() + " ");
System.out.print(sm.get(i).getSex() + " ");
System.out.print(sm.get(i).getAge() + " ");
System.out.print(sm.get(i).getSid() + " ");
System.out.println();
System.out.println();
}
System.out.println("共有学生" + sm.size() + "人");
System.out.println();
System.out.println();
}
public static void del(List<Student> sm) {
System.out.println("-----删除-----");
Scanner sc = new Scanner(System.in);
System.out.println("输入学号");
int xh = sc.nextInt();
boolean f = false;
for (int i = 0; i < sm.size(); i++) {
if (sm.get(i).getSid() == xh) {
f = true;
sm.remove(i);
}
}
if (f) {
System.out.println("删除成功");
} else {
System.out.println("学号错误或不存在");
}
System.out.println();
System.out.println();
}
public static void revise(List<Student> sm) {
System.out.println("-----修改-----");
Scanner sc = new Scanner(System.in);
System.out.println("输入要修改人学号");
int xg = sc.nextInt();
boolean f = false;
for (int i = 0; i < sm.size(); i++) {
if (sm.get(i).getSid() == xg) {
f = true;
}
if (f) {
System.out.println("输入修改:姓名|性别|年龄|学号");
switch (sc.next()) {
case "姓名":
System.out.println("修改后:");
sm.get(i).setName(sc.next());
break;
case "性别":
System.out.println("修改后:");
sm.get(i).setSex(sc.next());
break;
case "年龄":
System.out.println("修改后:");
sm.get(i).setAge(sc.nextInt());
break;
case "学号":
System.out.println("修改后:");
sm.get(i).setSid(sc.nextInt());
break;
}
}
}
}
}