1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > java开发的简易学生成绩管理系统

java开发的简易学生成绩管理系统

时间:2023-03-16 01:16:38

相关推荐

java开发的简易学生成绩管理系统

经过1个月的紧张学习和应用,终于做出了这个简易的学生成绩管理系统。

代码如下(仅供参考学习)

view包下的菜单

package com.xujulong.www.view;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Scanner;

import com.xujulong.www.po.Manager;

import com.xujulong.www.po.Teacher;

import com.xujulong.www.service.ChooseFunction;

public class Menu {

private String idOrName;

private String password;

private BufferedReader br; //可用于从字符输入流中读取文本

private Scanner input;

public Menu(){

input = new Scanner(System.in);

br = new BufferedReader(new InputStreamReader(System.in));

}

public void show(){

while(true){

System.out.println("请按下面信息提示输入数字");

System.out.println("1---管理员登陆 2---教师登录 3---退出");

int num = 0 ;

/**

* 检查用户输入的是否为数字

*/

boolean f = false;

do{

try{

num = Integer.parseInt(br.readLine());

f =true;

}catch(NumberFormatException e){ //捕抓输入的内容不能转换成int型

System.out.println("输入错误,请重新输入数字");

}catch(IOException e){

e.printStackTrace();

}

}while(f == false);

if(num==1)

this.managerMenue();

else if(num==2)

this.teacherMenue();

else if(num==3)

System.exit(0);

else

System.out.println("输入有误,请重新输入");

}

}

public void managerMenue() {

while (true){

System.out.println("欢迎管理员登陆");

System.out.println("请输账号或用户名");

idOrName = input.next();

System.out.println("请输密码");

password = input.next();

Manager m1 = new Manager("abc","TopView","123");

Manager m2 = new Manager("123","安卓","abc");

Manager[] mana = {m1,m2};

for(Manager manager:mana) { //用for each 遍历来检查账号密码

if((manager.getId().equals(idOrName) || manager.getUserName().equals(idOrName))&& manager.getPasswrod().equals(password)){

System.out.println("登陆成功");

System.out.println(manager.getUserName()+"管理员您好");

this.managerGetIn();

}

}

System.out.println("账号或密码错误请重新输入");

}

}

public void teacherMenue() {

while (true){

System.out.println("欢迎教师登陆");

System.out.println("请输入账号或用户名");

idOrName = input.next();

System.out.println("请输入密码");

password = input.next();

Teacher t1 = new Teacher("123","TopView","123");

Teacher t2 = new Teacher("abc","安卓","abc");

Teacher[] teac = {t1,t2};

for(Teacher teacher:teac) {

if((teacher.getId().equals(idOrName) || teacher.getUserName().equals(idOrName)) && teacher.getPasswrod().equals(password) ) {

System.out.println("登陆成功");

System.out.println(teacher.getUserName()+"老师您好");

this.teacherGetIn();

}

}

System.out.println("账号或密码错误请重新输入");

}

}

public void teacherGetIn(){

ChooseFunction cho = new ChooseFunction();

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("6---查询具体科目成绩");

System.out.println("7---返回上级菜单");

int num = 0;

boolean f = false;

do{

try{

num = Integer.parseInt(br.readLine());

f =true;

}catch(NumberFormatException e){

System.out.println("输入错误,请重新输入数字");

}catch(IOException e){

e.printStackTrace();

}

}while(f == false);

if( num >=1 && num <= 7 )

cho.teacherchoose(num);

else

System.out.println("输入错误请重新输入");

}

}

public void managerGetIn(){

ChooseFunction cho = new ChooseFunction();

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("6---查询具体科目成绩");

System.out.println("7---返回上级菜单");

int num = 0;

/**

* 检查用户输入的是否为数字

*/

boolean f = false;

do{

try{

num = Integer.parseInt(br.readLine());

f =true;

}catch(NumberFormatException e){

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---返回上级菜单");

}catch(IOException e){

e.printStackTrace();

}

}while(f == false);

if( num >=1 && num <= 7 )

cho.managerchoose(num);

else

System.out.println("输入错误请重新输入");

}

}

}

po包下的各种对象

教师:

package com.xujulong.www.po;

public class Teacher {

private String id;

private String userName ;

private String passwrod ;

public Teacher(String id,String userName,String passwrod){

this.id = id;

this.userName = userName;

this.passwrod = passwrod;

}

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public String getUserName() {

return userName;

}

public void setUserName(String userName) {

this.userName = userName;

}

public String getPasswrod() {

return passwrod;

}

public void setPasswrod(String passwrod) {

this.passwrod = passwrod;

}

}

管理员:

package com.xujulong.www.po;

