一:项目结构(Eclipse)
功能实现:基本的增删改查
二:代码
package student; public class Student { private String sno; //学号 private String name;//姓名 private String age;//年龄 private String sex;//性别 private String score;//成绩 public String getSno() { return sno; } public void setSno(String sno) { this.sno = sno; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getScore() { return score; } public void setScore(String score) { this.score = score; } public Student() { super(); // TODO Auto-generated constructor stub } }
package student; import java.util.ArrayList; import java.util.Scanner; public class Main { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { zhuye(); } private static void zhuye() { ArrayList<Student> list = new ArrayList<Student>(); for (;;) { // TODO Auto-generated method stub System.out.println("-----欢迎进入学生管理系统-----"); System.out.println("-----请输入数字代号进行操作-----"); System.out.println("1:所有学生信息"); System.out.println("2:添加学生信息"); System.out.println("3:删除学生信息"); System.out.println("4:修改学生信息"); System.out.println("5:查询学生信息"); System.out.println("6:退出系统"); int choice = sc.nextInt(); if (choice == 1) { showallstudent(list); } else if (choice == 2) { addstudent(list); } else if (choice == 3) { deletestudent(list); } else if (choice == 4) { updatestudent(list); } else if (choice == 5) { selectstudent(list); } else if (choice == 6) { System.out.println("-----退出系统成功-----"); break; } else { System.out.println("-----请输入正确代号-----"); } } } private static void updatestudent(ArrayList<Student> list) { // TODO Auto-generated method stub if (list.size() == 0) { System.out.println("-----当前没有学生信息-----"); } else { System.out.println("请输入修改学生的学号:"); String id = sc.next(); for (int i = 0; i < list.size(); i++) { if (id.equals(list.get(i).getSno())) { System.out.println("姓名:"); String name = sc.next(); System.out.println("年龄:"); String age = sc.next(); System.out.println("性别:"); String sex = sc.next(); System.out.println("成绩:"); String score = sc.next(); Student student = new Student(); student.setSno(id); student.setName(name); student.setAge(age); student.setSex(sex); student.setScore(score); list.set(i, student); System.out.println("-----修改成功-----"); } else { System.out.println("-----没有找到该学生,请重新输入-----"); updatestudent(list); } } } } private static void addstudent(ArrayList<Student> list) { // TODO Auto-generated method stub System.out.println("学号:"); String sno = sc.next(); System.out.println("姓名:"); String name = sc.next(); System.out.println("年龄:"); String age = sc.next(); System.out.println("性别:"); String sex = sc.next(); System.out.println("成绩:"); String score = sc.next(); Student student = new Student(); student.setSno(sno); student.setName(name); student.setAge(age); student.setSex(sex); student.setScore(score); if (list.add(student)) { // System.out.println(list); System.out.println(name + "学生信息添加成功"); } else { System.out.println(name + "学生信息添加失败"); } } private static void selectstudent(ArrayList<Student> list) { // TODO Auto-generated method stub if (list.size() == 0) { System.out.println("-----当前没有学生信息-----"); } else { System.out.println("请输入您想查询的学生学号:"); String id = sc.next(); for (int i = 0; i < list.size(); i++) { if (id.equals(list.get(i).getSno())) { System.out.println("学号:" + list.get(i).getSno() + " 姓名:" + list.get(i).getName() + " 年龄:" + list.get(i).getAge() + " 性别:" + list.get(i).getSex() + " 成绩:" + list.get(i).getScore()); } else { System.out.println("没有找到该学生,请重新输入"); selectstudent(list); } } } } private static void deletestudent(ArrayList<Student> list) { // TODO Auto-generated method stub if (list.size() == 0) { System.out.println("-----当前没有学生信息-----"); } else { System.out.println("请输入您想要删除的学生学号:"); String id = sc.next(); for (int i = 0; i < list.size(); i++) { if (id.equals(list.get(i).getSno())) { list.remove(i); System.out.println("学生信息删除成功"); } else { System.out.println("未找到您想要删除的学生信息,请重新输入"); deletestudent(list); } } } } private static void showallstudent(ArrayList<Student> list) { // TODO Auto-generated method stub if (list.size() == 0) { System.out.println("-----当前没有学生信息-----"); } else { //System.out.println(list); for (Student stu : list) { System.out.println("学号:" + stu.getSno() + " 姓名:" + stu.getName() + "年龄:" + stu.getAge() + " 性别:" + stu.getSex() + " 成绩:" + stu.getScore()); } } } }
三:运行截图