From e9206abb45e5ba87e781feb1714685d4b41fd535 Mon Sep 17 00:00:00 2001 From: Andy123qq4 <1394568964@qq.com> Date: Mon, 15 Apr 2024 23:23:29 +0800 Subject: [PATCH 1/2] Fix coding standards: assert, inconsistent, magic number and bool condition format, delete redundant code --- src/main/java/seedu/duke/Checker.java | 61 ++++++++++++++++++--------- 1 file changed, 41 insertions(+), 20 deletions(-) diff --git a/src/main/java/seedu/duke/Checker.java b/src/main/java/seedu/duke/Checker.java index 631b48778..823ebd121 100644 --- a/src/main/java/seedu/duke/Checker.java +++ b/src/main/java/seedu/duke/Checker.java @@ -3,7 +3,15 @@ import java.util.ArrayList; import java.util.List; +/** + * Checker class is used to check the correctness of the user's answer. + * It will store the user's answer and the correctness of the answer. + * It will also store the wrong answer and the corresponding problem. + */ public class Checker { + + private static final double TOLERANCE = 0.01; + private final String[] userAnswer; private final Test test; private final Boolean[] isCorrect; @@ -14,8 +22,16 @@ public class Checker { private double accuracy; private long time; + /** + * Constructor of the Checker class. + * + * @param test the test that the user is taking. + */ public Checker(Test test) { - assert test != null : "You must intialize the checker with a test!"; + if (test == null) { + throw new IllegalArgumentException("You must initialize the checker with a test!"); + } + // assert test != null : "You must initialize the checker with a test!"; this.userAnswer = new String[test.getNumber()]; this.test = test; this.isCorrect = new Boolean[test.getNumber()]; @@ -24,26 +40,39 @@ public Checker(Test test) { this.time = 0; } + /** + * Show the explanation of the problem. + * + * @param problem the problem that the user is solving. + */ public static void showExplanation(Problem problem) { - assert problem != null : "You must give a problem to show the explanation!"; + if (problem == null) { + throw new IllegalArgumentException("You must give a problem to show the explanation!"); + } + // assert problem != null : "You must give a problem to show the explanation!"; String explanations = problem.getExplanations(); Ui ui = new Ui(""); ui.print("The explanation of the problem: " + problem.solved()); - ui.print("Let us caculate it step by step:"); + ui.print("Let us calculate it step by step:"); ui.print(explanations); - - ui.print("From all the steps above, we can get the answer: " + problem.solved()+"\n"); + ui.print("From all the steps above, we can get the answer: " + problem.solved() + "\n"); } - + /** + * Check the correctness of the user's answer. + * + * @param problem the problem that the user is solving. + * @param answer the answer that the user input. + * @return true if the answer is correct, false otherwise. + */ Boolean checkCorrectness(Problem problem, double answer) { - if(Math.abs(problem.getAnswer() - answer) < 0.01) { - return true; - } - return false; + return Math.abs(problem.getAnswer() - answer) < TOLERANCE; } + /** + * Get the user's answer and check the correctness of the answer. + */ void getUserAnswer() { long startTime = System.currentTimeMillis(); ui.startAnswerTest(); @@ -60,17 +89,15 @@ void getUserAnswer() { ui.exitTest(); break; } - while (!isValid) { + while (!isValid) { try { answer = Double.parseDouble(userInput); isValid = true; } catch (NumberFormatException e) { - ui.print("Invalid Input, please enter a number"); ui.print(problem.unsolved()); userInput = ui.readCommand(); - } } @@ -83,18 +110,12 @@ void getUserAnswer() { } } + // hand with time and acc long endTime = System.currentTimeMillis(); accuracy = (double) correctNumber / test.getNumber(); // millisecond to second this.time = (endTime - startTime) / 1000; - for (int i = 0; i < test.getNumber(); i++) { - if (isCorrect[i] = false) { - Problem problem = test.getProblem().get(i); - wrongProblem.add(problem); - wrongAnswer.add(userAnswer[i]); - } - } } public Boolean[] checkAnswer() { From 2cf4b21842a4bb78401e43895e2a7aa18dce1226 Mon Sep 17 00:00:00 2001 From: Andy123qq4 <1394568964@qq.com> Date: Mon, 15 Apr 2024 23:35:51 +0800 Subject: [PATCH 2/2] add javadoc comments and fix minor bugs --- src/main/java/seedu/duke/Calculator.java | 63 +++++++++++++++-- src/main/java/seedu/duke/DIYProblemSet.java | 23 ++++++- src/main/java/seedu/duke/Duke.java | 5 +- src/main/java/seedu/duke/Parser.java | 31 ++++++++- src/main/java/seedu/duke/Problem.java | 11 +++ .../java/seedu/duke/ProblemGenerator.java | 69 ++++++++++++++----- src/main/java/seedu/duke/ProblemSetType.java | 3 + src/main/java/seedu/duke/Record.java | 35 +++++++++- src/main/java/seedu/duke/Storage.java | 15 ++++ src/main/java/seedu/duke/Test.java | 18 +++++ src/main/java/seedu/duke/Ui.java | 63 ++++++++++++----- src/test/java/seedu/duke/CheckerTest.java | 4 ++ src/test/java/seedu/duke/DukeTest.java | 6 +- .../java/seedu/duke/ProblemGeneratorTest.java | 4 ++ src/test/java/seedu/duke/StorageTest.java | 7 ++ src/test/java/seedu/duke/UiTest.java | 4 ++ 16 files changed, 317 insertions(+), 44 deletions(-) diff --git a/src/main/java/seedu/duke/Calculator.java b/src/main/java/seedu/duke/Calculator.java index c98422cae..d12cfc063 100644 --- a/src/main/java/seedu/duke/Calculator.java +++ b/src/main/java/seedu/duke/Calculator.java @@ -6,10 +6,20 @@ import static java.lang.Character.isDigit; +/** + * This class is responsible for calculating the result of the user input. + * It also generates the explanation of the calculation. + */ public class Calculator { - + private static List explanations; + /** + * This method calculates the result of the user input. + * + * @param sb The user input. + * @return The result of the calculation. + */ public double calculate(StringBuilder sb) { Stack numStack = new Stack<>(); Stack opStack = new Stack<>(); @@ -33,6 +43,15 @@ public double calculate(StringBuilder sb) { return Math.round(numStack.peek() * 1000.0) / 1000.0; } + /** + * This method generates the explanation of the calculation. + * + * @param num1 The first number in the calculation. + * @param num2 The second number in the calculation. + * @param op The operator in the calculation. + * @param answer The result of the calculation. + * @return The explanation of the calculation. + */ private static String getExplanation(double num1, double num2, String op, double answer) { String start = "The computation of the problem: " + num1 + " " + op + " " + @@ -84,6 +103,14 @@ private static String getExplanation(double num1, double num2, String op, double return start + alignedProblem + "\n"; } + /** + * This method calculates the result of the two numbers. + * + * @param num1 The first number in the calculation. + * @param num2 The second number in the calculation. + * @param op The operator in the calculation. + * @return The result of the calculation. + */ private static double calculateTwo(double num1, double num2, String op) { double answer; @@ -106,6 +133,12 @@ private static double calculateTwo(double num1, double num2, String op) { return answer; } + /** + * This method converts the formula to suffix notation. + * + * @param formula The formula to be converted. + * @return The formula in suffix notation. + */ private static ArrayList toSuffix(ArrayList formula) { ArrayList suffix = new ArrayList<>(); Stack opStack = new Stack<>(); @@ -129,9 +162,7 @@ private static ArrayList toSuffix(ArrayList formula) { } } opStack.push(op); - } - } while (!opStack.empty()) { @@ -140,16 +171,27 @@ private static ArrayList toSuffix(ArrayList formula) { return suffix; } + /** + * This method checks if the first operator has higher priority than the second operator. + * + * @param op1 The first operator. + * @param op2 The second operator. + * @return True if the first operator has higher priority than the second operator, false otherwise. + */ private static boolean prior(String op1, String op2) { int priority1; int priority2; - priority1 = getPriority(op1); priority2 = getPriority(op2); - return priority1 > priority2; } + /** + * This method returns the priority of the operator. + * + * @param op The operator. + * @return The priority of the operator. + */ static int getPriority(String op) { int priority = 0; switch (op) { @@ -167,6 +209,12 @@ static int getPriority(String op) { return priority; } + /** + * This method converts the formula to separate objects. + * + * @param sb The formula to be converted. + * @return The formula in separate objects. + */ private static ArrayList toFormula(StringBuilder sb) { ArrayList formula = new ArrayList<>(); for (int i = 0; i < sb.length(); ) { // change to separate objects @@ -189,6 +237,11 @@ private static ArrayList toFormula(StringBuilder sb) { return formula; } + /** + * This method returns the explanation of the calculation. + * + * @return The explanation of the calculation. + */ public String getExplanationsString() { StringBuilder builder = new StringBuilder(); for (int i = 0; i < explanations.size(); i++) { diff --git a/src/main/java/seedu/duke/DIYProblemSet.java b/src/main/java/seedu/duke/DIYProblemSet.java index 4379f273a..7d89d9e2b 100644 --- a/src/main/java/seedu/duke/DIYProblemSet.java +++ b/src/main/java/seedu/duke/DIYProblemSet.java @@ -4,6 +4,10 @@ import java.util.ArrayList; import java.util.Scanner; +/** + * Represents a DIY problem set. + * A DIYProblemSet object corresponds to a DIY problem set that is created by the user. + */ public class DIYProblemSet { ArrayList problemSet; @@ -11,19 +15,29 @@ public DIYProblemSet() { problemSet = new ArrayList<>(); } + /** + * Adds a DIY problem set to the list of problem sets. + * + * @param ui the ui for the user to interact with the program + */ public void addDIYProblemSet(Ui ui) { Scanner scanner = new Scanner(System.in); + ui.print("Please input your DIY problemSet: "); String description; String correctAnswer; double answer = 0.0; + String explanations = ""; String quit = ""; + while (!quit.equals("y")) { ui.print("input the description of the problem (e.g. 1+2*3): "); description = scanner.nextLine(); + ui.print("input the correct answer of the problem (e.g. 7): "); correctAnswer = scanner.nextLine(); + boolean isValidAnswer = false; while (!isValidAnswer) { try { @@ -34,22 +48,29 @@ public void addDIYProblemSet(Ui ui) { correctAnswer = scanner.nextLine(); } } + ui.print("Input the explanations of the problem (e.g. 1+2*3=7): "); explanations = scanner.nextLine(); - Problem problem = new Problem(description,answer,explanations); + + Problem problem = new Problem(description, answer, explanations); problemSet.add(problem); + ui.print("Have you finished adding problems? y/n: "); quit = scanner.nextLine(); + while (!quit.equals("y") && !quit.equals("n")) { ui.print("input is invalid! Please input 'y' or 'n': "); quit = scanner.nextLine(); } } + Record record = new Record(LocalDateTime.now(), 0.0, 0.0, problemSet, ProblemSetType.USER_DIY.getValue()); Storage.addRecord(record); + ui.print("Record successfully saved!"); record.print(true); Ui.showLine(); + scanner.close(); } diff --git a/src/main/java/seedu/duke/Duke.java b/src/main/java/seedu/duke/Duke.java index dbc1838da..59a0639bc 100644 --- a/src/main/java/seedu/duke/Duke.java +++ b/src/main/java/seedu/duke/Duke.java @@ -1,5 +1,9 @@ package seedu.duke; +/** + * Represents the Duke program. + * A Duke object corresponds to the Duke program that is created by the user. + */ public class Duke { private static final String name = "MathGenius"; @@ -8,7 +12,6 @@ public class Duke { /** * Main entry-point for the java.duke.Duke application. */ - public static void run() { ui.greet(); diff --git a/src/main/java/seedu/duke/Parser.java b/src/main/java/seedu/duke/Parser.java index 5ee70d0dc..4435c760c 100644 --- a/src/main/java/seedu/duke/Parser.java +++ b/src/main/java/seedu/duke/Parser.java @@ -3,8 +3,18 @@ import java.time.LocalDateTime; import java.util.List; +/** + * Represents a parser. + * A Parser object corresponds to a parser that parses the user input. + */ public class Parser { + /** + * Parses the user input. + * + * @param description the user input + * @param ui the ui for the user to interact with the program + */ public static void parseRetry(String description, Ui ui) { try { int id = Integer.parseInt(description); @@ -22,6 +32,12 @@ public static void parseRetry(String description, Ui ui) { } } + /** + * Parses the user input. + * + * @param description the user input + * @param ui the ui for the user to interact with the program + */ public static void parseRecord(String description, Ui ui) { String[] tokens = description.split(" "); int spdSortOp = 0; @@ -68,12 +84,19 @@ public static void parseRecord(String description, Ui ui) { ui.printRecords(Storage.sortRecords(dateSortOp, spdSortOp, accSortOp, probSortOp), probShowDetails); } + /** + * Solves the problem set. + * + * @param test the test to be solved + * @param ui the ui for the user to interact with the program + * @param retry whether the problem set is a retry + * @param id the id of the problem set + */ public static void solveProbSet(Test test, Ui ui, boolean retry, int id) { Checker checker = new Checker(test); checker.getUserAnswer(); // Use the % format to print acc ui.showTestResult(checker.getAccuracy(), checker.getTime()); - // Show the wrong answer List wrongAnswer = checker.getWrongAnswer(); @@ -117,6 +140,12 @@ public static void solveProbSet(Test test, Ui ui, boolean retry, int id) { Storage.writeFile(); } + /** + * Parses the user input. + * + * @param command the user input + * @param ui the ui for the user to interact with the program + */ public static void parse(String command, Ui ui) { /* diff --git a/src/main/java/seedu/duke/Problem.java b/src/main/java/seedu/duke/Problem.java index 4e1312e5e..a3a5c9e27 100644 --- a/src/main/java/seedu/duke/Problem.java +++ b/src/main/java/seedu/duke/Problem.java @@ -1,11 +1,22 @@ package seedu.duke; +/** + * Represents a problem. + * A Problem object corresponds to a problem that is solved by the user. + */ public class Problem { private final String description; private final double answer; private String explanations; + /** + * Constructor for a problem. + * + * @param description the description of the problem + * @param answer the answer to the problem + * @param explanations2 the explanations of the problem + */ public Problem(String description, double answer, String explanations2) { this.description = description; this.answer = answer; diff --git a/src/main/java/seedu/duke/ProblemGenerator.java b/src/main/java/seedu/duke/ProblemGenerator.java index 4b727d497..e30d4348c 100644 --- a/src/main/java/seedu/duke/ProblemGenerator.java +++ b/src/main/java/seedu/duke/ProblemGenerator.java @@ -3,8 +3,11 @@ import java.util.ArrayList; import java.util.HashMap; -import static java.lang.Character.isDigit; - +/** + * The ProblemGenerator class is responsible for generating math problems based on user input. + * It provides methods to parse user commands, check the validity of input parameters, generate problems, + * and apply specific rules to the generated problems. + */ public class ProblemGenerator { private static final int MINIMUM_NUMBER = 0; @@ -19,6 +22,13 @@ public class ProblemGenerator { private static final String DEFAULT_OPERATORS = VALID_OPERATORS; private static final String DEFAULT_LENGTH = "2"; + /** + * This method parses the user command and returns a HashMap of options. + * It splits the command into tokens and processes each token. + * + * @param command The command to be parsed. + * @return A HashMap containing the parsed options. + */ public static HashMap parseCommand(String command) { HashMap options = new HashMap<>(); String[] tokens = command.split("\\s+"); @@ -45,10 +55,15 @@ public static HashMap parseCommand(String command) { } } - //defaultOptions(command, options); return options; } + /** + * This method applies default options to the provided HashMap if certain options are missing. + * + * @param options The HashMap containing the parsed options. + * @return The updated HashMap with default options applied. + */ private static HashMap defaultOptions(HashMap options) { if (!options.containsKey("operators")) { options.put("operators", DEFAULT_OPERATORS); @@ -69,6 +84,13 @@ private static HashMap defaultOptions(HashMap op return options; } + /** + * This method processes the user command, checks the validity of the parameters, + * applies default options, and generates a Test object based on the parameters. + * + * @param command The user command. + * @return The generated Test object. + */ public Test typeChoose(String command) { HashMap parameter = parseCommand(command); parameter = checkValidity(parameter); @@ -76,14 +98,19 @@ public Test typeChoose(String command) { return generate(parameter); } + /** + * This method checks the validity of the parameters in the provided HashMap. + * It validates the number of problems, maximum digits, length, and operators. + * + * @param parameter The HashMap containing the parameters. + * @return The updated HashMap with valid parameters. + */ private HashMap checkValidity(HashMap parameter) { - try { NumberFormatException e = new NumberFormatException(); int number = Integer.parseInt(parameter.get("number")); if (number < MINIMUM_NUMBER) { System.out.println("Number of problems should be at least 1!"); - throw e; } if (number > MAXIMUM_NUMBER) { @@ -121,7 +148,6 @@ private HashMap checkValidity(HashMap parameter) if (length > MAXIMUM_LENGTH) { System.out.println("Number of operands should be at most 10!"); throw e; - } } catch (NumberFormatException e) { parameter.remove("length"); @@ -145,15 +171,18 @@ private HashMap checkValidity(HashMap parameter) return parameter; } + /** + * This method generates a Test object based on the provided parameters. + * It generates the specified number of problems with the given maximum digits, length, and operators. + * + * @param parameter The HashMap containing the parameters. + * @return The generated Test object. + */ private Test generate(HashMap parameter) { - int number = Integer.parseInt(parameter.get("number")); - int maxDigit = Integer.parseInt(parameter.get("maximumDigits")); String op = parameter.get("operators"); - int length = Integer.parseInt(parameter.get("length")); - ArrayList operations = new ArrayList<>(); if (op.contains("+")) { @@ -169,15 +198,12 @@ private Test generate(HashMap parameter) { operations.add("/"); } - Test test = new Test(op, maxDigit, number, length); - + Ui.showLine(); System.out.println("Generating " + number + " problems, you can preview them below:"); - for (int i = 0; i < number; i++) { - StringBuilder descriptionBuilder = new StringBuilder(); double answer; String explanations; @@ -202,17 +228,20 @@ private Test generate(HashMap parameter) { Problem p = new Problem(description, answer, explanations); System.out.println((i + 1) + ". " + p.unsolved()); test.addToTest(p); - } return test; } - + /** + * This method checks the generated description for division operations and replaces the divisor + * with a non-zero one-digit number. + * + * @param sb The StringBuilder containing the description. + * @return The updated StringBuilder with replaced divisors. + */ private StringBuilder division_check(StringBuilder sb) { - // change the divisor to a non-zero one-digit number for (int i = 0; i < sb.length(); i++) { if (sb.charAt(i) == '/') { - int numStart = i + 1; int numEnd = numStart; while (numEnd < sb.length() && isDigit(sb.charAt(numEnd))) { @@ -228,6 +257,10 @@ private StringBuilder division_check(StringBuilder sb) { return sb; } + // Helper method to check if a character is a digit + private boolean isDigit(char c) { + return c >= '0' && c <= '9'; + } } diff --git a/src/main/java/seedu/duke/ProblemSetType.java b/src/main/java/seedu/duke/ProblemSetType.java index dd6e2067c..1ce01d71e 100644 --- a/src/main/java/seedu/duke/ProblemSetType.java +++ b/src/main/java/seedu/duke/ProblemSetType.java @@ -1,5 +1,8 @@ package seedu.duke; +/** + * Represents the type of problem set. + */ public enum ProblemSetType { USER_DIY("user-DIY"), AUTO_GENERATED("auto-generated"); diff --git a/src/main/java/seedu/duke/Record.java b/src/main/java/seedu/duke/Record.java index f1d732951..123f96532 100644 --- a/src/main/java/seedu/duke/Record.java +++ b/src/main/java/seedu/duke/Record.java @@ -4,7 +4,12 @@ import java.time.format.DateTimeFormatter; import java.util.ArrayList; +/** + * Represents a record. + * A Record object corresponds to a record of the user's attempt. + */ public class Record { + private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); private LocalDateTime dateTime; @@ -18,6 +23,15 @@ public class Record { private int psIndex; private String problemSetType; + /** + * Constructor for a record. + * + * @param dateTime the date and time of the record + * @param speed the speed of the record + * @param accuracy the accuracy of the record + * @param probSet the problem set of the record + * @param problemSetType the type of the problem set + */ public Record(LocalDateTime dateTime, double speed, double accuracy, @@ -32,6 +46,16 @@ public Record(LocalDateTime dateTime, } + /** + * Constructor for a record. + * + * @param dateTime the date and time of the record + * @param speed the speed of the record + * @param accuracy the accuracy of the record + * @param probSet the problem set of the record + * @param psIndex the index of the problem set + * @param problemSetType the type of the problem set + */ public Record(LocalDateTime dateTime, double speed, double accuracy, @@ -46,7 +70,11 @@ public Record(LocalDateTime dateTime, setProblemSetType(problemSetType); } - + /** + * Prints the record. + * + * @param showProbDetails whether to show the problem details + */ public void print(boolean showProbDetails) { if (getSpeed() <= 0.0) { System.out.println("--User DIY Problem Set--"); @@ -71,6 +99,11 @@ public void print(boolean showProbDetails) { } } + /** + * Writes the record to a file. + * + * @return the record as a string + */ public String writeLine() { StringBuilder probStr = new StringBuilder(); for (Problem problem : probSet) { diff --git a/src/main/java/seedu/duke/Storage.java b/src/main/java/seedu/duke/Storage.java index 1d025dfb3..20185e7fb 100644 --- a/src/main/java/seedu/duke/Storage.java +++ b/src/main/java/seedu/duke/Storage.java @@ -32,6 +32,12 @@ public static void clearRecords() { records.clear(); } + /** + * Retrieves a Test object by its ID from the list of records. + * + * @param id The ID of the Test object to be retrieved. + * @return The Test object if found, null otherwise. + */ public static Test problemSetByID(int id) { for (Record record : records) { if (record.getPsIndex() == id) { @@ -41,6 +47,15 @@ public static Test problemSetByID(int id) { return null; } + /** + * Method for sorting the records + * + * @param dateSortOp the sorting option for date + * @param spdSortOp the sorting option for speed + * @param accSortOp the sorting option for accuracy + * @param probSortOp the sorting option for problem set index + * @return the sorted list of records + */ public static ArrayList sortRecords(int dateSortOp, int spdSortOp, int accSortOp, int probSortOp) { int notSortOp = 0; int toSort = 1; diff --git a/src/main/java/seedu/duke/Test.java b/src/main/java/seedu/duke/Test.java index ff3e5d1b8..f538cc8d4 100644 --- a/src/main/java/seedu/duke/Test.java +++ b/src/main/java/seedu/duke/Test.java @@ -2,6 +2,10 @@ import java.util.ArrayList; +/** + * Represents a test. + * A Test object corresponds to a test that is created by the user. + */ public class Test { String operators; @@ -12,6 +16,14 @@ public class Test { ArrayList problemList = new ArrayList<>(); String problemSetType; + /** + * Constructor for a test. + * + * @param operators the operators used in the test + * @param maxDigits the maximum number of digits in the test + * @param number the number of problems in the test + * @param length the length of the test + */ public Test(String operators, int maxDigits, int number, int length) { this.operators = operators; this.maxDigits = maxDigits; @@ -20,6 +32,12 @@ public Test(String operators, int maxDigits, int number, int length) { this.problemSetType = ProblemSetType.AUTO_GENERATED.getValue(); } + /** + * Constructor for a test. + * + * @param problemList the list of problems in the test + * @param problemSetType the type of the problem set + */ public Test(ArrayList problemList, String problemSetType) { this.operators = ""; this.maxDigits = 0; diff --git a/src/main/java/seedu/duke/Ui.java b/src/main/java/seedu/duke/Ui.java index 195d3004f..8590861f7 100644 --- a/src/main/java/seedu/duke/Ui.java +++ b/src/main/java/seedu/duke/Ui.java @@ -5,11 +5,12 @@ /** * Represents the user interface for interacting with the chatbot. + * This class handles all user interactions, including reading user input and displaying output. */ public class Ui { // Pre-defined sentences - private static final String FIRSTTIME_INSTRUCTION = + private static final String FIRST_INSTRUCTION = "First time instruction: To generate a problem set, Input like below:\n" + "< generate -t [type] -n [number] -d [maximum digit] -l [number of operands] >"; private static final String INPUT_INSTRUCTION = @@ -46,19 +47,14 @@ public class Ui { private static final String RETRY_COMMAND = "Retry a problem set you have solved before in the past: \t" + "retry PROBLEM_SET_ID\n" + "PROBLEM_SET_ID is an integer, and can be found by using the 'records' command.\n"; - private static final String DIY_COMMAND = "Add user defined problem sets in to the problem database: \t" + "DIY\n"; - - private static final String EXIT_COMMAND = "Exit program: exit\n"; - private static final String EXPLANATION_START = "\nExplanation Instruction: \n" + - "1.To view the explanation of a problem, type 'explanation' or 'exp'.\n" + + "1. To view the explanation of a problem, type 'explanation' or 'exp'.\n" + "2. The explanation of the problem will be shown step by step " + - "with the formulation of column caculation.\n" + + "with the formulation of column calculation.\n" + "3. If you want to exit the explanation, type 'exit'."; - private static final String EXPLANATION_END = "Explanation End."; @@ -80,6 +76,7 @@ public class Ui { "TEST FINISHED! \n"+ "Here is your test result: "; + private static final String EXIT_COMMAND = "Exit program: exit\n"; private final String name; private final Scanner scanner = new Scanner(System.in); @@ -96,7 +93,6 @@ public Ui(String name) { /** * Displays a greeting message. */ - public void greet() { showLine(); String logo = "__ __ _ _ ____ _\n" + @@ -107,10 +103,15 @@ public void greet() { System.out.println(logo); System.out.println("Hello! I'm " + name + "!"); System.out.println("Type 'help' to see all instructions.\n"); - System.out.println(FIRSTTIME_INSTRUCTION); + System.out.println(FIRST_INSTRUCTION); showLine(); } + /** + * Reads the user's command. + * + * @return The user's command as a String. + */ public String readCommand() { if (scanner.hasNextLine()) { return scanner.nextLine().trim(); @@ -118,10 +119,18 @@ public String readCommand() { return ""; } + /** + * Displays a line separator. + */ public static void showLine() { System.out.println("========================="); } + /** + * Displays help information based on the helpType. + * + * @param helpType The type of help information to display. + */ public void help(String helpType) { switch (helpType) { case "gen": @@ -148,12 +157,21 @@ public void help(String helpType) { showLine(); } - // print functions + /** + * Prints the given string. + * + * @param string The string to print. + */ public void print(String string) { System.out.println(string); } - // records input + /** + * Displays the records. + * + * @param records The records to display. + * @param showProbDetails Whether to show problem details. + */ public void printRecords(ArrayList records, boolean showProbDetails) { for (Record record : records) { showLine(); @@ -162,11 +180,10 @@ public void printRecords(ArrayList records, boolean showProbDetails) { showLine(); } - // invalid input - /** - * Displays an error message for an invalid command. + * Displays an error message for an invalid inputs. */ + public void invalidCommand() { System.out.println("Invalid command! Please try again or enter 'help' for help."); showLine(); @@ -186,6 +203,9 @@ static void missingMessage(String parameters) { System.out.println("Using default " + parameters + " instead!"); } + /** + * Displays the explanation start message. + */ public void showExplanation() { System.out.println(EXPLANATION_START); showLine(); @@ -196,6 +216,11 @@ public void showExplanationEnd() { showLine(); } + /** + * Displays the wrong answer message. + * + * @param i The number of wrong answers. + */ public void showWrongAnswer(int i) { System.out.println("You have " + i + " wrong answers."); System.out.println(WRONG_ANSWER); @@ -213,7 +238,13 @@ public void exitTest() { System.out.println(EXIT_TEST); showLine(); } - + + /** + * Displays the test result. + * + * @param acc The accuracy of the test. + * @param time The time spent on the test. + */ public void showTestResult(double acc, long time) { System.out.println(TEST_FINISH); String accRate = String.format("%.2f", acc * 100); diff --git a/src/test/java/seedu/duke/CheckerTest.java b/src/test/java/seedu/duke/CheckerTest.java index 04d8cb379..44553835c 100644 --- a/src/test/java/seedu/duke/CheckerTest.java +++ b/src/test/java/seedu/duke/CheckerTest.java @@ -3,6 +3,10 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; +/** + * Represents a checker test. + * A CheckerTest object corresponds to a test that checks the correctness of the answer. + */ public class CheckerTest { @org.junit.jupiter.api.Test diff --git a/src/test/java/seedu/duke/DukeTest.java b/src/test/java/seedu/duke/DukeTest.java index 5f8040485..bfa94e8ac 100644 --- a/src/test/java/seedu/duke/DukeTest.java +++ b/src/test/java/seedu/duke/DukeTest.java @@ -4,7 +4,11 @@ import static org.junit.jupiter.api.Assertions.assertTrue; -class DukeTest { +/** + * Represents a duke test. + * A DukeTest object corresponds to a test that checks the correctness of the answer. + */ +public class DukeTest { @Test public void sampleTest() { assertTrue(true); diff --git a/src/test/java/seedu/duke/ProblemGeneratorTest.java b/src/test/java/seedu/duke/ProblemGeneratorTest.java index 80ee805ae..b25d60a3d 100644 --- a/src/test/java/seedu/duke/ProblemGeneratorTest.java +++ b/src/test/java/seedu/duke/ProblemGeneratorTest.java @@ -9,6 +9,10 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; +/** + * Represents a problem generator test. + * A ProblemGeneratorTest object corresponds to a test that checks the correctness of the problem generator. + */ public class ProblemGeneratorTest { public static String[] commands = { "generate -t + -n 1 -d 2 -l 2", diff --git a/src/test/java/seedu/duke/StorageTest.java b/src/test/java/seedu/duke/StorageTest.java index f1af1a388..7fad2d071 100644 --- a/src/test/java/seedu/duke/StorageTest.java +++ b/src/test/java/seedu/duke/StorageTest.java @@ -7,8 +7,15 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.fail; +/** + * Represents a storage test. + * A StorageTest object corresponds to a test that checks the correctness of the storage. + */ public class StorageTest { + /** + * Tests the sortRecords method in Storage. + */ public static void testSortRecords() { Record record1 = new Record(LocalDateTime.now().minusDays(2), 5.0, 0.8, new ArrayList<>(), "user-DIY"); Record record2 = new Record(LocalDateTime.now().minusDays(1), 6.0, 0.7, new ArrayList<>(), "auto-generated"); diff --git a/src/test/java/seedu/duke/UiTest.java b/src/test/java/seedu/duke/UiTest.java index 9d447f9ea..ac04d90d8 100644 --- a/src/test/java/seedu/duke/UiTest.java +++ b/src/test/java/seedu/duke/UiTest.java @@ -8,6 +8,10 @@ import static org.junit.jupiter.api.Assertions.assertEquals; +/** + * Represents a UI test. + * A UiTest object corresponds to a test that checks the correctness of the UI. + */ public class UiTest { private final ByteArrayOutputStream output = new ByteArrayOutputStream(); private Ui ui;