Exam login system (integrated knowledge points)

package com.uncle.myutil.exam;
/**
 * 题目做成选择题
 * 			1.以下哪个选项不是Java的基本类型?
 * 			   A.short   B.boolean  C.String  D.char
 * 			   请您输入认为正确的选项?
 * 			2.以下哪个选项是Java的基本类型?
 * 			   A.Short   B.Boolean  C.String  D.char
 * 			   请您输入认为正确的选项?
 * 		学生考试之前需要先登录认证
 * 		设计一个学生  考试机  老师关系
 * 		考试机存储好多题目----题库10道
 * 		考试机随机生成试卷的方法5道
 * 		学生利用生成的试卷考试-->答案 5个选项 A D B C A
 * 		老师负责批卷子   最终成绩
 *
 * 		类的设计 方法 属性
 * 		类之间的关系
 * 		String类型的常用方法
 * 		集合的选择与使用(包含数组)
 *
 *
 * 	1.考试的题目该如何存储?
 * 		自己描述一个类--->一个题目类型
 * 		有两个属性---题干  真实答案
 * 		public class Question{
 * 			private String title;
 * 			private Stirng answer;
 *                }
 * 	2.有几个实体类(3个)  类的关系?
 * 		考试机
 * 		   属性---题库 存储好多Question类型的对象
 * 		             set无重复
 * 		   方法---随机抽题目 试卷
 * 		学生
 * 		   方法---考试(试卷)
 * 		老师
 * 		   方法---批卷子(学生最终的选项,真实的试卷)
 * 	3.具体添加每一个类中的成员描述
 * 		涉及到如何选择容器来存储的问题
 * 	4.具体的执行验证
 */

import java.util.ArrayList;
import java.util.Scanner;

public class TestMain {
    
    
    public static void main(String[] args){
    
    
        //创建考试机
        ExamMachine machine = new ExamMachine();//调用构造方法时 有一个块默认执行
        //创建学生对象 来考试
        Scanner input = new Scanner(System.in);
        System.out.println("欢迎进入考试系统,\n请输入用户名");
        String username = input.nextLine();
        System.out.println("请输入密码");
        String password = input.nextLine();
        Student student = new Student(username,password);
        String result = machine.login(student.getUsername(),student.getPassword());
        if(result.equals("登录成功")){
    
    
            System.out.println("登录成功\n"+student.getUsername()+"开始考试啦,不要作弊,认真作答");
            ArrayList<Question> paper = machine.getPaper();
            String[] answers = student.exam(paper);//学生考试
            Teacher teacher = new Teacher();
            int score = teacher.checkPaper(paper,answers);
            System.out.println(student.getUsername()+"最终的成绩为:"+score);
        }









//        Question question = new Question("1.如下哪个选项不是Java基本数据类型?\n\tA.String\n\tB.char\n\tC.int\n\tD.double","A");
//        System.out.println(question.getTitle());

        //题干  答案 两个字符串  需要存储在一起 变成一个完整的题目
        //String[2] {"title","answer"}
        //String   "title-answer"
        //ArrayList<String[]>
        //HashMap<title,answer>
        //面向对象的编程思想----   一道题目当做一个对象? title属性 answer属性
    }
}

package com.uncle.myutil.exam;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Random;

public class ExamMachine {
    
    

    //属性---记录学生账号和密码
    private HashMap<String,String> userBox = new HashMap<String,String>();
    {
    
    
        userBox.put("步尔斯特","123");
        userBox.put("大叔","666");
        userBox.put("Java","888");
    }

    //属性---题库  好多个Question类型的对象  每一个对象是一道题目
    //  Set集合  如果题库进行扩充 产生重复题目可以自动去掉了
    //  hash集合遵循的规则  equals和hashCode方法
    private HashSet<Question> questionBank = new HashSet<Question>();
    //利用块初始化hashSet集合内的题目对象
    {
    
    
        questionBank.add(new Question("以下选项哪个是Java基本数据类型?\n\tA.String\n\tB.Integer\n\tC.boolean\n\tD.Math","C"));
        questionBank.add(new Question("以下选项哪个不是Java基本数据类型?\n\tA.String\n\tB.int\n\tC.boolean\n\tD.double","A"));
        questionBank.add(new Question("以下选项哪个是Java引用数据类型?\n\tA.String\n\tB.int\n\tC.boolean\n\tD.char","A"));
        questionBank.add(new Question("以下选项哪个不是Java引用数据类型?\n\tA.String\n\tB.Integer\n\tC.boolean\n\tD.Math","C"));
        questionBank.add(new Question("以下选项哪个是java.util包中的类?\n\tA.String\n\tB.Integer\n\tC.Scanner\n\tD.Math","C"));
        questionBank.add(new Question("以下选项哪个不是java.util包中的类?\n\tA.Date\n\tB.Integer\n\tC.Calendar\n\tD.Random","B"));
        questionBank.add(new Question("以下选项哪个不是String类中的方法?\n\tA.compareTo\n\tB.append\n\tC.substring\n\tD.concat","B"));
        questionBank.add(new Question("以下选项哪个是String类中方法?\n\tA.append\n\tB.delete\n\tC.insert\n\tD.concat","D"));
        questionBank.add(new Question("以下选项哪个不是接口中属性的修饰符?\n\tA.public\n\tB.static\n\tC.final\n\tD.abstract","D"));
        questionBank.add(new Question("以下选项哪个不是Set集合的方法?\n\tA.set\n\tB.add\n\tC.remove\n\tD.iterator","A"));
    }

