1. 程式人生 > >考試管理系統,具備簡單的註冊,考試,檢視歷次成績,修改密碼等功能

考試管理系統,具備簡單的註冊,考試,檢視歷次成績,修改密碼等功能

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().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 + “]”; }

}