public class Manager {

private String id;

private String userName ;

private String passwrod ;

public Manager(String id,String userName,String passwrod){ //有参构造函数

this.id = id;

this.userName = userName;

this.passwrod = passwrod;

}

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public String getUserName() {

return userName;

}

public void setUserName(String userName) {

this.userName = userName;

}

public String getPasswrod() {

return passwrod;

}

public void setPasswrod(String passwrod) {

this.passwrod = passwrod;

}

}

学生:

package com.xujulong.www.po;

import java.io.Serializable;

import java.util.HashSet;

import java.util.Set;

public class Student implements Serializable{ //对象转换为字节序列,这些字节序列可以被完全存储以备以后重新生成原来的对象

private static final long serialVersionUID = 1L;//实现序列化类的不同版本间的兼容性,即在版本升级时反序列化仍保持对象的唯一性

private String id;

private String name;

private String stuclass;

private String email;

private Set<Course>course; //通过set容器来装科目以便于增删查改

private double Average;

private double AllScore;

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 getStuclass() {

return stuclass;

}

public void setStuclass(String stuclass) {

this.stuclass = stuclass;

}

public String getEmail() {

return email;

}

public void setEmail(String email) {

this.email = email;

}

public Set<Course> getCourse() {

return course;

}

public void setCourse(Set<Course> course) {

this.course = course;

}

public double getAverage() {

return Average;

}

public void setAverage(double average) {

Average = average;

}

public double getAllScore() {

return AllScore;

}

public void setAllScore(double allScore) {

AllScore = allScore;

}

public Student(String id, String name, String stuclass, String email) {

this.id = id;

this.name = name;

this.stuclass = stuclass;

this.email = email;

this.course = new HashSet<Course>();

}

}

课程:

package com.xujulong.www.po;

import java.io.Serializable;

public class Course implements Serializable{

/**

*

*/

private static final long serialVersionUID = 1L;

private String subjectName;

private String mark;

private String estimate;

public String getEstimate() {

return estimate;

}

public void setEstimate(String estimate) {

this.estimate = estimate;

}

public String getSubjectName() {

return subjectName;

}

public void setSubjectName(String subjectName) {

this.subjectName = subjectName;

}

public String getMark() {

return mark;

}

public void setMark(String mark) {

this.mark = mark;

}

public Course(String name, String mark,String estimath) {

super();

this.subjectName = name;

this.mark = mark;

this.estimate = estimath;

}

}

教师功能:

package com.xujulong.www.po;

import java.io.BufferedReader;

import java.io.EOFException;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.ObjectInputStream;

import java.util.Iterator;

import java.util.Scanner;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

import com.xujulong.www.view.Menu;

import com.xujulong.www.dao.ReadAndWrite;

import com.xujulong.www.po.Course;

import com.xujulong.www.po.Student;

