使用Java做一个简易的学员管理系统
代码如下:
1: 先定义个Student
这个很简单, 注释我就不打了
package Demo02;
import java.util.Objects;
public class Student {
private int id;
private String name;
private String sex;
private int age;
private String birthday;
private int score;
@Override
public String toString() {
return id+ "\t\t"+ name+ "\t\t" + sex + "\t\t" + age + "\t\t"+ birthday + "\t\t" + score;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return id == student.id &&
age == student.age &&
score == student.score &&
Objects.equals(name, student.name) &&
Objects.equals(sex, student.sex) &&
Objects.equals(birthday, student.birthday);
}
@Override
public int hashCode() {
return Objects.hash(id, name, sex, age, birthday, score);
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public Student() {
}
public Student(int id, String name, String sex, int age, String birthday, int score) {
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
this.birthday = birthday;
this.score = score;
}
}
2: 现在是主代码
package Demo02;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Scanner;
public class InformtionManagementSystem01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(" 【欢迎使用学员管理系统】");
Collection<Student> coll = new ArrayList<>();
coll.add(new Student(1, "张三", "男", 18, "1998-01-01", 88));
coll.add(new Student(2, "李四", "女", 11, "1998-12-31", 99));
coll.add(new Student(3, "王五", "男", 22, "1998-10-10", 75));
coll.add(new Student(4, "周六", "女", 19, "1997-08-08", 97));
//来个死循环
while (true) {
System.out.println("\n");
System.out.println("**************************************************************************************");
System.out.println(" 【请输入执行编号】");
System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
System.out.println("【1:添加成员 2:修改成员 3:删除成员 4:按学号查找 5:查找所有成员 6:退出系统】");
System.out.println("======================================================================================");
System.out.print("请输入:");
int number = sc.nextInt();
//根据输入数字调用对应方法
switch (number) {
case 1:
AddStudent(coll);
break;
case 2:
updateStudent(coll);
break;
case 3:
deleteStudent(coll);
break;
case 4:
findById(coll);
break;
case 5:
findAll(coll);
break;
case 6:
System.out.println("【谢谢使用学员管理系统,再见】");
System.exit(0);
default:
System.out.println("输入错误, 重新输入");
break;
}
}
}
//查找所有成员
private static void findAll(Collection<Student> coll) {
System.out.println("【所有成员信息】");
System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
System.out.println("学号\t姓名\t性别\t年龄\t出生日期\t\t分数");
//迭代集合
for (Student stu : coll) {
//输出结果
System.out.println(stu);
}
System.out.println("系统无数据");
}
//按学号查找成员
private static void findById(Collection<Student> coll) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要查询的学号; ");
int id = sc.nextInt();
System.out.println("**************************************************");
System.out.println("学号\t姓名\t\t性别\t年龄\t出生日期\t\t分数");
//迭代集合
for (Student stu : coll) {
//判断集合里是否有这个学号
if (stu.getId() == id) {
System.out.println(stu);
//打印完成, 结束方法
return;
}
}
System.out.println("输入错误, 没有" + id + "学号");//循环外输出
}
//删除成员
private static void deleteStudent(Collection<Student> coll) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要删除的成员学号: ");
int id = sc.nextInt();
//迭代集合
for (Student stu : coll) {
//判断集合是否有对应学号
if (stu.getId() == id) {
System.out.println("确认删除? (Y/N)");
String juage = sc.next();
//判断用户输入
if ("Y".equalsIgnoreCase(juage)) {
coll.remove(stu);//删除对应集合
return;
}
if ("N".equalsIgnoreCase(juage)) {
System.out.println("【操作已取消】");
return;
}
}
}
}
//修改成员
private static void updateStudent(Collection<Student> coll) {
Scanner sc = new Scanner(System.in);
System.out.println("【请输入要修改的成员学号: 】");
int id = sc.nextInt();
//迭代集合
for (Student stu : coll) {
//判断是否有该学号
if (stu.getId() == id) {
System.out.println("【要修改的成员信息】");
System.out.println("学号\t姓名\t\t性别\t年龄\t出生日期\t\t分数");
System.out.println(stu);
System.out.println("【请输入修改的内容: 】");
System.out.println("**********************");
System.out.println("【请输入姓名:(保留原值请输0)】");
String name = sc.next();
System.out.println("【请输入性别:(保留原值请输0)】");
String sex = sc.next();
System.out.println("【请输入年龄:(保留原值请输0)】");
int age = sc.nextInt();
System.out.println("【请输入出生日期:(保留原值请输0)】");
String birthday = sc.next();
System.out.println("【请输入分数:(保留原值请输0)】");
int score = sc.nextInt();
//判断用户输的数是否为0
if (!"0".equals(name)){
//如果用户输入的不是0 , 则执行stu.setName()
stu.setName(name);
}
if (!"0".equals(sex)){
stu.setSex(sex);
}
if (0 != age){
stu.setAge(age);
}
if (!"0".equals(birthday)){
stu.setBirthday(birthday);
}
if (0 != score){
stu.setScore(score);
}
System.out.println("【修改成功,查看请按 5 】");
return;
}
}
System.out.println("【修改失败,没有" + id + "此ID】");
}
//添加学员
private static void AddStudent(Collection<Student> coll) {
Scanner sc = new Scanner(System.in);
System.out.println("【添加成员,请按指示操作】");
System.out.println("***********************************");
System.out.println("【请输入ID:】");
int id = sc.nextInt();
//迭代集合
for (Student stu : coll) {
//判断集合是否有该学号
if (stu.getId() == id) {
System.out.println("【输入错误" + id + "此ID已存在】");
return;
}
}
System.out.println("【请输入姓名:】");
String name = sc.next();
System.out.println("【请输入性别:】");
String sex = sc.next();
System.out.println("【请输入年龄:】");
int age = sc.nextInt();
System.out.println("【请输入出生日期:】");
String birthday = sc.next();
System.out.println("【请输入分数:】");
int score = sc.nextInt();
//封装student 并存入集合
Student stu = new Student();
stu.setId(id);
stu.setName(name);
stu.setSex(sex);
stu.setAge(age);
stu.setBirthday(birthday);
stu.setScore(score);
//存入coll集合
coll.add(stu);
System.out.println("【添加成功,如需继续添加,请按 1 】");
return;
}
}