学生管理系统实体类与管理类代码优化
需求:
实体类:
学生类:
id, 姓名,年龄,性别,成绩
需要使用数组保存学生信息
Student[] allStu
需要完成的方法
1. 根据学生的ID,找到对应的学生对象
2. 完成方法,添加新学生
3. 完成方法,删除指定ID的学生
4. 完成方法,展示数组中所有的学生信息
5. 根据学生成绩,完成降序排序
学生类:
package com.qfedu.student.system.entity;
public class Student {
//成员变量
private int id;
private String name;
private int age;
private char gender;
private float score;
//构造方法
public Student() {
super();
}
public Student(int id, String name, int age, char gender, float score) {
super();
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.score = score;
}
//setter与getter方法
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 float getScore() {
return score;
}
public void setScore(float score) {
this.score = score;
}
/**
* 使用System.out.println打印展示Student类对象时
* 是直接自动调用toString方法,展示该方法返回String字符串内容
*/
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + ", gender=" + gender + ", score=" + score
+ "]";
}
}
管理类:
package com.qfedu.student.system.manager;
import com.qfedu.student.system.entity.Student;
public class StudentManager {
/**
* 私有化保存学生信息的数组,对外不能公开,有且只针对当前管理类使用
* 初始化为null
*/
private Student[] allStus = null;
/**
* DEFAULT_CAPACITY 默认容量,这里是一个带有名字的常量
*/
private static final int DEFAULT_CAPACITY = 10;
/**
* 数组最大容量,是int类型最大值 - 8
*/
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
/**
* 当前底层Student数组中有效元素个数
*/
private int size = 0;
/**
* 无参数构造方法,但是需要创建底层保存学生数据的Student数组,因为当前数组不存在,指向null
*
* 默认初始化容量为DEFAULT_CAPACITY,
*/
public StudentManager() {
allStus = new Student[DEFAULT_CAPACITY];
}
/**
* 用户指定初始化容量,但是要求初始化容量在合理范围以内,不能小于0,不能大于数组容量最大值,MAX_ARRAY_SIZE
* @param initCapacity 用户指定的初始化容量
*/
public StudentManager(int initCapacity) {
if (initCapacity < 0 || initCapacity > MAX_ARRAY_SIZE) {
System.out.println("Input Parameter is Invalid");
/*异常抛出!!!!,暂时未讲,补充知识点,暂时使用System.exit(0);退出程序*/
System.exit(0);
}
allStus = new Student[initCapacity];
}
/**
* 当前方法是添加学生类对象到StudentManager中,保存到底层Student类型数组
* @param student Student类对象
* @return 添加成功返回true,失败返回false
*/
public boolean add(Student student) {
/*
* 当有效元素个数size已经和数组容量Capacity一致时,进行扩容操作
*/
if (size == allStus.length) {
//调用数组扩容方法,最小容量为原数组容量+1
grow(size + 1);
}
allStus[size] = student;
size++;
return true;
}
/**
* 根据用户指定ID,删除对应学生类对象
* @param id 指定学生ID
* @return 删除成功返回true,失败返回false
*/
public boolean remove(int id) {
//调用findndexById方法,返回需要删除ID对应的元素下标
int index = findIndexById(id);
/*
* 以上代码循环结束,如果index = -1 ,证明未找到对应元素,当前方法无法进行删除操作,返回false
*/
if (-1 == index) {
System.out.println("Not Found!!!");
return false;
}
/*
* 如果index不等于-1,表示找到了对应需要删除的元素,进行删除操作
*/
for (int i = index; i < size - 1; i++) {
allStus[i] = allStus[i + 1];
}
//为原本最后一个有效元素赋值为null
allStus[size - 1] = null;
//有效元素减1
size--;
return true;
}
/**
* 根据指定的ID获取对应的Student类对象
* @param id 指定的ID
* @return 返回对应的Student类对象,如果没有找到,返回null
*/
public Student get(int id) {
int index = findIndexById(id);
return index > -1 ? allStus[index] : null;
}
/**
* 方法对allStus数组保存的学生类对象的成绩进行从高到底排序(选择排序法)
*/
public void descendingSort() {
for (int i = 0; i < size - 1; i++) {
int maxIndex = i;
for (int j = i + 1; j < size; j++) {
if (allStus[maxIndex].getScore() < allStus[j].getScore()) {
maxIndex = j;
}
}
if (maxIndex != i) {
Student temp = allStus[i];
allStus[i] = allStus[maxIndex];
allStus[maxIndex] = temp;
}
}
}
/**
* 类内私有化方法,用于在添加元素过程中,出现当前底层数组容量不足的情况下对底层数组进行扩容操作,满足使用要求
* @param minCapacity 添加操作要求的最小容量
*/
private void grow(int minCapacity) {
//1、获取原数组容量
int oldCapacity = allStus.length;
//2、计算得到新数组容量,新数组容量大约是原数组的1.5倍
int newCapacity = oldCapacity + oldCapacity / 2;
//3、判断新数组容量是否满足最小容量要求
if (minCapacity > newCapacity) {
newCapacity = minCapacity;
}
//4、判断当前容量是否超出了MAX_VALUE_SIZE
if (newCapacity > MAX_ARRAY_SIZE) {
/*这里需要一个异常处理,目前采用程序退出*/
}
//5、创建新数组
Student[] temp = new Student[newCapacity];
//6、数据拷贝
for (int i = 0; i < oldCapacity; i++) {
temp[i] = allStus[i];
}
//7、使用allStus保存新数组首地址
allStus = temp;
}
/**
* 类内私有化方法,用于根据指定id,获取对应下标位置
* @param id 用户指定id
* @return 返回下标
*/
private int findIndexById(int id) {
int index = -1;
//遍历数组的终止条件为size,有效元素个数
for (int i = 0; i < size; i++) {
if (id == allStus[i].getId()) {
index = i;
break;
}
}
return index;
}
public void showAllStudents() {
for (int i = 0; i < size; i++) {
System.out.println(allStus[i]);
}
}
}
方法功能测试:
package com.qfedu.student.system.testsystem;
import org.junit.Test;
import com.qfedu.student.system.entity.Student;
import com.qfedu.student.system.manager.StudentManager;
public class StudentTest {
@Test
public void testAdd() {
System.out.println("add方法测试");
StudentManager stm = new StudentManager();
stm.add(new Student(1, "小明", 16, '男', 90.0F));
stm.add(new Student(2, "小王", 16, '女', 86.5F));
stm.add(new Student(3, "小黄", 16, '男', 98.0F));
stm.add(new Student(4, "小郑", 15, '男', 59.5F));
stm.add(new Student(5, "小赵", 17, '女', 78.6F));
stm.add(new Student(6, "小孙", 17, '女', 66.0F));
stm.add(new Student(7, "小李", 13, '女', 100.0F));
stm.add(new Student(8, "小周", 16, '女', 100.0F));
stm.add(new Student(9, "小吴", 12, '女', 100.0F));
stm.add(new Student(10, "小张", 14, '女', 100.0F));
stm.add(new Student(11, "小贺", 16, '女', 100.0F));
stm.showAllStudents();
}
@Test
public void testRemove() {
System.out.println("remove方法测试");
StudentManager stm = new StudentManager();
stm.add(new Student(1, "小明", 16, '男', 90.0F));
stm.add(new Student(2, "小王", 16, '女', 86.5F));
stm.add(new Student(3, "小黄", 16, '男', 98.0F));
stm.add(new Student(4, "小郑", 15, '男', 59.5F));
stm.add(new Student(5, "小赵", 17, '女', 78.6F));
stm.add(new Student(6, "小孙", 17, '女', 66.0F));
stm.add(new Student(7, "小李", 13, '女', 100.0F));
stm.add(new Student(8, "小周", 16, '女', 100.0F));
stm.add(new Student(9, "小吴", 12, '女', 100.0F));
stm.add(new Student(10, "小张", 14, '女', 100.0F));
stm.add(new Student(11, "小贺", 16, '女', 100.0F));
stm.remove(3);
stm.remove(6);
stm.remove(9);
stm.showAllStudents();
}
@Test
public void testGet() {
System.out.println("get方法测试");
StudentManager stm = new StudentManager();
stm.add(new Student(1, "小明", 16, '男', 90.0F));
stm.add(new Student(2, "小王", 16, '女', 86.5F));
stm.add(new Student(3, "小黄", 16, '男', 98.0F));
stm.add(new Student(4, "小郑", 15, '男', 59.5F));
stm.add(new Student(5, "小赵", 17, '女', 78.6F));
stm.add(new Student(6, "小孙", 17, '女', 66.0F));
stm.add(new Student(7, "小李", 13, '女', 100.0F));
stm.add(new Student(8, "小周", 16, '女', 100.0F));
stm.add(new Student(9, "小吴", 12, '女', 100.0F));
stm.add(new Student(10, "小张", 14, '女', 100.0F));
stm.add(new Student(11, "小贺", 16, '女', 100.0F));
System.out.println(stm.get(3));
System.out.println(stm.get(22));
}
@Test
public void testDescendingSort() {
System.out.println("descendingSort方法测试");
StudentManager stm = new StudentManager();
stm.add(new Student(1, "小明", 16, '男', 90.0F));
stm.add(new Student(2, "小王", 16, '女', 86.5F));
stm.add(new Student(3, "小黄", 16, '男', 98.0F));
stm.add(new Student(4, "小郑", 15, '男', 59.5F));
stm.add(new Student(5, "小赵", 17, '女', 78.6F));
stm.add(new Student(6, "小孙", 17, '女', 66.0F));
stm.add(new Student(7, "小李", 13, '女', 100.0F));
stm.add(new Student(8, "小周", 16, '女', 100.0F));
stm.add(new Student(9, "小吴", 12, '女', 100.0F));
stm.add(new Student(10, "小张", 14, '女', 100.0F));
stm.add(new Student(11, "小贺", 16, '女', 100.0F));
stm.descendingSort();
stm.showAllStudents();
}
}
输出结果:
add方法测试
Student [id=1, name=小明, age=16, gender=男, score=90.0]
Student [id=2, name=小王, age=16, gender=女, score=86.5]
Student [id=3, name=小黄, age=16, gender=男, score=98.0]
Student [id=4, name=小郑, age=15, gender=男, score=59.5]
Student [id=5, name=小赵, age=17, gender=女, score=78.6]
Student [id=6, name=小孙, age=17, gender=女, score=66.0]
Student [id=7, name=小李, age=13, gender=女, score=100.0]
Student [id=8, name=小周, age=16, gender=女, score=100.0]
Student [id=9, name=小吴, age=12, gender=女, score=100.0]
Student [id=10, name=小张, age=14, gender=女, score=100.0]
Student [id=11, name=小贺, age=16, gender=女, score=100.0]
get方法测试
Student [id=3, name=小黄, age=16, gender=男, score=98.0]
null
descendingSort方法测试
Student [id=7, name=小李, age=13, gender=女, score=100.0]
Student [id=8, name=小周, age=16, gender=女, score=100.0]
Student [id=9, name=小吴, age=12, gender=女, score=100.0]
Student [id=10, name=小张, age=14, gender=女, score=100.0]
Student [id=11, name=小贺, age=16, gender=女, score=100.0]
Student [id=3, name=小黄, age=16, gender=男, score=98.0]
Student [id=1, name=小明, age=16, gender=男, score=90.0]
Student [id=2, name=小王, age=16, gender=女, score=86.5]
Student [id=5, name=小赵, age=17, gender=女, score=78.6]
Student [id=6, name=小孙, age=17, gender=女, score=66.0]
Student [id=4, name=小郑, age=15, gender=男, score=59.5]
remove方法测试
Student [id=1, name=小明, age=16, gender=男, score=90.0]
Student [id=2, name=小王, age=16, gender=女, score=86.5]
Student [id=4, name=小郑, age=15, gender=男, score=59.5]
Student [id=5, name=小赵, age=17, gender=女, score=78.6]
Student [id=7, name=小李, age=13, gender=女, score=100.0]
Student [id=8, name=小周, age=16, gender=女, score=100.0]
Student [id=10, name=小张, age=14, gender=女, score=100.0]
Student [id=11, name=小贺, age=16, gender=女, score=100.0]