public class TeacherChangeStudent {

private String id;

private String email;

private String Math;

private String English;

private String Physics;

// private String subject;

private String estimath;

private File stuInformation;

private BufferedReader br;

private Scanner input;

public TeacherChangeStudent(){

input = new Scanner(System.in);

stuInformation = new File("stuInformation.txt");

br = new BufferedReader(new InputStreamReader(System.in)); //创建一个输入缓冲区的缓冲字符输入流

}

public void addStuInformation() {

Student us = null;

ObjectInputStream in = null;

System.out.println("请输入要添加学生的个数");

int m = 0;

/**

* 判断用户输入是否为数字

*/

boolean f = false;

do{

try{

m = Integer.parseInt(br.readLine()); //判断用户输入的是否是数字

f =true;

}catch(NumberFormatException e){

System.out.println("输入错误,请重新输入数字");

}catch(IOException e){

e.printStackTrace();

}

}while(f == false);

for(int i = 1;i <= m;i++){//根据用户输入的个数循环m次

System.out.println("这是第"+i+"个学生的信息");

while(true){

int b = 0;

System.out.println("学号:");

id = input.next();

try{

in = new ObjectInputStream(new FileInputStream(stuInformation));

while(true){

us = (Student) in.readObject();

if(id.equals(us.getId())){

System.out.println("该学号已被占用");

b = 1;

break;

}

}

}catch(ClassNotFoundException e){

e.printStackTrace();

}catch(EOFException e){

System.out.println("该学号能用"); //避免学号相同

}catch(IOException e){

e.printStackTrace();

}finally{

try{

if(in != null) //关闭文件

in.close();

else

in = null;

}catch(IOException e){

e.printStackTrace();

}

}

if(b == 1)

continue;

else

break;

}

System.out.println("姓名:");

String name = input.next();

System.out.println("班级");

String stuclass = input.next();

while(true){

System.out.println("邮箱:");

email = input.next();

String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";

Pattern regex = pile(check); //指定为字符串的正则表达式必须首先被编译为此类的实例

Matcher matcher = regex.matcher(email); //匹配判断邮箱格式

if(matcher.matches()==true){

break;

}

else

System.out.println("输入错误,请重新输入");

}

Student stu = new Student(id,name,stuclass,email);

while(true){

System.out.println("数学:");

Math = input.next();

System.out.println("评价:");

estimath = input.next();

try{

if(Float.parseFloat(Math) >= 0 && Float.parseFloat(Math) <=100){

Course course1 = new Course("数学",Math,estimath);

stu.getCourse().add(course1);

break;

}

else

System.out.println("输入有误,请重新输入");

}catch(NumberFormatException e){

System.out.println("输入错误,请输入数字");//捕抓输入的内容不能转换成数字时的异常

}

}

while(true){

System.out.println("英语:");

English = input.next();

System.out.println("评价:");

estimath = input.next();

try{

if(Float.parseFloat(English) >= 0 && Float.parseFloat(English) <=100){

Course course1 = new Course("英语",English,estimath);

stu.getCourse().add(course1);

break;

}

else

System.out.println("输入有误,请重新输入");

}catch(NumberFormatException e){

System.out.println("输入错误,请输入数字");//捕抓输入的内容不能转换成数字时的异常

}

}

while(true){

System.out.println("物理:");

Physics = input.next();

System.out.println("评价:");

estimath = input.next();

try{

if(Float.parseFloat(Physics) >= 0 && Float.parseFloat(Physics) <=100){

Course course1 = new Course("物理",Physics,estimath);

stu.getCourse().add(course1);

break;

}

else

System.out.println("输入有误,请重新输入");

}catch(NumberFormatException e){

System.out.println("输入错误,请输入数字");//捕抓输入的内容不能转换成数字时的异常

}

}

ReadAndWrite wri = new ReadAndWrite();

wri.write(stu);

}

System.out.println("添加成功");

Menu menu = new Menu();

menu.teacherGetIn();

}

public void selectStuInformation(){

Student us = null;

ObjectInputStream in = null;

try{

System.out.println("请输入需要查询学生的学号或姓名");

String snum = input.next();

in = new ObjectInputStream(new FileInputStream(stuInformation));

while(true){

us = (Student) in.readObject();

if(snum.equals(us.getId()) || snum.equals(us.getName())){

System.out.println("学号:"+us.getId()+"\n姓名:"+us.getName()+"\n班级:"+us.getStuclass()+"\n邮箱:"+us.getEmail());

Iterator<Course> ite = us.getCourse().iterator(); //set容器的迭代器

while(ite.hasNext()){//遍历迭代器里的元素

Course cou = ite.next();

System.out.print(cou.getSubjectName());

System.out.print(":"+cou.getMark());

System.out.println(" "+cou.getEstimate());

}

break;

}

}

}catch(ClassNotFoundException e){

e.printStackTrace();

}catch(EOFException e){

System.out.println("该学生不存在");

try{

if(in != null)

in.close();

else

in = null;

}catch(IOException e1){

e1.printStackTrace();

}

Menu menu = new Menu();

menu.teacherGetIn();

}catch(IOException e){

e.printStackTrace();

}finally{

try{

if(in != null)

in.close();

else

in = null;

}catch(IOException e1){

e1.printStackTrace();

}

}

Menu menu = new Menu();

menu.teacherGetIn();

}

public void selectClassInformation(){

Student us = null;

ObjectInputStream in = null;

try{

System.out.println("请输入需要查询学生的班级");

String stuclass = input.next();

in = new ObjectInputStream(new FileInputStream(stuInformation));

while(true){

us = (Student) in.readObject();

if(stuclass.equals(us.getStuclass())){

System.out.println("学号:"+us.getId()+"\n姓名:"+us.getName()+"\n班级:"+us.getStuclass()+"\n邮箱:"+us.getEmail());

Iterator<Course> ite = us.getCourse().iterator();

while(ite.hasNext()){

Course cou = ite.next();

System.out.print(cou.getSubjectName());

System.out.print(":"+cou.getMark());

System.out.println(" "+cou.getEstimate());

}

}

}

}catch(ClassNotFoundException e){

e.printStackTrace();

}catch(EOFException e){

try{

if(in != null)

in.close();

else

in = null;

}catch(IOException e1){

e1.printStackTrace();

}

Menu menu = new Menu();

menu.teacherGetIn();

}catch(IOException e){

e.printStackTrace();

}finally{

try{

if(in != null)

in.close();

else

in = null;

}catch(IOException e1){

e1.printStackTrace();

}

}

}

public void selectSubjectInformation(){

Student us = null;

ObjectInputStream in = null;

try{

System.out.println("请输入需要查询学生的学号或姓名");

String snum = input.next();

System.out.println("请输入查询的科目名称");

String subject = input.next();

in = new ObjectInputStream(new FileInputStream(stuInformation));

while(true){

us = (Student) in.readObject();

if(snum.equals(us.getId()) || snum.equals(us.getName())){

Iterator<Course> ite = us.getCourse().iterator();

while(ite.hasNext()){

Course cou = ite.next();

if(subject.equals(cou.getSubjectName()))

{

System.out.println(cou.getSubjectName()+"成绩");

System.out.println(cou.getMark());

System.out.println("评价:"+cou.getEstimate());

break;

}

}

break;

}

}

}catch(ClassNotFoundException e){

e.printStackTrace();

}catch(EOFException e){

System.out.println("该学生不存在");

try{

if(in != null)

in.close();

else

in = null;

}catch(IOException e1){

e1.printStackTrace();

}

Menu menu = new Menu();

menu.teacherGetIn();

}catch(IOException e){

e.printStackTrace();

}finally{

try{

if(in != null)

in.close();

else

in = null;

}catch(IOException e1){

e1.printStackTrace();

}

}

Menu menu = new Menu();

menu.teacherGetIn();

}

public void deleteStuInformation() {

System.out.println("请输入需要删除的学生学号");

String stnum = input.next();

ReadAndWrite raw = new ReadAndWrite();

raw.readAndWrite(stnum);

System.out.println("删除成功");

Menu menu = new Menu();

menu.teacherGetIn();

}

public void changeStuInformation(){

System.out.println("请输入需要修改的学生学号");

String stnum = input.next();

ReadAndWrite raw = new ReadAndWrite();

raw.readAndWrite(stnum);

System.out.println("请输入修改后的信息");

System.out.println("姓名:");

String name = input.next();

System.out.println("班级:");

String stuclass = input.next();

while(true){

System.out.println("邮箱:");

email = input.next();

String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";

Pattern regex = pile(check);

Matcher matcher = regex.matcher(email);

if(matcher.matches()==true){

break;

}

else

System.out.println("输入错误,请重新输入");

}

Student stu = new Student(stnum,name,stuclass,email);

while(true){

System.out.println("数学:");

Math = input.next();

System.out.println("评价:");

estimath = input.next();

try{

if(Float.parseFloat(Math) >= 0 && Float.parseFloat(Math) <=100){

Course course1 = new Course("数学",Math,estimath);

stu.getCourse().add(course1);

break;

}

else

System.out.println("输入错误,请重新输入");

}catch(NumberFormatException e){

System.out.println("输入错误,请输入数字");

}

}

while(true){

System.out.println("英语:");

English = input.next();

System.out.println("评价:");

estimath = input.next();

try{

if(Float.parseFloat(English) >= 0 && Float.parseFloat(English) <=100){

Course course1 = new Course("英语",English,estimath);

stu.getCourse().add(course1);

break;

}

else

System.out.println("输入错误,请重新输入");

}catch(NumberFormatException e){

System.out.println("输入错误,请输入数字");

}

}

while(true){

System.out.println("物理");

Physics = input.next();

System.out.println("评价:");

estimath = input.next();

try{

if(Float.parseFloat(Physics) >= 0 && Float.parseFloat(Physics) <=100){

Course course1 = new Course("物理",Physics,estimath);

stu.getCourse().add(course1);

break;

}

else

System.out.println("输入错误,请重新输入");

}catch(NumberFormatException e){

System.out.println("输入错误,请输入数字");

}

}

ReadAndWrite wri = new ReadAndWrite();

wri.write(stu);

System.out.println("修改成功");

Menu menu = new Menu();

menu.teacherGetIn();

}

}

