import java.util.ArrayList;
import java.util.Scanner;
public class StudentTest {
public static void main(String[] args) {
//初始界面
Welcome();
ArrayList<Student> students = new ArrayList<>();
Scanner sc = new Scanner(System.in);
String choice;
do {
System.out.println("请输入您的选择:");
System.out.println("1.添加学生 2.删除学生 3.修改学生 4.查询学生 5.退出");
choice = sc.next();
switch (choice) {
case "添加学生":
addStudent(students);break;
case "删除学生":
delStudent(students);break;
case "修改学生":
modifyStudent(students);break;
case "查询学生":
QueryStudent(students);break;
case "退出":
Exit();break; //System.exit(0);
default:
System.out.println("输入有误请重新操作:");break;
}
}while (!choice.equals("退出"));
}
//初始界面
public static void Welcome(){
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("请输入您的选择:");
}
//检查学生ID
public static boolean checkId(ArrayList<Student> students,String id){
Scanner sc = new Scanner(System.in);
Student stu = new Student();
for(int i = 0; i < students.size(); i++){
stu = students.get(i);
if(id.equals(stu.getId())){
return false;
}
}
return true;
}
//添加学生
public static void addStudent(ArrayList<Student> students){
Scanner sc = new Scanner(System.in);
Student stu = new Student();
System.out.println("请输入学生学号:");
String id = sc.next();
boolean flag =checkId(students,id);
if(flag){
stu.setId(id);
System.out.println("请输入学生姓名:");
String name = sc.next();
stu.setName(name);
System.out.println("请输入学生年龄:");
int age = sc.nextInt();
stu.setAge(age);
System.out.println("请输入学生家庭住址:");
String address = sc.next();
stu.setAddress(address);
students.add(stu);
System.out.println("添加成功");
}else{
System.out.println("该学生学号已存在");
}
}
//删除学生
public static void delStudent(ArrayList<Student> students){
Scanner sc = new Scanner(System.in);
Student stu = new Student();
System.out.println("请输入要删除的学生的学号");
String id = sc.next();
boolean flag =checkId(students,id);
if(!flag){
for(int i = 0 ; i < students.size(); i++){
stu = students.get(i);
if(id.equals(stu.getId())){
students.remove(i);
System.out.println("删除成功");
}
}
}else{
System.out.println("要删除的学号信息不存在");
}
}
//修改学生信息
public static void modifyStudent(ArrayList<Student> students){
Scanner sc = new Scanner(System.in);
Student stu = new Student();
System.out.println("请输入要修改的学生学号:");
String id = sc.next();
boolean flag =checkId(students,id);
if(flag){
System.out.println("该学生学号不存在");
}else{
int index;
for(index = 0; index < students.size(); index++){
if(id.equals(students.get(index).getId())){
stu=students.get(index);break;
}
}
stu.setId(id);
System.out.println("请输入学生姓名:");
String name = sc.next();
stu.setName(name);
System.out.println("请输入学生年龄:");
int age = sc.nextInt();
stu.setAge(age);
System.out.println("请输入学生家庭住址:");
String address = sc.next();
stu.setAddress(address);
students.set(index, stu);
System.out.println("修改成功");
}
}
//查询学生
public static void QueryStudent(ArrayList<Student> students){
Scanner sc= new Scanner(System.in);
Student stu= new Student();
System.out.println("请输入要查询的学生学号:");
String id = sc.next();
boolean flag =checkId(students,id);
if(flag){
System.out.println("没有该学生学号");
}else{
int index;
for(index = 0; index < students.size(); index++){
if(id.equals(students.get(index).getId())){
stu = students.get(index);break;
}
}
OutPut(stu);
}
}
//输出单个学生信息
public static void OutPut(Student stu){
System.out.println("该学生的信息为");
System.out.println("学号:"+stu.getId()+",姓名:"+stu.getName()+",年龄:"+stu.getAge()+",住址:"+stu.getAddress());
}
//退出系统
public static void Exit(){
System.out.println("你已退出学生管理系统");
}
}
public 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;
}
public void setAddress(String address) {
this.address = address;
}public String getAddress() {
return address;
}
public void setAge(int age) {
this.age = age;
}public int getAge() {
return age;
}
public void setId(String id) {
this.id = id;
}public String getId() {
return id;
}
public void setName(String name) {
this.name = name;
}public String getName() {
return name;
}
}