    //设计一个方法 随机生成试卷
    //      参数 确定试卷5道题 不用  返回值-->试卷ArrayList<Question>
    public ArrayList<Question> getPaper(){
    
    
        System.out.println("考试机正在随机的生成试卷,请耐心等待。。。");
        try {
    
    
            Thread.sleep(5000);
        }catch(Exception e){
    
    
            e.printStackTrace();
        }
        //随机抽取试卷的时候  试卷的题目应该是不重复  set存-->arrayList
        HashSet<Question> paper = new HashSet<Question>();//试卷
        //产生一个随机序号 去找寻题目  题库是个set没有序号---题库-->ArrayList
        ArrayList<Question> questionBank = new ArrayList<Question>(this.questionBank);
        //随机抽
        while(paper.size()!=5) {
    
    
            int index = new Random().nextInt(this.questionBank.size());// [0-10)
            paper.add(questionBank.get(index));
        }
        return new ArrayList<Question>(paper);
    }

    //考试机中还有一个登录方法
    public String login(String username,String password){
    
    
        String realPassword = this.userBox.get(username);
        if(realPassword!=null && realPassword.equals(password)){
    
    
            return "登录成功";
        }
        return "用户名或密码错误";
    }
}

package com.uncle.myutil.exam;

public class Question {
    
    
    private String title;//题目题干
    private String answer;//真实答案
    public Question(String title,String answer){
    
    
        this.title=title;
        this.answer=answer;
    }
    //重写方法   将默认比较题目对象的地址规则  改成 比较题干  题干一致认为是同一个题目
    public boolean equals(Object obj){
    
    
        if(this==obj){
    
    
            return true;
        }
        if(obj instanceof Question){
    
    
            Question anotherQuestion = (Question)obj;
            //this.title  按照?截取 与anotherQuestion.title?截取之前的部分比较
            if(this.title.equals(anotherQuestion.title)){
    
    
                return true;
            }
        }
        return false;
    }
    public int hashCode(){
    
    
        return this.title.hashCode();
    }

    public String getTitle(){
    
    
        return this.title;
    }
    public String getAnswer(){
    
    
        return this.answer;
    }
}

package com.uncle.myutil.exam;

import java.util.ArrayList;
import java.util.Scanner;

public class Student {
    
    
    //属性
    private String username;
    private String password;

    public Student(String username,String password){
    
    
        this.username=username;
        this.password=password;
    }
    public String getUsername(){
    
    
        return this.username;
    }
    public String getPassword(){
    
    
        return this.password;
    }

    //学生需要考试---方法
    //      参数---是一套试卷   返回值---学生作答的所有选项String[] "A"  "D"  "B"  "C"  "D"
    public String[] exam(ArrayList<Question> paper){
    
    
        String[] answers = new String[paper.size()];
        Scanner input = new Scanner(System.in);
        for(int i=0;i<paper.size();i++){
    
    
            Question question = paper.get(i);//题干 真实答案
            System.out.println((i+1)+"."+question.getTitle());
            System.out.println("请输入您认为正确的选项?");
            answers[i] = input.nextLine();//接受学生输入的选项
        }
        return answers;
    }
}

package com.uncle.myutil.exam;

import java.util.ArrayList;

public class Teacher {
    
    

    //负责批卷子
    //  参数 学生作答所有选项   真实的试卷 跟学生随机的那套一致
    //  返回值 int
    public int checkPaper(ArrayList<Question> paper, String[] answers){
    
    
        System.out.println("老师正在批卷子,请耐心等待最终成绩。。。");
        try {
    
    
            Thread.sleep(5000);
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
        int score = 0;
        for(int i=0;i<paper.size();i++){
    
    
            Question question = paper.get(i);
            if(question.getAnswer().equalsIgnoreCase(answers[i])){
    
    
                score+=(100/paper.size());// 100/paper.size();
            }
        }
        return score;
    }
}

Guess you like

Origin blog.csdn.net/m0_51945027/article/details/112845952