import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class Admin {
static Map map=new HashMap();
static Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
while (true) {
System.out.println("========欢迎光临学生信息管理系统=========");
System.out.println("1、添加学生信息");
System.out.println("2、删除学生信息");
System.out.println("3、修改学生信息");
System.out.println("4、查询学生信息");
System.out.println("5、退出系统");
try {
System.out.print("=====>请输入选项:");
int choose = sc.nextInt();
switch (choose) {
case 1:
add();
break;
case 2:
delete();
break;
case 3:
update();
break;
case 4:
query();
break;
case 5:
System.exit(0);
break;
default:
System.out.println("选项无效,请重新选择");
break;
}
} catch (Exception e) {
// TODO: handle exception
System.out.println("选项无效,请核查选项");
System.exit(0);
}
}
}
public static void add() {
//System.out.println("添加");
System.out.println("请输入学生姓名:");
String name=sc.next();
System.out.println("请输入学生年龄:");
int age=sc.nextInt();
System.out.println("请输入学生学号:");
int id=sc.nextInt();
map.put(id, new Student(name,age,id));
System.out.println("添加后学生信息为:");
query();
}
public static void delete() {
//System.out.println("删除");
System.out.println("请输入要删除学生学号");
int delID=sc.nextInt();
map.remove(delID);
System.out.println("删除后为:");
query();
}
public static void update() {
//System.out.println("修改");
System.out.println("请输入要修改的学生学号");
int udID=sc.nextInt();
System.out.println("请输入修改后学生姓名:");
String name=sc.next();
System.out.println("请输入修改后学生年龄:");
int age=sc.nextInt();
map.put(udID, new Student(name,age,udID));
System.out.println("修改后学生信息");
query();
}
public static void query() {
//System.out.println("查看");
Set> set=map.entrySet();
Iterator> it=set.iterator();
while(it.hasNext()) {
Map.Entry entry= it.next();
System.out.println(entry.getValue().toString());
}
System.out.println();
System.out.println();
}
}
class Student{
String name;
int age;
int id;
public Student(String name,int age,int id) {
// TODO Auto-generated constructor stub
this.name=name;
this.age=age;
this.id=id;
}
@Override
public String toString() {
//String s="学号:"+id+"\t姓名:"+name+"\t年龄:"+age;
return "学生 :姓名 " + name + ", 年龄 " + age + ", 学号 " + id ;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}