一、学生信息管理程序 基本要求:
1.要求实现学生信息的使用添加、查找、删除、修改等几个功能,每个功能模块均能实现从模块中退出,从而完成一个学生管理系统所需功能。
2.要使用结构体来实现对学生信息的存储。
3.学生信息需至少包括:学号,姓名,年龄。
4.使用文件完成数据的存储与读取,要求每次运行某个功能模块时将数据读入结构体中,并给用户提供保存选项,可以将结构体中的数据保存在文件中。
1.1项目演示
1.2学生管理系统实现思路
- 定义学生类
- 主界面的代码编写
- 添加学生的代码编写
- 查看学生的代码编写
- 删除学生的代码编写
- 修改学生的代码编写
1.3定义学生类
学生类: Student
成员变量: 学号:sid
姓名:name
年龄:age
成绩: score
构造方法:无参构造、带四个参数的构造
成员方法:每个成员变量给出相应的get/set方法
package StudentManage;
public class Student {private String sid;private String name;private String age;private String score;public Student(){}public Student(String sid, String name, String age, String score) {this.sid = sid;this.name = name;this.age = age;this.score = score;}public String getSid() {return sid;}public void setSid(String sid) {this.sid = sid;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}public String getScore() {return score;}public void setScore(String score) {this.score = score;}
}
1.4主界面的代码编写
思路:
- 用输出语句完成主界面的编写
- 用Scanner实现键盘录入数据
- 用switch语句完成操作的选择
- 用循环完成再次回到主界面
package StudentManage;import java.util.Scanner;public class StudentManager {public static void main(String[] args) {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("请输入你的选择");Scanner sc = new Scanner(System.in);String line = sc.nextLine();switch (line) {case "1":System.out.println("1.添加学生");break;case "2":System.out.println("2.删除学生");break;case "3":System.out.println("3.修改学生");break;case "4":System.out.println("4.查看作业学生");break;case "5":System.out.println("谢谢使用");System.exit(0);}}}
}
1.5添加学生的代码编写
- 用键盘录入选择添加学生
- 定义一个方法,用于添加学生
- 调用方法
- 2.1显示提示信息,提示要输入何种信息
- 2.2键盘录入学生对象所需要的数据
- 2.3创建学生对象,把键盘录入的信息赋值给学生对象的成员变量
- 2.4将学生对象添加到集合中(保存)
- 2.5给出添加成功提示
public static void addStudent(ArrayList<Student>array){Scanner sc=new Scanner(System.in);System.out.println("请输入学生学号");String sid=sc.nextLine();System.out.println("请输入学生姓名");String name=sc.nextLine();System.out.println("请输入学生年龄");String age=sc.nextLine();System.out.println("请输入学生成绩");String score=sc.nextLine();Student s = new Student();s.setSid(sid);s.setName(name);s.setAge(age);s.setScore(score);array.add(s);System.out.println("添加学生成功");}
1.6查看所有学生的代码编写
思路
- 用键盘录入选择查看的学生信息
- 定义一个方法,用于查看学生信息 (1.显示表头信息 。2.将集合中数据按照对应的格式显示学生信息,年龄显示补充“岁”)
- 调用函数
public static void findStudent(ArrayList<Student> array){if(array.size()==0){System.out.println("无信息,清线添加再查询");return ;}System.out.println("学号\t\t姓名\t\t年龄\t\t成绩");for(int i=0;i<array.size();i++){Student s =array.get(i);System.out.println(s.getSid()+"\t"+s.getName()+"\t\t"+s.getAge()+"岁\t"+s.getScore());}}
1.7删除学生的代码编写
思路
- 用键盘录入选择删除的学生信息
- 定义一个方法,用于删除学生信息(1.显示提示信息。2.键盘录入要删除学生的学号。3.遍历集合将对应的学生对象从集合中删除。4.给出删除成功的提示)
- 调用方法
public static void deleteStudent(ArrayList<Student> array){Scanner sc =new Scanner(System.in);System.out.println("请输入你要删除的学生的学号");String sid=sc.nextLine();int index=-1;for(int i=0;i<array.size();i++){Student s=array.get(i);if(s.getSid().equals(sid)){index=i;break;}}if(index==-1){System.out.println("信息不存在");} else{array.remove(index);System.out.println("删除成功");}}
1.8修改学生的代码编写
- 用键盘录入选择修改的学生信息
- 定义一个方法,用于修改学生信息(1.显示提示信息。2.键盘录入要修改学生的学号。3.键盘录入要修改学生的信息。4.遍历集合修改对应的学生信息5.给出修改成功的提示)
- 调用方法
public static void updataStudent(ArrayList<Student> array){Scanner sc =new Scanner(System.in);System.out.println("请输入要修改学生的学号");String sid=sc.nextLine();System.out.println("请输入学生的新姓名");String name =sc.nextLine();System.out.println("请输入学生的新年龄");String age =sc.nextLine();System.out.println("请输入学生的新成绩");String score=sc.nextLine();Student s =new Student();s.setSid(sid);s.setName(name);s.setAge(age);s.setScore(score);for(int i=0;i<array.size();i++){Student student=array.get(i);if(student.getSid().equals(sid)){array.set(i,s);break;}}System.out.println("修改学生信息成功");}
最终的主函数如下:
package StudentManage;import java.util.ArrayList;
import java.util.Scanner;public class StudentManager {public static void main(String[] args) {ArrayList<Student> array=new ArrayList<Student>();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("请输入你的选择");Scanner sc = new Scanner(System.in);String line = sc.nextLine();switch (line) {case "1":addStudent(array);break;case "2":deleteStudent(array);break;case "3":updataStudent(array);break;case "4":findStudent(array);break;case "5":System.out.println("谢谢使用");System.exit(0);}}}public static void addStudent(ArrayList<Student>array){Scanner sc=new Scanner(System.in);System.out.println("请输入学生学号");String sid=sc.nextLine();System.out.println("请输入学生姓名");String name=sc.nextLine();System.out.println("请输入学生年龄");String age=sc.nextLine();System.out.println("请输入学生成绩");String score=sc.nextLine();Student s = new Student();s.setSid(sid);s.setName(name);s.setAge(age);s.setScore(score);array.add(s);System.out.println("添加学生成功");}public static void findStudent(ArrayList<Student> array){if(array.size()==0){System.out.println("无信息,清线添加再查询");return ;}System.out.println("学号\t\t姓名\t\t年龄\t\t成绩");for(int i=0;i<array.size();i++){Student s =array.get(i);System.out.println(s.getSid()+"\t"+s.getName()+"\t\t"+s.getAge()+"岁\t"+s.getScore());}}public static void deleteStudent(ArrayList<Student> array){Scanner sc =new Scanner(System.in);System.out.println("请输入你要删除的学生的学号");String sid=sc.nextLine();int index=-1;for(int i=0;i<array.size();i++){Student s=array.get(i);if(s.getSid().equals(sid)){index=i;break;}}if(index==-1){System.out.println("信息不存在");} else{array.remove(index);System.out.println("删除成功");}}public static void updataStudent(ArrayList<Student> array){Scanner sc =new Scanner(System.in);System.out.println("请输入要修改学生的学号");String sid=sc.nextLine();System.out.println("请输入学生的新姓名");String name =sc.nextLine();System.out.println("请输入学生的新年龄");String age =sc.nextLine();System.out.println("请输入学生的新成绩");String score=sc.nextLine();Student s =new Student();s.setSid(sid);s.setName(name);s.setAge(age);s.setScore(score);for(int i=0;i<array.size();i++){Student student=array.get(i);if(student.getSid().equals(sid)){array.set(i,s);break;}}System.out.println("修改学生信息成功");}}
注:记得建立学生类
学生类如下
package StudentManage;
public class Student {private String sid;private String name;private String age;private String score;public Student(){}public Student(String sid, String name, String age, String score) {this.sid = sid;this.name = name;this.age = age;this.score = score;}public String getSid() {return sid;}public void setSid(String sid) {this.sid = sid;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}public String getScore() {return score;}public void setScore(String score) {this.score = score;}
}
注:需将以上两个代码放在同一个包下才可运行