管理员功能:

package com.xujulong.www.po;

import java.io.BufferedReader;

import java.io.EOFException;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.ObjectInputStream;

import java.util.Iterator;

import java.util.Scanner;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

import com.xujulong.www.view.Menu;

import com.xujulong.www.dao.ReadAndWrite;

import com.xujulong.www.po.Course;

import com.xujulong.www.po.Student;

public class ManagerChangeStudent {

Scanner input;

private String id;

private String email;

private String Math;

private String English;

private String Physics;

// private String subject;

private String estimath;

private File stuInformation;

private BufferedReader br;

public ManagerChangeStudent(){

input = new Scanner(System.in);

stuInformation = new File("stuInformation.txt");

br = new BufferedReader(new InputStreamReader(System.in)); //创建一个输入缓冲区的缓冲字符输入流

}

public void addStuInformation() {

Student us = null;

ObjectInputStream in = null;

System.out.println("请输入要添加学生的个数");

int m = 0;

boolean f = false;

do{

try{

m = Integer.parseInt(br.readLine()); //判断用户输入的是否是数字

f =true;

}catch(NumberFormatException e){

System.out.println("输入错误,请重新输入数字");

}catch(IOException e){

e.printStackTrace();

}

}while(f == false);

for(int i = 1;i <= m;i++){ //根据用户输入的个数循环m次

System.out.println("这是第"+i+"个学生的信息");

while(true){

int b = 0;

System.out.println("学号:");

id = input.next();

try{

in = new ObjectInputStream(new FileInputStream(stuInformation));

while(true){

us = (Student) in.readObject();

if(id.equals(us.getId())){

System.out.println("该学号已被占用"); //避免相同学号

b = 1;

break;

}

}

}catch(ClassNotFoundException e){

e.printStackTrace();

}catch(EOFException e){

System.out.println("该学号能用");

}catch(IOException e){

e.printStackTrace();

}finally{

try{

if(in != null) //关闭文件

in.close();

else

in = null;

}catch(IOException e){

e.printStackTrace();

}

}

if(b == 1)//当b == 1时证明学号被占用,所以继续输入学号

continue;

else

break; //当b != 1时跳出循环

}

System.out.println("姓名:");

String name = input.next();

System.out.println("班级:");

String stuclass = input.next();

while(true){

System.out.println("邮箱:");

email = input.next();

String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";

Pattern regex = pile(check); //指定为字符串的正则表达式必须首先被编译为此类的实例。然后,可将得到的模式用于创建 Matcher 对象

Matcher matcher = regex.matcher(email); //匹配判断邮箱格式

if(matcher.matches()==true){

break;

}

else

System.out.println("输入错误,请重新输入");

}

Student stu = new Student(id,name,stuclass,email);

System.out.println("科目为数英物还是自定义?");

System.out.println("1---数英物"); //管理员可以选择增删该学生的科目

System.out.println("2---自定义");

int num = 0 ;

boolean f1 = false;

while(true){

do{

try{

num = Integer.parseInt(br.readLine());

f1 =true;

}catch(NumberFormatException e){

System.out.println("输入错误,请重新输入数字");

}catch(IOException e){

e.printStackTrace();

}

}while(f1 == false);

if(num == 1){

while(true){

System.out.println("数学:");

Math = input.next();

System.out.println("评价:");

estimath = input.next();

try{

if(Float.parseFloat(Math) >= 0 && Float.parseFloat(Math) <=100){ //将String的内容转换为float,判断用户输入的是否只有数字

Course course1 = new Course("数学",Math,estimath);

stu.getCourse().add(course1);

break;

}

else

System.out.println("输入有误,请重新输入");

}catch(NumberFormatException e){

System.out.println("输入错误,请输入数字");//捕抓输入的内容不能转换成数字时的异常

}

}

while(true){

System.out.println("英语:");

English = input.next();

System.out.println("评价:");

estimath = input.next();

try{

if(Float.parseFloat(English) >= 0 && Float.parseFloat(English) <=100){

Course course1 = new Course("英语",English,estimath);

stu.getCourse().add(course1);

break;

}

else

System.out.println("输入有误,请重新输入");

}catch(NumberFormatException e){

System.out.println("输入错误,请输入数字");//捕抓输入的内容不能转换成数字时的异常

}

}

while(true){

System.out.println("物理:");

Physics = input.next();

System.out.println("评价:");

estimath = input.next();

try{

if(Float.parseFloat(Physics) >= 0 && Float.parseFloat(Physics) <=100){

Course course1 = new Course("物理",Physics,estimath);

stu.getCourse().add(course1);

break;

}

else

System.out.println("输入有误,请重新输入");

}catch(NumberFormatException e){

System.out.println("输入错误,请输入数字");//捕抓输入的内容不能转换成数字时的异常

}

}

break;

}

else if(num == 2){

System.out.println("请输入学生科目总数");

int numb = 0;

boolean f2 = false;

do{

try{

numb = Integer.parseInt(br.readLine());

f2 =true;

}catch(NumberFormatException e){

System.out.println("输入错误,请重新输入数字");

}catch(IOException e){

e.printStackTrace();

}

}while(f2 == false);

for(int j = 1;j <= numb;j++){

while(true){

System.out.println("这是第"+j+"门学科的信息");

System.out.println("科目名称");

String subjectname = input.next();

System.out.println("科目分数");

String subjectmark = input.next();

System.out.println("评价:");

estimath = input.next();

try{

if(Float.parseFloat(subjectmark) >= 0 && Float.parseFloat(subjectmark) <=100){

Course course = new Course(subjectname,subjectmark,estimath);

stu.getCourse().add(course);

break;

}

else

System.out.println("输入有误,请重新输入");

}catch(NumberFormatException e){

System.out.println("输入错误,请输入数字");//捕抓输入的内容不能转换成数字时的异常

}

}

}

break;

}

else

{

System.out.println("输入错误,请输入正确数字");

continue;

}

}

ReadAndWrite wri = new ReadAndWrite();

wri.write(stu);

}

System.out.println("添加成功");

Menu menu = new Menu();

menu.managerGetIn();

}

public void selectStuInformation(){

Student us = null;

ObjectInputStream in = null;

try{

System.out.println("请输入需要查询学生的学号或姓名");

String snum = input.next();

in = new ObjectInputStream(new FileInputStream(stuInformation));

while(true){

us = (Student) in.readObject();

if(snum.equals(us.getId()) || snum.equals(us.getName())){

System.out.println("学号:"+us.getId()+"\n姓名:"+us.getName()+"\n班级:"+us.getStuclass()+"\n邮箱:"+us.getEmail());

Iterator<Course> ite = us.getCourse().iterator();

while(ite.hasNext()){

Course cou = ite.next();

System.out.print(cou.getSubjectName());

System.out.print(":"+cou.getMark());

System.out.println(" "+cou.getEstimate());

}

break;

}

}

}catch(ClassNotFoundException e){ //捕抓 没有找到具有指定名称的类

e.printStackTrace();

}catch(EOFException e){

System.out.println("该学生不存在"); //捕抓到达流的末尾来判断没有该学生

try{

if(in != null)

in.close();

else

in = null;

}catch(IOException e1){//捕抓其它IO异常

e1.printStackTrace();

}

Menu menu = new Menu();

menu.managerGetIn();

}catch(IOException e){

e.printStackTrace();

}finally{

try{

if(in != null)

in.close();

else

in = null;

}catch(IOException e1){

e1.printStackTrace();

}

}

Menu menu = new Menu();

menu.managerGetIn();

}

public void selectClassInformation(){

Student us = null;

ObjectInputStream in = null;

try{

System.out.println("请输入需要查询学生的班级");

String stuclass = input.next();

in = new ObjectInputStream(new FileInputStream(stuInformation));

while(true){

us = (Student) in.readObject();

if(stuclass.equals(us.getStuclass())){

System.out.println("学号:"+us.getId()+"\n姓名:"+us.getName()+"\n班级:"+us.getStuclass()+"\n邮箱:"+us.getEmail());

Iterator<Course> ite = us.getCourse().iterator();

while(ite.hasNext()){

Course cou = ite.next();

System.out.print(cou.getSubjectName());

System.out.println(":"+cou.getMark());

System.out.println(" "+cou.getEstimate());

}

}

}

}catch(ClassNotFoundException e){

e.printStackTrace();

}catch(EOFException e){

try{

if(in != null)

in.close();

else

in = null;

}catch(IOException e1){

e1.printStackTrace();

}

Menu menu = new Menu();

menu.teacherGetIn();

}catch(IOException e){

e.printStackTrace();

}finally{

try{

if(in != null)

in.close();

else

in = null;

}catch(IOException e1){

e1.printStackTrace();

}

}

}

public void selectSubjectInformation(){

Student us = null;

ObjectInputStream in = null;

try{

System.out.println("请输入需要查询学生的学号或姓名");

String snum = input.next();

System.out.println("请输入查询的科目名称");

String subject = input.next();

in = new ObjectInputStream(new FileInputStream(stuInformation));

while(true){

us = (Student) in.readObject();

if(snum.equals(us.getId()) || snum.equals(us.getName())){

Iterator<Course> ite = us.getCourse().iterator();

while(ite.hasNext()){

Course cou = ite.next();

if(subject.equals(cou.getSubjectName()))

{

System.out.println(cou.getSubjectName()+"成绩");

System.out.println(cou.getMark());

System.out.println("评价:"+cou.getEstimate());

break;

}

}

break;

}

}

}catch(ClassNotFoundException e){

e.printStackTrace();

}catch(EOFException e){

System.out.println("该学生不存在");

try{

if(in != null)

in.close();

else

in = null;

}catch(IOException e1){

e1.printStackTrace();

}

Menu menu = new Menu();

menu.teacherGetIn();

}catch(IOException e){

e.printStackTrace();

}finally{

try{

if(in != null)

in.close();

else

in = null;

}catch(IOException e1){

e1.printStackTrace();

}

}

}

public void deleteStuInformation() {

System.out.println("请输入需要删除的学生学号");

String stnum = input.next();

ReadAndWrite raw = new ReadAndWrite();

raw.readAndWrite(stnum);

System.out.println("删除成功");

Menu menu = new Menu();

menu.managerGetIn();

}

public void changeStuInformation(){

System.out.println("请输入需要修改的学生学号");

String stnum = input.next();

ReadAndWrite raw = new ReadAndWrite();

raw.readAndWrite(stnum);

System.out.println("请输入修改后的信息");

System.out.println("姓名");

String name = input.next();

System.out.println("班级");

String stuclass = input.next();

while(true){

System.out.println("邮箱");

email = input.next();

String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";

Pattern regex = pile(check);

Matcher matcher = regex.matcher(email);

if(matcher.matches()==true){

break;

}

else

System.out.println("输入错误,请重新输入");

}

Student stu = new Student(stnum,name,stuclass,email);

System.out.println("科目为数英物还是自定义?");

System.out.println("1---数英物"); //管理员可以选择增删该学生的科目

System.out.println("2---自定义");

int num = 0 ;

boolean f = false;

while(true){

do{

try{

num = Integer.parseInt(br.readLine());

f =true;

}catch(NumberFormatException e){

System.out.println("输入错误,请重新输入数字");

}catch(IOException e){

e.printStackTrace();

}

}while(f == false);

if(num == 1){

while(true){

System.out.println("数学:");

Math = input.next();

System.out.println("评价:");

estimath = input.next();

try{

if(Float.parseFloat(Math) >= 0 && Float.parseFloat(Math) <=100){

Course course1 = new Course("数学",Math,estimath);

stu.getCourse().add(course1);

break;

}

else

System.out.println("输入错误,请重新输入");

}catch(NumberFormatException e){

System.out.println("输入错误,请输入数字");

}

}

while(true){

System.out.println("英语:");

English = input.next();

System.out.println("评价:");

estimath = input.next();

try{

if(Float.parseFloat(English) >= 0 && Float.parseFloat(English) <=100){

Course course1 = new Course("英语",English,estimath);

stu.getCourse().add(course1);

break;

}

else

System.out.println("输入错误,请重新输入");

}catch(NumberFormatException e){

System.out.println("输入错误,请输入数字");

}

}

while(true){

System.out.println("物理:");

Physics = input.next();

System.out.println("评价:");

estimath = input.next();

try{

if(Float.parseFloat(Physics) >= 0 && Float.parseFloat(Physics) <=100){

Course course1 = new Course("物理",Physics,estimath);

stu.getCourse().add(course1);

break;

}

else

System.out.println("输入错误,请重新输入");

}catch(NumberFormatException e){

System.out.println("输入错误,请输入数字");

}

}

break;

}

else if(num == 2){

System.out.println("请输入学生科目总数");

int m = 0;

// jt.judgeType(m);

boolean f1 = false;

do{

try{

m = Integer.parseInt(br.readLine());

f1 =true;

}catch(NumberFormatException e){

System.out.println("输入错误,请重新输入数字");

}catch(IOException e){

e.printStackTrace();

}

}while(f1 == false);

for(int i = 1;i <= m;i++){

while(true){

System.out.println("这是第"+i+"门学科的信息");

System.out.println("科目名称");

String subjectname = input.next();

System.out.println("科目分数");

String subjectmark = input.next();

System.out.println("评价:");

estimath = input.next();

try{

if(Float.parseFloat(subjectmark) >= 0 && Float.parseFloat(subjectmark) <=100){

Course course = new Course(subjectname,subjectmark,estimath);

stu.getCourse().add(course);

break;

}

else

System.out.println("输入有误,请重新输入");

}catch(NumberFormatException e){

System.out.println("输入错误,请输入数字");//捕抓输入的内容不能转换成数字时的异常

}

}

}

break;

}

else

{

System.out.println("输入错误,请输入正确数字");

continue;

}

}

ReadAndWrite wri = new ReadAndWrite();

wri.write(stu);

System.out.println("修改成功");

Menu menu = new Menu();

menu.managerGetIn();

}

}

