From 27e68adbd4fc126e1f44ada61d81887f76108198 Mon Sep 17 00:00:00 2001 From: Andy123qq4 <1394568964@qq.com> Date: Mon, 15 Apr 2024 20:56:24 +0800 Subject: [PATCH 1/3] Clear unused comment, refine syntax and coding standards --- src/main/java/seedu/duke/Calculator.java | 14 +++---- src/main/java/seedu/duke/Checker.java | 2 +- src/main/java/seedu/duke/DIYProblemSet.java | 11 +++--- src/main/java/seedu/duke/Parser.java | 17 ++++----- src/main/java/seedu/duke/Problem.java | 2 - .../java/seedu/duke/ProblemGenerator.java | 2 - src/main/java/seedu/duke/Record.java | 2 - src/main/java/seedu/duke/Storage.java | 29 ++++++-------- src/main/java/seedu/duke/Test.java | 4 +- .../java/seedu/duke/ProblemGeneratorTest.java | 38 +++++++++++-------- src/test/java/seedu/duke/StorageTest.java | 2 +- 11 files changed, 57 insertions(+), 66 deletions(-) diff --git a/src/main/java/seedu/duke/Calculator.java b/src/main/java/seedu/duke/Calculator.java index 0ff0a04725..b386ff30df 100644 --- a/src/main/java/seedu/duke/Calculator.java +++ b/src/main/java/seedu/duke/Calculator.java @@ -7,7 +7,7 @@ import static java.lang.Character.isDigit; public class Calculator { - + private static List explanations = new ArrayList<>(); public double calculate(StringBuilder sb) { @@ -35,14 +35,14 @@ public double calculate(StringBuilder sb) { } private static String getExplanation(double num1, double num2, String op, double answer) { - String start = "The computation of the problem: "+ + String start = "The computation of the problem: " + String.valueOf(num1) + " " + op + " " + String.valueOf(num2) + " = " + String.valueOf(answer) + "\n\n"; List explanation = new ArrayList<>(); StringBuilder builder = new StringBuilder(); String alignedProblem = ""; - if (op.equals("/")){ + if (op.equals("/")) { alignedProblem = "The division of " + num1 + " and " + num2 + " is " + answer + "\n"; } else { String firstString = String.valueOf(num1); @@ -54,7 +54,7 @@ private static String getExplanation(double num1, double num2, String op, double firstString = secondString; secondString = temp; } - if(op.equals("*")){ + if (op.equals("*")) { op = "x"; } // align the problem to the . place @@ -80,10 +80,6 @@ private static String getExplanation(double num1, double num2, String op, double } alignedProblem = builder.toString(); - - - - } return start + alignedProblem + "\n"; } @@ -117,7 +113,7 @@ private static ArrayList toSuffix(ArrayList formula) { for (Object object : formula) { if (object instanceof Integer) { suffix.add(object); - } else if (object instanceof String ) { + } else if (object instanceof String) { String op = (String) object; if (opStack.empty()) { diff --git a/src/main/java/seedu/duke/Checker.java b/src/main/java/seedu/duke/Checker.java index 146cd7bf19..9d5fdc63d8 100644 --- a/src/main/java/seedu/duke/Checker.java +++ b/src/main/java/seedu/duke/Checker.java @@ -83,7 +83,7 @@ void getUserAnswer() { // hand with time and acc long endTime = System.currentTimeMillis(); accuracy = (double) correctNumber / test.getNumber(); - //millisecond to second + // millisecond to second this.time = (endTime - startTime) / 1000; for (int i = 0; i < test.getNumber(); i++) { if (isCorrect[i] = false) { diff --git a/src/main/java/seedu/duke/DIYProblemSet.java b/src/main/java/seedu/duke/DIYProblemSet.java index 7d1300557b..3dad5075ed 100644 --- a/src/main/java/seedu/duke/DIYProblemSet.java +++ b/src/main/java/seedu/duke/DIYProblemSet.java @@ -6,9 +6,11 @@ public class DIYProblemSet { ArrayList problemSet; + public DIYProblemSet() { problemSet = new ArrayList<>(); } + public void addDIYProblemSet(Ui ui) { Scanner scanner = new Scanner(System.in); ui.print("Please input your DIY problemSet: "); @@ -23,7 +25,7 @@ public void addDIYProblemSet(Ui ui) { ui.print("input the correct answer of the problem (e.g. 7): "); correctAnswer = scanner.nextLine(); boolean isValidAnswer = false; - while(isValidAnswer == false){ + while (!isValidAnswer) { try { answer = Double.parseDouble(correctAnswer); isValidAnswer = true; @@ -34,7 +36,7 @@ public void addDIYProblemSet(Ui ui) { } - 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(); @@ -43,12 +45,11 @@ public void addDIYProblemSet(Ui ui) { quit = scanner.nextLine(); } } - Record record = new Record(LocalDateTime.now(),0.0, 0.0,problemSet,ProblemSetType.USER_DIY.getValue()); + 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.print("========================="); - + Ui.showLine(); } diff --git a/src/main/java/seedu/duke/Parser.java b/src/main/java/seedu/duke/Parser.java index 4b047565db..22c8d4c76a 100644 --- a/src/main/java/seedu/duke/Parser.java +++ b/src/main/java/seedu/duke/Parser.java @@ -17,10 +17,11 @@ public static void parseRetry(String description, Ui ui) { System.out.println(" " + problem.getDescription()); } solveProbSet(test, ui, true, id); - } catch(Exception e) { + } catch (Exception e) { ui.print("failed to parse a valid problem set ID. Please double-check format."); } } + public static void parseRecord(String description, Ui ui) { String[] tokens = description.split(" "); int spdSortOp = 0; @@ -29,12 +30,12 @@ public static void parseRecord(String description, Ui ui) { int probSortOp = 0; boolean probShowDetails = false; for (String token : tokens) { - if(token.isEmpty()) { + if (token.isEmpty()) { continue; } if (token.equals("-details")) { probShowDetails = true; - } else if(token.length() > 3) { + } else if (token.length() > 3) { ui.invalidParameter("records"); return; } @@ -79,17 +80,17 @@ public static void solveProbSet(Test test, Ui ui, boolean retry, int id) { for (int i = 0; i < wrongProblem.size(); i++) { Problem problem = wrongProblem.get(i); - ui.print("The "+ String.valueOf(i+1)+"th wrong answer of you: "); + ui.print("The " + (i + 1) + "th wrong answer of you: "); ui.print("Your answer: " + problem.getDescription() + " = " + wrongAnswer.get(i)); ui.print("Correct Answer: " + problem.solved()); // need further implementation for 3 more operators ui.print("If you want to see the explanation, type exp or explanation, else just type enter, " + "type exit to stop showing the answer"); String userInput = ui.readCommand(); - if(userInput.equals("exit")) { + if (userInput.equals("exit")) { break; } - if(userInput.equals("exp")||userInput.equals("explanation")) { + if (userInput.equals("exp") || userInput.equals("explanation")) { Checker.showExplanation(problem); } } @@ -125,8 +126,6 @@ public static void parse(String command, Ui ui) { * help */ - - //ui.help(""); // Split the command into two parts: action and description String[] parts = command.split(" ", 2); String action = parts[0]; @@ -136,8 +135,6 @@ public static void parse(String command, Ui ui) { } switch (action) { - //case "": // by default, it will be "gen" - // I deleted this "backdoor" at 4.15 18:19 ycg case "gen": case "generate": ProblemGenerator pb = new ProblemGenerator(); diff --git a/src/main/java/seedu/duke/Problem.java b/src/main/java/seedu/duke/Problem.java index 1959ffbeca..4e1312e5ef 100644 --- a/src/main/java/seedu/duke/Problem.java +++ b/src/main/java/seedu/duke/Problem.java @@ -10,8 +10,6 @@ public Problem(String description, double answer, String explanations2) { this.description = description; this.answer = answer; this.explanations = explanations2; - - } public String solved() { diff --git a/src/main/java/seedu/duke/ProblemGenerator.java b/src/main/java/seedu/duke/ProblemGenerator.java index 5705805d5d..a630fe7bd9 100644 --- a/src/main/java/seedu/duke/ProblemGenerator.java +++ b/src/main/java/seedu/duke/ProblemGenerator.java @@ -195,8 +195,6 @@ private Test generate(HashMap parameter) { explanations = calculator.getExplanationsString(); String description = descriptionBuilder.toString(); - - Problem p = new Problem(description, answer, explanations); System.out.println((i + 1) + ". " + p.unsolved()); test.addToTest(p); diff --git a/src/main/java/seedu/duke/Record.java b/src/main/java/seedu/duke/Record.java index 6c0c2e8253..2dbd29c33f 100644 --- a/src/main/java/seedu/duke/Record.java +++ b/src/main/java/seedu/duke/Record.java @@ -48,8 +48,6 @@ public Record(LocalDateTime dateTime, public void print(boolean showProbDetails) { - // ui.printRecords(showProbDetails, this); - System.out.println("Date Time: " + getDateTime().format(formatter)); System.out.println("ProblemSet ID: " + getPsIndex()); if (showProbDetails) { diff --git a/src/main/java/seedu/duke/Storage.java b/src/main/java/seedu/duke/Storage.java index 638484e583..ebaa07a4d7 100644 --- a/src/main/java/seedu/duke/Storage.java +++ b/src/main/java/seedu/duke/Storage.java @@ -1,11 +1,6 @@ package seedu.duke; -import java.io.BufferedReader; -import java.io.FileReader; -import java.io.BufferedWriter; -import java.io.FileWriter; -import java.io.IOException; - +import java.io.*; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; @@ -35,7 +30,7 @@ public static void clearRecords() { public static Test problemSetByID(int id) { for (Record record : records) { if (record.getPsIndex() == id) { - return new Test(record.getProbSet(),record.getProblemSetType()); + return new Test(record.getProbSet(), record.getProblemSetType()); } } return null; @@ -48,22 +43,22 @@ public static ArrayList sortRecords(int dateSortOp, int spdSortOp, int a ArrayList sortedRecords = new ArrayList<>(records); if (dateSortOp != notSortOp) { sortedRecords.sort(Comparator.comparing(Record::getDateTime)); - if(dateSortOp == reverseSortOp) { + if (dateSortOp == reverseSortOp) { Collections.reverse(sortedRecords); } - } else if(spdSortOp != notSortOp) { + } else if (spdSortOp != notSortOp) { sortedRecords.sort(Comparator.comparing(Record::getSpeed)); - if(spdSortOp == reverseSortOp) { + if (spdSortOp == reverseSortOp) { Collections.reverse(sortedRecords); } - } else if(accSortOp != notSortOp) { + } else if (accSortOp != notSortOp) { sortedRecords.sort(Comparator.comparing(Record::getAccuracy)); - if(accSortOp == reverseSortOp) { + if (accSortOp == reverseSortOp) { Collections.reverse(sortedRecords); } - } else if(probSortOp != notSortOp) { + } else if (probSortOp != notSortOp) { sortedRecords.sort(Comparator.comparing(Record::getPsIndex)); - if(probSortOp == reverseSortOp) { + if (probSortOp == reverseSortOp) { Collections.reverse(sortedRecords); } } @@ -72,6 +67,7 @@ public static ArrayList sortRecords(int dateSortOp, int spdSortOp, int a /** * Method for processing a line of input + * * @param line the line to be processed * @throws Exception exception is thrown whenever the input format is corrupt. */ @@ -80,7 +76,7 @@ public static void processLine(String line) throws Exception { String[] words = line.split(" "); - if (words.length < minimumLength ) { + if (words.length < minimumLength) { throw new Exception(); } @@ -100,8 +96,7 @@ public static void processLine(String line) throws Exception { } - - Record record = new Record(dateTime, speed, accuracy, probSet, psIndex,problemSetType); + Record record = new Record(dateTime, speed, accuracy, probSet, psIndex, problemSetType); addRecord(record); } diff --git a/src/main/java/seedu/duke/Test.java b/src/main/java/seedu/duke/Test.java index ac3cff0c3e..ff3e5d1b8a 100644 --- a/src/main/java/seedu/duke/Test.java +++ b/src/main/java/seedu/duke/Test.java @@ -32,11 +32,11 @@ public void addToTest(Problem p) { problemList.add(p); } - public int getNumber(){ + public int getNumber() { return number; } - public ArrayList getProblem() { + public ArrayList getProblem() { return problemList; } diff --git a/src/test/java/seedu/duke/ProblemGeneratorTest.java b/src/test/java/seedu/duke/ProblemGeneratorTest.java index 3ee0cfe9b2..94dcad8f12 100644 --- a/src/test/java/seedu/duke/ProblemGeneratorTest.java +++ b/src/test/java/seedu/duke/ProblemGeneratorTest.java @@ -1,22 +1,26 @@ package seedu.duke; + import java.util.ArrayList; import java.util.HashMap; import java.util.regex.Matcher; import java.util.regex.Pattern; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; -import static org.junit.jupiter.api.Assertions.assertEquals; + +import static org.junit.jupiter.api.Assertions.*; public class ProblemGeneratorTest { - public static String[] commands = {"generate -t + -n 1 -d 2 -l 2", "generate -t - -n 2 -d 3 -l 3", - "generate -t * -n 3 -d 4 -l 4", "generate -t / -n 4 -d 5 -l 5"}; + public static String[] commands = { + "generate -t + -n 1 -d 2 -l 2", + "generate -t - -n 2 -d 3 -l 3", + "generate -t * -n 3 -d 4 -l 4", + "generate -t / -n 4 -d 5 -l 5"}; + @org.junit.jupiter.api.Test public void operatorTest() { - for (String command: commands) { + for (String command : commands) { ProblemGenerator pb = new ProblemGenerator(); Test test = pb.typeChoose(command); ArrayList problems = test.getProblem(); - for (Problem problem: problems) { + for (Problem problem : problems) { if (command.equals(commands[0])) { assertTrue(problem.unsolved().contains("+"), "+: Problem format is incorrect: " + problem.unsolved()); @@ -35,6 +39,7 @@ public void operatorTest() { } } } + private HashMap parseCommand(String command) { return ProblemGenerator.parseCommand(command); } @@ -43,16 +48,17 @@ private ArrayList parseNumbers(String problem) { Pattern pattern = Pattern.compile("-?\\d+"); Matcher matcher = pattern.matcher(problem); - + ArrayList numbers = new ArrayList<>(); while (matcher.find()) { numbers.add(Integer.parseInt(matcher.group())); } return numbers; } + @org.junit.jupiter.api.Test public void numberTest() { - for (String command: commands) { + for (String command : commands) { HashMap parsedCommand = parseCommand(command); ProblemGenerator pb = new ProblemGenerator(); Test test = pb.typeChoose(command); @@ -60,29 +66,31 @@ public void numberTest() { assertEquals(Integer.parseInt(parsedCommand.get("number")), problems.size()); } } + @org.junit.jupiter.api.Test public void digitTest() { - for (String command: commands) { + for (String command : commands) { HashMap parsedCommand = parseCommand(command); ProblemGenerator pb = new ProblemGenerator(); Test test = pb.typeChoose(command); ArrayList problems = test.getProblem(); - for (Problem problem: problems) { + for (Problem problem : problems) { ArrayList numbers = parseNumbers(problem.unsolved()); - for (int number: numbers) { + for (int number : numbers) { assertTrue(Integer.parseInt(parsedCommand.get("maximumDigits")) >= (int) Math.log10(number) + 1); } } } } + @org.junit.jupiter.api.Test public void lengthTest() { - for (String command: commands) { + for (String command : commands) { HashMap parsedCommand = parseCommand(command); ProblemGenerator pb = new ProblemGenerator(); Test test = pb.typeChoose(command); ArrayList problems = test.getProblem(); - for (Problem problem: problems) { + for (Problem problem : problems) { ArrayList numbers = parseNumbers(problem.unsolved()); assertEquals(numbers.size(), Integer.parseInt(parsedCommand.get("length")), "length" + problem.unsolved() + "is incorrect"); @@ -103,6 +111,6 @@ public void calculateTest() { formula.append("*"); formula.append(3); Calculator calculator = new Calculator(); - assertEquals(calculator.calculate(formula),85); + assertEquals(calculator.calculate(formula), 85); } } diff --git a/src/test/java/seedu/duke/StorageTest.java b/src/test/java/seedu/duke/StorageTest.java index 0a48c19a14..f1af1a3880 100644 --- a/src/test/java/seedu/duke/StorageTest.java +++ b/src/test/java/seedu/duke/StorageTest.java @@ -60,7 +60,7 @@ public static void testSortRecords() { } public static void testAddRecord() { - Record record = new Record(LocalDateTime.now(), 5.0, 0.8, new ArrayList<>(),"auto-generated"); + Record record = new Record(LocalDateTime.now(), 5.0, 0.8, new ArrayList<>(), "auto-generated"); Storage.addRecord(record); assertEquals(1, Storage.getRecords().size()); assertEquals(record, Storage.getRecords().get(0)); From 4081555e350468cdb5cb8dcae697c9e9ea092f50 Mon Sep 17 00:00:00 2001 From: Andy123qq4 <1394568964@qq.com> Date: Mon, 15 Apr 2024 20:56:24 +0800 Subject: [PATCH 2/3] Clear unused comment, refine syntax and coding standards --- src/main/java/seedu/duke/Calculator.java | 14 +++---- src/main/java/seedu/duke/Checker.java | 2 +- src/main/java/seedu/duke/DIYProblemSet.java | 11 +++--- src/main/java/seedu/duke/Parser.java | 17 ++++----- src/main/java/seedu/duke/Problem.java | 2 - .../java/seedu/duke/ProblemGenerator.java | 2 - src/main/java/seedu/duke/Record.java | 2 - src/main/java/seedu/duke/Storage.java | 29 ++++++-------- src/main/java/seedu/duke/Test.java | 4 +- .../java/seedu/duke/ProblemGeneratorTest.java | 38 +++++++++++-------- src/test/java/seedu/duke/StorageTest.java | 2 +- 11 files changed, 57 insertions(+), 66 deletions(-) diff --git a/src/main/java/seedu/duke/Calculator.java b/src/main/java/seedu/duke/Calculator.java index 0ff0a04725..b386ff30df 100644 --- a/src/main/java/seedu/duke/Calculator.java +++ b/src/main/java/seedu/duke/Calculator.java @@ -7,7 +7,7 @@ import static java.lang.Character.isDigit; public class Calculator { - + private static List explanations = new ArrayList<>(); public double calculate(StringBuilder sb) { @@ -35,14 +35,14 @@ public double calculate(StringBuilder sb) { } private static String getExplanation(double num1, double num2, String op, double answer) { - String start = "The computation of the problem: "+ + String start = "The computation of the problem: " + String.valueOf(num1) + " " + op + " " + String.valueOf(num2) + " = " + String.valueOf(answer) + "\n\n"; List explanation = new ArrayList<>(); StringBuilder builder = new StringBuilder(); String alignedProblem = ""; - if (op.equals("/")){ + if (op.equals("/")) { alignedProblem = "The division of " + num1 + " and " + num2 + " is " + answer + "\n"; } else { String firstString = String.valueOf(num1); @@ -54,7 +54,7 @@ private static String getExplanation(double num1, double num2, String op, double firstString = secondString; secondString = temp; } - if(op.equals("*")){ + if (op.equals("*")) { op = "x"; } // align the problem to the . place @@ -80,10 +80,6 @@ private static String getExplanation(double num1, double num2, String op, double } alignedProblem = builder.toString(); - - - - } return start + alignedProblem + "\n"; } @@ -117,7 +113,7 @@ private static ArrayList toSuffix(ArrayList formula) { for (Object object : formula) { if (object instanceof Integer) { suffix.add(object); - } else if (object instanceof String ) { + } else if (object instanceof String) { String op = (String) object; if (opStack.empty()) { diff --git a/src/main/java/seedu/duke/Checker.java b/src/main/java/seedu/duke/Checker.java index 146cd7bf19..9d5fdc63d8 100644 --- a/src/main/java/seedu/duke/Checker.java +++ b/src/main/java/seedu/duke/Checker.java @@ -83,7 +83,7 @@ void getUserAnswer() { // hand with time and acc long endTime = System.currentTimeMillis(); accuracy = (double) correctNumber / test.getNumber(); - //millisecond to second + // millisecond to second this.time = (endTime - startTime) / 1000; for (int i = 0; i < test.getNumber(); i++) { if (isCorrect[i] = false) { diff --git a/src/main/java/seedu/duke/DIYProblemSet.java b/src/main/java/seedu/duke/DIYProblemSet.java index 7d1300557b..3dad5075ed 100644 --- a/src/main/java/seedu/duke/DIYProblemSet.java +++ b/src/main/java/seedu/duke/DIYProblemSet.java @@ -6,9 +6,11 @@ public class DIYProblemSet { ArrayList problemSet; + public DIYProblemSet() { problemSet = new ArrayList<>(); } + public void addDIYProblemSet(Ui ui) { Scanner scanner = new Scanner(System.in); ui.print("Please input your DIY problemSet: "); @@ -23,7 +25,7 @@ public void addDIYProblemSet(Ui ui) { ui.print("input the correct answer of the problem (e.g. 7): "); correctAnswer = scanner.nextLine(); boolean isValidAnswer = false; - while(isValidAnswer == false){ + while (!isValidAnswer) { try { answer = Double.parseDouble(correctAnswer); isValidAnswer = true; @@ -34,7 +36,7 @@ public void addDIYProblemSet(Ui ui) { } - 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(); @@ -43,12 +45,11 @@ public void addDIYProblemSet(Ui ui) { quit = scanner.nextLine(); } } - Record record = new Record(LocalDateTime.now(),0.0, 0.0,problemSet,ProblemSetType.USER_DIY.getValue()); + 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.print("========================="); - + Ui.showLine(); } diff --git a/src/main/java/seedu/duke/Parser.java b/src/main/java/seedu/duke/Parser.java index 4b047565db..22c8d4c76a 100644 --- a/src/main/java/seedu/duke/Parser.java +++ b/src/main/java/seedu/duke/Parser.java @@ -17,10 +17,11 @@ public static void parseRetry(String description, Ui ui) { System.out.println(" " + problem.getDescription()); } solveProbSet(test, ui, true, id); - } catch(Exception e) { + } catch (Exception e) { ui.print("failed to parse a valid problem set ID. Please double-check format."); } } + public static void parseRecord(String description, Ui ui) { String[] tokens = description.split(" "); int spdSortOp = 0; @@ -29,12 +30,12 @@ public static void parseRecord(String description, Ui ui) { int probSortOp = 0; boolean probShowDetails = false; for (String token : tokens) { - if(token.isEmpty()) { + if (token.isEmpty()) { continue; } if (token.equals("-details")) { probShowDetails = true; - } else if(token.length() > 3) { + } else if (token.length() > 3) { ui.invalidParameter("records"); return; } @@ -79,17 +80,17 @@ public static void solveProbSet(Test test, Ui ui, boolean retry, int id) { for (int i = 0; i < wrongProblem.size(); i++) { Problem problem = wrongProblem.get(i); - ui.print("The "+ String.valueOf(i+1)+"th wrong answer of you: "); + ui.print("The " + (i + 1) + "th wrong answer of you: "); ui.print("Your answer: " + problem.getDescription() + " = " + wrongAnswer.get(i)); ui.print("Correct Answer: " + problem.solved()); // need further implementation for 3 more operators ui.print("If you want to see the explanation, type exp or explanation, else just type enter, " + "type exit to stop showing the answer"); String userInput = ui.readCommand(); - if(userInput.equals("exit")) { + if (userInput.equals("exit")) { break; } - if(userInput.equals("exp")||userInput.equals("explanation")) { + if (userInput.equals("exp") || userInput.equals("explanation")) { Checker.showExplanation(problem); } } @@ -125,8 +126,6 @@ public static void parse(String command, Ui ui) { * help */ - - //ui.help(""); // Split the command into two parts: action and description String[] parts = command.split(" ", 2); String action = parts[0]; @@ -136,8 +135,6 @@ public static void parse(String command, Ui ui) { } switch (action) { - //case "": // by default, it will be "gen" - // I deleted this "backdoor" at 4.15 18:19 ycg case "gen": case "generate": ProblemGenerator pb = new ProblemGenerator(); diff --git a/src/main/java/seedu/duke/Problem.java b/src/main/java/seedu/duke/Problem.java index 1959ffbeca..4e1312e5ef 100644 --- a/src/main/java/seedu/duke/Problem.java +++ b/src/main/java/seedu/duke/Problem.java @@ -10,8 +10,6 @@ public Problem(String description, double answer, String explanations2) { this.description = description; this.answer = answer; this.explanations = explanations2; - - } public String solved() { diff --git a/src/main/java/seedu/duke/ProblemGenerator.java b/src/main/java/seedu/duke/ProblemGenerator.java index 5705805d5d..a630fe7bd9 100644 --- a/src/main/java/seedu/duke/ProblemGenerator.java +++ b/src/main/java/seedu/duke/ProblemGenerator.java @@ -195,8 +195,6 @@ private Test generate(HashMap parameter) { explanations = calculator.getExplanationsString(); String description = descriptionBuilder.toString(); - - Problem p = new Problem(description, answer, explanations); System.out.println((i + 1) + ". " + p.unsolved()); test.addToTest(p); diff --git a/src/main/java/seedu/duke/Record.java b/src/main/java/seedu/duke/Record.java index 6c0c2e8253..2dbd29c33f 100644 --- a/src/main/java/seedu/duke/Record.java +++ b/src/main/java/seedu/duke/Record.java @@ -48,8 +48,6 @@ public Record(LocalDateTime dateTime, public void print(boolean showProbDetails) { - // ui.printRecords(showProbDetails, this); - System.out.println("Date Time: " + getDateTime().format(formatter)); System.out.println("ProblemSet ID: " + getPsIndex()); if (showProbDetails) { diff --git a/src/main/java/seedu/duke/Storage.java b/src/main/java/seedu/duke/Storage.java index 638484e583..ebaa07a4d7 100644 --- a/src/main/java/seedu/duke/Storage.java +++ b/src/main/java/seedu/duke/Storage.java @@ -1,11 +1,6 @@ package seedu.duke; -import java.io.BufferedReader; -import java.io.FileReader; -import java.io.BufferedWriter; -import java.io.FileWriter; -import java.io.IOException; - +import java.io.*; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; @@ -35,7 +30,7 @@ public static void clearRecords() { public static Test problemSetByID(int id) { for (Record record : records) { if (record.getPsIndex() == id) { - return new Test(record.getProbSet(),record.getProblemSetType()); + return new Test(record.getProbSet(), record.getProblemSetType()); } } return null; @@ -48,22 +43,22 @@ public static ArrayList sortRecords(int dateSortOp, int spdSortOp, int a ArrayList sortedRecords = new ArrayList<>(records); if (dateSortOp != notSortOp) { sortedRecords.sort(Comparator.comparing(Record::getDateTime)); - if(dateSortOp == reverseSortOp) { + if (dateSortOp == reverseSortOp) { Collections.reverse(sortedRecords); } - } else if(spdSortOp != notSortOp) { + } else if (spdSortOp != notSortOp) { sortedRecords.sort(Comparator.comparing(Record::getSpeed)); - if(spdSortOp == reverseSortOp) { + if (spdSortOp == reverseSortOp) { Collections.reverse(sortedRecords); } - } else if(accSortOp != notSortOp) { + } else if (accSortOp != notSortOp) { sortedRecords.sort(Comparator.comparing(Record::getAccuracy)); - if(accSortOp == reverseSortOp) { + if (accSortOp == reverseSortOp) { Collections.reverse(sortedRecords); } - } else if(probSortOp != notSortOp) { + } else if (probSortOp != notSortOp) { sortedRecords.sort(Comparator.comparing(Record::getPsIndex)); - if(probSortOp == reverseSortOp) { + if (probSortOp == reverseSortOp) { Collections.reverse(sortedRecords); } } @@ -72,6 +67,7 @@ public static ArrayList sortRecords(int dateSortOp, int spdSortOp, int a /** * Method for processing a line of input + * * @param line the line to be processed * @throws Exception exception is thrown whenever the input format is corrupt. */ @@ -80,7 +76,7 @@ public static void processLine(String line) throws Exception { String[] words = line.split(" "); - if (words.length < minimumLength ) { + if (words.length < minimumLength) { throw new Exception(); } @@ -100,8 +96,7 @@ public static void processLine(String line) throws Exception { } - - Record record = new Record(dateTime, speed, accuracy, probSet, psIndex,problemSetType); + Record record = new Record(dateTime, speed, accuracy, probSet, psIndex, problemSetType); addRecord(record); } diff --git a/src/main/java/seedu/duke/Test.java b/src/main/java/seedu/duke/Test.java index ac3cff0c3e..ff3e5d1b8a 100644 --- a/src/main/java/seedu/duke/Test.java +++ b/src/main/java/seedu/duke/Test.java @@ -32,11 +32,11 @@ public void addToTest(Problem p) { problemList.add(p); } - public int getNumber(){ + public int getNumber() { return number; } - public ArrayList getProblem() { + public ArrayList getProblem() { return problemList; } diff --git a/src/test/java/seedu/duke/ProblemGeneratorTest.java b/src/test/java/seedu/duke/ProblemGeneratorTest.java index 3ee0cfe9b2..94dcad8f12 100644 --- a/src/test/java/seedu/duke/ProblemGeneratorTest.java +++ b/src/test/java/seedu/duke/ProblemGeneratorTest.java @@ -1,22 +1,26 @@ package seedu.duke; + import java.util.ArrayList; import java.util.HashMap; import java.util.regex.Matcher; import java.util.regex.Pattern; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; -import static org.junit.jupiter.api.Assertions.assertEquals; + +import static org.junit.jupiter.api.Assertions.*; public class ProblemGeneratorTest { - public static String[] commands = {"generate -t + -n 1 -d 2 -l 2", "generate -t - -n 2 -d 3 -l 3", - "generate -t * -n 3 -d 4 -l 4", "generate -t / -n 4 -d 5 -l 5"}; + public static String[] commands = { + "generate -t + -n 1 -d 2 -l 2", + "generate -t - -n 2 -d 3 -l 3", + "generate -t * -n 3 -d 4 -l 4", + "generate -t / -n 4 -d 5 -l 5"}; + @org.junit.jupiter.api.Test public void operatorTest() { - for (String command: commands) { + for (String command : commands) { ProblemGenerator pb = new ProblemGenerator(); Test test = pb.typeChoose(command); ArrayList problems = test.getProblem(); - for (Problem problem: problems) { + for (Problem problem : problems) { if (command.equals(commands[0])) { assertTrue(problem.unsolved().contains("+"), "+: Problem format is incorrect: " + problem.unsolved()); @@ -35,6 +39,7 @@ public void operatorTest() { } } } + private HashMap parseCommand(String command) { return ProblemGenerator.parseCommand(command); } @@ -43,16 +48,17 @@ private ArrayList parseNumbers(String problem) { Pattern pattern = Pattern.compile("-?\\d+"); Matcher matcher = pattern.matcher(problem); - + ArrayList numbers = new ArrayList<>(); while (matcher.find()) { numbers.add(Integer.parseInt(matcher.group())); } return numbers; } + @org.junit.jupiter.api.Test public void numberTest() { - for (String command: commands) { + for (String command : commands) { HashMap parsedCommand = parseCommand(command); ProblemGenerator pb = new ProblemGenerator(); Test test = pb.typeChoose(command); @@ -60,29 +66,31 @@ public void numberTest() { assertEquals(Integer.parseInt(parsedCommand.get("number")), problems.size()); } } + @org.junit.jupiter.api.Test public void digitTest() { - for (String command: commands) { + for (String command : commands) { HashMap parsedCommand = parseCommand(command); ProblemGenerator pb = new ProblemGenerator(); Test test = pb.typeChoose(command); ArrayList problems = test.getProblem(); - for (Problem problem: problems) { + for (Problem problem : problems) { ArrayList numbers = parseNumbers(problem.unsolved()); - for (int number: numbers) { + for (int number : numbers) { assertTrue(Integer.parseInt(parsedCommand.get("maximumDigits")) >= (int) Math.log10(number) + 1); } } } } + @org.junit.jupiter.api.Test public void lengthTest() { - for (String command: commands) { + for (String command : commands) { HashMap parsedCommand = parseCommand(command); ProblemGenerator pb = new ProblemGenerator(); Test test = pb.typeChoose(command); ArrayList problems = test.getProblem(); - for (Problem problem: problems) { + for (Problem problem : problems) { ArrayList numbers = parseNumbers(problem.unsolved()); assertEquals(numbers.size(), Integer.parseInt(parsedCommand.get("length")), "length" + problem.unsolved() + "is incorrect"); @@ -103,6 +111,6 @@ public void calculateTest() { formula.append("*"); formula.append(3); Calculator calculator = new Calculator(); - assertEquals(calculator.calculate(formula),85); + assertEquals(calculator.calculate(formula), 85); } } diff --git a/src/test/java/seedu/duke/StorageTest.java b/src/test/java/seedu/duke/StorageTest.java index 0a48c19a14..f1af1a3880 100644 --- a/src/test/java/seedu/duke/StorageTest.java +++ b/src/test/java/seedu/duke/StorageTest.java @@ -60,7 +60,7 @@ public static void testSortRecords() { } public static void testAddRecord() { - Record record = new Record(LocalDateTime.now(), 5.0, 0.8, new ArrayList<>(),"auto-generated"); + Record record = new Record(LocalDateTime.now(), 5.0, 0.8, new ArrayList<>(), "auto-generated"); Storage.addRecord(record); assertEquals(1, Storage.getRecords().size()); assertEquals(record, Storage.getRecords().get(0)); From da04d29381219d3292e922d47ad78aa606691844 Mon Sep 17 00:00:00 2001 From: Andy123qq4 <1394568964@qq.com> Date: Mon, 15 Apr 2024 21:02:49 +0800 Subject: [PATCH 3/3] Avoid * imports --- src/main/java/seedu/duke/Storage.java | 7 ++++++- src/test/java/seedu/duke/ProblemGeneratorTest.java | 4 +++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/java/seedu/duke/Storage.java b/src/main/java/seedu/duke/Storage.java index ebaa07a4d7..1d025dfb3f 100644 --- a/src/main/java/seedu/duke/Storage.java +++ b/src/main/java/seedu/duke/Storage.java @@ -1,6 +1,11 @@ package seedu.duke; -import java.io.*; +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.BufferedWriter; +import java.io.FileWriter; +import java.io.IOException; + import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; diff --git a/src/test/java/seedu/duke/ProblemGeneratorTest.java b/src/test/java/seedu/duke/ProblemGeneratorTest.java index 94dcad8f12..80ee805ae7 100644 --- a/src/test/java/seedu/duke/ProblemGeneratorTest.java +++ b/src/test/java/seedu/duke/ProblemGeneratorTest.java @@ -5,7 +5,9 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; public class ProblemGeneratorTest { public static String[] commands = {