简介
最近学了java基础后对以前不会写的作业深有感触,想起以前各种在网上找资料找别人的代码参考,所以今天特地写了了简单的基于控制台的学生信息管理系统供还在学基础的同学参考,写的有问题的地方也希望能指出来,好了,下面开始进入正题。
环境
我使用的是idea编译器和1.8的jdk版本,有使用1.7以下jdk版本的同学运行会报错,解决方法是把ArrayLis<Student> arrayList=new Array<>();改成 ArrayLis<Student> arrayList=new Array<Student>(),下面开始讲解学生信息管理系统的实现过程。
系统分析
学生信息管理系统主要是实现让老师登入账号(账号和密码我设置的都是“123”,你们也可以改成其他的字符串)后可以对学生进行增删改查的操作和查看所有学生的信息,所以我各自定义了一个方法来实现它的功能。
代码实现
首先定义一个学生类Student.java,关于toString是重写了父类也就是Object的方法,是用于方便查看结果的,学生的属性可以按需求添加多个,这里我为了方便只写了四个。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
public class Student { //学号 private int sno; //姓名 private String name; //性别 private String xes; //年龄 private int age; //创建get/set方法 public int getSno() { return sno; } public void setSno( int sno) { this .sno = sno; } public String getName() { return name; } public void setName(String name) { this .name = name; } public String getXes() { return xes; } public void setXes(String xes) { this .xes = xes; } public int getAge() { return age; } public void setAge( int age) { this .age = age; } //重写toString方法方便打印 @Override public String toString() { return "Student{" + "sno=" + sno + ", name='" + name + '\ '' + ", xes='" + xes + '\ '' + ", age=" + age + '}' ; } } |
然后定义一个结构类Structure.java,把需要实现的功能方法写出来。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
import java.util.ArrayList; import java.util.Scanner; /** * 老师进行登录操作后(老师的账号密码固定为123) * 才能对学生进行增删改查 */ public class Structure { //用来给学生学号赋值自增 static int i= 1 ; //创建ArrayList集合对象用来存放学生对象 ArrayList<Student> arrayList= new ArrayList<>(); Scanner scanner= new Scanner(System.in); public void enter(){ System.out.print( "请输入用户名:" ); if (scanner.next().equals( "123" )){ System.out.print( "请输入密码:" ); if (scanner.next().equals( "123" )){ System.out.println( "登录成功!" ); } else { System.out.println( "密码错误!请重新登入" ); enter(); } } else { System.out.println( "用户名错误!请重新登入" ); enter(); } } //增加学生信息 public void add(){ //创建学生对象 Student student= new Student(); //System.out.print("学生ID自增"); student.setSno(i); System.out.print( "学生姓名:" ); student.setName(scanner.next()); System.out.print( "性别:" ); student.setXes(scanner.next()); System.out.print( "年龄:" ); student.setAge(scanner.nextInt()); //把学生对象存入集合中 arrayList.add(student); i++; } //查看所有学生信息 public void print(){ if (arrayList.size()== 0 ){ System.out.println( "没有学生!!!" ); } else { //使用增强for循环遍历集合 for (Student student:arrayList){ System.out.println(student.toString()); } } } //通过学号查找指定的学生信息 public void inquiry(){ if (arrayList.size()== 0 ){ System.out.println( "没有学生!!!" ); } else { System.out.print( "请输入想要查找的学生的学号:" ); for (Student student:arrayList){ if (student.getSno()==scanner.nextInt()){ System.out.println(student.toString()); } } } } //删除指定学生的信息 public void delete(){ if (arrayList.size()== 0 ){ System.out.println( "没有学生!!!" ); } else { System.out.print( "请输入想要删除的学生的学号:" ); for ( int i= 0 ;i<=arrayList.size();i++){ if (arrayList.get(i).getSno()==scanner.nextInt()){ arrayList.remove(i); System.out.println( "删除成功!" ); } } } } //修改指定学生的信息 public void revisesp(){ if (arrayList.size()== 0 ){ System.out.println( "没有学生!!!" ); } else { System.out.print( "请输入想要修改的学生的学号:" ); for (Student student:arrayList){ if (student.getSno()==scanner.nextInt()){ revisesp01(student); System.out.println( "修改成功!" ); } } } } public void revisesp01(Student student) { System.out.println( "请根据序号选择指令" ); System.out.println( "1.修改姓名" ); System.out.println( "2.修改性别" ); System.out.println( "2.修改年龄" ); switch (scanner.nextInt()){ case 1 : student.setName(scanner.next()); break ; case 2 : student.setXes(scanner.next()); break ; case 3 : student.setAge(scanner.nextInt()); break ; } } } |
最后创建一个测试类Test.java用来测试功能的实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
import java.util.Scanner; public class Test { Structure structure= new Structure(); static Scanner scanner= new Scanner(System.in); public static void main(String[] args) { Test test= new Test(); test.a(); } public void a(){ System.out.println( "老师登录(账号密码固定123/123)" ); structure.enter(); while ( true ){ print(); switch (scanner.nextInt()){ case 1 : structure.add(); break ; case 2 : structure.print(); break ; case 3 : structure.inquiry(); break ; case 4 : structure.delete(); break ; case 5 : structure.revisesp(); break ; case 0 : return ; default : System.out.println( "请输入正确的指令!" ); } } } public void print(){ 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( "0.退出" ); } } |
最后我们就顺利的完成了一个简单的学生管理系统,能独立完成这个对我们巩固基础知识是能起到一定的帮助的。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_45424990/article/details/96469610