dao包下的读写文件:

创建文件

package com.xujulong.www.dao;

import java.io.File;

import java.io.IOException;

import com.xujulong.www.po.Course;

import com.xujulong.www.po.Student;

public class CreatFile {

private File stuInformation;

public CreatFile(){

stuInformation = new File("stuInformation.txt");

}

/**

* 创建一个文件来储存学生信息

* 先录入了一个学生

* 用户可根据需要添加

*/

public void creatFile() {

try{

if(!stuInformation.exists()) {

stuInformation.createNewFile();

Student stu1 = new Student("100","小明","网络1班","12345678@");

Course course1 = new Course("数学","80","优");

stu1.getCourse().add(course1);

Course course2 = new Course("英语","80","优");

stu1.getCourse().add(course2);

Course course3 = new Course("物理","80","优");

stu1.getCourse().add(course3);

ReadAndWrite wri = new ReadAndWrite();

wri.write(stu1);

}

}catch(IOException e){

e.printStackTrace();

}

}

}

重写ObjectOutputStream:

package com.xujulong.www.dao;

import java.io.IOException;

import java.io.ObjectOutputStream;

import java.io.OutputStream;

/**

* 该类用于解决ObjectOutputStream追加对象时,读取错误的问题

* 因为ObjectOutputStream建立后第一次写入一个对象时, 会在对象数据前写入一些标志的数据

*因此写多一个方法判断是不是第一次写入一个文件,如果是向一个文件大小不为零的文件追加的话,就不需要在对象前写标志的数据

*/

