import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Manage {
//存储学生容器
public static Map<String, Student> studentIco = new HashMap<>();
//接收屏幕上输入的内容
public static Scanner scanner = new Scanner(System.in);
/**
* 菜单
*
* @return
*/
public static String menu() {
System.out.println(" 1、添加学生");
System.out.println(" 2、修改学生");
System.out.println(" 3、删除学生");
System.out.println(" 4、查询学生");
System.out.println(" 5、退出");
System.out.println("请输入菜单id:");
//开关,用于判断输入是否正确
boolean switcha = true;
//接收选择的菜单,
String menuIndex;
//region 数据校验
do {
menuIndex = scanner.next();
if (CheckUtil.checkIsIntNumber(menuIndex)) {
if ("12345".contains(menuIndex)) {
switcha = false;
break;
}
}
System.out.println("输入菜单id有误,请重新输入:");
} while (switcha);
//endregion
return menuIndex;
}
/**
* 添加学生
*/
public static boolean addStudent() {
//获取输入的学生信息
System.out.println("请输入学生学号:");
String studentNum = scanner.next();
//数据校验
while (studentIco.get(studentNum) != null) {
System.out.println("学号重复,请重新输入学号:");
studentNum = scanner.next();
}
System.out.println("请输入学生姓名:");
String studentName = scanner.next();
System.out.println("请输入学生语文成绩:");
String studentChinese = scanner.next();
//数据校验
while (!CheckUtil.checkIsDoubleNumber(studentChinese)) {
System.out.println("语文成绩输入错误,请重新输入:");
studentChinese = scanner.next();
}
System.out.println("请输入学生数学成绩:");
String studentMath = scanner.next();
//数据校验
while (!CheckUtil.checkIsDoubleNumber(studentMath)) {
System.out.println("数学成绩输入错误,请重新输入:");
studentMath = scanner.next();
}
System.out.println("请输入学生英语成绩:");
String studentEnglinsh = scanner.next();
//数据校验
while (!CheckUtil.checkIsDoubleNumber(studentEnglinsh)) {
System.out.println("英语成绩输入错误,请重新输入:");
studentEnglinsh = scanner.next();
}
//创建学生对象
Student student = new Student();
student.num = studentNum;
student.name = studentName;
student.chineseAch = Double.valueOf(studentChinese);
student.mathAch = Double.valueOf(studentMath);
student.englishAch = Double.valueOf(studentEnglinsh);
//存储
studentIco.put(studentNum, student);
return true;
}
/**
* 修改学生
*/
public static boolean editStudent() {
//学生原始信息
Student student=null;
//获取输入的学生信息
System.out.println("请输入需要修改的学生学号:");
String studentNum = scanner.next();
//数据校验
while ((student = studentIco.get(studentNum)) == null) {
System.out.println("学号不存在,请重新输入学号(输入q退出):");
studentNum = scanner.next();
if ("q".equals(studentNum)) {
return false;
}
}
System.out.println("请输入学生姓名(输入n不修改):");
String studentName = scanner.next();
if ("n".equals(studentName)) {
studentName = student.name;
}
System.out.println("请输入学生语文成绩(输入n不修改):");
String studentChinese = scanner.next();
while (true) {
if ("n".equals(studentChinese)) {
studentChinese = String.valueOf(student.chineseAch);
break;
}
//数据校验
if (!CheckUtil.checkIsDoubleNumber(studentChinese)) {
System.out.println("语文成绩输入错误,请重新输入:");
studentChinese = scanner.next();
}else{
break;
}
}
System.out.println("请输入学生数学成绩(输入n不修改):");
String studentMath = scanner.next();
while (true) {
if ("n".equals(studentMath)) {
studentMath = String.valueOf(student.mathAch);
break;
}
//数据校验
if (!CheckUtil.checkIsDoubleNumber(studentMath)) {
System.out.println("数学成绩输入错误,请重新输入:");
studentMath = scanner.next();
}else{
break;
}
}
System.out.println("请输入学生英语成绩(输入n不修改):");
String studentEnglinsh = scanner.next();
while (true) {
if ("n".equals(studentEnglinsh)) {
studentEnglinsh = String.valueOf(student.englishAch);
break;
}
//数据校验
if (!CheckUtil.checkIsDoubleNumber(studentEnglinsh)) {
System.out.println("英语成绩输入错误,请重新输入:");
studentEnglinsh = scanner.next();
}else{
break;
}
}
//修改学生信息
student.name = studentName;
student.chineseAch = Double.valueOf(studentChinese);
student.mathAch = Double.valueOf(studentMath);
student.englishAch = Double.valueOf(studentEnglinsh);
studentIco.put(student.num, student);
return true;
}
/**
* 删除学生
*/
public static boolean delStudent() {
System.out.println("请输入需要修改的学生学号:");
String studentNum = scanner.next();
//数据校验
while (studentIco.get(studentNum) == null) {
System.out.println("学号不存在,请重新输入学号(输入q退出):");
studentNum = scanner.next();
if ("q".equals(studentNum)) {
return false;
}
}
studentIco.remove(studentNum);
return true;
}
/**
* 查询所有学生
*/
public static boolean selectStudent() {
System.out.println("学号 姓名 语文成绩 数学成绩 英语成绩");
Collection<Student> students = studentIco.values();
for (Student student : students) {
System.out.printf("%s %s %s %s %s", student.num, student.name, student.chineseAch, student.mathAch, student.englishAch);
System.out.println();
}
return true;
}
/**
* 登录
*
* @return
*/
public static boolean login() throws IOException {
System.out.println("请输入用户名:");
String username = scanner.next();
System.out.println("请输入密码:");
String password = scanner.next();
//写死用户名密码均为root
if (!("root".equals(username) && "root".equals(password))) {
System.out.println("用户名密码错误");
return false;
}
return