From 38bb7e56efc482d539a26606f307c4b0b9399ea1 Mon Sep 17 00:00:00 2001 From: Parcly Taxel Date: Mon, 19 Aug 2019 00:04:27 +0800 Subject: [PATCH] Comply with a coding standard --- build.gradle | 5 + config/checkstyle/checkstyle.xml | 257 ++++++++++++++++++ src/main/java/duke/command/AddCommand.java | 12 +- src/main/java/duke/command/DeleteCommand.java | 6 +- src/main/java/duke/command/DoneCommand.java | 8 +- src/main/java/duke/task/Deadline.java | 6 +- src/main/java/duke/task/Event.java | 6 +- src/main/java/duke/task/Task.java | 4 +- src/main/java/duke/ui/Ui.java | 2 +- src/test/java/duke/DukeTest.java | 15 +- 10 files changed, 290 insertions(+), 31 deletions(-) create mode 100644 config/checkstyle/checkstyle.xml diff --git a/build.gradle b/build.gradle index 8d80677a2f..84f1a4771b 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,7 @@ plugins { id "java" id "application" + id "checkstyle" id "com.github.johnrengelman.shadow" version "5.1.0" } @@ -15,6 +16,10 @@ dependencies { testImplementation "org.junit.jupiter:junit-jupiter:5.5.1" } +checkstyle { + toolVersion = "8.23" +} + shadowJar { archiveBaseName = "duke" archiveVersion = "0.5.0" diff --git a/config/checkstyle/checkstyle.xml b/config/checkstyle/checkstyle.xml new file mode 100644 index 0000000000..b1a57ba6c0 --- /dev/null +++ b/config/checkstyle/checkstyle.xml @@ -0,0 +1,257 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/java/duke/command/AddCommand.java b/src/main/java/duke/command/AddCommand.java index 537ca57f7a..6950114d5e 100644 --- a/src/main/java/duke/command/AddCommand.java +++ b/src/main/java/duke/command/AddCommand.java @@ -10,15 +10,15 @@ * Class representing a command to add a new task. */ public class AddCommand extends Command { - private final Task t; + private final Task task; /** * Creates a new AddCommand with the given task. * - * @param t The task to add. + * @param task The task to add. */ - public AddCommand(Task t) { - this.t = t; + public AddCommand(Task task) { + this.task = task; } /** @@ -29,9 +29,9 @@ public AddCommand(Task t) { * @param storage The place where tasks will be stored. */ public void execute(TaskList tl, Ui ui, Storage storage) { - tl.add(t); + tl.add(task); ui.printMessage("Got it. I've added this task:"); - ui.printMessage(" " + t); + ui.printMessage(" " + task); int n = tl.size(); ui.printMessage(String.format("Now you have %d task%s in the list.", n, n == 1 ? "" : "s")); diff --git a/src/main/java/duke/command/DeleteCommand.java b/src/main/java/duke/command/DeleteCommand.java index dc3335c93a..b94de5acf5 100644 --- a/src/main/java/duke/command/DeleteCommand.java +++ b/src/main/java/duke/command/DeleteCommand.java @@ -10,7 +10,7 @@ * Class representing a command to delete an item from the task list. */ public class DeleteCommand extends Command { - private final int i; + private final int ind; /** * Creates a new DeleteCommand with the specified index. @@ -19,7 +19,7 @@ public class DeleteCommand extends Command { */ public DeleteCommand(String s) { try { - this.i = Integer.parseInt(s); + this.ind = Integer.parseInt(s); } catch (NumberFormatException e) { throw new IllegalArgumentException("Task index for the delete command must be an integer."); } @@ -34,7 +34,7 @@ public DeleteCommand(String s) { */ public void execute(TaskList tl, Ui ui, Storage storage) { try { - Task t = tl.delete(i); + Task t = tl.delete(ind); ui.printMessage("Noted. I've removed this task:"); ui.printMessage(" " + t); int n = tl.size(); diff --git a/src/main/java/duke/command/DoneCommand.java b/src/main/java/duke/command/DoneCommand.java index 24ed3bf3c9..756697a614 100644 --- a/src/main/java/duke/command/DoneCommand.java +++ b/src/main/java/duke/command/DoneCommand.java @@ -9,7 +9,7 @@ * Class representing a command to mark an item in the task list as done. */ public class DoneCommand extends Command { - private final int i; + private final int ind; /** * Creates a new DoneCommand with the specified index. @@ -18,7 +18,7 @@ public class DoneCommand extends Command { */ public DoneCommand(String s) { try { - this.i = Integer.parseInt(s); + this.ind = Integer.parseInt(s); } catch (NumberFormatException e) { throw new IllegalArgumentException("Task index for the done command must be an integer."); } @@ -33,9 +33,9 @@ public DoneCommand(String s) { */ public void execute(TaskList tl, Ui ui, Storage storage) { try { - tl.markDone(i); + tl.markDone(ind); ui.printMessage("Nice! I've marked this task as done:"); - ui.printMessage(" " + tl.get(i)); + ui.printMessage(" " + tl.get(ind)); } catch (IndexOutOfBoundsException e) { ui.printError("Task index must be between 1 and " + tl.size() + "."); } diff --git a/src/main/java/duke/task/Deadline.java b/src/main/java/duke/task/Deadline.java index 552dac23e5..80787e2775 100644 --- a/src/main/java/duke/task/Deadline.java +++ b/src/main/java/duke/task/Deadline.java @@ -33,14 +33,12 @@ public class Deadline extends Task { */ public static Deadline parse(String line) throws IllegalArgumentException { if (line.isEmpty()) { - throw new IllegalArgumentException("The description " + - "of a deadline cannot be empty."); + throw new IllegalArgumentException("The description of a deadline cannot be empty."); } Matcher m = PAT.matcher(line); if (!m.matches()) { - throw new IllegalArgumentException("Deadline description " + - "must include /by surrounded by spaces."); + throw new IllegalArgumentException("Deadline description must include /by surrounded by spaces."); } try { diff --git a/src/main/java/duke/task/Event.java b/src/main/java/duke/task/Event.java index 05dec59615..a913aabe86 100644 --- a/src/main/java/duke/task/Event.java +++ b/src/main/java/duke/task/Event.java @@ -33,14 +33,12 @@ public class Event extends Task { */ public static Event parse(String line) throws IllegalArgumentException { if (line.isEmpty()) { - throw new IllegalArgumentException("The description " + - "of an event cannot be empty."); + throw new IllegalArgumentException("The description of an event cannot be empty."); } Matcher m = PAT.matcher(line); if (!m.matches()) { - throw new IllegalArgumentException("Event description " + - "must include /at surrounded by spaces."); + throw new IllegalArgumentException("Event description must include /at surrounded by spaces."); } try { diff --git a/src/main/java/duke/task/Task.java b/src/main/java/duke/task/Task.java index 9f90e45a93..bb27305e89 100644 --- a/src/main/java/duke/task/Task.java +++ b/src/main/java/duke/task/Task.java @@ -29,7 +29,7 @@ public void markDone() { * (tick for yes, cross for no). */ private String getDoneChar() { - return done ? "\u2713" : "\u2718"; + return done ? "✓" : "✘"; } /** @@ -48,7 +48,7 @@ public String toString() { * with other applications or just viewing bare. * * @return A string representation of this task containing its done status (0 or 1) - * and its description. + * and its description. */ public String export() { return (done ? "1|" : "0|") + desc; diff --git a/src/main/java/duke/ui/Ui.java b/src/main/java/duke/ui/Ui.java index 3b6fd70fcc..6a2f0bf74f 100644 --- a/src/main/java/duke/ui/Ui.java +++ b/src/main/java/duke/ui/Ui.java @@ -30,6 +30,6 @@ public void printWelcome() { * @param msg The specifics of the error. */ public void printError(String msg) { - printMessage("\u2639 OOPS!!! " + msg); + printMessage("☹ OOPS!!! " + msg); } } diff --git a/src/test/java/duke/DukeTest.java b/src/test/java/duke/DukeTest.java index 28a5df55bd..17b2aae556 100644 --- a/src/test/java/duke/DukeTest.java +++ b/src/test/java/duke/DukeTest.java @@ -15,17 +15,18 @@ class DukeTest { @Test void testTask() { Todo t1 = Todo.parse("dig a trench from the fence to lunchtime"); - Event t2 = Event.parse("Beijing Olympics Opening Ceremony /at 8/8/2008 2000"); - Deadline t3 = Deadline.parse("my birth /by 4/12/1998 1251"); - Todo t4 = Todo.parse("nothing"); + assertEquals(t1.toString(), "[T][✘] dig a trench from the fence to lunchtime"); + Event t2 = Event.parse("Beijing Olympics Opening Ceremony /at 8/8/2008 2000"); t2.markDone(); - t4.markDone(); - - assertEquals(t1.toString(), "[T][\u2718] dig a trench from the fence to lunchtime"); - assertEquals(t2.toString(), "[E][\u2713] Beijing Olympics Opening Ceremony " + assertEquals(t2.toString(), "[E][✓] Beijing Olympics Opening Ceremony " + "(at: 8 August 2008 20:00)"); + + Deadline t3 = Deadline.parse("my birth /by 4/12/1998 1251"); assertEquals(t3.export(), "D|0|my birth|4/12/1998 1251"); + + Todo t4 = Todo.parse("nothing"); + t4.markDone(); assertEquals(t4.export(), "T|1|nothing|"); assertThrows(IllegalArgumentException.class, () -> Deadline.parse("heat death /by eternity"));