public class MyObjectOutputStream extends ObjectOutputStream {

protected MyObjectOutputStream() throws IOException{ //第一次写文件时保留标志

super();

}

public MyObjectOutputStream(OutputStream out) throws IOException { //追加时不需要标志

super(out);

}

//

@Override

protected void writeStreamHeader() {

return ;

}

}

读和写:

package com.xujulong.www.dao;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.EOFException;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.io.OutputStream;

import java.util.Iterator;

import com.xujulong.www.po.Course;

import com.xujulong.www.po.Student;

import com.xujulong.www.dao.MyObjectOutputStream;

public class ReadAndWrite {

private File stuInformation;

private File stuInformation1;

private OutputStream out1;

public ReadAndWrite(){

stuInformation = new File("stuInformation.txt");

stuInformation1 = new File("stuInformation1.txt");

try {

out1 = new FileOutputStream(stuInformation, true);

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

public void write(Student stu) {

ObjectOutputStream out = null;

try{

if(stuInformation.length()<1)

out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(stuInformation)));

else

out = new MyObjectOutputStream(out1);

out.writeObject(stu);

}catch(FileNotFoundException e){

e.printStackTrace();

}catch(IOException e){

e.printStackTrace();

}finally{//关闭文件

try{

if(out != null)

out.close();

else

out = null;

out1.close();

}catch(IOException e){

e.printStackTrace();

}

}

}

/**

* 把除了要删除或修改的学生之外的所有学生复制到另一个文件

* 再把原文件删除

* 把复制文件更名为原文件

* @param num

*/

public void readAndWrite(String num) {

Student stu = null;

ObjectInputStream in = null;

ObjectOutputStream out = null;

try{

in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(stuInformation)));

if(stuInformation1.length()<1)

out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(stuInformation1)));

