📄 knowsession.java
字号:
package com.wepull.test.knowledge;
import java.io.*;
import java.net.Socket;
import java.util.*;
public class KnowSession extends Thread {
private Socket socket;
private BufferedReader br = null;
private PrintStream ps = null;
private String command;
private String argument;
private Properties pro;
private Properties pro1;
private Properties pro2;
private List<String> total = new ArrayList<String>();
private List<String> corrent = new ArrayList<String>();
private float ZQL;
private static Map<String,UserInfo> map=new HashMap<String,UserInfo>();
UserInfo user;
private OutputStream os;
public KnowSession(Socket socket) {
this.socket = socket;
}
public void run() {
try {
br = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
ps = new PrintStream(socket.getOutputStream());
sendWelcomeMessage();
// 加载属性文件,准备出题
loadQuestion("1.txt", "2.txt");
Enumeration<String> enu = (Enumeration<String>) pro1
.propertyNames();
while (enu.hasMoreElements()) {
String number = enu.nextElement();
sendQuestion(number);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void Validate() {
parse("3.txt");
try {
String name="";
String pass="";
ps.print("请输入你的姓名:"+name);
name=br.readLine();
ps.print("请输入密码:"+pass);
pass=br.readLine();
if(map.containsKey(name)){
UserInfo user=map.get(name);
if (pass.equals(user.getPassWord())){
ps.println("合法用户,开始答题!");
}
else {
ps.println("你的密码错误,请重新输入:");
pass=br.readLine();
if (pass.equals(user.getPassWord())){
ps.println("合法用户,开始答题!");
}
else{
ps.println("两遍都错了,你的智商还不适合进入本系统,谢谢参与!");
socket.close();
}
}
}
else{
ps.println("你不是本系统合法用户,请选择:1.注册\t2.退出");
String s = br.readLine();
if (s.equalsIgnoreCase("1")) {
zhuche();
} else if (s.equalsIgnoreCase("2")) {
ps.println("欢迎下次进入");
socket.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void parse(String path){
Properties pro=new Properties();
try {
FileInputStream fis=new FileInputStream(path);
pro.load(fis);
fis.close();
Enumeration emu=pro.propertyNames();
while(emu.hasMoreElements()){
String key=(String)emu.nextElement();
String value=pro.getProperty(key);
user=new UserInfo();
user.setUserName(key);
user.setPassWord(value);
map.put(key,user);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void zhuche() {
pro = new Properties();
try {
String name="";
String pass="";
ps.print("请输入你的姓名:");
name = br.readLine();
ps.print("请设置密码:");
pass=br.readLine();
pro.setProperty(name,pass);
os=new FileOutputStream("3.txt",true);
pro.store(os,null);
ps.println("恭喜你,注册成功!");
Validate();
} catch (IOException e) {
e.printStackTrace();
}
}
private void checkAnswer(String number) {
String answer;
try {
answer = br.readLine();
String realAns = pro2.getProperty(number);
total.add(number);
if (answer.equalsIgnoreCase(realAns)) {
ps.println("恭喜你,答对了");
corrent.add(number);
} else{
ps.println("OH YEAH!你答错了,正确答案是:"+realAns);
}
ZQL=(float)corrent.size()/total.size();
while(total.size()>9){
ps.println("你总共答了"+total.size()+"道题");
ps.println("答对:"+corrent.size()+"道题");
ps.println("你的正确率是:"+ZQL);
ps.println("答题结束,谢谢参与!");
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void sendQuestion(String number) {
String content = pro1.getProperty(number);
int[] num = new int[10];
for(int i=1;i<num.length;i++){
num[i]=(int)((Math.random()*19+1));
ps.println(num[i] + " " + content);
ps.println("请选择答案:");
checkAnswer(Integer.toString(num[i]));
ps.flush();
}
}
public void sendWelcomeMessage() {
ps.println(socket.getInetAddress() + " 欢迎登陆本考试系统:1 管理员\t 2 用户");
try {
String s1 = br.readLine();
if ("1".equalsIgnoreCase(s1)) {
ps.println("从题库中选题[y/n]:");
String s=br.readLine();
if(s.equals("y")){
Validate();
}
else
sendWelcomeMessage();
} else if ("2".equalsIgnoreCase(s1)) {
Validate();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void loadQuestion(String ques, String answer) {
pro1 = new Properties();
pro2 = new Properties();
try {
pro1.load(new FileInputStream(ques));
pro2.load(new FileInputStream(answer));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -