一、程序功能
功能概述
- 利用Java完成一个学生信息程序的简单改动。
- 添加学生信息
- 修改学生信息
- 查询学生信息
- 删除学生信息
二、程序包括的学生信息
1.学号 2.姓名 3.班级 4.专业 5.电话号码
- 当然,这只是简单的包括几个信息,其他的信息,如果也想要可以自己加入
三、编程实现
1.建立学生实体类
2.学生实体类代码编写
(1)无参构造方法
(2)有参构造方法
(3)完整的代码
public class Student {
private String id;
private String name;
private String classes;
private String major;
private String telephone;
public Student() {
}
public Student(String id, String name, String classes, String major, String telephone) {
this.id = id;
this.name = name;
this.classes = classes;
this.major = major;
this.telephone = telephone;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClasses() {
return classes;
}
public void setClasses(String classes) {
this.classes = classes;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
@Override
public String toString() {
return "Card{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", classes='" + classes + '\'' +
", major='" + major + '\'' +
", telephone='" + telephone + '\'' +
'}';
}
}
2.建立学生信息管理
- 继续在test包里创建----StudentManagement(学生信息管理)
3.建立学生数组列表对象
private static ArrayList<Student> students = new ArrayList<>();
public static void main(String[] args) {
int mc1;
Scanner sc = new Scanner(System.in);
boolean isRunning = true;
while (isRunning) {
System.out.println(" 学生信息管理系统");
System.out.println("================");
System.out.println(" 1. 登录");
System.out.println(" 0. 退出");
System.out.println("================");
System.out.print("输入菜单编号[1,0]:");
mc1 = sc.nextInt();
switch (mc1) {
case 1:
login();
break;
case 0:
System.out.println("谢谢使用~再见~");
isRunning = false;
break;
default:
System.out.println("输入的菜单编号有误!");
}
}
}
-创建好了列表,下面就该解决如何登录了
4.登录方法
private static void login() {
String username, password;
Scanner sc = new Scanner(System.in);
System.out.print("用户名:");
username = sc.next();
System.out.print("密 码:");
password = sc.next();
if (username.equals("gumeng") && password.equals("1314520")) {
System.out.println("登录成功~");
studentManagement();
} else {
System.out.println("用户名或密码有误,登录失败~");
}
}
- 这里的登录账号是:gumneg ,登录密码是:1314520
- 登录账号和密码随自己的喜好设置
5.学生信息管理方法
(1)一级菜单
- 在上面我们已经建立了一级菜单
(2)二级菜单
- 下面我们绘制二级菜单,二级菜单是我们成功登录进去后的功能面板
- 总共有七个功能面板
1、添加学生信息 2、修改学生信息 3、查询学生信息 4、删除学生信息 5、读取学生信息 6、保存学生信息 7、返回上级菜单 - 分别对每个功能面板进行编写
(3)完整学生管理方法代码
private static void studentManagement() {
int mc2;
Scanner sc = new Scanner(System.in);
boolean isRunning = true;
while (isRunning) {
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.print("输入菜单编号[1,2,3,4,5,6,7]:");
mc2 = sc.nextInt();
switch (mc2) {
case 1:
System.out.println("执行添学生信息功能~");
break;
case 2:
System.out.println("执行修改学生信息功能~");
break;
case 3:
System.out.println("执行查询学生信息功能~");
break;
case 4:
System.out.println("执行删除学生信息功能~");
break;
case 5:
System.out.println("读取学生信息文件到学生数组列表~");
break;
case 6:
System.out.println("将学生信息数组列表写入名片文件~");
break;
case 7:
isRunning = false;
break;
default:
System.out.println("输入的菜单编号有误!");
}
}
}
6.添加学生信息方法
private static void addStudent() {
String id;
String name;
String classes;
String major;
String telephone;
Scanner sc = new Scanner(System.in);
System.out.print("学号:");
id = sc.next();
System.out.print("姓名:");
name = sc.next();
System.out.print("班级:");
classes = sc.next();
System.out.print("专业:");
major = sc.next();
System.out.print("手机号码:");
telephone = sc.next();
Student student = new Student(id, name, classes, major,telephone);
students.add(student);
}
- 将学生添加到学生数组列表
- 添加学生总不能不保存,所以下面我们要将学生信息保存到文件
7.将学生信息保存到文件
(1)文件名
这是学生信息将要保存的文件名,当然文件可以随便自己设置
(2)完整代码
private static void saveStudents() {
try {
FileWriter fw = new FileWriter("students.txt");
StringBuffer cardsBuffer = new StringBuffer();
for (Student student : students) {
System.out.println(student);
StringBuffer buffer = new StringBuffer();
buffer.append(student.getId() + " ");
buffer.append(student.getName() + " ");
buffer.append(student.getClasses() + " ");
buffer.append(student.getMajor() + " ");
buffer.append(student.getTelephone());
cardsBuffer.append(buffer.toString() + "\n");
}
fw.write(students.toString());
fw.close();
System.out.println("恭喜,学生信息数据写入文件成功~");
} catch (IOException e) {
e.printStackTrace();
}
}
(3)调用学生信息保存方法
- 写好了要在保存学生信息功能面板上调用
8.读取学生信息并保存到学生数组列表
(1)读取源
- 你是从哪个文件读取的学生信息,这里读取名和上面的保存名要一致,不然读取不到学生信息
(2)读取学生信息完整代码
- 在这里还有一个问题,如果文件名没有问题,但是你要读取的学生不存在的话程序要出错,所以我们正在这里要把好关
private static void readStudentFile() {
try {
FileReader fr = new FileReader("students.txt");
BufferedReader br = new BufferedReader(fr);
String nextLine = " ";
while ((nextLine = br.readLine()) != null) {
String[] fields = nextLine.split(" ");
Student student = new Student();
student.setId(fields[0]);
student.setName(fields[1]);
student.setClasses(fields[2]);
student.setMajor(fields[3]);
student.setTelephone(fields[4]);
System.out.println("添加" + student + "到学生数组列表");
students.add(student);
}
br.close();
fr.close();
} catch (FileNotFoundException e) {
System.out.println("温馨提示:学生文件不存在~");
} catch (IOException e) {
e.printStackTrace();
}
}
(3)调用读取学生信息方法
9.查询学生信息
(1)判断按哪种查询
- 怎样查询学生信息呢?是按学号查询还是按姓名查询呢?还是一起查询?
if (id.equals("")) {
if (name.equals("")) {
for (Student student : students) {
System.out.println(student.getId() + " "
+ student.getName() + " " + student.getClasses() + " "
+ student.getMajor() + " " + student.getTelephone());
}
(2)按学生姓名查询
System.out.println("~按姓名查询学生信息~");
boolean found = false;
for (Student student : students) {
if (student.getName().equals(name)) {
found = true;
break;
}
}
- 当查询的时候遇到同名情况怎么办,所以我们要解决这个问题
if (found) {
for (Student student : students) {
if (student.getName().equals(name)) {
System.out.println(student.getId() + " "
+ student.getName() + " " + student.getClasses() + " "
+ student.getMajor() + " " + student.getTelephone());
}
}
} else {
System.out.println("温馨提示:查无此人~");
}
(3)按学生学号查询
else {
if (name.equals("")) {
System.out.println("~按学号查询学生信息~");
boolean found = false;
for (Student student : students) {
if (student.getId().equals(id)) {
found = true;
System.out.println(student.getId() + " "
+ student.getName() + " " + student.getClasses() + " "
+ student.getMajor() + " " + student.getTelephone());
break;
}
}
if (!found) {
System.out.println("温馨提示:查无此人~");
}
(4)按学生学号和姓名一起查询
else {
System.out.println("~按学号和姓名查询学生学习~");
boolean found = false;
for (Student student : students) {
if (student.getId().equals(id) && student.getName().equals(name)) {
found = true;
System.out.println(student.getId() + " "
+ student.getName() + " " + student.getClasses() + " "
+ student.getMajor() + " " + student.getTelephone());
break;
}
}
if (!found) {
System.out.println("温馨提示:查无此人~");
}
}
(5)三种查询情况的完整代码
private static void findStudent() {
String id, name;
Scanner sc = new Scanner(System.in);
System.out.print("学号:");
id = sc.nextLine();
System.out.print("姓名:");
name = sc.nextLine();
if (id.equals("")) {
if (name.equals("")) {
for (Student student : students) {
System.out.println(student.getId() + " "
+ student.getName() + " " + student.getClasses() + " "
+ student.getMajor() + " " + student.getTelephone());
}
} else {
System.out.println("~按姓名查询学生信息~");
boolean found = false;
for (Student student : students) {
if (student.getName().equals(name)) {
found = true;
break;
}
}
if (found) {
for (Student student : students) {
if (student.getName().equals(name)) {
System.out.println(student.getId() + " "
+ student.getName() + " " + student.getClasses() + " "
+ student.getMajor() + " " + student.getTelephone());
}
}
} else {
System.out.println("温馨提示:查无此人~");
}
}
} else {
if (name.equals("")) {
System.out.println("~按学号查询学生信息~");
boolean found = false;
for (Student student : students) {
if (student.getId().equals(id)) {
found = true;
System.out.println(student.getId() + " "
+ student.getName() + " " + student.getClasses() + " "
+ student.getMajor() + " " + student.getTelephone());
break;
}
}
if (!found) {
System.out.println("温馨提示:查无此人~");
}
} else {
System.out.println("~按学号和姓名查询学生学习~");
boolean found = false;
for (Student student : students) {
if (student.getId().equals(id) && student.getName().equals(name)) {
found = true;
System.out.println(student.getId() + " "
+ student.getName() + " " + student.getClasses() + " "
+ student.getMajor() + " " + student.getTelephone());
break;
}
}
if (!found) {
System.out.println("温馨提示:查无此人~");
}
}
}
}
(6)调用查询学生信息方法
10.修改学生信息
(1)判断学生是否找到
- 我们要修改学生信息,首先要找到我们要修改的学生在不在这个列表里,在并且找到了我们才能进行修改
if (found) {
System.out.println("说明:不输入信息,直接敲回车,表明不修改字段值");
System.out.print("姓名:");
name = sc.nextLine();
System.out.print("班级:");
classes = sc.nextLine();
System.out.print("专业:");
major= sc.nextLine();
System.out.print("手机号码:");
telephone = sc.nextLine();
for (int i = 0; i < students.size(); i++) {
Student student = students.get(i);
if (student.getId().equals(id)) {
if (!name.equals("")) {
student.setName(name);
}
if (!classes.equals("")) {
student.setClasses(classes);
}
if (!major.equals("")) {
student.setMajor(major);
}
if (!telephone.equals("")) {
student.setTelephone(telephone);
}
students.set(i, student);
System.out.println(students.get(i));
break;
}
}
} else {
System.out.println("温馨提示:查无此人~");
}
}
(2)修改学习学习的完整代码
private static void modifyStudent() {
String id, name, classes, major, telephone;
Scanner sc = new Scanner(System.in);
boolean found = false;
System.out.print("输入要修改的学生学号:");
id = sc.nextLine();
for (Student student : students) {
if (student.getId().equals(id)) {
found = true;
System.out.println(student.getId() + " "
+ student.getName() + " " + student.getClasses() + " "
+ student.getMajor() + " " + student.getTelephone());
break;
}
}
if (found) {
System.out.println("说明:不输入信息,直接敲回车,表明不修改字段值");
System.out.print("姓名:");
name = sc.nextLine();
System.out.print("班级:");
classes = sc.nextLine();
System.out.print("专业:");
major= sc.nextLine();
System.out.print("手机号码:");
telephone = sc.nextLine();
for (int i = 0; i < students.size(); i++) {
Student student = students.get(i);
if (student.getId().equals(id)) {
if (!name.equals("")) {
student.setName(name);
}
if (!classes.equals("")) {
student.setClasses(classes);
}
if (!major.equals("")) {
student.setMajor(major);
}
if (!telephone.equals("")) {
student.setTelephone(telephone);
}
students.set(i, student);
System.out.println(students.get(i));
break;
}
}
} else {
System.out.println("温馨提示:查无此人~");
}
}
(3)调用学生信息修改方法
11.删除学生信息
(1)删除学生信息的完整代码
private static void deleteStudent() {
Scanner sc = new Scanner(System.in);
System.out.print("请输入你要删除的学生学号:");
String id = sc.nextLine();
int index = -1;
for (int i = 0;i < students.size();i++) {
Student student = students.get(i);
if (student.getId().equals(id)) {
index = i;
break;
}
}
if (index == -1) {
System.out.print("该学号不存在,请重新输入!");
} else {
students.remove(index);
System.out.print("删除学生成功!");
}
}
(2)调用删除学生信息方法
四、编程实现完整代码
package test;
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManagement {
private static ArrayList<Student> students = new ArrayList<>();
public static void main(String[] args) {
int mc1;
Scanner sc = new Scanner(System.in);
boolean isRunning = true;
while (isRunning) {
System.out.println(" 学生信息管理系统");
System.out.println("================");
System.out.println(" 1. 登录");
System.out.println(" 0. 退出");
System.out.println("================");
System.out.print("输入菜单编号[1,0]:");
mc1 = sc.nextInt();
switch (mc1) {
case 1:
login();
break;
case 0:
System.out.println("谢谢使用~再见~");
isRunning = false;
break;
default:
System.out.println("输入的菜单编号有误!");
}
}
}
private static void login() {
String username, password;
Scanner sc = new Scanner(System.in);
System.out.print("用户名:");
username = sc.next();
System.out.print("密 码:");
password = sc.next();
if (username.equals("gumeng") && password.equals("1314520")) {
System.out.println("登录成功~");
studentManagement();
} else {
System.out.println("用户名或密码有误,登录失败~");
}
}
private static void studentManagement() {
int mc2;
Scanner sc = new Scanner(System.in);
boolean isRunning = true;
while (isRunning) {
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.print("输入菜单编号[1,2,3,4,5,6,7]:");
mc2 = sc.nextInt();
switch (mc2) {
case 1:
System.out.println("执行添学生信息功能~");
addStudent();
break;
case 2:
System.out.println("执行修改学生信息功能~");
modifyStudent();
break;
case 3:
System.out.println("执行查询学生信息功能~");
findStudent();
break;
case 4:
System.out.println("执行删除学生信息功能~");
deleteStudent();
break;
case 5:
System.out.println("读取学生信息文件到学生数组列表~");
readStudentFile();
break;
case 6:
System.out.println("将学生信息数组列表写入名片文件~");
saveStudents();
break;
case 7:
isRunning = false;
break;
default:
System.out.println("输入的菜单编号有误!");
}
}
}
private static void addStudent() {
String id;
String name;
String classes;
String major;
String telephone;
Scanner sc = new Scanner(System.in);
System.out.print("学号:");
id = sc.next();
System.out.print("姓名:");
name = sc.next();
System.out.print("班级:");
classes = sc.next();
System.out.print("专业:");
major = sc.next();
System.out.print("手机号码:");
telephone = sc.next();
Student student = new Student(id, name, classes, major,telephone);
students.add(student);
}
private static void saveStudents() {
try {
FileWriter fw = new FileWriter("students.txt");
StringBuffer studentsBuffer = new StringBuffer();
for (Student student : students) {
System.out.println(student);
StringBuffer buffer = new StringBuffer();
buffer.append(student.getId() + " ");
buffer.append(student.getName() + " ");
buffer.append(student.getClasses() + " ");
buffer.append(student.getMajor() + " ");
buffer.append(student.getTelephone());
studentsBuffer.append(buffer.toString() + "\n");
}
fw.write(studentsBuffer.toString());
fw.close();
System.out.println("恭喜,学生信息数据写入文件成功~");
} catch (IOException e) {
e.printStackTrace();
}
}
private static void readStudentFile() {
try {
FileReader fr = new FileReader("students.txt");
BufferedReader br = new BufferedReader(fr);
String nextLine = " ";
while ((nextLine = br.readLine()) != null) {
String[] fields = nextLine.split(" ");
Student student = new Student();
student.setId(fields[0]);
student.setName(fields[1]);
student.setClasses(fields[2]);
student.setMajor(fields[3]);
student.setTelephone(fields[4]);
System.out.println("添加" + student + "到学生数组列表");
students.add(student);
}
br.close();
fr.close();
} catch (FileNotFoundException e) {
System.out.println("温馨提示:学生文件不存在~");
} catch (IOException e) {
e.printStackTrace();
}
}
private static void findStudent() {
String id, name;
Scanner sc = new Scanner(System.in);
System.out.print("学号:");
id = sc.nextLine();
System.out.print("姓名:");
name = sc.nextLine();
if (id.equals("")) {
if (name.equals("")) {
for (Student student : students) {
System.out.println(student.getId() + " "
+ student.getName() + " " + student.getClasses() + " "
+ student.getMajor() + " " + student.getTelephone());
}
} else {
System.out.println("~按姓名查询学生信息~");
boolean found = false;
for (Student student : students) {
if (student.getName().equals(name)) {
found = true;
break;
}
}
if (found) {
for (Student student : students) {
if (student.getName().equals(name)) {
System.out.println(student.getId() + " "
+ student.getName() + " " + student.getClasses() + " "
+ student.getMajor() + " " + student.getTelephone());
}
}
} else {
System.out.println("温馨提示:查无此人~");
}
}
} else {
if (name.equals("")) {
System.out.println("~按学号查询学生信息~");
boolean found = false;
for (Student student : students) {
if (student.getId().equals(id)) {
found = true;
System.out.println(student.getId() + " "
+ student.getName() + " " + student.getClasses() + " "
+ student.getMajor() + " " + student.getTelephone());
break;
}
}
if (!found) {
System.out.println("温馨提示:查无此人~");
}
} else {
System.out.println("~按学号和姓名查询学生学习~");
boolean found = false;
for (Student student : students) {
if (student.getId().equals(id) && student.getName().equals(name)) {
found = true;
System.out.println(student.getId() + " "
+ student.getName() + " " + student.getClasses() + " "
+ student.getMajor() + " " + student.getTelephone());
break;
}
}
if (!found) {
System.out.println("温馨提示:查无此人~");
}
}
}
}
private static void modifyStudent() {
String id, name, classes, major, telephone;
Scanner sc = new Scanner(System.in);
boolean found = false;
System.out.print("输入要修改的学生学号:");
id = sc.nextLine();
for (Student student : students) {
if (student.getId().equals(id)) {
found = true;
System.out.println(student.getId() + " "
+ student.getName() + " " + student.getClasses() + " "
+ student.getMajor() + " " + student.getTelephone());
break;
}
}
if (found) {
System.out.println("说明:不输入信息,直接敲回车,表明不修改字段值");
System.out.print("姓名:");
name = sc.nextLine();
System.out.print("班级:");
classes = sc.nextLine();
System.out.print("专业:");
major= sc.nextLine();
System.out.print("手机号码:");
telephone = sc.nextLine();
for (int i = 0; i < students.size(); i++) {
Student student = students.get(i);
if (student.getId().equals(id)) {
if (!name.equals("")) {
student.setName(name);
}
if (!classes.equals("")) {
student.setClasses(classes);
}
if (!major.equals("")) {
student.setMajor(major);
}
if (!telephone.equals("")) {
student.setTelephone(telephone);
}
students.set(i, student);
System.out.println(students.get(i));
break;
}
}
} else {
System.out.println("温馨提示:查无此人~");
}
}
private static void deleteStudent() {
Scanner sc = new Scanner(System.in);
System.out.print("请输入你要删除的学生学号:");
String id = sc.nextLine();
int index = -1;
for (int i = 0;i < students.size();i++) {
Student student = students.get(i);
if (student.getId().equals(id)) {
index = i;
break;
}
}
if (index == -1) {
System.out.print("该学号不存在,请重新输入!");
} else {
students.remove(index);
System.out.print("删除学生成功!");
}
}
}
五、测试程序
1.运行程序,登录系统
2.添加学生信息
202201 楚梦瑶 软件1班 软件技术 15981324667 202202 林海 大数据2班 大数据技术与应用 15372783998 202203 王明 人智1班 人工智能 18881588702 202204 顾瑶 计应4班 计算机技术应用 15891457734 202205 杨曦蕊 学前3班 学前教育 13891342266
- 可以根据自己的喜好添加更多或者更少
- 注意:这里每输入一条数据都要保存一次,不然最后才保存的话文件里全是一排
3.保存学生信息到文件
(1)保存学生信息
(2)查看文件
- 运行程序后,会在根目录里自动生成我们命名好的文件
- 点开文件,查看信息
4.查询学生信息
(1)按学生学号查询
(2)按学生姓名查询
(3)同时按学号和姓名查询
5.修改学生信息
(1)将王明改成张心怡
- 修改完之后要保存,只有保存,文件里的学生信息也才跟着改变
- 再来看看文件里的学生信息
(2)将楚梦瑶的班级改为学前五班
- 查看文件的学生信息
6.删除学生信息(删除学号为202203)
- 查看文件里的学生信息
- 已经被删除了
7.读取学生信息
8.退出程序
六.实训总结
1.实训项目存在不足的地方
项目内容也就是学生的信息有点少,还可以添加学生其他的信息,如性别、年龄、家庭住址、兴趣爱好等。还有就是没有采用数据库。
2.实训项目的有点
内容紧凑,条理清晰,图文并茂,使人一看就明白
3.实训个人感悟
1、 经过一学期的学习和实训,明白自己还有许多不足的地方,很多地方需要改进,很多地方需要向同学学习,相比起上学期这学期的干劲足多了,但是还不够,还需要多多努力,正所谓奋斗是创造幸福的源泉 2、未来这个词听上去就是美好,可是你别忘了呀,每一个我们所期待的美好未来,都必须有一个努力的现在。
|