学生管理系统1.1(排序算法,计数器思想)

编程入门 行业动态 更新时间:2024-10-07 05:19:02

学生<a href=https://www.elefans.com/category/jswz/34/1769858.html style=管理系统1.1(排序算法,计数器思想)"/>

学生管理系统1.1(排序算法,计数器思想)

实体类:

package com.mhj.student.entity;public class Student {/*** 学生管理系统*  实现类:*  iD*  名字*  年龄*  性别*  成绩*/private int iD;private String name;private int age;private char gender;private int score;// 无参构造方法public Student() {}// 成员变量初始化public Student(int iD, String name, int age, char gender, int score) {super();this.iD = iD;this.name = name;this.age = age;this.gender = gender;this.score = score;}// get(查询)和set(修改)方法public int getiD() {return iD;}public void setiD(int iD) {this.iD = iD;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public char getGender() {return gender;}public void setGender(char gender) {this.gender = gender;}public int getScore() {return score;}public void setScore(int score) {this.score = score;}/*** 使用System.out.println打印展示Student类对象时* 是直接自动调用toString方法,展示该方法返回String字符串内容*/@Overridepublic String toString() {return "Student [id=" + iD + ", name=" + name + ", age=" + age + ", gender=" + gender + ", score=" + score+ "]"; }// 展示对象属性public void mprint() {System.out.println(" id : " + iD + ", 名字  : " + name + ", 年龄 : " + age +", 性别 : " + gender +", 成绩  : " + score);}
}

管理类

