📄 knowledgequizsession.java
字号:
package com.softeem.knowledgeQuiz;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class KnowledgeQuizSession extends Thread {
private static Properties allQuiz = new Properties(); //题目属性文件
private static Properties allUser = new Properties(); //普通用户属性文件
private static Properties allAdmin = new Properties();//管理员属性文件
private Socket s;
private BufferedReader br = null;
private PrintStream ps = null;
private String userName; //临时用户名
private static ArrayList onListUser = new ArrayList(); //ArrList集合用来存放已经登陆的用户名
//静态初始化快
static {
InputStream is;
try {
is = new FileInputStream("quiz.properties");
allQuiz.load(is); // load方法读取所关联的属性文件
is = new FileInputStream("user.properties");
allUser.load(is);
is.close();
is = new FileInputStream("admin.properties");
allAdmin.load(is);
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public KnowledgeQuizSession(Socket s) {
this.s = s;
}
public void run() {
try {
s.setSoTimeout(300000);//空闲多少时间抛出IO异常 (3分钟后断开连接)
// 产生用于网络传输数据两个流对象
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
ps = new PrintStream(s.getOutputStream());
sendWelcomeMsg(); // 给系统发送欢迎信息 (做成方法)代码重用性 (Alt+Shilt+M 选定代码创建方法)
sendSystemMenu(); // 给客户端发送系统导航菜单
handRequest(); // 根据客户请求选择进行相应处理
} catch (IOException e) {
ps.println("对不起,您超时了!");
try {
doQuit();
} catch (IOException e1) {
e1.printStackTrace();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void handRequest() throws IOException, InterruptedException {
String line = null;
line = br.readLine();
if ("1".equals(line)) {
doRegister(); // 注册
} else if ("2".equals(line)) {
doLogin();
} else if ("3".equals(line)) {
doExit();
} else {
}
}
private void doExit() throws IOException {
String line;
ps.println("您确信要退出吗?(Y/N)");
line = br.readLine();
if ("y".equalsIgnoreCase(line)) {
doQuit();
}
}
private void doLogin() throws IOException, InterruptedException {
ps.println("请选择合适的身份登陆:");
ps.println("a.普通用户(默认)");
ps.println("b.管理员");
String line = null;
line = br.readLine();
if ("b".equalsIgnoreCase(line)) {
int Admincount = 0;
ps.println("请您输入用户名:");
while ((line = br.readLine()) != null) {
if (allAdmin.containsKey(line)) {
//用containsKey方法从属性文件(allAdmin)中取出用户名
userName = line;
break;
} else if (Admincount < 3) {
ps.println("输入用户名在本系统中不存在!");
ps.println("请重新输入!");
Admincount++;
} else {
ps.println("你输入错误次数已达到最大限度!");
doQuit();
return;
}
}
ps.println("请您输入密码:");
while ((line = br.readLine()) != null) {
if (line.equals(allAdmin.getProperty(userName))) { //用getProperty方法 调用之前取出的用户名来关联后面的密码
break;
} else if (Admincount < 3) {
ps.println("密码错误");
ps.println("请重新输入!");
Admincount++;
} else {
ps.println("密码输入有误!");
doQuit();
return;
}
}
ps.println("恭喜您,登陆成功!");
ps.println();
ps.println("请您选择服务种类:");
ps.println("a.逐条录入题目");
ps.println("b.批量录入题目");
ps.println("c.编辑所选题目");
ps.println("d.查阅所有题目");
ps.println("e.退出系统");
line = br.readLine();
if ("a".equals(line)) {
ps.println("请输入问题和答案(以\"=\")分割");
line = br.readLine();
String[] ques = line.split("="); //前后用=连接
allQuiz.put(ques[0], ques[1]); //分开写入allQuiz属性中
allQuiz.store(new FileOutputStream("quiz.properties"), null); //用输出流读到allQuiz所关联的属性文件中去
ps.println("还要继续录入吗?(Y/N)");
line = br.readLine();
if ("Y".equalsIgnoreCase(line)) {
int i = 0;
while (i < 10) {
ps.println("请输入问题和答案(以\"=\")分割");
line = br.readLine();
String[] que = line.split("=");
allQuiz.put(que[0], que[1]);
allQuiz.store(new FileOutputStream("quiz.properties"),
null);
}
} else if ("N".equalsIgnoreCase(line)) {
doQuit();
return;
}
} else if ("b".equals(line)) {
} else if ("c".equals(line)) {
} else if ("d".equals(line)) {
} else {
ps.println("欢迎您下次使用本系统");
doQuit();
}
} else {
int count = 1;
ps.println("请您输入用户名:");
while ((line = br.readLine()) != null) {
if (allUser.containsKey(line)) {//用containsKey方法从属性文件(allUser)中取出用户名
if (onListUser.contains(line)) //如果集合中有已经登陆的用户名
{
ps.println("该用户正在线");
doQuit();
return;
}
userName = line;
break;
} else if (count < 3) {
ps.println("输入用户名在本系统中不存在!");
ps.println("请重新输入!");
count++;
} else {
ps.println("你输入错误次数已达到最大限度!");
doQuit();
return;
}
}
ps.println("请您输入密码:");
line = br.readLine();
if (line.equals(allUser.getProperty(userName))) {//用getProperty方法 调用之前取出的用户名来关联后面的密码
onListUser.add(userName); //登陆成功的用户名放到Arraylist集合里面去
ps.println("恭喜您,登陆成功!");
ps.println();
} else {
ps.println("密码输入有误");
doQuit();
return;
}
//给客户端发题目
sendQuiz();
}
}
private void sendQuiz() throws IOException, InterruptedException {
ps.print("系统正在加载题库,请稍候");
for (int i = 0; i < 6; i++) {
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
ps.print(".");
}
ps.println();
ps.println();
ps.println("**************************************");
ps.println("请您仔细思考,认真答题!");
ps.println("如有任何问题,请与我们联系!");
ps.println("**************************************");
// 获得所有题目
Set questions = allQuiz.keySet();
Iterator it = questions.iterator();
int count1 = 0;
int rightNum = 0;
while (it.hasNext()) {
if (count1 > 5) {
break;
}
count1++;
String question = (String) it.next();
ps.println("●●●● 第" + count1 + "题:『" + question + "』");
String answer = br.readLine();
String rightAnswer = allQuiz.getProperty(question);
if (answer.equalsIgnoreCase(rightAnswer)) {
rightNum++;
ps.println("您答对了!");
} else {
ps.println("您答错了,正确答案为:『" + rightAnswer + "』");
}
ps.println();
}
ps.println("答题完毕!系统正在给您汇总成绩:");
ps.println();
Thread.sleep(1000);
String correctRate = rightNum * 100 / count1 + "%";
ps.println("总共『" + count1 + "』题,您答对『" + rightNum + "』题,您的正确率为:『"
+ correctRate + "』!");
// 与客户端断开连接
doQuit();
}
private void doRegister() throws IOException {
String line;
ps.println("请您输入注册用户名");
line = br.readLine();
//判断注册用户名是否存在
userName = line;
ps.println("请您输入注册密码");
line = br.readLine();
ps.println("请您再次输入密码");
line = br.readLine();
//把用户名userName,密码line用put方法存到allUser里面去
allUser.put(userName, line);
//把集合里面的用户密码用store方法通过输出流存放到user,properties属性文件里面去
allUser.store(new FileOutputStream("user.properties"), null);
ps.println("恭喜您注册成功");
try {
sendQuiz();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void sendSystemMenu() {
ps.println("请您选择服务种类");
ps.println("1.新用户注册");
ps.println("2.老用户登陆");
ps.println("3.退出本系统");
}
private void sendWelcomeMsg() {
ps.println("欢迎光临T-cay知识问答服务器");
ps.println();
}
private void doQuit() throws IOException {
onListUser.remove(userName);
ps.println("感谢您对本系统的支持,如有需要,请联系T-cay");
br.close();
ps.close();
s.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -