1.首先创建一个学生类
一个标准的类需要有构造器,get和set方法以及toString方法
package Student_system;
public class Student {
private int number;
private String name;
private int age;
private String birthday;
public Student(int number, String name, int age, String birthday) {
this.number = number;
this.name = name;
this.age = age;
this.birthday = birthday;
}
public Student() {
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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;
}
@Override
public String toString() {
return "Student{" +
"number=" + number +
", name='" + name + '\'' +
", age=" + age +
", birthday='" + birthday + '\'' +
'}';
}
}
2.控制台打印系统主页面
package Student_system;
import java.util.ArrayList;
import java.util.Scanner;
public class login {
public static void main(String[] args) {
ArrayList<Student> list = new ArrayList<>();
Scanner sc = new Scanner(System.in);
ok:
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 根据学号查看所有学生");
System.out.println("6 退出");
System.out.println("请输入您的选择");
int i = sc.nextInt();
switch (i) {
case 1:
AddClassMates.math1(list);
break ;
case 2:
DeleteClassMates.math2(list);
break ;
case 3:
ChangeClassMates.math3(list);
break ;
case 4:
LookForClassMates.math4(list);
break ;
case 5:
LookForClassGetById.math5(list);
break ;
case 6:
System.out.println("谢谢使用");
break ok;
}
}
}
}
3.编写添加学生功能
package Student_system;
import java.util.ArrayList;
import java.util.Scanner;
public class AddClassMates {
public static void math1(ArrayList<Student> list) {
Student student = new Student();
Scanner sc = new Scanner(System.in);
System.out.println("请输入添加的信息");
System.out.println("请输入学号");
while (true) {
int num2 = sc.nextInt();
int index = IndexGetById(num2, list);
if (index == -1) {
student.setNumber(num2);
break;
} else {
System.out.println("输入错误请重新输入");
}
}
System.out.println("请输入姓名");
student.setName(sc.next());
System.out.println("请输入年龄");
student.setAge(sc.nextInt());
System.out.println("请输入出生日期");
student.setBirthday(sc.next());
list.add(student);
System.out.println("添加学生成功");
}
//不能重复且持续输入的情况
public static int IndexGetById(int a, ArrayList<Student> list) {
int index=-1;
for (int i = 0; i < list.size(); i++) {
Student st = list.get(i);
if (st.getNumber() == a) {
index=i;
return i;
}
}
return index;//不在集合里
}
}
4.编写删除学生功能
package Student_system;
import javax.xml.ws.soap.Addressing;
import java.util.ArrayList;
import java.util.Scanner;
public class DeleteClassMates {
public static void math2(ArrayList<Student> list) {
AddClassMates Ad = new AddClassMates();
Scanner sc = new Scanner(System.in);
System.out.println("请输入你想删除的学生学号");
int i = sc.nextInt();
int index=Ad.IndexGetById(i,list);
if (index==-1){
System.out.println("查无此人,删除失败");
return;
}
for (int j = 0; j < list.size(); j++) {
Student st = list.get(j);
if(st.getNumber()==i){
list.remove(j);
}
}
System.out.println("删除学生成功");
}
}
5.编写修改学生功能
package Student_system;
import java.util.ArrayList;
import java.util.Scanner;
public class ChangeClassMates {
public static void math3(ArrayList<Student> list) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你想修改的学生学号");
int i = sc.nextInt();
if (list.size() == 0) {
System.out.println("集合是空的,快去添加吧~~~");
} else {
AddClassMates ad = new AddClassMates();
int b = ad.IndexGetById(i, list);
if (b ==-1) {
System.out.println("查无此人修改失败");
return;
}else {
for (int j = 0; j < list.size(); j++) {
Student st = list.get(j);
if (st.getNumber() == i) {
System.out.println("请输入修改后的姓名");
st.setName(sc.next());
System.out.println("请输入修改后的年龄");
st.setAge(sc.nextInt());
System.out.println("请输入修改后的出生日期");
st.setBirthday(sc.next());
}
}
}
System.out.println("修改学生成功");
}
}
}
6.编写查询学生功能
这个功能我写了两个一个是根据id查找学生,一个是查询所有学生,代码如下:
第一个是查找所有学生
package Student_system;
import java.util.ArrayList;
public class LookForClassMates {
public static void math4(ArrayList<Student> list) {
if (list.size()==0){
System.out.println("集合是空的,快去添加吧~~~");
}else{
System.out.println("学号\t\t姓名\t\t年龄\t\t出生日期\t\t");
for (int i = 0; i < list.size(); i++) {
Student st = list.get(i);
System.out.println(st.getNumber() + "\t\t" + st.getName() + "\t\t" + st.getAge() + "\t\t" + st.getBirthday());
}
System.out.println("查看成功");
}
}
}x下面这个是根据id查找学生
package Student_system;
import java.util.ArrayList;
import java.util.Scanner;
public class LookForClassGetById {
public static void math5(ArrayList<Student> list) {
Scanner sc = new Scanner(System.in);
AddClassMates ad = new AddClassMates();
System.out.println("请输入学号");
int a = sc.nextInt();
if (list.size() == 0) {
System.out.println("集合是空的,快去添加吧~~~");
} else {
int i=ad.IndexGetById(a,list);
if (i==-1){
System.out.println("查无此人查找失败");
}else {
for (int j = 0; j < list.size(); j++) {
Student st = list.get(j);
if (st.getNumber()==a){
System.out.println("学号\t\t姓名\t\t年龄\t\t出生日期\t\t");
System.out.println(st.getNumber() + "\t\t" + st.getName() + "\t\t" + st.getAge() + "\t\t" + st.getBirthday());
System.out.println("根据ID查找学生成功");
}
}
}
}
}
}