目录
步骤一:创建Student包
(1)封装:创建好成员变量,并写好getters and setters方法
步骤二:创建用于写学生管理系统的Main包
(1)人性化系统入口
(2)Student包中提前创建好新的构造方法,用于定义添加系统中的student为(id,name,mobile,address)
(3)switch语句下的添加学生信息系统
(4)Student包中提前创建好新的构造方法,用于定义删除系统中的student为(id)
(5)Student包中重写equals
(6)switch语句下的删除学生信息系统
(7)switch语句下的修改学生信息系统
(8)Student包中提前创建好新的To String方法,用于输出查询系统中的学生信息
(9)switch语句下的查询学生信息系统
注:完整代码请见个人博客(java集合撰写学生信息管理系统)https://blog.csdn.net/m0_46383618/article/details/105589279
public class Student {
private String id;
private String name;
private String mobile;
private String address;
//source-getter-setter
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 getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
package com.zzu.test1;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
private static ArrayList list = new ArrayList();//自定义集合
public static void main(String[] args) {
System.out.println("*********************************");
System.out.println("*\t\t\t\t*");
System.out.println("*\t欢迎使用学生信息管理系统\t*");
System.out.println("*\t\t\t\t*");
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、查询学生信息");
System.out.println("请输入操作,以Enter键结束:");
Scanner scanner = new Scanner(System.in);
int option = scanner.nextInt();//用于选择程序的分支入口
public Student(String id, String name, String mobile, String address) {
this.id = id;
this.name = name;
this.mobile = mobile;
this.address = address;
}
switch (option) {
case 1:{//此大括号用于防止case之间数据的互相干扰
//输入学生信息
System.out.println("请输入学号:");
String id = scanner.next();
System.out.println("请输入姓名:");
String name = scanner.next();
System.out.println("请输入电话号码:");
String mobile = scanner.next();
System.out.println("请输入家庭地址:");
String address = scanner.next();
//创建新的对象
Student student = new Student(id,name,mobile,address);
//括号的内容为下面代码所要调用的对象-因此要在Student创建新的构造方法
//用于判断所输入的信息在已经存在的集合内是否已存在
if(list.contains(student)){
System.out.println("学号不允许重复");
return;
}
//用于将输入的内容添加到list集合中
if(list.add(student)) {
System.out.println("添加成功");
return;
}
System.out.println("添加失败");
}
break;
public Student(String id) {
this.id = id;
}
//source--equals
@Override
public boolean equals(Object obj) {
if(obj instanceof Student) {
Student student = (Student)obj;//下转型
return this.id.equals(student.id);//将调用此方法所输入的id与集合里的id比较
}
return false;
}
case 2:{
System.out.println("请输入学号:");
String id = scanner.next();
Student student = new Student(id);
if(!list.contains(student)) {
System.out.println("学号不存在");
return;
}
if(list.remove(student)) {
System.out.println("删除成功");
return;
}
System.out.println("删除失败");
}
break;
case 3:{
System.out.println("请输入学号:");
String id = scanner.next();
Student student = new Student(id);
if(!list.contains(student)) {
System.out.println("学号不存在,查询失败");
}
for(Student stu : list) {
if(id.equals(stu.getId())){
System.out.println("请输入新姓名:");
String name = scanner.next();
stu.setName(name);
System.out.println("请输入新电话号码:");
String mobile = scanner.next();
stu.setMobile(mobile);
System.out.println("请输入新家庭地址:");
String address = scanner.next();
stu.setAddress(address);
System.out.println("修改成功");
return;
}
System.out.println("修改失败");
}
}
break;
//source--To String
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", mobile=" + mobile + ", address=" + address + "]";
}
case 4:{
System.out.println("请输入学号:");
String id = scanner.next();
Student student = new Student(id);
if(!list.contains(student)) {
System.out.println("学号不存在,查询失败");
return;
}
for(Student stu : list) {
if(id.equals(stu.getId())){
System.out.println(stu);
System.out.println("查询成功");
return;
}
}
System.out.println("查询失败");
}
break;