您的位置:首页 > 其它

考试管理系统,具备简单的注册,考试,查看历次成绩,修改密码等功能

2018-11-12 09:01 274 查看

package com.ems.tomzhang;
/**

  • 这是一个考试管理系统
  • 包含注册,考试,查看历史成绩,修改密码等功能
  • @author :Tom:Zhang
    */

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Scanner;

public class ExamManagemSystem {
public void showInfo() {
Scanner sc=new Scanner(System.in);
System.out.println(“欢迎来到XXX考试系统****”);
while(true){
System.out.println("\t1-注册;2-开始考试;3-查看历次成绩;4-修改密码;5-退出系统");
System.out.print(“请选择想要执行的操作:”);
int choice=Integer.parseInt(sc.nextLine());
switch (choice) {
case 1:
System.out.print(“请输入用户名:”);
String username=sc.nextLine();
System.out.print(“请输入密码:”);
String pwd=sc.nextLine();
try {
register(username,pwd);
} catch (IOException e1) {
e1.printStackTrace();
}
break;
case 2:
Exam e=null;
try {
e = generateExam();//生成考场(一张试卷,若干考生)
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.print(“请输入您的姓名:”);
String name=sc.nextLine();
System.out.print(“请输入您的密码:”);
pwd=sc.nextLine();
boolean flag=check(name,pwd,e.getStu());//校验学生是否存在
if(flag){
System.out.println("\t\t"+name+“同学,请开始答题!”);
LinkedHashMap<String, String> myAnswer = beginExam(e);
int score=checkAnswer(myAnswer,e.getPaper());//批改试卷
System.out.println(name+“本次考试得分:”+score);
Date d=new Date();
DateFormat df=new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
String date=df.format(d);
try {
scoreIntoDB(name,pwd,score,date);
} catch (IOException e1) {
e1.printStackTrace();
}
}
break;
case 3:
try {
verifyInfo();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
break;
case 4:
try {
changePassword();
} catch (IOException e1) {
e1.printStackTrace();
}
break;
case 5:
sc.close();
System.exit(0);
break;
default:
System.out.println(“输入错误,请重新输入!”);
break;
}
}

}
/*
* 查看考试成绩前的信息校验
*/
public  void verifyInfo() throws IOException {
@SuppressWarnings("resource")
Scanner sc=new Scanner(System.in);
System.out.print("请输入用户名:");
String name=sc.nextLine();
System.out.print("请输入密码:");
String pwd=sc.nextLine();
ArrayList<Student> student = readDataBase();
boolean flag = check(name, pwd, student);
if(flag){
try {
showMyScore(name);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
/*
* 查看历次考试成绩
*/
public  void showMyScore(String name) throws IOException, ClassNotFoundException {
File f=new File("E:\\EMS\\score");
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(f));
ArrayList<Student> stuList=new ArrayList<>();
Student student=null;
try {
while((student=(Student) ois.readObject())!=null){
stuList.add(student);
}
} catch (EOFException e) {

}
boolean flag=false;//用来记录是否该用户是否参加过考试
for (int i = 0; i < stuList.size(); i++) {
if(stuList.get(i).getName()
4000
.equals(name)){
flag=true;
break;
}
}
if(flag){
System.out.println(name+"的历次考试成绩如下:");
for (int i = 0; i < stuList.size(); i++) {
if(stuList.get(i).getName().equals(name)){
System.out.println("分数:"+stuList.get(i).getScore()+"\t时间:"+stuList.get(i).getDate());
}
}
}else{
System.out.println("您还没有参加过考试!");
}
ois.close();

}

/*
* 成绩写入数据库
*/
public  void scoreIntoDB(String name, String pwd, int score, String date) throws IOException {
Student student=new Student();
student.setName(name);
student.setPwd(pwd);
student.setDate(date);
student.setScore(score);
File f=new File("E:\\EMS\\score");
ObjectOutputStream oos=null;
if(f.length()<1){
oos=new ObjectOutputStream(new FileOutputStream(f,true));
oos.writeObject(student);
}else{
oos=new MyObjectOutputStream(new FileOutputStream(f,true));
oos.writeObject(student);
}
oos.close();

}
/*
* 修改密码
*/
public  void changePassword() throws IOException {
@SuppressWarnings("resource")
Scanner sc=new Scanner(System.in);
System.out.print("请输入用户名:");
String name=sc.nextLine();
System.out.print("请输入密码:");
String pwd=sc.nextLine();
ArrayList<Student> student =readDataBase();
for (int i = 0; i < student.size(); i++) {
if(student.get(i).getName().equals(name)&&student.get(i).getPwd().equals(pwd)){
System.out.print("请输入新密码:");
String newPwd1=sc.nextLine();
System.out.print("请再次输入新密码:");
String newPwd2=sc.nextLine();
if(newPwd1.equals(newPwd2)){
Student s=new Student();
s.setName(name);
s.setPwd(newPwd1);
student.set(i, s);
writeDataBase(student);
System.out.println("修改成功!");
return;
}else{
System.out.println("两次新密码不一致,请重新操作!");
return;
}

}
if(student.get(i).getName().equals(name)&&!student.get(i).getPwd().equals(pwd)){
System.out.println("密码错误,请重新操作!");
return;

}
if(!student.get(i).getName().equals(name)&&!student.get(i).getPwd().equals(pwd)){
if(i==student.size()-1){
System.out.println("您还未注册,请先注册!");
}
}
}

}

/*
* 注册功能
*/
public  void register(String username, String pwd) throws IOException {

ArrayList<Student> stuList = readDataBase();
for (int i = 0; i < stuList.size(); i++) {
if(stuList.get(i).getName().equals(username)){
System.out.println("您已注册,无须重新注册!");
return;
}
}
Student stu=new Student();
stu.setName(username);
stu.setPwd(pwd);
stuList.add(stu);
writeDataBase(stuList);
System.out.println("注册成功!");

}
/*
* 用户信息写入文本
*/
public  void writeDataBase(ArrayList<Student> stuList) throws IOException {
BufferedWriter bw=new BufferedWriter(new FileWriter("E:\\EMS\\userInformation.txt"));
for (int i = 0; i < stuList.size(); i++) {
bw.write(stuList.get(i).getName()+"\t"+stuList.get(i).getPwd());
bw.newLine();
bw.flush();
}
bw.close();
}

/*
* 读取用户信息文本
*/
public  ArrayList<Student> readDataBase() throws IOException {
BufferedReader br=new BufferedReader(new FileReader("E:\\EMS\\userInformation.txt"));
String st=null;
ArrayList<Student> stuList=new ArrayList<>();
while((st=br.readLine())!=null){
Student student=new Student();
String[] ss=st.split("\t");
student.setName(ss[0]);
student.setPwd(ss[1]);
stuList.add(student);
}
br.close();
return stuList;
}

/*
* 批改试卷
*/
public  int checkAnswer(LinkedHashMap<String, String> myAnswer, LinkedHashMap<Integer, Question> paper) {
int score=0;
//LinkedHashMap<Integer, Question> paper = e.getPaper();
for (String key :myAnswer.keySet()) {
for ( Integer question : paper.keySet()) {
if(paper.get(question).getQid().equals(key)&&myAnswer.get(key).equals(paper.get(question).getRightAnswer())){
score+=paper.get(question).getScore();
}
}
}
return score;

}

/*
* 考生开始答题
*/
public  LinkedHashMap<String, String> beginExam(Exam e) {
LinkedHashMap<Integer, Question> paper=e.getPaper();
@SuppressWarnings("resource")
Scanner sc=new Scanner(System.in);
LinkedHashMap<String, String> myAnswer=new LinkedHashMap<>();
for ( Integer key : paper.keySet()) {
System.out.println(paper.get(key));
System.out.print("请选择您的答案:");
String mychoice=sc.nextLine();
myAnswer.put(paper.get(key).getQid(), mychoice);
}
return myAnswer;

}

/*
* 生成考场
*/
public  Exam generateExam() throws IOException {
LinkedHashMap<Integer, Question> paper = null;
try {
paper = paperInitial();//初始化试卷
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}

ArrayList<Student> stuList = readDataBase();//获取学生名单

Exam e=new Exam(paper,stuList);
return e;

}

/*
* 校验学生信息
*/
public  boolean check(String name, String pwd, ArrayList<Student> student) {
//ArrayList<Student> student = e.getStu();
for (int i = 0; i < student.size(); i++) {
if(student.get(i).getName().equals(name)&&student.get(i).getPwd().equals(pwd)){
return true;
}
if(student.get(i).getName().equals(name)&&!student.get(i).getPwd().equals(pwd)){
System.out.println("密码错误,请重新操作!");
return false;
}
if(!student.get(i).getName().equals(name)&&!student.get(i).getPwd().equals(pwd)){
if(i==student.size()-1){
System.out.println("您还未注册,请先注册!");
}
}
}
return false;

}

/*
* 初始化试卷
*/
public  LinkedHashMap<Integer, Question> paperInitial() throws IOException, ClassNotFoundException {
LinkedHashMap<Integer, Question> paper=new LinkedHashMap<>();
File file=new File("E:\\EMS\\questionList.txt");
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));
Question st=null;
try {
while((st=(Question) ois.readObject())!=null){
paper.put(Integer.parseInt(st.getQid()), st);
}

} catch (EOFException e) {//EOFException,反序列化时,读到文件末尾时,JVM自动抛出此异常
//处理方法有两种:1.是try-catch捕获
//2.在序列化时候在最后序列化一个null进入文件
//System.out.println("读取结束");
}finally{
ois.close();
}
return paper;
}

}

package com.ems.tomzhang;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
/*

  • 这个类是为了解决向同一个文件追加写入序列化对象时会重复写入序列化对象header的问题,通过重写writeStreamHeader()方法
    */
    public class MyObjectOutputStream extends ObjectOutputStream{

    public MyObjectOutputStream(OutputStream out) throws IOException {
    super(out);
    }
    @Override
    protected void writeStreamHeader() throws IOException {

    return;

    }

}

package com.ems.tomzhang;
/*

  • 考试类,包含学生列表和试卷
    */
    import java.util.ArrayList;
    import java.util.LinkedHashMap;

public class Exam {
private LinkedHashMap<Integer, Question> paper;//考卷
private ArrayList stu;//学生

public Exam() {
super();
}
/**
* @param paper
* @param stu
*/
public Exam(LinkedHashMap<Integer, Question> paper, ArrayList<Student> stu) {
super();
this.paper = paper;
this.stu = stu;
}
public LinkedHashMap<Integer, Question> getPaper() {
return paper;
}
public void setPaper(LinkedHashMap<Integer, Question> paper) {
this.paper = paper;
}
public ArrayList<Student> getStu() {
return stu;
}
public void setStu(ArrayList<Student> stu) {
this.stu = stu;
}
@Override
public String toString() {
return "Exam [paper=" + paper + ", stu=" + stu + "]";
}

}

package com.ems.tomzhang;
/*

  • 题目类:包括题号,题干,选项,正确答案,分值
    */
    import java.io.Serializable;
    import java.util.HashMap;
    import java.util.LinkedHashMap;

public class Question implements Serializable{
/**
*
/
private static final long serialVersionUID = -7671844670077235917L;
/*
*
*/

private String qid;//题号
private String content;//题干
private LinkedHashMap<Character, String> choices;//选项
private String rightAnswer;//正确答案
private int score;//分值

public Question() {
super();
}

public Question(String qid, String content, LinkedHashMap<Character, String> choices, String rightAnswer, int score) {
super();
this.qid = qid;
this.content = content;
this.choices = choices;
this.rightAnswer = rightAnswer;
this.score = score;
}
public String getQid() {
return qid;
}
public void setQid(String qid) {
this.qid = qid;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public HashMap<Character, String> getChoices() {
return choices;
}
public void setChoices(LinkedHashMap<Character, String> choices) {
this.choices = choices;
}
public String getRightAnswer() {
return rightAnswer;
}
public void setRightAnswer(String rightAnswer) {
this.rightAnswer = rightAnswer;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
String s ="";
for (Character choice : choices.keySet()) {
s+=choice+"."+choices.get(choice)+"\t";
}
return qid+"、"+content+"("+score+"分)"+"\r\n"+s;
}

}

package com.ems.tomzhang;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
/*

  • 生成题库
    /
    public class QuestionGenerate {
    public static void main(String[] args) throws IOException {
    ArrayList question=new ArrayList<>();
    LinkedHashMap<Character, String> choices=new LinkedHashMap<>();
    String content=“以下哪个城市不在安徽省?”;
    choices.put(‘A’, “上海”);
    choices.put(‘B’, “池州”);
    choices.put(‘C’, “合肥”);
    choices.put(‘D’, “巢湖”);
    String rightAnswer=“A”;
    int score=5;;
    Question q=new Question(“1”, content, choices, rightAnswer, score);
    question.add(q);
    content="33+3%3=?";
    choices=new LinkedHashMap<>();
    choices.put(‘A’, “9”);
    choices.put(‘B’, “8”);
    choices.put(‘C’, “10”);
    choices.put(‘D’, “18”);
    rightAnswer=“A”;
    score=5;;
    q=new Question(“2”, content, choices, rightAnswer, score);
    question.add(q);
    ObjectOutputStream oos=null;
    for (Question question2 : question) {
    File f=new File(“E:\EMS\questionList.txt”);
    //用ObjectOutputStream多次序列化对象写入文件时,每次都会向文件中序列化一个header
    //为了避免这种情况,自定义一个类MyObjectOutputStream继承ObjectOutputStream
    //重写writeStreamHeader()方法
    if(f.length()<1){
    oos=new ObjectOutputStream(new FileOutputStream(f,true));
    oos.writeObject(question2);
    }else{
    oos=new MyObjectOutputStream(new FileOutputStream(f,true));
    oos.writeObject(question2);
    }

    //System.out.println(question2);
    }
    System.out.println("题库已生成!");
    oos.close();

    }
    }

package com.ems.tomzhang;

/*

  • 学生类:包括姓名,密码,成绩,考试试卷deng
    */
    import java.io.Serializable;
    //import java.util.LinkedHashMap;

public class Student implements Serializable{
/**
*
/
private static final long serialVersionUID = -887677263137315800L;
private String name;//学生姓名
private String pwd;//密码
// private LinkedHashMap<Integer, Question> mypaper;//考卷
// private LinkedHashMap<String, Character> myAnswer ;//答卷
private int score;//成绩
private String date;//每次考试的时间
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
/*
*
/
public Student() {
super();
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
/*
* @param name
* @param mypaper
* @param myAnswer
* @param score
*/
// public Student(String name, LinkedHashMap<Integer, Question> mypaper, LinkedHashMap<String, Character> myAnswer, int score) {
// super();
// this.name = name;
// this.mypaper = mypaper;
// this.myAnswer = myAnswer;
// this.score = score;
// }

public String getName() {
return name;
}
/**
* @param name
* @param pwd
* @param score
* @param date
*/
public Student(String name, String pwd, int score, String date) {
super();
this.name = name;
this.pwd = pwd;
this.score = score;
this.date = date;
}
public void setName(String name) {
this.name = name;
}

// public LinkedHashMap<Integer, Question> getMypaper() {
// return mypaper;
// }
// public void setMypaper(LinkedHashMap<Integer, Question> mypaper) {
// this.mypaper = mypaper;
// }
// public LinkedHashMap<String, Character> getMyAnswer() {
// return myAnswer;
// }
// public void setMyAnswer(LinkedHashMap<String, Character> myAnswer) {
// this.myAnswer = myAnswer;
// }
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return “Student [name=” + name + “, mypaper=” + “, myAnswer=” + “, score=” + score + “]”;
}

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