package com.lovo.manager;
import java.util.Scanner;
/**
* 学生管理
*
* @author 向往的生活
*
*/
public class StudentManager {
public static void main(String[] args) {
String[] user = new String[10];
int[] password = new int[10];
while (true) {
// 菜单
System.out.println("A 注册");
System.out.println("B 登录");
System.out.println("C 退出");
System.out.println("请选择菜单:");
Scanner scan = new Scanner(System.in);
String operator = scan.nextLine();
operator = operator.toUpperCase();// 大小写互转
// "SFCDS" 转换为一个一个的‘S‘,‘F‘,‘C‘,‘D‘,‘S‘
char[] chr = operator.toCharArray();// 把字符串(String)转化为char数组
switch (chr[0]) {
case ‘A‘:
register(user, password);
break;
case ‘B‘:
login(user, password);
break;
case ‘C‘:
System.out.println("退出系统!");
System.exit(-1);
break;
default:
System.out.println("操作错误,请从新输入!");
break;
}
}
}
/**
* 登录
*/
public static void login(String[] user, int[] password) {
System.out.println("输入用户名:");
Scanner input = new Scanner(System.in);
String userName = input.nextLine();
System.out.println("输入密码:");
int pwd = input.nextInt();
int index = -1;
for (int i = 0; i < user.length; i++) {
if (userName.equals(user[i]) && pwd == password[i]) {// 如果用户名与密码匹配
System.out.println("登录成功!");
studentMenu(userName);
index = i;
break;
}
}
if (index == -1) {// 如果没有找到下标,则登录失败
System.out.println("登录失败!");
return;
}
}
/**
* 注册
*/
public static void register(String[] user, int[] password) {
System.out.println("请输入用户名");
Scanner scan = new Scanner(System.in);
String userName = scan.nextLine();
System.out.println("请输入密码");
int pwd = scan.nextInt();
// 字符串中默认的数据是null,循环检查,一旦遇到null,
// 就输入用户名和密码,把值赋值进去。
for (int i = 0; i < user.length; i++) {
if (user[i] == null) {
user[i] = userName;
password[i] = pwd;
break;
}
}
}
/**
* 学生菜单
*/
public static void studentMenu(String user) {
// 学生姓名与分数
String[] studentName = new String[2];
int[] score = new int[2];
boolean flag = false;
while (true) {
System.out.println("***欢迎您" + user + "***");
System.out.println("A 新增学生成绩");
System.out.println("B 修改学生成绩");
System.out.println("C 删除学生成绩");
System.out.println("D 查询学生成绩");
System.out.println("E 返回上级菜单");
System.out.println("请选择菜单:");
Scanner input = new Scanner(System.in);
String operator = input.nextLine();
operator = operator.toUpperCase();
char op = operator.toCharArray()[0];
switch (op) {
case ‘A‘:
addInfo(studentName, score);
break;
case ‘B‘:
updateInfo(studentName, score);
break;
case ‘C‘:
deleteInfo(studentName, score);
break;
case ‘D‘:
queryInfo(studentName, score);
break;
case ‘E‘:
flag = true;
break;// 退出这个switch,如果在while中不多增加一个break,则会继续循环不会返回上一级菜单
default:
System.out.println("操作错误,请从新输入!");
break;
}
if (flag) {
break;// 退出这个while,就返回到了登陆菜单
}
}
}
/**
* 新增学生信息
*
* @param studentName
* @param score
*/
public static void addInfo(String[] studentName, int[] score) {
for (int i = 0; i < studentName.length; i++) {
System.out.println("请输入学生姓名:");
Scanner scan = new Scanner(System.in);
String stName = scan.nextLine();
studentName[i] = stName;
System.out.println("请输入学生成绩:");
int scr = scan.nextInt();
score[i] = scr;
}
}
/**
* 修改学生姓名
*/
public static void updateInfo(String[] studentName, int[] score) {
System.out.println("请输入学生姓名:");
Scanner scan = new Scanner(System.in);
String stName = scan.nextLine();
// 通过名字修改分数
int index = -1;
for (int i = 0; i < score.length; i++) {// 通过遍历,输入的数与数组中的数据元素比较
if (stName.equals(studentName[i])) {// 如果找到这个学生姓名,就记录下标
index = i;
}
}
// 输入一个分数
System.out.println("请输入学生成绩:");
int scr = scan.nextInt();
score[index] = scr;// 把输入的成绩赋值给,index记录找到的这个 下标
}
/**
* 删除员工数据
*/
public static void deleteInfo(String[] studentName, int[] score) {
// 删除人名、删除分数
System.out.println("请输入学生姓名:");
Scanner scan = new Scanner(System.in);
String stName = scan.nextLine();
boolean flag = false;
for (int i = 0; i < studentName.length; i++) {
if (studentName.length - 1 == i) { // 当前下标是最后一个的话,就退出循环
studentName[i] = null;
score[i] = 0;
break;
}
if (stName.equals(studentName[i]) || flag) { // 如果发现这个名字,就用当前数组元素的下一个元素,向前移动替换当前这个元素
flag = true;
studentName[i] = studentName[i + 1];
score[i] = score[i + 1];
}
}
for (int i = 0; i < studentName.length; i++) {
System.out.println("学生姓名=" + studentName[i] + ",成绩=" + score[i]);
}
}
/**
* 查询学生信息
*/
public static void queryInfo(String[] studentName, int[] score) {
boolean flag = false;
while (true) {
System.out.println("A 查询单个学生信息");
System.out.println("B 查询所有学生信息");
System.out.println("C 返回上级菜单");
System.out.println("请选择菜单:");
Scanner scan = new Scanner(System.in);
String operator = scan.nextLine();
operator = operator.toUpperCase();// 大小写互转
char chr = operator.toCharArray()[0];
switch (chr) {
case ‘A‘:
querySinglStudentInfo(studentName, score);
break;
case ‘B‘:
queryAllStudentInfo(studentName, score);
break;
case ‘C‘:
flag = true;
break;
default:
System.out.println("操作错误,请从新输入!");
break;
}
if(flag) {
break;
}
}
}
/**
* 查询单个学生信息
*/
public static void querySinglStudentInfo(String[] studentName, int[] score) {
System.out.println("请输入学生姓名:");
Scanner scan = new Scanner(System.in);
String stName = scan.nextLine();
for (int i = 0; i < studentName.length; i++) {
if (stName.equals(studentName[i])) {
System.out.println("学生:" + studentName[i] + ",成绩:" + score[i]);
break;
}
}
}
/**
* 查询所有学生信息
*/
public static void queryAllStudentInfo(String[] studentName, int[] score) {
for (int i = 0; i < studentName.length; i++) {
System.out.println("学生:" + studentName[i] + ",成绩:" + score[i]);
}
}
}