数组、集合、链表实现学生成绩管理系统(JavaSe)
数组实现:
功能描述:学号为唯一识别符,不可重复,可进行增加、修改、删除、查看操作、升序输出。
学生类:
package com.lili.StudentManagerSystem.smsChar; /** * 学生实体类 * * @author: QiJingJing * @create: 2021/7/19 */ public class Student { /** * 学号 */ private long id; /** * 年龄 */ private int age; /** * 姓名 */ private String name; /** * 分数 */ private double score; public Student() { } public Student(long id, int age, String name, double score) { this.id = id; this.age = age; this.name = name; this.score = score; } public long getId() { return id; } public void setId(long id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getScore() { return score; } public void setScore(double score) { this.score = score; } @Override public String toString() { return "Student{" + "id=" + id + ", age=" + age + ", name='" + name + '\'' + ", score=" + score + '}'; } }
学生管理接口:
package com.lili.StudentManagerSystem.smsChar; /** * 学生管理接口 * * @author: QiJingJing * @create: 2021/7/19 */ public interface StudentManager { /** * 添加一个学生 * * @param student * @return: void * @author: QijingJing * @date: 2021/7/19 */ void add(Student student); /** * 根据学号删除一个学生 * * @param id * @return: void * @author: QijingJing * @date: 2021/7/19 */ void delete(long id); /** * 修改学生信息 * * @param student * @return: void * @author: QijingJing * @date: 2021/7/19 */ void update(Student student); /** * 查询所有学生信息 */ void selectAll(); /** * 通过学号查询学生信息 * * @param id * @return: com.lili.StudentManagerSystem.smsChar.Student * @author: QijingJing * @date: 2021/7/19 */ Student selectById(long id); }
学生管理具体实现:
package com.lili.StudentManagerSystem.smsChar; import java.util.Arrays; import java.util.Comparator; import java.util.Objects; /** * 学生管理系统具体实现类 * * @author: QiJingJing * @create: 2021/7/19 */ public class StudentManagerImpl implements StudentManager { /** * 创建一个默认长度为10的数组存放学生信息 */ private Student[] students = new Student[10]; /** * 用于判断数组元素个数,以便及时扩充 */ private static int count = 0; @Override public void add(Student student) { if (count >= students.length) { // 扩充数组 students = Arrays.copyOf(students, students.length * 2); } // 添加学生 students[count] = student; count++; selectAll(); } @Override public void delete(long id) { for (int i = 0; i < count; i++) { if (students[i].getId() == id) { if (count - 1 - i >= 0) { System.arraycopy(students, i + 1, students, i, count - 1 - i); students[count - 1] = null; count--; break; } } } System.out.println("---删除成功---"); selectAll(); } @Override public void update(Student student) { for (int i = 0; i < count; i++) { if (students[i].getId() == student.getId()) { students[i] = student; break; } } System.out.println("-----修改成功------"); selectAll(); } @Override public void selectAll() { // 排序后输出 Arrays.stream(students).filter(Objects::nonNull).sorted(Comparator.comparing(Student::getId)).forEach(System.out::println); } @Override public Student selectById(long id) { for (int i = 0; i < count; i++) { if (students[i].getId() == id) { return students[i]; } } return null; } }
测试类:
package com.lili.StudentManagerSystem.smsChar; import java.util.Scanner; /** * @author: QiJingJing * @create: 2021/7/19 */ public class TestStudent { private static StudentManagerImpl impl = new StudentManagerImpl(); private static Scanner input = new Scanner(System.in); /** * 从键盘录入学生信息 */ private static void getStudentInfo() { System.out.println("请输入学生的学号:"); long id = input.nextLong(); // 先进行判断是否已经存在 // 判断该学号是否已经存在 if (impl.selectById(id) != null) { System.out.println("----此学号已经存在,请重新操作----"); } else { System.out.println("请输入学生的姓名:"); String name = input.next(); System.out.println("请输入学生的年龄:"); int age = input.nextInt(); System.out.println("请输入学生的分数:"); double score = input.nextDouble(); impl.add(new Student(id, age, name, score)); } } /** * 从键盘删除学生信息 */ private static void delStudentInfo() { System.out.println("请输入需要删除的学生学号:"); long id = input.nextLong(); if (impl.selectById(id) == null) { System.out.println("------此学号不存在,请重新操作------"); } else { // 调用删除方法 impl.delete(id); } } /** * 从键盘修改学生信息 */ private static void updateStudentInfo() { System.out.println("请输入需要修改的学生学号:"); long id = input.nextLong(); if (impl.selectById(id) == null) { System.out.println("------此学号不存在,请重新操作------"); } else { System.out.println("请输入新的学生姓名:"); String name = input.next(); System.out.println("请输入新的学生年龄:"); int age = input.nextInt(); System.out.println("请输入新的学生分数:"); double score = input.nextDouble(); impl.update(new Student(id, age, name, score)); } } /** * 从键盘修改学生信息 */ private static void selectStudentInfo() { System.out.println("请输入你需要查询的学生学号:"); long id = input.nextLong(); Student student = impl.selectById(id); if (student == null) { System.out.println("-----该学号不存在-----"); } else { System.out.println(student); } } public static void main(String[] args) { System.out.println("------欢迎进入学生成绩管理系统------"); while (true) { System.out.println("请选择你要操作的选项\n1.添加学生信息 2.删除学生信息 3.修改学生信息 4.查询单个学生信息"); int num = input.nextInt(); if (num == 1) { // 调用录入学生信息的方法 getStudentInfo(); } if (num == 2) { delStudentInfo(); } if (num == 3) { updateStudentInfo(); } if (num == 4) { selectStudentInfo(); } } } }
集合实现(对数组实现稍加修改即可)
接口实现类:
package com.lili.StudentManagerSystem.smsList; import com.lili.StudentManagerSystem.smsChar.StudentManager; import java.util.ArrayList; import java.util.Comparator; import java.util.List; /** * 学生管理系统具体实现类 * * @author: QiJingJing * @create: 2021/7/19 */ public class StudentManagerImpl2 implements StudentManager2 { List<Student2> list = new ArrayList<>(); @Override public void add(Student2 student) { list.add(student); System.out.println("添加成功"); selectAll(); } @Override public void delete(long id) { list.remove(selectById(id)); System.out.println("------删除成功-----"); selectAll(); } @Override public void update(Student2 student) { for (Student2 student2 : list) { if (student2.getId() == student.getId()) { student2.setAge(student.getAge()); student2.setName(student.getName()); student2.setScore(student.getScore()); break; } } System.out.println("-----修改成功----"); selectAll(); } @Override public void selectAll() { list.stream().sorted(Comparator.comparing(Student2::getId)).forEach(System.out::println); } @Override public Student2 selectById(long id) { for (Student2 student2 : list) { if (student2.getId() == id) { return student2; } } return null; } }
测试类:
package com.lili.StudentManagerSystem.smsList; import com.lili.StudentManagerSystem.smsChar.Student; import com.lili.StudentManagerSystem.smsChar.StudentManagerImpl; import java.util.Scanner; /** * @author: QiJingJing * @create: 2021/7/19 */ public class TestStudent2 { private static StudentManagerImpl2 impl = new StudentManagerImpl2(); private static Scanner input = new Scanner(System.in); /** * 从键盘录入学生信息 */ private static void getStudentInfo() { System.out.println("请输入学生的学号:"); long id = input.nextLong(); // 判断该学号是否已经存在 if (impl.selectById(id) != null) { System.out.println("----此学号已经存在,请重新操作----"); } else { System.out.println("请输入学生的姓名:"); String name = input.next(); System.out.println("请输入学生的年龄:"); int age = input.nextInt(); System.out.println("请输入学生的分数:"); double score = input.nextDouble(); impl.add(new Student2(id, age, name, score)); } } /** * 从键盘删除学生信息 */ private static void delStudentInfo() { System.out.println("请输入需要删除的学生学号:"); long id = input.nextLong(); if (impl.selectById(id) == null) { System.out.println("------此学号不存在,请重新操作------"); } else { // 调用删除方法 impl.delete(id); } } /** * 从键盘修改学生信息 */ private static void updateStudentInfo() { System.out.println("请输入需要修改的学生学号:"); long id = input.nextLong(); if (impl.selectById(id) == null) { System.out.println("------此学号不存在,请重新操作------"); } else { System.out.println("请输入新的学生姓名:"); String name = input.next(); System.out.println("请输入新的学生年龄:"); int age = input.nextInt(); System.out.println("请输入新的学生分数:"); double score = input.nextDouble(); impl.update(new Student2(id, age, name, score)); } } /** * 从键盘修改学生信息 */ private static void selectStudentInfo() { System.out.println("请输入你需要查询的学生学号:"); long id = input.nextLong(); Student2 student = impl.selectById(id); if (student == null) { System.out.println("-----该学号不存在-----"); } else { System.out.println(student); } } public static void main(String[] args) { System.out.println("------欢迎进入学生成绩管理系统------"); while (true) { System.out.println("请选择你要操作的选项\n1.添加学生信息 2.删除学生信息 3.修改学生信息 4.查询单个学生信息"); int num = input.nextInt(); if (num == 1) { // 调用录入学生信息的方法 getStudentInfo(); } if (num == 2) { delStudentInfo(); } if (num == 3) { updateStudentInfo(); } if (num == 4) { selectStudentInfo(); } } } }
链表实现:
package com.lili.StudentManagerSystem.smsLink; import com.lili.StudentManagerSystem.smsChar.Student; /** * 链表实现学生成绩管理系统 * * @author: QiJingJing * @create: 2021/7/19 */ public class Node { public static void main(String[] args) { Node node = new Node(); node.add(new Student(1, 12, "13", 12f)); node.add(new Student(0, 12, "13", 12f)); node.add(new Student(3, 12, "13", 12f)); node.select(); node.update(new Student(0, 112, "113", 112f)); node.select(); } private StudentNode node = null; // 增加学生的方法 public void add(Student student) { if (node == null) { node = new StudentNode(student); } else { node.add(student); } } // 删除一个学生 public void del(long id) { if (node != null) { if (node.getStudent().getId() == id) { node = node.next; } else { node.del(id); } } } // 修改一个学生 public void update(Student student) { if (node != null) { if (node.getStudent().getId() == student.getId()) { node.student = student; } else { node.update(student); } } } // 查询所有学生 public void select() { if (node != null) { System.out.println(node.getStudent()); node.select(); } } static class StudentNode { private Student student; private StudentNode next; public StudentNode(Student student) { this.student = student; } public Student getStudent() { return student; } // 增加学生的方法 public void add(Student student) { if (this.next == null) { this.next = new StudentNode(student); } else { this.next.add(student); } } // 删除一个学生 public void del(long id) { if (this.next != null) { if (this.next.getStudent().getId() == id) { this.next = this.next.next; } } } // 修改一个学生 public void update(Student student) { if (this.next != null) { if (this.next.getStudent().getId() == student.getId()) { this.next.student = student; } else { this.next.update(student); } } } // 查询所有学生 public void select() { if (this.next != null) { System.out.println(this.next.getStudent()); this.next.select(); } } } }