package com.mhj.student.manager;import java.util.Scanner;import com.mhj.student.entity.Student;public class StudentManager {/*** 新建对象数组 初始化为null* * 推荐使用private修饰 类内操作该数组*/private Student[] allStus = null;/*** 定义一个类内使用的静态常量,名字叫做DEFAULT_CAPACITY* 用来初始化数组容量*/private static final int DEFAULT_CAPACITY = 10;/** int数组容量的最大值,*/private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;// 无参构造方法public StudentManager() {// 用户如不初始化数组容量,则在创建对象时先给予数组一个容量allStus = new Student[DEFAULT_CAPACITY];}/*** 用户指定初始化对象数组的容量,但数组的容量不能小于0且不能大于MAX_ARRAY_SIZE* @param newCapacity 这是用户需要的数组容量个数*/// 公开的构造方法 ,参数是用户需要的数组容量public StudentManager(int newCapacity) {// 判断用户输入的是否合法if(newCapacity < 0 || newCapacity > MAX_ARRAY_SIZE) {System.out.println("越界!!!");System.exit(0);}// 如果容量没有越界则将容量扩展至用户需要的容量allStus = new Student[newCapacity];}private int size = 0;//添加加学生对象的方法/*** 添加学生对象的方法size为保存对象的下标位置* * @param stu 添加保存的对象* @return 添加成功返回true*/public boolean add(Student stu) {if(size == allStus.length) {System.out.println("扩容");grow(size + 1);}allStus[size] = stu;size += 1;return true;}/** 但是目前有一个问题, 如果用户不指定容量,初始容量对象只能储* 10个,这里需要一个扩容的方法,*//*** 该方法是用来扩展allStus数组的方法,通过拷贝,更换首地址来完成扩容* * @param minCapacity 需要至少要扩容到多少容量*/private void grow(int minCapacity) {// 获取旧数组的容量int oldCapacity = allStus.length;// 创建新数组,扩容百分之五十左右int newCapacity = oldCapacity + oldCapacity / 2;// 判断新数组容量是否足够if(newCapacity < minCapacity) {newCapacity = minCapacity;}// 判断当前数组容量是否超出了MAX_ARRAY_SIZEif(newCapacity > MAX_ARRAY_SIZE) {// 如果超出范围则退出程序,目前而言,后期可抛出异常System.exit(0);}// 新建数组作来拷贝旧数组的内容Student[] temp = new Student[newCapacity];// 拷贝数组for (int i = 0; i < oldCapacity; i++) {temp[i] = allStus[i];}// allStus保存新数组的首地址allStus = temp;}// 根据对应的ID号删除指定学生public boolean remove(int iD) {// 设定一个负数假设没有对应的id// 遍历所有对象 如果找到对应id的对象,以index保存下标位置int index = findIndexById(iD);// 判断如果没找到则返回falseif (index < 0) {System.out.println("未找到");return false;}// 删除操作:将要删除学生的对象被后面的覆盖,起始位置是index,以此类推,/** */for (int i = index; i < size; i++) {allStus[i] = allStus[i + 1];}// 原来最后一个对象赋值为nullallStus[size - 1] = null;size -= 1;return true;}/*** 该方法是展示所有数组中对象保存属性的方法*/public void print() {for (int i = 0; i < size; i++) {allStus[i].mprint();}}/*** 该方法是通过用户给出的ID寻找对应的对象* * @param id 需要寻找对象的id* @return 返回需要寻找的对象*/public Student get(int id) {/** 调用类内私有化方法findIndexById* 以index接收下标位置*/int index = findIndexById(id);if (index < 0) {System.out.println("未找到");return null;}allStus[index].mprint();return allStus[index];}/*** 该方法是类内私有化方法,用于寻找ID所对应对象的下标位置然后作为返回值供* 其他方法使用* * @param id 需要寻找的id* @return 返回寻找到的下标位置*/private int findIndexById(int id) {// 初始化一个变量,假设要寻找的id不存在int index = -1;// 以for循环遍历整个数组,寻找对应id的对象下标位置for (int i = 0; i < size; i++) {if (id == allStus[i].getiD()) {index = i;break;}}return index;}/*** 该方法是修改对应id的学生信息的方法* @param id 需要修改学生的id* @return 修改成功返回true,失败返回false*/public boolean modify(int id) {// 调用类内get方法Student stu = get(id);// 判断对象的值是否为nullif (null == stu) {System.out.println("id信息有误");return false;}// 创建一个Scanner对象Scanner sc = new Scanner(System.in);int choose = 0;// 以while循环完成修改信息while (choose != 5) {System.out.println("id" + stu.getiD());System.out.println("名字" + stu.getName());System.out.println("年龄" + stu.getAge());System.out.println("性别" + stu.getGender());System.out.println("成绩" + stu.getScore());// 功能提示System.out.println("1 : 修改姓名 ");System.out.println("2 :修改年龄 ");System.out.println("3 : 修改性别" );System.out.println("4 : 修改成绩 ");System.out.println("5 : 退出程序");choose = sc.nextInt();sc.nextLine();// 以switch选择要操作的内容switch (choose) {case 1:System.out.println("请输入要修改的名字");String name = sc.nextLine();stu.setName(name);break;case 2:System.out.println("请输入要修改的年龄");int age = sc.nextInt();stu.setAge(age);break;case 3:System.out.println("请输入要修改的性别");char gender = sc.nextLine().charAt(0);stu.setGender(gender);break;case 4:System.out.println("请输入要修改的成绩");int score = sc.nextInt();stu.setScore(score);break;case 5:System.out.println("保存退出");break;default:System.out.println("输入有误");break;}}// 修改成功返回truereturn true;}/*** 该方法是降序排序学生成绩的方法* 通过学生成绩降序排序展示学生的信息*/public void scoreSortDesc() {// 该方法不可随意操作对象数组的顺序,所以重新定义一个对象数组Student[] sortTemp = new Student[size];// 拷贝数组内容for (int i = 0; i < sortTemp.length; i++) {sortTemp[i] = allStus[i];}for (int i = 0; i < sortTemp.length; i++) {// index用来保存下标用于交换int index = i;for (int j = i + 1; j < sortTemp.length; j++) {if (sortTemp[index].getScore() > sortTemp[j].getScore()) {index = j;}}// 定义一个临时对象来交换if (index != i) {Student stu = sortTemp[index];sortTemp[index] = sortTemp[i];sortTemp[i] = stu;}}// 展示保存后的效果for (int i = 0; i < sortTemp.length; i++) {sortTemp[i].mprint();}}}

测试

package com.mhj.student.system;import java.util.Scanner;import org.junit.Test;import com.mhj.student.entity.Student;
import com.mhj.student.manager.StudentManager;public class TestGrow {public static void main(String[] args) {StudentManager stu = new StudentManager();// 测试for (int i = 0; i < 15; i++) {Student stm = new Student();stm.setiD(i + 1);stm.setName("吴彦祖" + i);stm.setAge(10 + i * 2);stm.setGender('男');stm.setScore(60 + i * 2);stu.add(stm);}Scanner sc = new Scanner(System.in);int choose = 0;int id = 0;while (choose != 7) {System.out.println("1 : 添加新学生");System.out.println("2 :删除指定id学生");System.out.println("3 : 修改指定id学生");System.out.println("4 :查询指定id学生");System.out.println("5 : 查询所有学生");System.out.println("6 : 按照程序降序排序");System.out.println("7 : 退出程序");choose = sc.nextInt();sc.nextLine();switch (choose) {case 1:stu.add (new Student( 0, " ", 0, ' ', 0));break;case 2:System.out.println("请输入要删除学生的ID");id = sc.nextInt();stu.remove(id);break;case 3:System.out.println("请输入要修改学生的id");id = sc.nextInt();stu.modify(id);break;case 4:System.out.println("请输入要查询学生的id");id = sc.nextInt();stu.get(id);break;case 5:stu.print();break;case 6:stu.scoreSortDesc();break;case 7:System.out.println("程序结束...");break;default:System.out.println("输入有误,请重新输入");break;}}}
}

项目后期内容

  1. 数据持久化保存
    IO操作文件内容 String字符串解析
  2. Sout界面分离
    除了viewer层,其他任何代码中不能出现任何一个Sout
  3. 排序算法优化
    可以让排序方法能够根据用户指定条件进行排序操作
  4. 自定义异常添加
    优化代码中错误提示和处理过程
  5. 数据根据条件过滤展示
    根据用户指定条件,过滤展示对应的数据

更多推荐

学生管理系统1.1(排序算法,计数器思想)

本文发布于:2024-02-27 22:58:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1766721.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:管理系统   算法   计数器   思想   学生

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!