package com.zzu.test;import java.util.HashSet;
import java.util.Scanner;import com.zzu.vo.Student;
public class Main {
private static HashSetset = new HashSet ();
public static void main(String[] args) {
System.out.println("*********************************");
System.out.println("*欢迎使用学生信息管理系统");
System.out.println("*********************************");
while (true) {
menu();
}
}
static void menu() {
System.out.println("1、添加学生信息");
System.out.println("2、删除学生信息");
System.out.println("3、修改学生信息");//地址传递
System.out.println("4、查询学生信息");//name
System.out.println("输入指令:");
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
int order = scanner.nextInt();
switch (order) {
case 1:{
System.out.print("学号:");
String id=scanner.next();
System.out.print("姓名:");
String name=scanner.next();
System.out.print("手机号:");
String mobile=scanner.next();
System.out.print("家庭住址:");
String address=scanner.next();
Student student=new Student();
student.setId(id);
if(set.contains(student)) {
System.out.println("已有学号,无法添加!");
return;
}
student.setName(name);
student.setMobile(mobile);
student.setAddress(address);
set.add(student);
System.out.println("添加成功");
}
break;
case 2:{
System.out.print("输入学号:");
String id=scanner.next();
Student student=new Student();
student.setId(id);
if(set.remove(student)) {
System.out.println("删除成功");
return;
}
break;
}
case 3:{
System.out.println("输入学号:");
String id=scanner.next();
Student student=new Student();
student.setId(id);
if(!set.contains(student)) {
System.out.println("无此学号!");
return;
}
for(Student stu:set) {
if(stu.getId().equals(id)) {
System.out.print("新姓名:");
String name=scanner.next();
System.out.print("新手机号:");
String mobile=scanner.next();
System.out.print("新家庭住址:");
String address=scanner.next();
stu.setName(name);
stu.setMobile(mobile);
stu.setAddress(address);
System.out.println("修改成功");
return;
}
}
break;
}
case 4:{
System.out.println("输入学号:");
String id=scanner.next();
Student student =new Student();
student.setId(id);
if(!set.contains(student)) {
System.out.println("无此学号!");
}
for(Student stu:set) {
if(stu.getId().equals(id)) {
System.out.println(stu);
return;
}
}
break;
}
default:
System.out.println("指令错误!");
}
}public static HashSet
getSet() {
return set;
}public static void setSet(HashSet
set) {
Main.set = set;
}
}
package com.zzu.vo;
public class Student {
private String id;
private String name;
private String mobile;
private String address;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getAdress() {
return address;
}
public void setAddress(String ad) {
this.address = ad;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", mobile=" + mobile + ", ad=" + address + "]";
}
@Override
public int hashCode() {
return id.hashCode();
}
@Override
public boolean equals(Object obj) {
if(obj instanceof Student) {
Student stu =(Student)obj;
return stu.id.equals(this.id);
}
return false;
}
}