Skip to content

Commit

Permalink
Merge pull request #109 from Andy123qq4/master
Browse files Browse the repository at this point in the history
Clear unused comment, refine syntax and coding standards
  • Loading branch information
Andy123qq4 authored Apr 15, 2024
2 parents 3405db9 + da04d29 commit edf9d98
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 58 deletions.
14 changes: 5 additions & 9 deletions src/main/java/seedu/duke/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import static java.lang.Character.isDigit;

public class Calculator {

private static List<String> explanations = new ArrayList<>();

public double calculate(StringBuilder sb) {
Expand Down Expand Up @@ -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<String> 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);
Expand All @@ -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
Expand All @@ -80,10 +80,6 @@ private static String getExplanation(double num1, double num2, String op, double
}
alignedProblem = builder.toString();





}
return start + alignedProblem + "\n";
}
Expand Down Expand Up @@ -117,7 +113,7 @@ private static ArrayList<Object> toSuffix(ArrayList<Object> 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()) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/duke/Checker.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/seedu/duke/DIYProblemSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

public class DIYProblemSet {
ArrayList<Problem> problemSet;

public DIYProblemSet() {
problemSet = new ArrayList<>();
}

public void addDIYProblemSet(Ui ui) {
Scanner scanner = new Scanner(System.in);
ui.print("Please input your DIY problemSet: ");
Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -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();
}


Expand Down
17 changes: 7 additions & 10 deletions src/main/java/seedu/duke/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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];
Expand All @@ -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();
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/seedu/duke/Problem.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ public Problem(String description, double answer, String explanations2) {
this.description = description;
this.answer = answer;
this.explanations = explanations2;


}

public String solved() {
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/seedu/duke/ProblemGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,6 @@ private Test generate(HashMap<String, String> 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);
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/seedu/duke/Record.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/seedu/duke/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,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;
Expand All @@ -48,22 +48,22 @@ public static ArrayList<Record> sortRecords(int dateSortOp, int spdSortOp, int a
ArrayList<Record> 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);
}
}
Expand All @@ -72,6 +72,7 @@ public static ArrayList<Record> 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.
*/
Expand All @@ -80,7 +81,7 @@ public static void processLine(String line) throws Exception {

String[] words = line.split(" ");

if (words.length < minimumLength ) {
if (words.length < minimumLength) {
throw new Exception();
}

Expand All @@ -100,8 +101,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);
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/duke/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ public void addToTest(Problem p) {
problemList.add(p);
}

public int getNumber(){
public int getNumber() {
return number;
}

public ArrayList<Problem> getProblem() {
public ArrayList<Problem> getProblem() {
return problemList;
}

Expand Down
36 changes: 23 additions & 13 deletions src/test/java/seedu/duke/ProblemGeneratorTest.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;

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<Problem> 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());
Expand All @@ -35,6 +41,7 @@ public void operatorTest() {
}
}
}

private HashMap<String, String> parseCommand(String command) {
return ProblemGenerator.parseCommand(command);
}
Expand All @@ -43,46 +50,49 @@ private ArrayList<Integer> parseNumbers(String problem) {

Pattern pattern = Pattern.compile("-?\\d+");
Matcher matcher = pattern.matcher(problem);

ArrayList<Integer> 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<String, String> parsedCommand = parseCommand(command);
ProblemGenerator pb = new ProblemGenerator();
Test test = pb.typeChoose(command);
ArrayList<Problem> problems = test.getProblem();
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<String, String> parsedCommand = parseCommand(command);
ProblemGenerator pb = new ProblemGenerator();
Test test = pb.typeChoose(command);
ArrayList<Problem> problems = test.getProblem();
for (Problem problem: problems) {
for (Problem problem : problems) {
ArrayList<Integer> 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<String, String> parsedCommand = parseCommand(command);
ProblemGenerator pb = new ProblemGenerator();
Test test = pb.typeChoose(command);
ArrayList<Problem> problems = test.getProblem();
for (Problem problem: problems) {
for (Problem problem : problems) {
ArrayList<Integer> numbers = parseNumbers(problem.unsolved());
assertEquals(numbers.size(), Integer.parseInt(parsedCommand.get("length")),
"length" + problem.unsolved() + "is incorrect");
Expand All @@ -103,6 +113,6 @@ public void calculateTest() {
formula.append("*");
formula.append(3);
Calculator calculator = new Calculator();
assertEquals(calculator.calculate(formula),85);
assertEquals(calculator.calculate(formula), 85);
}
}
2 changes: 1 addition & 1 deletion src/test/java/seedu/duke/StorageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit edf9d98

Please sign in to comment.