学生实体类
/**
* 学生实体类
*
* @author 无形风
*
*/
public class Student {
private int id;
private String name;
private int age;
private char sex;
private int score;
public Student() {}
public Student(int id, String name, int age, char sex, int score) {
super();
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
this.score = score;
}
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 getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
/**
* 使用System.out.println打印展示Student类对象时
* 是直接自动调用toString方法,展示该方法返回String字符串内容
*/
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + ", score=" + score
+ "]";
}
}
import java.util.Scanner;
import student.system.entity.Student;
public class StudentManager {
/**
* 私有化保存学生信息的数组,对外不能公开,有且只针对当前管理类使用,初始化为null
*/
private Student[] allStus = null;
/**
* DEFAULT_CAPACITIY 默认容量,这里是一个带有名字的常量
* */
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
* */
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);
}
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 += 1;
return true;
}
/**
* 根据用户指定ID号,删除对应学生类对象
*
* @param id 指定学生的id
* return 成功返回true ,失败返回false
* */
public boolean remove(int id) {
int index = -1;
//遍历数组的终止条件为size,有效元素个数
for (int i = 0; i < size; i++) {
if (id == allStus[i].getId()) {
index = i;
break;
}
}
/*
* 以上代码循环结束,如果index的值为-1,证明没有找到对应的元素 当前方法无法进行删除操作,返回false
*/
if (-1 == index) {
System.out.println("Not Found!");
return false;
}
/*
* 如果index值不是-1,表示找到了对应需要删除的元素,进行删除操作
*
* 假设原数组容量10,有效元素个数为10,删除下标为5的元素 数组[5] = 数组[6]; 数组[6] = 数组[7]; 数组[7] = 数组[8];
* 数组[8] = 数组[9]; 数组[9] = null;
*
* 数组[i] = 数组[i + 1];
*/
for (int i = index; i < size - 1; i++) {
allStus[i] = allStus[i + 1];
}
// 原本最后一个有效元素位置赋值为null
allStus[size - 1] = null;
// 有效元素个数 - 1
size -= 1;
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;
}
/**
* 根据用户指定的ID号,修改对应学生的信息
*
* @param id 用户指定的ID号
* @return 方法运行成功返回true,如果修改无法完成返回false
*/
public boolean modify(int id) {
// 通过类内其他成员方法获取对应ID的学生类对象
Student stu = get(id);
// 没有对应的学生类对象
if (null == stu) {
System.out.println("Not Found");
return false;
}
// 类似于点菜系统,需要完成修改操作
Scanner sc = new Scanner(System.in);
int choose = 0;
while (choose != 5) {
System.out.println("ID: " + stu.getId());
System.out.println("Name: " + stu.getName());
System.out.println("Age: " + stu.getAge());
System.out.println("Gender: " + stu.getSex());
System.out.println("Score: " + 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();
// 1\n ==> 1 程序中剩余\n
sc.nextLine();
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 sex = sc.nextLine().charAt(0);
stu.setSex(sex);
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;
}
}
return 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 - 1; i++) {
// 找出极值
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++) {
System.out.println(sortTemp[i]);
}
}
/**
* 按照成绩升序排序的算法
*/
public void scoreSortAsc() {
/*
* 排序算法操作不能在原数据数组中进行直接操作,需要另外准备
* 一个用于排序的临时数组,这里就涉及到一个数据拷贝过程。
*/
Student[] sortTemp = new Student[size];
for (int i = 0; i < sortTemp.length; i++) {
sortTemp[i] = allStus[i];
}
for (int i = 0; i < sortTemp.length - 1; i++) {
// 找出极值
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++) {
System.out.println(sortTemp[i]);
}
}
/**
* 类内私有化方法,用于在添加元素过程中,出现当前底层数组容量不足的情况下 对底层数组进行扩容操作,满足使用要求
*
* @param minCapacity 添加操作要求的最小容量
*/
private void grow(int minCapacity) {
System.out.println("Grow Method Called....");
// 1. 获取原数组容量
int oldCapacity = allStus.length;
// 2. 计算得到新数组容量,新数组容量大约是原数组容量的1.5倍
int newCapacity = oldCapacity + oldCapacity / 2;
// 3. 判断新数组容量是否满足最小容量要求
if (minCapacity > newCapacity) {
newCapacity = minCapacity;
}
// 4. 判断当前容量是否超出了MAX_ARRAY_SIZE
if (newCapacity > MAX_ARRAY_SIZE) {
/* 这里需要一个异常处理,目前我们采用程序退出 */
System.exit(0);
}
// 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 返回值是对应的下标位置,返回值大于等于0 表示找到对应元素,返回-1没有找到
*/
private int findIndexById(int id) {
int index = -1;
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]);
}
}
}
import java.util.Scanner;
import student.system.entity.Student;
import student.system.manager.StudentManager;
public class MainProject {
public static void main(String[] args) {
StudentManager stm = new StudentManager();
for (int i = 0; i < 15; i++) {
Student student = new Student();
student.setId(i + 1);
student.setName("测试" + i);
student.setSex(Math.random() > 0.5 ? '男' : '女');
// 随机数
student.setAge((int) (Math.random() * 100));
student.setScore((int) (Math.random() * 100));
stm.add(student);
}
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:
/*
* 大家自己完成提供给用户一些输入提示,完成添加操作
*/
stm.add(new Student(100, "骚磊", 16, '男', 119));
break;
case 2:
System.out.println("请输入需要删除学生的ID号");
id = sc.nextInt();
stm.remove(id);
break;
case 3:
System.out.println("请输入需要修改信息的学生ID号");
id = sc.nextInt();
stm.modify(id);
break;
case 4:
System.out.println("请输入需要查询信息的学生ID号");
id = sc.nextInt();
Student student = stm.get(id);
if (null == student) {
System.out.println("查无此人");
} else {
System.out.println(student);
}
break;
case 5:
stm.showAllStudents();
break;
case 6:
stm.scoreSortDesc();
break;
case 7:
System.out.println("程序退出...");
break;
default:
System.out.println("选择错误");
break;
}
}
}