else

out = new MyObjectOutputStream(out1);

while(true)

{

stu = (Student) in.readObject();

if(stu.getId().equals(num)){

System.out.println("该学生信息为");

System.out.println("学号:"+stu.getId()+"\n姓名:"+stu.getName()+"\n班级:"+stu.getStuclass()+"\n邮箱:"+stu.getEmail());

Iterator<Course> ite = stu.getCourse().iterator();

while(ite.hasNext()){

Course cou = ite.next();

System.out.print(cou.getSubjectName());

System.out.println(":"+cou.getMark());

System.out.println("评价:"+cou.getEstimate());

}

continue;

}

out.writeObject(stu);

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch(ClassNotFoundException e){

e.printStackTrace();

}catch(EOFException e){

}catch (IOException e)

{

e.printStackTrace();

}

finally{//把输入流文件和输出流文件关闭

try{

if(in != null)

in.close();

else

in = null;

if(out != null)

out.close();

else

out = null;

out1.close();

}catch(IOException e){

e.printStackTrace();

}

}

this.stuInformation.delete();

this.stuInformation1.renameTo(new File("stuinformation.txt"));

}

}

service包下:

功能选择:

package com.xujulong.www.service;

import com.xujulong.www.po.TeacherChangeStudent;

import com.xujulong.www.po.ManagerChangeStudent;

