package myprogrammer;
/*
简易的学生管理系统:
主要功能:
增
删
查
改
展示
退出系统
*/
/*
总结:在java中内存的管理和对容器类型的操作在java自带的java单元库中都有相关的操作,不需要人为进行函数的编写
只需要人为将这些功能利用和整合一下就行了,更加方便和易于维护,而且自带的容器类非常强大,性能稳定。
*/
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.*;
class Student3 {//创建一个Student的类,
//有点类似于是c语言中新写的一个结构体来的新类型
//学生的两个属性
String name;//学生的名字
int id;//学生的学号
Student3(String name, int id){//学生的一个构造方法:在新建学生引用的时候同时初始化
this.id = id;//给id属性赋值
this.name = name;//给名字属性赋值
}
}
public class 学生管理系统3 {//这是一整个系统:public的类
static LinkedList<Student3> list = new LinkedList<>();//新建一个学生链表
static void add(){ // 静态的add方法:用于添加学生
Student3 temp = new Student3("null",0);//先新建一个临时的学生变量:用于存储用户用的输入
System.out.println("please input the student's name");//提示用于输入名字
Scanner input = new Scanner(System.in);//获取读取工具用于接受用户的输入
temp.name = input.nextLine();//存储用户的输入
System.out.println("please input the student's id: ");//请求学号
temp.id = input.nextInt();//获取学号
list.add(temp);//添加这个学生到LinkedList
System.out.println("Ok,this student is added");//添加成功给用户提示
}
static void remove(){ //删除方法
int ret = 0;//c中的经典伪bool变量
System.out.println("input the student's id to remove:");//告诉用户:输入id来删除
Scanner input = new Scanner(System.in);//获取工具来读取
int temp = input.nextInt();//读取数字
Iterator<Student3> it = list.iterator();//生成一个迭代器
while(it.hasNext()){//使用迭代器遍历整个LinkedList
Student3 temp2 = it.next();//建立临时变量存储遍历到的元素
if(temp2.id == temp){//寻找符合条件的temp2
it.remove();//一旦符合条件就删除
ret = 1;//删除以后伪bool就设为1
break;//结束遍历
}
}
if(ret == 1){//如果删除成功
System.out.println("Ok,this student is removed");//就提示用户删除成功
}else{//否则
System.out.println("Sorry ,this student is not found");//就查无此人
}
}
static void search(){//查
int ret = 0;//还是经典的伪bool套路
System.out.println("input the student's id :");
Scanner input = new Scanner(System.in);//
int temp = input.nextInt();//得到id
Iterator<Student3> it = list.iterator();//取得迭代器
while(it.hasNext()){//遍历整个LinkedList
Student3 temp2 = it.next();
if(temp2.id == temp){//如果匹配用户的输入
System.out.println("Student's name:" +temp2.name + " , " + "Student's id: " + temp2.id);//就输出这个学生的信息
ret = 1;//然后这个伪bool的变量就为1
}
break;
}
if(ret == 0){//否则
System.out.println("Sorry , this student is not found!");//查无此人
}
}
static void update(){//改
int ret = 0;//老套路来的
System.out.println("Please input the old id: ");
Scanner input = new Scanner(System.in);
int old_id = input.nextInt();
input.nextLine();
Student3 temp = new Student3("null",0);
System.out.println("Please input new id:");
temp.id = input.nextInt();
input.nextLine();//使用nextLine能不能清除回车呢
//答案是可以的,但是要注意这个方法中有两个地方需要清除回车,只用一个nextLine无效!
System.out.println("Please input the new name of the student: ");
temp.name = input.nextLine();
Iterator<Student3> it = list.iterator();
while (it.hasNext()) {
Student3 temp2 = it.next();
if(temp2.id == old_id){
temp2.id = temp.id;
temp2.name = temp.name;
ret = 1;
System.out.println("Ok,update is ok!");
break;
}
}
if(ret == 0){
System.out.println("Sorry,this student is not found");
}
}
static void print(){
System.out.println("Student'ID Name");
for(Student3 s : list){
System.out.println(s.id + " " + s.name);
}
}
public static void main(String[] args) {
while(true) {//循环启动菜单展示,直到用户完成输入手动退出系统
System.out.println("input your order: like add, remove ,search, update, print and quit");
//给予用户这几个选项来实现学生的管理
String input = new Scanner(System.in).nextLine();//接收用户的指令
switch (input) {//这里使用switch代替c中的函数指针的实现方式
case "add":
add();
break;
case "remove":
remove();
break;
case "search":
search();
break;
case "update":
update();
break;
case "print":
print();
break;
case "quit":
System.out.println("Ok,this programmer will shut down!");
System.exit(0);
default:
System.out.println("shit dumd, shit!");//无效输入的负反馈
break;
}
}
}
}