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

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 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 = 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 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().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 =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 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 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 readDataBase() throws IOException {
	BufferedReader br=new BufferedReader(new FileReader("E:\\EMS\\userInformation.txt"));
	String st=null;
	ArrayList 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 myAnswer, LinkedHashMap paper) {
	int score=0;
	//LinkedHashMap 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 beginExam(Exam e) {
	LinkedHashMap paper=e.getPaper();
	@SuppressWarnings("resource")
	Scanner sc=new Scanner(System.in);
	LinkedHashMap 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 paper = null;
	try {
		paper = paperInitial();//初始化试卷
	} catch (ClassNotFoundException e1) {
		e1.printStackTrace();
	}

	ArrayList stuList = readDataBase();//获取学生名单
	
	Exam e=new Exam(paper,stuList);
	return e;
	
}

/*
 * 校验学生信息
 */
public  boolean check(String name, String pwd, ArrayList student) {
	//ArrayList 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 paperInitial() throws IOException, ClassNotFoundException {
	LinkedHashMap 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 paper;//考卷
private ArrayList stu;//学生

public Exam() {
	super();
}
/**
 * @param paper
 * @param stu
 */
public Exam(LinkedHashMap paper, ArrayList stu) {
	super();
	this.paper = paper;
	this.stu = stu;
}
public LinkedHashMap getPaper() {
	return paper;
}
public void setPaper(LinkedHashMap paper) {
	this.paper = paper;
}
public ArrayList getStu() {
	return stu;
}
public void setStu(ArrayList 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 choices;//选项
private String rightAnswer;//正确答案
private int score;//分值

public Question() {
	super();
}

public Question(String qid, String content, LinkedHashMap 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 getChoices() {
	return choices;
}
public void setChoices(LinkedHashMap 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 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="3
    3+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 mypaper;//考卷
// private LinkedHashMap 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 mypaper, LinkedHashMap 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 getMypaper() {
// return mypaper;
// }
// public void setMypaper(LinkedHashMap mypaper) {
// this.mypaper = mypaper;
// }
// public LinkedHashMap getMyAnswer() {
// return myAnswer;
// }
// public void setMyAnswer(LinkedHashMap 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 + “]”;
}

}

你可能感兴趣的:(考试管理系统,具备简单的注册,考试,查看历次成绩,修改密码等功能)