编程语言
首页 > 编程语言> > 结队编程 - 队友代码分析

结队编程 - 队友代码分析

作者:互联网

结对编程队友代码分析 - 李林畅li神

李神个人项目代码结构

优点:

  1. 整个工程仅使用一个包,充分体现架构设计中less is more的思想,大道至简,对于轻量级的应用无需使用过多复杂的项目架构,包结构。使整个工程结构清晰,易于阅读。
  2. ClassInterface命名规范,采用Java语言规范的大驼峰命名法,使各个类的功能更加清晰,代码规范性更高。

缺点:

  1. 题目生成使用绝对路径,不大方便

李神源码

package org;

import java.util.HashMap;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.File;
import java.io.IOException;

public class MenuImpl implements Menu {
    private String option;
    private User currentUser;
    private Scanner in;
    private GeneratorImpl generatorImpl;
    private HashMap<String, String> namePasswordMap; //map username&password
    private HashMap<String, Integer> nameStatusMap; //map username&status
    private HashMap<String, Integer> gradeStatusMap; //map grade&status
    private String[] statusToGrade = {"", "小学", "初中", "高中"}; //map status&grade
    private boolean quit;

    public MenuImpl() throws IOException {
        in = new Scanner(System.in);
        namePasswordMap = new HashMap<>();
        nameStatusMap = new HashMap<>();
        gradeStatusMap = new HashMap<>();
        gradeStatusMap.put("小学", 1);
        gradeStatusMap.put("初中", 2);
        gradeStatusMap.put("高中", 3);
        quit = false;
        defaultUserInit();//add default users
        //create project folder
        String dir = "D:\\A_File\\Documents\\PersonalProject\\";
        File folder = new File(dir);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        //create account file(if doesn't exit)
        File accounts = new File("D:\\A_File\\Documents\\PersonalProject\\Accounts.txt");
        if (!accounts.exists()) {
            accounts.createNewFile();
        }
        //get existing accounts
        BufferedReader bufferedReader = new BufferedReader(new FileReader(accounts));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            String[] userInfo = line.split("\t");
            String grade = userInfo[0];
            if (!namePasswordMap.containsKey(userInfo[1])) {
                namePasswordMap.put(userInfo[1], userInfo[2]);
                nameStatusMap.put(userInfo[1], gradeStatusMap.get(grade));
            }
        }
        bufferedReader.close();
    }

    public void defaultUserInit() {
        String defaultPassword = "123";
        int nowStatus = 1;
        namePasswordMap.put("张三1", defaultPassword);
        nameStatusMap.put("张三1", nowStatus);
        namePasswordMap.put("张三2", defaultPassword);
        nameStatusMap.put("张三2", nowStatus);
        namePasswordMap.put("张三3", defaultPassword);
        nameStatusMap.put("张三3", nowStatus);
        nowStatus = 2;
        namePasswordMap.put("李四1", defaultPassword);
        nameStatusMap.put("李四1", nowStatus);
        namePasswordMap.put("李四2", defaultPassword);
        nameStatusMap.put("李四2", nowStatus);
        namePasswordMap.put("李四3", defaultPassword);
        nameStatusMap.put("李四3", nowStatus);
        nowStatus = 3;
        namePasswordMap.put("王五1", defaultPassword);
        nameStatusMap.put("王五1", nowStatus);
        namePasswordMap.put("王五2", defaultPassword);
        nameStatusMap.put("王五2", nowStatus);
        namePasswordMap.put("王五3", defaultPassword);
        nameStatusMap.put("王五3", nowStatus);
    }

    public void run() throws IOException {
        while (!quit) {
            welcomeMessage();
            if (!tryLogin()) {
                continue;
            }
            tryGenerate();
        }
    }

    public void welcomeMessage() {
        System.out.println("欢迎进入题目生成系统");
        System.out.println("登录请输入1,注册请输入2,退出输入-1");
        currentUser = null;
    }

    public void tryGenerate() throws IOException {
        System.out.println("当前选择为" + statusToGrade[currentUser.getStatus()] + "出题");
        System.out.println("出题难度为" + currentUser.getStatus());
        System.out.println("开始出题输入1,若要切换难度请输入对应年级切换。退出登录输入-1");
        int switchTo = 0;
        while (true) {
            option = in.nextLine();
            if (option.equals("1")) {
                while (true) {
                    System.out.println("请输入题目数量");
                    int num = 0;
                    String numLine = in.nextLine();
                    try {
                        num = Integer.parseInt(numLine);
                    } catch (java.lang.NumberFormatException exception) {
                        System.out.println("请输入1-30之间的数字");
                        continue;
                    }
                    if (0 >= num || num > 30) {
                        System.out.println("请输入1-30之间的数字");
                        continue;
                    }
                    int difficulty = (switchTo == 0) ? currentUser.status : switchTo;
                    generatorImpl = new GeneratorImpl(difficulty,num,currentUser);
                    generatorImpl.createPaper();
                    System.out.println("难度" + difficulty + "题目生成成功");
                    String op = "";
                    while (!op.equals("1")) {
                        System.out.println("继续出题请输入1,若要切换难度请输入对应年级切换。退出登陆输入-1");
                        op = in.nextLine();
                        if (op.equals("-1")) {
                            return;
                        } else if (gradeStatusMap.containsKey(op)) {
                            switchTo = gradeStatusMap.get(op);
                            System.out.println("现在为" + op + "出题");
                        }
                    }
                }
            } else if (option.equals("-1")) {
                return;
            } else if (gradeStatusMap.containsKey(option)) {
                switchTo = gradeStatusMap.get(option);
                System.out.println("当前选择为" + option + "出题");
                System.out.println("出题难度为" + switchTo);
                System.out.println("开始出题输入1,若要切换难度请输入对应年级切换。退出登录输入-1");
            } else {
                System.out.println("指令错误,请重新输入。若想切换难度请输入小学、初中和高中三个选项中的一个");
            }
        }

    }

    public boolean tryLogin() throws IOException {
        while (true) {
            option = in.nextLine();
            if (option.equals("1")) {
                System.out.println("请依次输入用户名,密码,以空格分隔。返回登陆界面请输入-1");
                while (true) {
                    String infoLine = in.nextLine();
                    if (infoLine.equals("-1")) {
                        return false;
                    }
                    String[] userInfo = infoLine.split("\\s+");
                    if (userInfo.length != 2) {
                        System.out.println("请以空格分隔输入用户名,密码。返回登陆界面请输入-1");
                        continue;
                    }
                    if (!namePasswordMap.containsKey(userInfo[0])) {
                        System.out.println("用户名不存在,请重新输入");
                    } else if (namePasswordMap.get(userInfo[0]).equals(userInfo[1])) {
                        currentUser = new User(userInfo[0], userInfo[1], nameStatusMap.get(userInfo[0]));
                        return true;
                    } else {
                        System.out.println("密码错误,请重新输入");
                    }
                }
            } else if (option.equals("2")) {
                System.out.println("请依次输入年级,用户名,密码,以空格分隔。返回登陆界面请输入-1");
                while (true) {
                    String infoLine = in.nextLine();
                    if (infoLine.equals("-1")) {
                        return false;
                    }
                    String[] userInfo = infoLine.split("\\s+");
                    if (userInfo.length != 3) {
                        System.out.println("请以空格分隔输入年级,用户名,密码。返回登陆界面请输入-1");
                        continue;
                    }
                    if (namePasswordMap.containsKey(userInfo[1])) {
                        System.out.println("用户已存在,请重新输入");
                    } else if (!gradeStatusMap.containsKey(userInfo[0])) {
                        System.out.println("年级请输入小学,初中或高中");
                    } else {
                        namePasswordMap.put(userInfo[1], userInfo[2]);
                        nameStatusMap.put(userInfo[1], gradeStatusMap.get(userInfo[0]));
                        currentUser = new User(userInfo[1], userInfo[2], nameStatusMap.get(userInfo[1]));
                        String dir = "D:\\A_File\\Documents\\PersonalProject\\Accounts.txt";
                        FileWriter fileWriter = new FileWriter(dir, true);
                        BufferedWriter writer = new BufferedWriter(fileWriter);
                        String fileInfo = userInfo[0] + "\t" + userInfo[1] + "\t" + userInfo[2];
                        writer.write(fileInfo + "\n");
                        writer.close();
                        fileWriter.close();
                        return true;
                    }
                }
            } else if (option.equals("-1")) {
                quit = true;
                return false;
            } else {
                System.out.println("指令错误,请重新输入");
            }
        }
    }
}

