实体类:
package com.mhj.student.entity;
public class Student {
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;
}
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;
}
@Override
public 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 {
private Student[] allStus = null;
private static final int DEFAULT_CAPACITY = 10;
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
public StudentManager() {
allStus = new Student[DEFAULT_CAPACITY];
}
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;
public boolean add(Student stu) {
if(size == allStus.length) {
System.out.println("扩容");
grow(size + 1);
}
allStus[size] = stu;
size += 1;
return true;
}
private void grow(int minCapacity) {
int oldCapacity = allStus.length;
int newCapacity = oldCapacity + oldCapacity / 2;
if(newCapacity < minCapacity) {
newCapacity = minCapacity;
}
if(newCapacity > MAX_ARRAY_SIZE) {
System.exit(0);
}
Student[] temp = new Student[newCapacity];
for (int i = 0; i < oldCapacity; i++) {
temp[i] = allStus[i];
}
allStus = temp;
}
public boolean remove(int iD) {
int index = findIndexById(iD);
if (index < 0) {
System.out.println("未找到");
return false;
}
for (int i = index; i < size; i++) {
allStus[i] = allStus[i + 1];
}
allStus[size - 1] = null;
size -= 1;
return true;
}
public void print() {
for (int i = 0; i < size; i++) {
allStus[i].mprint();
}
}
public Student get(int id) {
int index = findIndexById(id);
if (index < 0) {
System.out.println("未找到");
return null;
}
allStus[index].mprint();
return allStus[index];
}
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 boolean modify(int id) {
Student stu = get(id);
if (null == stu) {
System.out.println("id信息有误");
return false;
}
Scanner sc = new Scanner(System.in);
int choose = 0;
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 (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;
}
}
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; 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++) {
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;
}
}
}
}
项目后期内容
- 数据持久化保存
IO操作文件内容 String字符串解析 - Sout界面分离
除了viewer层,其他任何代码中不能出现任何一个Sout - 排序算法优化
可以让排序方法能够根据用户指定条件进行排序操作 - 自定义异常添加
优化代码中错误提示和处理过程 - 数据根据条件过滤展示
根据用户指定条件,过滤展示对应的数据