Java实现无界面的学生管理系统
文章目录
Student类
对于一个学生管理系统,存储管理的都是学生的信息,所以我们定义一个学生类用于定义学生的属性和方法。
属性
String name;//姓名
String sex;//性别
int age;//年龄
String no;//学号
String classes;//班级
方法
student类只定义两个构造方法(一个为有参构造方法,一个为无参构造方法)
- 有参构造方法
public Student(String name,String sex,int age,String no,String classes) {
this.name=name;
this.sex=sex;
this.age=age;
this.no=no;
this.classes=classes;
}
- 无参构造方法
public Student() {
}
StudentManage类
用于定义我们所需要的方法
学生管理系统功能如下:
//2.添加学生信息
//3.根据学号查看一个学生信息
//4.根据班级查看所有学生信息
//5.根据年龄和性别查看所有学生信息
//6.根据学号修改学生信息
//7.根据学号删除学生信息
//8.显示所有学生信息
添加学生信息
定义Student类类型的数组array[ ],用于存储多个Student实例化的对象
Student []array=new Student[20];
public void addStuInfo(Student stu) {//添加学生信息
for (int i=0;i<array.length;i++) {
if(array[i]==null) {
array[i]=stu;
break;
}
}
}
Student s=new Student();//存储学生信息数组
System.out.println("请输入学生的姓名");
s.name=sc.next();
System.out.println("请输入学生的性别");
s.sex=sc.next();
System.out.println("请输入学生的年龄");
s.age=sc.nextInt();
System.out.println("请输入学生的学号");
s.no=sc.next();
System.out.println("请输入学生的班级");
s.classes=sc.next();
sm.addStuInfo(s);
在主函数中通过Student实例化对象s ,通过用户键盘输入为对象s的属性赋值,主函数通过对象sm调用addStuInfo()
函数将对象传给array数组,如果数组的某个下标处为空,则可以在该位置存储信息。
根据学号查看一个学生信息
用户输入学号,调用函数showInfoByNo(String id)
循环array[ ]数组,如果数组某处为空(即未存储信息)那么就跳出,如果数组中某个对象的no值与用户输入的id值相同,那么则对这个对象的所有属性进行打印输出。同时将标识符flag置为true;否则,标识符置为false,输出信息“对不起,没有学号为:"+id+"的学生信息”。
public boolean showInfoByNo(String id) {//根据学号查看一个学生信息
boolean find=false;
for (int i=0;i<array.length;i++) {
if(array[i]==null) {
break;
}
else if(array[i].no.equals(id)) {
System.out.println("学号为:"+id+"的学生的信息为:");
System.out.println("------------------------------");
System.out.println("姓名:"+array[i].name+"\n"+"性别:"+array[i].sex+"\n"+"年龄:"+array[i].age+"岁"+"\n"+"学号:"+array[i].no+"\n"+"班级:"+array[i].classes+"班");
System.out.println("------------------------------");
find=true;
}
}
if(!find) {
System.out.println("对不起,没有学号为:"+id+"的学生信息");
}
return find;
}
根据班级查看所有学生信息
public void showInfoByClasses(String classes) {//根据班级查看所有学生信息
boolean find=false;
System.out.println(classes+"班的所有学生信息为:");
System.out.println("------------------------------");
for (int i=0;i<array.length;i++) {
if(array[i]==null) {
break;
}
else if(array[i].classes.equals(classes)) {
System.out.println("姓名:"+array[i].name+"\n"+"性别:"+array[i].sex+"\n"+"年龄:"+array[i].age+"岁"+"\n"+"学号:"+array[i].no+"\n"+"班级:"+array[i].classes+"班");
System.out.println("------------------------------");
find=true;
}
}
if(!find) {
System.out.println("对不起,没有"+classes+"班的学生信息");
}
}
根据班级查看所有学生信息
public void showInfoByClasses(String classes) {//根据班级查看所有学生信息
boolean find=false;
System.out.println(classes+"班的所有学生信息为:");
System.out.println("------------------------------");
for (int i=0;i<array.length;i++) {
if(array[i]==null) {
break;
}
else if(array[i].classes.equals(classes)) {
System.out.println("姓名:"+array[i].name+"\n"+"性别:"+array[i].sex+"\n"+"年龄:"+array[i].age+"岁"+"\n"+"学号:"+array[i].no+"\n"+"班级:"+array[i].classes+"班");
System.out.println("------------------------------");
find=true;
}
}
if(!find) {
System.out.println("对不起,没有"+classes+"班的学生信息");
}
}
根据性别年龄查看所有学生信息
public void showInfoBySexandAge(String sex,int age) {//根据性别年龄查看所有学生信息
boolean find=false;
System.out.println("性别为"+sex+",年龄为"+age+"的所有学生信息为:");
System.out.println("------------------------------");
for (int i=0;i<array.length;i++) {
if(array[i]==null) {
break;
}
else if(array[i].sex.equals(sex)&&array[i].age==age) {
System.out.println("姓名:"+array[i].name+"\n"+"性别:"+array[i].sex+"\n"+"年龄:"+array[i].age+"岁"+"\n"+"学号:"+array[i].no+"\n"+"班级:"+array[i].classes+"班");
System.out.println("------------------------------");
find=true;
}
}
if(!find) {
System.out.println("对不起,没有性别为"+sex+",年龄为"+age+"的学生信息");
}
}
显示所有学生信息
public void showInfo() {//显示所有学生信息
System.out.println("所有的学生信息为:");
for(int i=0;i<array.length;i++) {
if(array[i]!=null) {
System.out.println("第"+(i+1)+"个学生的信息为:");
System.out.println("姓名:"+array[i].name+"\n"+"性别:"+array[i].sex+"\n"+"年龄:"+array[i].age+"岁"+"\n"+"学号:"+array[i].no+"\n"+"班级:"+array[i].classes+"班");
System.out.println("------------------------------");
}
}
}
根据学号修改学生信息
在主函数中通过键盘输入学号,Scanner对象sc接收,传值给函数showInfoByNo(id),这样就可以先利用这个函数判断学生信息数组里是否有这个学生的信息,如果有再进行学生信息的修改,修改原理就是把这个学号的学生信息数组下标的值重新赋值array[i]=stu;
System.out.println("请输入要修改的学生的学号:");
String id=sc.next();
boolean f=sm.showInfoByNo(id);
if(f) {
Student s1=new Student();//存储学生信息数组
System.out.println("请输入学生的姓名");
s1.name=sc.next();
System.out.println("请输入学生的性别");
s1.sex=sc.next();
System.out.println("请输入学生的年龄");
s1.age=sc.nextInt();
System.out.println("请输入学生的学号");
s1.no=sc.next();
System.out.println("请输入学生的班级");
s1.classes=sc.next();
sm.updateInfoByNo(id, s1);
}
public void updateInfoByNo(String id,Student stu) {//根据学号修改学生信息
for (int i=0;i<array.length;i++) {
if(array[i]==null) {
break;
}
else if(array[i].no.equals(id)) {
array[i]=stu;
System.out.println("修改成功");
break;
}
}
}
根据学号删除学生信息
public void deleteInfoByNo(String id1) {//根据学号删除学生信息
for (int i=0;i<array.length;i++) {
if(array[i]==null) {
break;
}
else if(array[i].no.equals(id1)) {
array[i]=null;
System.out.println("删除成功!");
break;
}
}
}
StudentManage完整代码
public class StudentManage {
Student []array=new Student[20];
public void addStuInfo(Student stu) {//添加学生信息
for (int i=0;i<array.length;i++) {
if(array[i]==null) {
array[i]=stu;
break;
}
}
}
public boolean showInfoByNo(String id) {//根据学号查看一个学生信息
boolean find=false;
for (int i=0;i<array.length;i++) {
if(array[i]==null) {
break;
}
else if(array[i].no.equals(id)) {
System.out.println("学号为:"+id+"的学生的信息为:");
System.out.println("------------------------------");
System.out.println("姓名:"+array[i].name+"\n"+"性别:"+array[i].sex+"\n"+"年龄:"+array[i].age+"岁"+"\n"+"学号:"+array[i].no+"\n"+"班级:"+array[i].classes+"班");
System.out.println("------------------------------");
find=true;
}
}
if(!find) {
System.out.println("对不起,没有学号为:"+id+"的学生信息");
}
return find;
}
public void showInfoByClasses(String classes) {//根据班级查看所有学生信息
boolean find=false;
System.out.println(classes+"班的所有学生信息为:");
System.out.println("------------------------------");
for (int i=0;i<array.length;i++) {
if(array[i]==null) {
break;
}
else if(array[i].classes.equals(classes)) {
System.out.println("姓名:"+array[i].name+"\n"+"性别:"+array[i].sex+"\n"+"年龄:"+array[i].age+"岁"+"\n"+"学号:"+array[i].no+"\n"+"班级:"+array[i].classes+"班");
System.out.println("------------------------------");
find=true;
}
}
if(!find) {
System.out.println("对不起,没有"+classes+"班的学生信息");
}
}
public void showInfoBySexandAge(String sex,int age) {//根据性别年龄查看所有学生信息
boolean find=false;
System.out.println("性别为"+sex+",年龄为"+age+"的所有学生信息为:");
System.out.println("------------------------------");
for (int i=0;i<array.length;i++) {
if(array[i]==null) {
break;
}
else if(array[i].sex.equals(sex)&&array[i].age==age) {
System.out.println("姓名:"+array[i].name+"\n"+"性别:"+array[i].sex+"\n"+"年龄:"+array[i].age+"岁"+"\n"+"学号:"+array[i].no+"\n"+"班级:"+array[i].classes+"班");
System.out.println("------------------------------");
find=true;
}
}
if(!find) {
System.out.println("对不起,没有性别为"+sex+",年龄为"+age+"的学生信息");
}
}
public void showInfo() {//显示所有学生信息
System.out.println("所有的学生信息为:");
for(int i=0;i<array.length;i++) {
if(array[i]!=null) {
System.out.println("第"+(i+1)+"个学生的信息为:");
System.out.println("姓名:"+array[i].name+"\n"+"性别:"+array[i].sex+"\n"+"年龄:"+array[i].age+"岁"+"\n"+"学号:"+array[i].no+"\n"+"班级:"+array[i].classes+"班");
System.out.println("------------------------------");
}
}
}
public void deleteInfoByNo(String id1) {//根据学号删除学生信息
for (int i=0;i<array.length;i++) {
if(array[i]==null) {
break;
}
else if(array[i].no.equals(id1)) {
array[i]=null;
System.out.println("删除成功!");
break;
}
}
}
public void updateInfoByNo(String id,Student stu) {//根据学号修改学生信息
for (int i=0;i<array.length;i++) {
if(array[i]==null) {
break;
}
else if(array[i].no.equals(id)) {
array[i]=stu;
System.out.println("修改成功");
break;
}
}
}
}
StudentMain类
public class StudentMain {
public static void main(String[] args) {
boolean Flag=true;
Scanner sc=new Scanner(System.in);
StudentManage sm= new StudentManage();//全局可用的对象
while(Flag) {
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("7.查看所有学生信息");
System.out.println("***********************");
System.out.println("请输入要进行的操作:");
switch (sc.nextInt()) {
case 1://添加学生信息
Student s=new Student();//存储学生信息数组
System.out.println("请输入学生的姓名");
s.name=sc.next();
System.out.println("请输入学生的性别");
s.sex=sc.next();
System.out.println("请输入学生的年龄");
s.age=sc.nextInt();
System.out.println("请输入学生的学号");
s.no=sc.next();
System.out.println("请输入学生的班级");
s.classes=sc.next();
sm.addStuInfo(s);
break;
case 2://根据学号查看一个学生信息
System.out.println("请输入要查询的学生的学号:");
sm.showInfoByNo(sc.next());
break;
case 3://根据班级查看所有学生信息
System.out.println("请输入要查询的班级:");
sm.showInfoByClasses(sc.next());
break;
case 4://根据年龄和性别查看所有学生信息
System.out.println("请输入要查询的学生性别:");
String sex=sc.next();
System.out.println("请输入要查询的学生年龄:");
int age=sc.nextInt();
sm.showInfoBySexandAge(sex,age);
break;
case 5://根据学号修改学生信息
System.out.println("请输入要修改的学生的学号:");
String id=sc.next();
boolean f=sm.showInfoByNo(id);
if(f) {
Student s1=new Student();//存储学生信息数组
System.out.println("请输入学生的姓名");
s1.name=sc.next();
System.out.println("请输入学生的性别");
s1.sex=sc.next();
System.out.println("请输入学生的年龄");
s1.age=sc.nextInt();
System.out.println("请输入学生的学号");
s1.no=sc.next();
System.out.println("请输入学生的班级");
s1.classes=sc.next();
sm.updateInfoByNo(id, s1);
}
break;
case 6://根据学号删除学生信息
System.out.println("请输入要删除的学生的学号:");
String id1=sc.next();
boolean f1=sm.showInfoByNo(id1);
if(f1) {
sm.deleteInfoByNo(id1);
}
break;
case 7://查看所有学生信息
sm.showInfo();
break;
default:
System.out.println("您的输入有误!!");
break;
}
System.out.println("是否继续?Y/N");
if(sc.next().equals("Y")) {
Flag=true;
}
else {
Flag=false;
System.out.println("欢迎下次使用!");
}
}
}
}