超级简单的学生成绩管理系统
1. 底层实现
底层以数组实现对学生成绩记录的增删改查
2. 亮点
- 工具类中使用类正则表达式
- 在Dao模块使用类Stream流
3. 实现的功能
- 录入成绩
- 删除成绩
- 修改成绩
- 全部成绩
- 成绩排序
- 及格学生
- 姓名查找
- 退 出
4. 代码
1. 目录结构
2. model模块
package com.renshuming.model;
public class StudentScore {
private int id;
private String name;
private char gender;
private int score;
public StudentScore() {
}
public StudentScore(String name, char gender, int score) {
this.name = name;
this.gender = gender;
this.score = score;
}
public StudentScore(int id, String name, char gender, int score) {
this.id = id;
this.name = name;
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 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 id + "\t" + name + "\t" + gender + "\t" + score;
}
}
3. Dao模块
package com.renshuming.dao;
import java.util.Arrays;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import com.renshuming.model.StudentScore;
public class StudentScoreDao {
private final int MAX_SIZE;
private StudentScore[] students;
private int count = 0;
public StudentScoreDao(int size) {
this.MAX_SIZE = size;
this.students = new StudentScore[MAX_SIZE];
}
public boolean add(StudentScore student) {
if(count == MAX_SIZE ||
getIndexId(student.getId()) != -1) return false;
students[count++] = student;
return true;
}
public boolean delete(int id) {
if(getIndexId(id) == -1) return false;
for(int i = getIndexId(id); i < count-1; i++) {
students[i] = students[i+1];
}
students[--count] = null;
return true;
}
private int getIndexId(int id) {
for(int i = 0; i < count; i++) {
if(students[i].getId() == id) return i;
}
return -1;
}
public boolean update(StudentScore student) {
if(getIndexId(student.getId()) == -1) return false;
students[getIndexId(student.getId())] = student;
return true;
}
public StudentScore find(int id) {
if(getIndexId(id) == -1) return null;
return students[getIndexId(id)];
}
public StudentScore[] list() {
if(count == 0) return null;
StudentScore[] results = Arrays.copyOf(students, count);
return results;
}
public StudentScore[] sortedByScore() {
if(count == 0) return null;
StudentScore[] results = Arrays.copyOf(list(), count);
bubbleSort(results);
return results;
}
// 及格学生
public StudentScore[] passStudent() {
// int nums = 0;
// for(int i = 0; i < count; i++) {
// if(students[i].getScore() >= 60) nums++;
// }
// if(nums == 0) return null;
// StudentScore[] newStudents = new StudentScore[nums];
// int len = 0;
// for(int i = 0; i < count; i++) {
// if(students[i].getScore() >= 60) {
// newStudents[len++] = students[i];
// }
// }
// return newStudents;
// 流实现
StudentScore[] results = Arrays.copyOf(students, count);
return Arrays.stream(results)
.filter(stu -> stu.getScore() >= 60)
.collect(Collectors.toList())
.toArray(new StudentScore[students.length]);
}
public StudentScore[] findByName(String name) {
int nums = 0;
if(count == 0) return null;
for(int i = 0; i < count; i++) {
if(students[i].getName().equals(name)) nums++;
}
if(nums == 0) return null;
StudentScore[] newstudents = new StudentScore[nums];
int len = 0;
for(int i = 0; i < count; i++) {
if(students[i].getName().equals(name)) {
newstudents[len++] = students[i];
}
}
return newstudents;
}
private void bubbleSort(StudentScore[] results) {
for(int i = 1; i < results.length; i++) {
for(int j = 0; j < results.length-i; j++) {
if(results[j].getScore() > results[j+1].getScore()) {
StudentScore temp = results[j];
results[j] = results[j+1];
results[j+1] = temp;
}
}
}
}
}
4. View模块
package com.renshuming.view;
import com.renshuming.dao.StudentScoreDao;
import com.renshuming.model.StudentScore;
import com.renshuming.utils.Utility;
public class StudentScoreView {
private String tip;
private char key;
private final String head = "学号\t姓名\t性别\t总成绩";
private StudentScoreDao dao = new StudentScoreDao(5);
public void homePage() {
tip = "-------------------欢迎来到学生成绩管理系统-------------------\n"
+ "\t\t1 录入成绩\n"
+ "\t\t2 删除成绩\n"
+ "\t\t3 修改成绩\n"
+ "\t\t4 全部成绩\n"
+ "\t\t5 成绩排序\n"
+ "\t\t6 及格学生\n"
+ "\t\t7 姓名查找\n"
+ "\t\t0 退 出\n"
+ "请输入您的选择:";
System.out.print(tip);
key = Utility.readChar();
switch(key) {
case '1':
addPage();
break;
case '2':
deletePage();
break;
case '3':
updatePage();
break;
case '4':
listPage();
break;
case '5':
sortScorePage();
break;
case '6':
passStudentPage();
break;
case '7':
findByNamePage();
break;
case '0':
System.out.println("谢谢使用");
System.exit(0);
break;
default:
System.out.println("你的输入有误");
homePage();
}
}
private void findByNamePage() {
tip = "=========查找学生(0 退出)=========";
System.out.println(tip);
getAll();
System.out.print("请输入学生姓名:");
String name = Utility.readName();
if(name.equals("0")) {
homePage();
return;
}
if(dao.findByName(name) == null) {
System.out.println("不存在 " + name + " 的信息");
findByNamePage();
return;
}
System.out.println(head);
for(StudentScore student: dao.findByName(name)) {
System.out.println(student);
}
findByNamePage();
}
private void passStudentPage() {
tip = "===========及格学生===========";
System.out.println(tip);
if(dao.passStudent() == null) {
System.out.println("系统中及格成绩信息");
homePage();
return;
}
System.out.println(head);
for(StudentScore student: dao.passStudent()) {
System.out.println(student);
}
homePage();
}
private void sortScorePage() {
tip = "===========成绩排序===========";
System.out.println(tip);
if(dao.sortedByScore() == null) {
System.out.println("系统中没有成绩信息,请先录入");
homePage();
return;
}
System.out.println(head);
for(StudentScore student: dao.sortedByScore()) {
System.out.println(student);
}
homePage();
}
private void listPage() {
tip = "===========全部成绩===========";
getAll();
homePage();
}
private void updatePage() {
tip = "=========修改成绩(0 退出)=========";
System.out.println(tip);
getAll();
System.out.print("请输入学生学号:");
int id = Utility.readInt();
if(id == 0) {
homePage();
return;
}
if(dao.find(id) == null) {
System.out.println("不存在学号为 " + id + "的学生");
updatePage();
return;
}
System.out.print("请输入学生姓名("
+ dao.find(id).getName()
+ "): ");
String name = Utility.readName();
if(name.equals("0")) {
updatePage();
return;
}
System.out.print("请输入学生性别("
+ dao.find(id).getGender()
+ "):");
char gender = Utility.readGender();
if(gender == '0') {
updatePage();
return;
}
System.out.print("请输入学生成绩("
+ dao.find(id).getScore()
+ "):");
int score = Utility.readInt();
if(score == 0) {
updatePage();
return;
}
StudentScore student = new StudentScore(id, name, gender, score);
if(dao.update(student)) {
System.out.println("修改成功");
}else {
System.out.println("修改失败");
}
updatePage();
}
private void deletePage() {
tip = "=========删除成绩(0 退出)=========";
System.out.println(tip);
getAll();
System.out.print("请输入学生学号:");
int id = Utility.readInt();
if(id == 0) {
homePage();
return;
}
if(dao.delete(id)) {
System.out.println("删除成功");
}else {
System.out.println("不存在学号为" + id + "的学生");
}
deletePage();
}
private void addPage() {
tip = "=========录入成绩(0 退出)=========\n"
+ "请输入学生学号:";
System.out.print(tip);
int id = Utility.readInt();
if(id == 0) {
homePage();
return;
}
System.out.print("请输入学生姓名: ");
String name = Utility.readName();
if(name.equals("0")) {
addPage();
return;
}
System.out.print("请输入学生性别:");
char gender = Utility.readGender();
if(gender == '0') {
addPage();
return;
}
System.out.print("请输入学生成绩:");
int score = Utility.readInt();
if(score == 0) {
addPage();
return;
}
StudentScore student = new StudentScore(id, name, gender, score);
if(dao.add(student)) {
System.out.println("录入成功");
addPage();
return;
}else {
System.out.println("录入失败,系统已满,或学号" + id + "的学生已存在");
homePage();
return;
}
}
private void getAll() {
if(dao.list() == null) {
System.out.println("系统中没有成绩信息,请先录入");
homePage();
return;
}
System.out.println(head);
for (StudentScore student : dao.list()) {
System.out.println(student);
}
}
}
5. 工具类
package com.renshuming.utils;
import java.util.Scanner;
/**
* 工具类
* @author admin
*
*/
public class Utility {
// 构造器私有化,防止实例化
private Utility() {};
private static Scanner scanner = new Scanner(System.in);
/**
* 控制台读取int类型数据
* @return
*/
public static int readInt() {
String input = scanner.next();
while(!input.matches("\\d+")) {// 正则表达式匹配数字
System.out.println("输入必须是个整数,请重新输入");
input = scanner.next();
}
return Integer.parseInt(input);
}
/**
* 控制台读取char类型字符
* @return
*/
public static char readChar() {
String input = scanner.next();
while(input.length() != 1) {
System.out.println("输入必须只包含一个字符,请重新输入");
input = scanner.next();
}
return input.charAt(0);
}
/**
* 控制台读取字符串
* @return
*/
public static String readString() {
return scanner.next();
}
/**
* 控制台读取性别
* @return
*/
public static char readGender() {
String gender = scanner.next();
while(!(gender.equals("男") || gender.equals("女"))) {
System.out.println("性别只能为男或女,并且只能是一个字符,请重新输入");
gender = scanner.next();
}
return gender.charAt(0);
}
/**
* 控制台读取姓名
* @return
*/
public static String readName() {
String name = scanner.next();
// 正则表达式匹配汉字或字母且长度必须在[2,5]
while(!name.matches("([a-zA-Z\\u4e00-\\u9fa5]{2,5})")) {
System.out.println("姓名必须是字母或汉字,且长度必须在大于2,且小于等于5");
name = scanner.next();
}
return name;
}
}
6. 启动器
package com.renshuming;
import com.renshuming.view.StudentScoreView;
public class App {
public static void main(String[] args) {
new StudentScoreView().homePage();
}
}