package com.zhang.test;
import java.util.ArrayList;
import java.util.Scanner;
/**
* @author zph
* @version 1.0
*/
public class StudentSystem {
// 学生信息容器
public static ArrayList<Student> studentList = new ArrayList<>();
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
// 标记变量,用来退出系统
boolean flag = true;
while(flag){
// 生成主面板
masterPanel();
// 录入用户输入数据
int input = s.nextInt();
// 检查用户录入数据
check(input);
switch (input){
// 添加学生
case 1:
boolean flag1 = addStudent(s);
if (!flag1){
System.out.println("添加学生的id重复,请重新输入!");
break;
}
System.out.println("添加学生信息成功!");
break;
// 删除学生
case 2:
while (true){
System.out.println("请输入你要删除学生的id:");
String id = s.next();
boolean boo = deleteStudent(id);
if (boo){
break;
}
}
break;
// 修改学生
case 3:
boolean b = updateStudent(s);
if (!b){
System.out.println("要修改的学生id不存在,请重新输入!");
break;
}
System.out.println("修改学生信息成功!");
break;
// 查询学生
case 4:
selectStudent();
break;
// 退出系统
case 5:
System.out.println("退出系统成功!欢迎下次光临");
// System.exit(0);
flag = false;
}
}
}
/**
* 生成主面板
*/
public static void masterPanel(){
System.out.println("\"----------欢迎来到学生管理系统----------\"");
System.out.println("\"1: 添加学生\"");
System.out.println("\"2: 删除学生\"");
System.out.println("\"3: 修改学生\"");
System.out.println("\"4: 查询学生\"");
System.out.println("\"5: 退出系统\"");
System.out.println("\"请输入你的选择:(1-5)\"");
}
/**
* 对用户录入数据进行简单校验
* @param input
*/
public static void check(int input){
if (!(input >=1 && input <= 5)){
System.out.println("你输入的数据有误,请重新输入!");
}
}
/**
* 检查id是否重复
* @param list
* @param id
* @return
*/
public static boolean checkId(ArrayList<Student> list,String id){
for (Student student : list) {
if (student.getId().equals(id)){
return false;
}
}
return true;
}
/**
* 根据学生id找到在集合中的索引
* @param id
* @return
*/
public static int findIndex(String id){
for (int i = 0; i < studentList.size(); i++) {
if (studentList.get(i).getId().equals(id)){
return i;
}
}
return -1;
}
/**
* 添加学生
* @param s
* @return
*/
public static boolean addStudent(Scanner s){
Student student = new Student();
System.out.println("请输入学生ID:");
String id = s.next();
boolean flag = checkId(studentList, id);
if (!flag){
return false;
}
student.setId(id);
System.out.println("请输入学生姓名:");
String name = s.next();
student.setName(name);
System.out.println("请输入学生年龄:");
int age = s.nextInt();
student.setAge(age);
System.out.println("请输入学生的家庭地址:");
String address = s.next();
student.setAddress(address);
studentList.add(student);
return true;
}
/**
* 查询所有学生信息
*/
public static void selectStudent(){
if (studentList.size() == 0){
System.out.println("当前无学生信息~~~");
}
System.out.println("-------------------学生信息表-------------------");
System.out.println("id\t\t\t\t姓名\t\t\t年龄\t\t\t家庭住址");
for (Student student : studentList) {
System.out.println(student.getId()+"\t\t"
+student.getName()+"\t\t\t"
+student.getAge()+"\t\t\t"
+student.getAddress()
);
}
}
/**
* 删除学生
* @param id
* @return
*/
public static boolean deleteStudent(String id){
int index = findIndex(id);
if (index < 0){
System.out.println("id不存在,请重新输入要删除学生的id");
return false;
}else {
studentList.remove(index);
System.out.println("删除学生成功!");
return true;
}
}
/**
* 修改学生信息
* @param s
* @return
*/
public static boolean updateStudent(Scanner s){
Student student = new Student();
System.out.println("请输入你要修改的学生ID:");
String id = s.next();
int index = findIndex(id);
boolean flag = checkId(studentList, id);
if (flag){
return false;
}
student.setId(id);
System.out.println("请输入学生姓名:");
String name = s.next();
student.setName(name);
System.out.println("请输入学生年龄:");
int age = s.nextInt();
student.setAge(age);
System.out.println("请输入学生的家庭地址:");
String address = s.next();
student.setAddress(address);
studentList.set(index,student);
return true;
}
}
/**
* 学生类
*/
class Student{
private String id;
private String name;
private int age;
private String address;
public Student() {
}
public Student(String id, String name, int age, String address) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
/**
* 获取
* @return id
*/
public String getId() {
return id;
}
/**
* 设置
* @param id
*/
public void setId(String id) {
this.id = id;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(int age) {
this.age = age;
}
/**
* 获取
* @return address
*/
public String getAddress() {
return address;
}
/**
* 设置
* @param address
*/
public void setAddress(String address) {
this.address = address;
}
public String toString() {
return "Student{id = " + id + ", name = " + name + ", age = " + age + ", address = " + address + "}";
}
}