java实现简单的学生管理系统
文件搭建:直接复制到项目下
Admin.txt
357886-张三-12345
357887-李四-12345
357888-王五-12345
Student.txt
101-乔峰-99.0
102-虚竹-99.2
103-段誉-98.0
## 实体类的封装
Admin.java
package com.batis.beans;
public class Admin {
private Integer id;
private String name;
private String pass;
public Admin() {
super();
}
public Admin(Integer id, String name, String pass) {
super();
this.id = id;
this.name = name;
this.pass = pass;
}
public int getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
@Override
public String toString() {
return this.id + "-" + this.name + "-" + this.pass;
}
}
Student.java
package com.batis.beans;
public class Student {
private Integer id;
private String name;
private double score;
public Student() {
super();
}
public Student(Integer id, String name, double score) {
super();
this.id = id;
this.name = name;
this.score = score;
}
public int getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public String toString() {
return this.id + "-" + this.name + "-" + this.score;
}
}
测试类
package com.batis.test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import com.batis.beans.Admin;
import com.batis.beans.Student;
public class Test {
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
menu1();
}
public static void menu1() {
System.out.println("==学生管理系统==");
System.out.println("1:登录");
System.out.println("2:注册");
System.out.println("3:退出");
System.out.println("请输入你的选择");
int choose = sc.nextInt();
switch (choose) {
case 1:
login();
break;
case 2:
register();
break;
case 3:
System.out.println("Bye~~");
System.exit(0);
break;
default:
System.out.println("输入的信息有误");
break;
}
menu1();
}
public static void login() {
System.out.println("请输入您的工号");
int id = sc.nextInt();
System.out.println("请输入您的密码");
String pass = sc.next();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("Admin.txt"));
String str = null;
while ((str = br.readLine()) != null) {
String[] split = str.split("-");
if (Integer.parseInt(split[0]) == id && split[2].equals(pass)) {
System.out.println("登录成功");
menu2(split[1]);
return;
}
}
System.out.println("登入失败");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void register() {
System.out.println("请输入您的工号:");
int id = sc.nextInt();
System.out.println("请输入姓名:");
String name = sc.next();
System.out.println("请输入密码:");
String pass = sc.next();
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader("Admin.txt"));
bw = new BufferedWriter(new FileWriter("Admin.txt", true));
String str = null;
while ((str = br.readLine()) != null) {
String[] split = str.split("-");
if (Integer.parseInt(split[0]) == id) {
System.out.println("该工号已注册!");
return;
}
}
bw.write(new Admin(id, name, pass).toString());
bw.newLine();
bw.flush();
System.out.println("注册成功!");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void menu2(String x) {
System.out.println("欢迎" + x + "用户登录");
System.out.println("1:根据学号查找学生");
System.out.println("2:展示所有学生");
System.out.println("3:新增一个学生");
System.out.println("4:移除一个学生");
System.out.println("5:修改学生成绩");
System.out.println("6:返回上级菜单");
int choose = sc.nextInt();
switch (choose) {
case 1:
findOneStudentById();
break;
case 2:
showAll();
break;
case 3:
addOne();
break;
case 4:
removeOne();
break;
case 5:
changeStudentScore();
break;
case 6:
menu1();
break;
default:
System.out.println("输入有误");
break;
}
menu2(x);
}
public static Student findOneStudentById() {
System.out.println("请输入要查找的学生编号:");
int id = sc.nextInt();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("Student.txt"));
String str = null;
while ((str = br.readLine()) != null) {
String[] split = str.split("-");
if (Integer.parseInt(split[0]) == id) {
System.out.println("学号\t姓名\t成绩");
System.out.println(split[0] + "\t" + split[1] + "\t" + split[2]);
return new Student(Integer.parseInt(split[0]), split[1], Double.parseDouble(split[2]));
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("该学生不存在!");
return null;
}
public static void showAll() {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("Student.txt"));
String str = null;
System.out.println("学号\t姓名\t成绩");
while ((str = br.readLine()) != null) {
String[] split = str.split("-");
System.out.println(split[0] + "\t" + split[1] + "\t" + split[2]);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void addOne() {
System.out.println("请输入添加的学生id");
int stuId = sc.nextInt();
System.out.println("请输入添加的学生的姓名");
String name = sc.next();
System.out.println("请输入添加的学生的成绩");
double score = sc.nextDouble();
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader("Student.txt"));
bw = new BufferedWriter(new FileWriter("Student.txt", true));
String str = null;
while ((str = br.readLine()) != null) {
String[] split = str.split("-");
if (Integer.parseInt(split[0]) == stuId) {
System.out.println("该学生已经存在");
return;
}
}
bw.write(new Student(stuId, name, score).toString());
bw.newLine();
bw.flush();
System.out.println("添加成功");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void removeOne() {
Student stu = findOneStudentById();
if (stu == null) {
return;
}
System.out.println("是否确定删除(Y/N)");
String choose = sc.next();
if (choose.equals("N")) {
return;
}
BufferedReader br = null;
BufferedWriter bw = null;
ArrayList<Student> list = new ArrayList<Student>();
try {
br = new BufferedReader(new FileReader("Student.txt"));
String str = null;
while ((str = br.readLine()) != null) {
String[] split = str.split("-");
if (stu.getId() == Integer.parseInt(split[0])) {
continue;
}
list.add(new Student(Integer.parseInt(split[0]), split[1], Double.parseDouble(split[2])));
}
bw = new BufferedWriter(new FileWriter("student.txt"));
for (Student student : list) {
bw.write(student.toString());
bw.newLine();
bw.flush();
;
}
System.out.println("移除成功");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void changeStudentScore() {
Student student = findOneStudentById();
if (student == null) {
return;
}
System.out.println("请输入学生新的成绩");
double newScore = sc.nextDouble();
BufferedReader br = null;
BufferedWriter bw = null;
ArrayList<Student> list = new ArrayList<Student>();
try {
br = new BufferedReader(new FileReader("Student.txt"));
String str = null;
while ((str = br.readLine()) != null) {
String[] split = str.split("-");
if (student.getId() == Integer.parseInt(split[0])) {
list.add(new Student(Integer.parseInt(split[0]), split[1], newScore));
continue;
}
list.add(new Student(Integer.parseInt(split[0]), split[1], Double.parseDouble(split[2])));
}
bw = new BufferedWriter(new FileWriter("Student.txt"));
for (Student stu : list) {
bw.write(stu.toString());
bw.newLine();
bw.flush();
}
System.out.println("修改成功!");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}