Skip to content

Commit

Permalink
Use JUnit as testing
Browse files Browse the repository at this point in the history
  • Loading branch information
shaokiat committed Aug 26, 2020
1 parent d90bb91 commit 7c8538f
Show file tree
Hide file tree
Showing 48 changed files with 145 additions and 35 deletions.
Binary file removed output/production/ip/main/java/Duke.class
Binary file not shown.
Binary file removed output/production/ip/main/java/Parser.class
Binary file not shown.
Binary file removed output/production/ip/main/java/Storage.class
Binary file not shown.
Binary file removed output/production/ip/main/java/command/Command.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added output/production/ip/main/java/duke/Duke.class
Binary file not shown.
Binary file added output/production/ip/main/java/duke/Parser.class
Binary file not shown.
Binary file added output/production/ip/main/java/duke/Storage.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed output/production/ip/main/java/tasks/Deadline.class
Binary file not shown.
Binary file removed output/production/ip/main/java/tasks/Event.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
23 changes: 8 additions & 15 deletions src/main/java/duke/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,14 @@ public static Command parseCommand(String userInput) throws DukeException {
}

public static Command addCommand(String command) throws DukeException {
String[] cmdLine = command.split(" ");
String taskType = cmdLine[0];

switch (taskType) {
case "todo":
return new TodoCommand(command);

case "deadline":
return new DeadlineCommand(command);

case "event":
return new EventCommand(command);

default:
throw new DukeException("☹ OOPS!!! I'm sorry, but I don't know what that means :-(");
if (command.startsWith("todo")) {
return new TodoCommand(command);
} else if (command.startsWith("deadline")) {
return new DeadlineCommand(command);
} else if (command.startsWith("event")) {
return new EventCommand(command);
} else {
throw new DukeException("☹ OOPS!!! I'm sorry, but I don't know what that means :-(");
}
}
}
9 changes: 7 additions & 2 deletions src/main/java/duke/Storage.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package main.java.duke;

import main.java.duke.exceptions.InvalidFileException;
import main.java.duke.exceptions.InvalidInputException;
import main.java.duke.tasks.*;

import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;