import com.xujulong.www.view.Menu;

public class ChooseFunction {

/**

* 不同用户有不同权限,所以要分开两个方法

* 判断用户要进行的功能

*/

public void teacherchoose(int number){

Menu menu = new Menu();

TeacherChangeStudent change = new TeacherChangeStudent();

if(number == 1 ){

change.addStuInformation();

}

else if(number == 2){

change.changeStuInformation();

}

else if(number == 3){

change.deleteStuInformation();

}

else if(number == 4){

change.selectStuInformation();

}

else if (number == 5){

change.selectClassInformation();

}

else if(number == 6){

change.selectSubjectInformation();

}

else if(number == 7){

menu.show();

}

}

public void managerchoose(int number){

Menu menu = new Menu();

ManagerChangeStudent managerchange = new ManagerChangeStudent();

if(number == 1 ){

managerchange.addStuInformation();

}

else if(number == 2){

managerchange.changeStuInformation();

}

else if(number == 3){

managerchange.deleteStuInformation();

}

if(number == 4){

managerchange.selectStuInformation();

}

else if(number == 5){

managerchange.selectClassInformation();

}

else if(number == 6){

managerchange.selectSubjectInformation();

}

else if (number == 7){

menu.show();

}

}

}

主函数(main函数):

package com.xujulong.www.service;

import com.xujulong.www.dao.CreatFile;

import com.xujulong.www.view.Menu;

public class Test {

public static void main(String[] args) {

CreatFile creat = new CreatFile();

creat.creatFile();

Menu menu = new Menu();

menu.show();

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。