package org;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Random;

public class GeneratorImpl implements Generator {
    private User currentUser;
    private int status;
    private int amount;
    private String dir = "D:\\A_File\\Documents\\PersonalProject\\";
    private String[] symbols = {"+", "-", "*", "/", "^(2)", "√", "sin", "cos", "tan"};
    private HashSet<String> prevProblems;

    public GeneratorImpl() {
    }

    public GeneratorImpl(int status, int amount, User aUser) {
        this.status = status;
        this.amount = amount;
        this.currentUser = aUser;
    }

    public User getCurrentUser() {
        return currentUser;
    }

    public void setCurrentUser(User currentUser) {
        this.currentUser = currentUser;
    }

    public String getDir() {
        return dir;
    }

    public void setDir(String dir) {
        this.dir = dir;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    public void getPrevProblems() throws IOException {
        prevProblems = new HashSet<>();
        //get problem folder
        String directory = dir + currentUser.name + "\\" + status;
        File folder = new File(directory);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        //save all problems
        File[] papers = folder.listFiles();
        for (var paper : papers) {
            BufferedReader reader = new BufferedReader(new FileReader(paper));
            String thisProblem = reader.readLine().substring(3);
            prevProblems.add(thisProblem);
            reader.close();
        }
    }

    public String getProblem() {
        String newProblem = "";
        Random random = new Random();
        int problemLength = random.nextInt(4) + 3;
        if ((problemLength & 1) == 0) {
            problemLength++;
        }
        int haveBraces = 0;//how many braces should be added at the end
        int braces = random.nextInt(4);//how many braces this problem at most can have
        if ((braces & 1) == 1) {
            braces++;
        }
        int turn = 1;//choose when to add numbers or symbols
        if (status == 1) {
            while ((problemLength--) > 0) {
                if (turn == 1) {
                    int number = random.nextInt(100) + 1;
                    if (braces > 0 && (braces & 1) == 0 && random.nextInt(2) == 1) {
                        newProblem += "(";//add left brace before a number
                        braces--;
                        haveBraces++;
                    }
                    newProblem += number;
                    if (braces > 0 && (braces & 1) == 1 && random.nextInt(2) == 1) {
                        newProblem += ")";//add right brace after a number
                        haveBraces--;
                        braces--;
                    }
                    turn = 0;
                } else {
                    newProblem += symbols[random.nextInt(4)];
                    turn = 1;
                }
            }
            if (haveBraces > 0) {
                for (int i = 0; i < haveBraces; i++) {
                    newProblem += ")";
                }
            }
        } else if (status == 2) {
            while ((problemLength--) > 0) {
                if (turn == 1) {
                    int number = random.nextInt(100) + 1;
                    if (braces > 0 && (braces & 1) == 0 && random.nextInt(2) == 1) {
                        newProblem += "(";
                        braces--;
                        haveBraces++;
                    }
                    newProblem += number;
                    if (braces > 0 && (braces & 1) == 1 && random.nextInt(2) == 1) {
                        newProblem += ")";
                        braces--;
                        haveBraces--;
                    }
                    turn = 0;
                } else {
                    int idx = random.nextInt(6);
                    newProblem += symbols[idx];
                    if (idx == 4) {
                        newProblem += symbols[random.nextInt(4)];
                    }
                    turn = 1;
                }
            }
            if (haveBraces > 0) {
                for (int i = 0; i < haveBraces; i++) {
                    newProblem += ")";
                }
            }
        } else {
            boolean preTrig = false;//make sure number in trigonometric function surrounded by braces
            while ((problemLength--) > 0) {
                if (turn == 1) {
                    int number = random.nextInt(100) + 1;
                    if (!preTrig && braces > 0 && (braces & 1) == 0 && random.nextInt(2) == 1) {
                        newProblem += "(";
                        braces--;
                        haveBraces++;
                    }
                    newProblem += number;
                    if (!preTrig && braces > 0 && (braces & 1) == 1 && random.nextInt(2) == 1) {
                        newProblem += ")";
                        braces--;
                        haveBraces--;
                    }
                    if (preTrig) {
                        newProblem += ")";
                        preTrig = false;
                    }
                    turn = 0;
                } else {
                    int idx = random.nextInt(9);
                    if (6 <= idx && idx <= 8) {
                        newProblem += symbols[random.nextInt(4)];
                        preTrig = true;
                    }
                    newProblem += symbols[idx];
                    if (idx == 4) {
                        newProblem += symbols[random.nextInt(4)];
                    }
                    if (preTrig) {
                        newProblem += "(";
                    }
                    turn = 1;
                }
            }
            if (haveBraces > 0) {
                for (int i = 0; i < haveBraces; i++) {
                    newProblem += ")";
                }
            }
        }
        return newProblem;
    }

    public void createPaper() throws IOException {
        getPrevProblems();
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DATE);
        int hour = calendar.get(Calendar.HOUR);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        String directory = dir + currentUser.name + "\\" + status + "\\";
        directory = directory + year + "-" + month + "-" + day + "-" + hour + "-" + minute + "-" + second + ".txt";
        FileWriter fileWriter = new FileWriter(directory, true);
        BufferedWriter writer = new BufferedWriter(fileWriter);
        for (int i = 1; i <= amount; i++) {
            String problem = getProblem();
            //problem="85-54+(11-95)";
            if (!prevProblems.contains(problem)) {
                writer.write(i + ". " + problem + "\n" + "\n");
                prevProblems.add(problem);
            } else {
                writer.write(i + ". Duplicate" + "\n" + "\n");
            }
        }
        writer.close();
    }
}

标签:结队,int,编程,System,println,队友,userInfo,put,out
来源: https://www.cnblogs.com/JCKeep/p/16690584.html