package itcast;
public class Student {
String name;
String ID;
int age;
String class1;
@Override
public String toString() {
return class1 +"\t"+ ID+ "\t" + name +"\t"+ age;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((ID == null) ? 0 : ID.hashCode());
result = prime * result + age;
result = prime * result + ((class1 == null) ? 0 : class1.hashCode());
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 (ID == null) {
if (other.ID != null)
return false;
} else if (!ID.equals(other.ID))
return false;
if (age != other.age)
return false;
if (class1 == null) {
if (other.class1 != null)
return false;
} else if (!class1.equals(other.class1))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
package itcast;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManagementSystem {
public static void main(String[] args) {
//定义学生管理系统,实现功能
// 1.查询学生信息
// 2.添加学生信息
// 3.删除学生信息
// 4.修改学生信息
// 5.退出系统
//创建集合
ArrayList<Student> list = new ArrayList<Student>();
init(list);
while(true){
int x = jieShou();
switch (x) {
case 1:
chaXun(list);
break;
case 2:
addStudent(list);
break;
case 3:
revemoStudent(list);
break;
case 4:
xiuGaiStudent(list);
break;
case 5:
exit();
break;
default:
System.out.println("你的输入有误,请重新输入!");
break;
}
}
}
//定义初始化学生信息,创建对象赋值属性
public static void init(ArrayList<Student> list) {
Student s1 = new Student();
s1.name = "令狐冲";
s1.ID = "00011";
s1.class1 = "三年级(2)班";
s1.age = 20;
Student s2 = new Student();
s2.name = "东方不败";
s2.ID = "00032";
s2.class1 = "三年级(2)班";
s1.age = 18;
Student s3 = new Student();
s3.name = "郭靖";
s3.ID ="00211";
s3.class1 = "三年级(1)班";
s1.age = 21;
Student s4 = new Student();
s4.name = "小师妹";
s4.ID = "00211";
s4.class1 = "三年级(1)班";
s4.age = 17;
//添加对象到集合
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
}
//1.查询学生信息
public static void chaXun(ArrayList<Student> list){
System.out.println("--------------------------------");
System.out.println("欢迎来到查询界面!");
System.out.println("班级 \t编号\t姓名\t年龄");
for (Student student : list) {
System.out.println(student);
}
}
//2.添加学生信息
public static void addStudent(ArrayList<Student> list){
Scanner sc = new Scanner(System.in);
Student s = new Student();
System.out.println("请输入你要添加的学生班级:");
s.class1 = sc.next();
System.out.println("请输入你要添加的学生编号:");
s.ID = sc.next();
System.out.println("请输入你要添加的学生姓名:");
s.name = sc.next();
System.out.println("请输入你要添加的学生年龄:");
s.age = sc.nextInt();
list.add(s);
}
//3.删除学生信息
public static void revemoStudent(ArrayList<Student> list){
//键盘录入
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要删除的学生姓名:");
String name = sc.next();
//for遍历
for (int i = 0; i < list.size(); i++) {
Student s = list.get(i);
if(s.name.equals(name)){
list.remove(i);
System.out.println("已成功删除"+s.name+"的姓名");
return;
}
}
System.out.println("未能成功删除学生的姓名");
}
// 4.修改学生信息
public static void xiuGaiStudent(ArrayList<Student> list) {
//键盘录入
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要修改的学生姓名:");
String name = sc.next();
//for遍历
for(int i = 0;i < list.size();i++){
Student s = list.get(i);
//判断输入的学生信息是否正确
if( s.name.equals(name)){
System.out.println("请输入新学生的班级:");
s.ID = sc.next();
System.out.println("请输入新学生的编号:");
s.ID = sc.next();
System.out.println("请输入新学生的姓名:");
s.name = sc.next();
System.out.println("请输入新学生的年龄:");
s.age = sc.nextInt();
System.out.println("修改学生信息成功!");
return;
}
}
System.out.println("未能成功删除学生的信息");
}
//5.退出系统
public static void exit(){
System.exit(0);
System.out.println("退出成功");
}
//接收多个方法的调用
public static int jieShou(){
System.out.println("欢 迎 来到学生信息管理系统 ");
System.out.println("1.查询学生信息");
System.out.println("2.添加学生信息");
System.out.println("3.删除学生信息");
System.out.println("4.修改学生信息");
System.out.println("5.退出学生管理系统");
Scanner sc = new Scanner(System.in);
System.out.println("请选择功能:");
int x = sc.nextInt();
return x;
}
}
|