public class Storage {

public DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");

public final String path;

public Storage(String storagePath) {
Expand Down Expand Up @@ -59,10 +64,10 @@ public TaskList load() throws InvalidFileException {
newTask = new Todo(readLine[2]);
break;
case "D":
newTask = new Deadline(readLine[2], readLine[3]);
newTask = new Deadline(readLine[2], LocalDateTime.parse(readLine[3], formatter));
break;
case "E":
newTask = new Event(readLine[2], readLine[3]);
newTask = new Event(readLine[2], LocalDateTime.parse(readLine[3], formatter));
break;
default:
break;
Expand Down
24 changes: 18 additions & 6 deletions src/main/java/duke/command/DeadlineCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,36 @@
import main.java.duke.tasks.Task;
import main.java.duke.tasks.TaskList;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

public class DeadlineCommand extends Command {

public static final String MESSAGE_SUCCESS = "Got it. I've added this task:\n";
public static final String MESSAGE_PARSE_ERROR = "Invalid date and time format.\n" +
"Please enter date and time in the format: yyyy-MM-dd HH:mm";

public DeadlineCommand(String input) {
super(input);
}

@Override
public void execute(TaskList tasks, Ui ui, Storage storage) throws InvalidInputException, InvalidFileException {
if (super.input.length() <= 5) {
if (super.input.length() <= 8) {
throw new InvalidInputException("☹ OOPS!!! The description of a deadline cannot be empty.\n");
}
String[] split = super.input.substring(9).split("/by ", 2);
Task deadline = new Deadline(split[0], split[1]);
tasks.addTask(deadline);
ui.printMessage(MESSAGE_SUCCESS + deadline.toString() + "\nNow you have " + tasks.taskListSize() + " tasks in the list.");
storage.save(tasks);
try {
String[] split = super.input.substring(9).split("/by ", 2);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime date = LocalDateTime.parse(split[1], formatter);
Task deadline = new Deadline(split[0], date);
tasks.addTask(deadline);
ui.printMessage(MESSAGE_SUCCESS + deadline.toString() + "\nNow you have " + tasks.taskListSize() + " tasks in the list.");
storage.save(tasks);
} catch (DateTimeParseException e) {
throw new InvalidInputException(MESSAGE_PARSE_ERROR);
}
}

@Override
Expand Down
25 changes: 19 additions & 6 deletions src/main/java/duke/command/EventCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@
import main.java.duke.tasks.Task;
import main.java.duke.tasks.TaskList;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Date;

public class EventCommand extends Command {
public static final String MESSAGE_SUCCESS = "Got it. I've added this task:\n";
public static final String MESSAGE_PARSE_ERROR = "Invalid date and time format.\n" +
"Please enter date and time in the format: yyyy-MM-dd HH:mm";

public EventCommand(String input) {
super(input);
Expand All @@ -20,12 +27,18 @@ public void execute(TaskList tasks, Ui ui, Storage storage) throws InvalidInputE
if (super.input.length() <= 5) {
throw new InvalidInputException("☹ OOPS!!! The description of a event cannot be empty.\n");
}
String[] split = super.input.substring(6).split("/at ", 2);
Task event = new Event(split[0], split[1]);
tasks.addTask(event);
ui.printMessage(MESSAGE_SUCCESS + event.toString() + "\nNow you have " + tasks.taskListSize() + " tasks in the list.");

storage.save(tasks);
try {
String[] split = super.input.substring(6).split("/at ", 2);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime date = LocalDateTime.parse(split[1], formatter);
Task event = new Event(split[0], date);
tasks.addTask(event);
ui.printMessage(MESSAGE_SUCCESS + event.toString() + "\nNow you have "
+ tasks.taskListSize() + " tasks in the list.");
storage.save(tasks);
} catch (DateTimeParseException e) {
throw new InvalidInputException(MESSAGE_PARSE_ERROR);
}
}

@Override
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/duke/tasks/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
public class Deadline extends Task {
protected LocalDateTime date;

public Deadline(String description, String date) {
public Deadline(String description, LocalDateTime date) {
super(description);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
this.date = LocalDateTime.parse(date, formatter);
this.date = date;
}

@Override
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/duke/tasks/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
public class Event extends Task {
protected LocalDateTime date;

public Event(String description, String date) {
public Event(String description, LocalDateTime date) {
super(description);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
this.date = LocalDateTime.parse(date, formatter);
this.date = date;
}

@Override
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/duke/ParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package test.java.duke;

import main.java.duke.Parser;
import main.java.duke.command.Command;
import main.java.duke.exceptions.DukeException;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class ParserTest {

@Test
public void testParseCommand() {
try {
Parser.parseCommand("event read book /at 2020-10-10 20:00");
assertTrue(true);
} catch (DukeException e) {
fail();
}
}

@Test
public void testParseError() {
try {
Parser.parseCommand("even read book 2020-10-10 20:00");
fail();
} catch (DukeException e) {
assertTrue(true);
}
}

}
28 changes: 28 additions & 0 deletions src/test/java/duke/tasks/DeadlineTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package test.java.duke.tasks;

import main.java.duke.tasks.Deadline;
import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import static org.junit.jupiter.api.Assertions.assertEquals;

class DeadlineTest {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");

@Test
public void constructorTest() {
Deadline deadlineTask = new Deadline("read book ",
LocalDateTime.parse("2020-10-10 10:10", formatter));
assertEquals("[D][✘] read book (by:10/10/2020 10:10 AM)", deadlineTask.toString());
}

@Test
void getState() {
Deadline deadlineTask = new Deadline("read book ",
LocalDateTime.parse("2020-10-10 10:10", formatter));
assertEquals("D|0|read book |2020-10-10 10:10", deadlineTask.getState());
}

}
29 changes: 29 additions & 0 deletions src/test/java/duke/tasks/TodoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package test.java.duke.tasks;

import main.java.duke.tasks.Todo;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;


class TodoTest {

@Test
public void constructorTest() {
Todo todoTask = new Todo("read book");
assertEquals("[T][✘] read book", todoTask.toString());
}

@Test
void getState() {
Todo todoTask = new Todo("read book");
assertEquals("T|0|read book", todoTask.getState());
}

@Test
void doneTask() {
Todo todoTask = new Todo("read book");
todoTask.markAsDone();
assertEquals("[T][✓] read book", todoTask.toString());
}
}

0 comments on commit 7c8538f

Please sign in to comment.