⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testcenter.java

📁 纯JAVA代码的考试系统
💻 JAVA
字号:
/**
 * TestCenter.java
 * 本代码为教育目的而编写,但遵循产品代码规范。
 * 任何人任何时候都可以使用此代码,但需说明引用的代码来源于
 * www.fangsoft.org。
 * 欢迎任何建议。
 * 访问我们:
 * 		电子邮件:fangsoft.com@gmail.com
 * 		网站:   www.fangsoft.org
 * =====================================
 * This code is for software education,but it follows production code quality.
 * Anyone can use this code anywhere, but you should comment the code is from
 * www.fangsoft.org.
 * Any suggestion from you is appreciated. 
 * Visit us by
 *       email:    fangsoft.com@gmail.com
 *       websiste: www.fangsoft.org
 */
package org.fangsoft.testcenter.main;

import static org.fangsoft.util.Console.output;
import org.fangsoft.testcenter.view.console.WelcomeView;
/*import static org.fangsoft.util.Console.prompt;
import static org.fangsoft.util.Console.promptYesNo;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;


import org.fangsoft.testcenter.dao.db.CustomerDBDao;
import org.fangsoft.testcenter.dao.db.QuestionDBDao;
import org.fangsoft.testcenter.dao.db.QuestionResultDBDao;
import org.fangsoft.testcenter.dao.db.TestDBDao;
import org.fangsoft.testcenter.dao.db.TestResultDBDao;
import org.fangsoft.testcenter.model.ChoiceItem;
import org.fangsoft.testcenter.model.Customer;
import org.fangsoft.testcenter.model.Question;
import org.fangsoft.testcenter.model.QuestionResult;
import org.fangsoft.testcenter.model.Test;
import org.fangsoft.testcenter.model.TestResult;

import org.fangsoft.util.Console;*/

/**
 * @author fangsoft 2007-2-10
 * 
 */
public class Testcenter {
      
	
	public static void main(String[] args) {
		//run();
		runOnMVC();
	}
	private static void runOnMVC() {
		WelcomeView view=new WelcomeView();
		TestController tc=new TestController();
		view.addObserver(tc);
		view.display();
	}
	
	/*public static void main(String[] args) {
		welcome();
		Customer customer = login();
		if (customer == null) {
			exit("用户名或口令错,不能登录,系统退出");
		}
		boolean response = promptYesNo("确认参加考试吗?", "是:y", "否,退出:n");
		if (!response)
			exit("系统退出");
				Test test = selectTest();
				TestResult tr = takeTest(test, customer);
				reportTestResult(tr);
				exit(0);
	}*/

	/*public static Customer login() {
		output("参加考试前请先登录,输入完成后按enter确认:");
		int count = 0;
		String userId, password;
		int maxLogin = 3;
		Customer customer = null;
		for (count = 0; count < maxLogin; count++) {
			userId = prompt("输入用户名称:");
			password = prompt("输入用户密码:");
			// customer=Customer.login(userId, password);
			customer = CustomerDBDao.getInstance().login(userId, password);
			if (customer != null) {
				break;
			}
			output("用户名或密码错误不能登录,重新登录." + "登录" + maxLogin + "次不成功,系统将退出."
					+ "这是" + (count + 1) + "次");
		}
		return customer;
	}

	public static void welcome() {
		String user = System.getProperty("user.name");
		output("欢迎你," + user + " 参加fangsoft考试中心的考试!");
		output("今天是%1$tY年%1$tm月%1$td日%1$ta%n", Calendar.getInstance());
		output("你的操作系统是:" + System.getProperty("os.name"));
	}

	public static TestResult takeTest(Test test, Customer customer) {
		output("==========开始考试===========");
		output("考试名称:%1$5s%n" + "考试简述:%2$5s%n" + "考试问题:%3$5s%n"
				+ "考试时间:%4$5s分钟%n", test.getName(), test.getDescription(), test
				.getNumQuestion(), test.getTimeLimitMin());
		long start = System.currentTimeMillis();
		output("注意你有%1$s分钟答题,现在时间是:%2$tT%n", test.getTimeLimitMin(), start);
		TestResult tr = new TestResult();
		tr.setCustomer(customer);
		tr.setTest(test);
		tr.setStatus(TestResult.Status.TESTING);
		tr.setStartTime(new Timestamp(start));
		QuestionDBDao.getInstance().loadQuestion(test);
		for (Question q : test.getQuestion()) {
			QuestionResult qr = new QuestionResult();
			qr.setQuestion(q);
			qr.setAnswer(null);
			tr.addQuestionResult(qr);
		}
		tr.save();
		TimeThread t = new TimeThread();		
		int count = 0;
		tr.setStartTime(new Timestamp(start));
		t.start(test, tr);
		for (Question q : test.getQuestion()) {
			if (t.timeTread == null) {
				System.out.println("超时提交试卷!");
				exit(0);
			}
			q.assignLabel(ChoiceItem.LABEL);
			String answer = prompt(count + 1, q);
			QuestionResult qr =tr.getQuestionResult(count);
			qr.setQuestion(q);
			qr.setAnswer(answer);
			qr.computeAnswer();
            tr.reworkList(qr, count);
			tr.setCount(count++);
			long end = System.currentTimeMillis();
			tr.setEndTime(new Timestamp(end));
		}
		long end = tr.getEndTime().getTime();
		tr.setEndTime(new Timestamp(end));
		tr.commitTest();
		tr.updatte();
		output("考试结束,现在时间是:%1$tT%n", end);
		return tr;
	}

	// 报告考试结果
	public static void reportTestResult(TestResult tr) {
		output("==========考试报告===========");
		long duration = (tr.getEndTime().getTime() - tr.getStartTime()
				.getTime())
				/ (1000 * 60);
		output("你花了%1$s分钟考试%n", duration);
		output("%1$8s%2$8s%3$8s%4$8s%n", "题号", "你的答案", "正确答案", "对错");
		int count = 0;
		for (QuestionResult qr : tr.getQuestionResult()) {
			output("%1$8s%2$8s%3$8s%4$8s%n", ++count, qr.getAnswer(), qr
					.getQuestion().getAnswer(), qr.getResult() ? "right"
					: "wrong");
		}
		boolean pass = tr.Pass() == 1 ? true : false;
		String displayPass = "";
		if (pass)
			displayPass = "恭喜,你通过了考试";
		else
			displayPass = "很遗憾,你没有通过考试";
		output("你考试的得分是:" + tr.getScore() + "," + displayPass);
	}

	public static Test selectTest() {
		output("fangsoft考试中心提供的所有考试:");
		List<Test> allTest = TestDBDao.getInstance().findAllTest();
		int count = 0;
		for (Test test : allTest) {
			output((++count) + ". " + test.getName() + ", 输入:" + count);
		}
		while (true) {
			String response = Console.prompt("请选择考试类型:");
			try {
				int index = Integer.parseInt(response);
				if (1 <= index && index <= allTest.size()) {
					return allTest.get(index - 1);
				}
			} catch (Exception ex) {
			}
		}
		
	}	*/

	static void exit(Object msg) {
		output(msg);
		System.exit(1);// 异常退出
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -