Skip to content

Commit

Permalink
Completed A-JUnit
Browse files Browse the repository at this point in the history
  • Loading branch information
ZiHawkEye committed Sep 3, 2019
1 parent da7bff7 commit 6bd3400
Show file tree
Hide file tree
Showing 24 changed files with 164 additions and 116 deletions.
18 changes: 18 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="bin/main" path="src/main/java">
<attributes>
<attribute name="gradle_scope" value="main"/>
<attribute name="gradle_used_by_scope" value="main,test"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="bin/test" path="src/test/java">
<attributes>
<attribute name="gradle_scope" value="test"/>
<attribute name="gradle_used_by_scope" value="test"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11/"/>
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
<classpathentry kind="output" path="bin/default"/>
</classpath>
23 changes: 23 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>duke</name>
<comment>Project duke created by Buildship.</comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
</natures>
</projectDescription>
2 changes: 2 additions & 0 deletions .settings/org.eclipse.buildship.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
connection.project.dir=
eclipse.preferences.version=1
15 changes: 14 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
plugins {
id 'java'
id 'application'
id 'checkstyle'
}

checkstyle {
toolVersion = '8.23'
}

dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.5.0'
}

test {
useJUnitPlatform()
}

group 'seedu.duke'
Expand All @@ -12,7 +25,7 @@ repositories {

application {
// Change this to your main class.
mainClassName = "seedu.duke.Duke"
mainClassName = "duke.Duke"
}

run {
Expand Down
14 changes: 5 additions & 9 deletions src/main/java/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,11 @@ public Command clone(String fullCommand) {
}

public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException {
try {
Task task = Parser.parseTask(this.fullCommand);
tasks.add(task);
ui.printResponse("Got it. I've added this task:\n "
+ task.toString() + "\n"
+ "Now you have " + tasks.size() +" tasks in the list.");
} catch(DukeException e) {
throw new DukeException("☹ OOPS!!! The description of a todo cannot be empty.");
}
Task task = Parser.parseTask(this.fullCommand);
tasks.add(task);
ui.printResponse("Got it. I've added this task:\n "
+ task.toString() + "\n"
+ "Now you have " + tasks.size() +" tasks in the list.");
}

public boolean isExit() {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/Deadline.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package duke.task;

import java.util.Date;
import java.util.Scanner;

public class Deadline extends Task {
public Deadline(String description, Date date) {
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ public Command clone(String fullCommand) {
return new DeleteCommand(fullCommand);
}

public void execute(TaskList tasks, Ui ui, Storage storage) {
public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException {
int itemId = Parser.parseDelete(this.fullCommand);
Task item = tasks.remove(itemId);
ui.printResponse("Noted. I've removed this task: \n "
try {
Task item = tasks.remove(itemId);
ui.printResponse("Noted. I've removed this task: \n "
+ item.toString() + "\n"
+ "Now you have " + tasks.size() +" tasks in the list.");
} catch(IndexOutOfBoundsException e) {
throw new DukeException("☹ OOPS!!! There is no item " + itemId + ".");
}
}

public boolean isExit() {
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/DoneCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ public Command clone(String fullCommand) {
return new DoneCommand(fullCommand);
}

public void execute(TaskList tasks, Ui ui, Storage storage) {
public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException {
int itemId = Parser.parseDone(this.fullCommand);
tasks.markAsDone(itemId);
try {
tasks.markAsDone(itemId);
} catch(IndexOutOfBoundsException e) {
throw new DukeException("☹ OOPS!!! There is no item " + itemId + ".");
}
ui.printResponse("Nice! I've marked this task as done: \n "
+ tasks.get(itemId).toString());
}
Expand Down
1 change: 0 additions & 1 deletion src/main/java/Event.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package duke.task;

import java.util.Date;
import java.util.Scanner;

public class Event extends Task {
public Event(String description, Date date) {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public Main(String filePath) {
public void run() {
this.ui.showWelcome();

Parser parser = new Parser();
boolean isExit = false;
while(!isExit) {
try {
Expand Down
153 changes: 57 additions & 96 deletions src/main/java/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,139 +3,100 @@
import duke.command.*;
import duke.DukeException;
import duke.task.*;
import java.util.Scanner;
import java.util.HashMap;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Parser {
protected static HashMap<String, Command> keywordToCommand;
protected static HashMap<String, Command> keywordToCommand = createMap();
protected static SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy HHmm");

public Parser() {
keywordToCommand = new HashMap<>();
static HashMap<String, Command> createMap() {
HashMap<String, Command> keywordToCommand = new HashMap<>();
keywordToCommand.put("bye", new ExitCommand());
keywordToCommand.put("list", new ListCommand());
keywordToCommand.put("done", new DoneCommand());
keywordToCommand.put("delete", new DeleteCommand());
keywordToCommand.put("todo", new AddCommand());
keywordToCommand.put("deadline", new AddCommand());
keywordToCommand.put("event", new AddCommand());
return keywordToCommand;
}

public static Command parse(String input) throws DukeException {
if(keywordToCommand.get(input) != null) {
if(keywordToCommand.containsKey(input)) {
return keywordToCommand.get(input);
} else {
Scanner scanner = new Scanner(input);
String keyword = scanner.next();
scanner.close();
String keyword = input.split(" ")[0];
Command command;
try {
if(keywordToCommand.containsKey(keyword)) {
command = keywordToCommand.get(keyword).clone(input);
} catch(NullPointerException e) {
} else {
throw new DukeException("☹ OOPS!!! I'm sorry, but I don't know what that means :-(");
}
return command;
}
}

public static int parseDone(String input) throws AssertionError {
Scanner scanner = new Scanner(input);

String command = scanner.next();
assert(command.equals("done"));

int itemId = scanner.nextInt();
scanner.close();
public static int parseDone(String input) throws DukeException {
String[] words = input.split(" ");
int itemId;
try {
assert(words.length == 2);
String command = words[0];
assert(command.equals("done"));
itemId = Integer.parseInt(words[1]);
} catch(AssertionError e) {
throw new DukeException("☹ OOPS!!! Incorrect format for done command.");
}
return itemId;
}

public static int parseDelete(String input) throws AssertionError {
Scanner scanner = new Scanner(input);

String command = scanner.next();
assert(command.equals("delete"));

int itemId = scanner.nextInt();
scanner.close();
public static int parseDelete(String input) throws DukeException {
String[] words = input.split(" ");
int itemId;
try {
assert(words.length == 2);
String command = words[0];
assert(command.equals("delete"));
itemId = Integer.parseInt(words[1]);
} catch(AssertionError e) {
throw new DukeException("☹ OOPS!!! Incorrect format for delete command.");
}
return itemId;
}

public static Task parseTask(String input) throws DukeException {
Scanner scanner = new Scanner(input);
String type = scanner.next();
scanner.close();

String[] words = input.split(" ");
String type = words[0];

String info = input.substring((type + " ").length());
switch(type) {
case "todo":
return createTodo(input.substring("todo".length()));
if(info.equals("")) {
throw new DukeException("☹ OOPS!!! The description of a todo cannot be empty.");
}
return new Todo(info);
case "deadline":
return createDeadline(input.substring("deadline".length()));
String[] args = info.split("/by");
String description = args[0].trim();
Date date;
try {
date = dateFormatter.parse(args[1].trim());
} catch(Exception e) {
throw new DukeException("☹ OOPS!!! Incorrect date format.");
}
return new Deadline(description, date);
case "event":
return createEvent(input.substring("event".length()));
args = info.split("/at");
description = args[0].trim();
try {
date = dateFormatter.parse(args[1].trim());
} catch(Exception e) {
throw new DukeException("☹ OOPS!!! Incorrect date format.");
}
return new Event(description, date);
}
throw new DukeException("Invalid task input.");
}

public static Todo createTodo(String input) throws DukeException {
String description = input.length() == 0
? input
: input.substring(1);
if(description.equals("")) {
throw new DukeException("☹ OOPS!!! The description of a todo cannot be empty.");
}
return new Todo(description);
}

public static Deadline createDeadline(String input) {
StringBuffer stringBuffer = new StringBuffer();
Scanner scanner = new Scanner(input);

String temp = scanner.next();
while(!temp.equals("/by")) {
stringBuffer.append(" " + temp);
temp = scanner.next();
}

String description = stringBuffer.length() == 0
? stringBuffer.toString()
: stringBuffer.toString().substring(1);

String date = scanner.nextLine();

scanner.close();
return new Deadline(description, parseDate(date));
}

public static Event createEvent(String input) {
StringBuffer stringBuffer = new StringBuffer();
Scanner scanner = new Scanner(input);

String temp = scanner.next();
while(!temp.equals("/at")) {
stringBuffer.append(" " + temp);
temp = scanner.next();
}

String description = stringBuffer.length() == 0
? stringBuffer.toString()
: stringBuffer.toString().substring(1);

String date = scanner.nextLine();

scanner.close();
return new Event(description, parseDate(date));
}

public static Date parseDate(String date) {
String[] dayAndTime = date.trim().split(" ");
String[] ddMmYy = dayAndTime[0].split("/");
String hhSs = dayAndTime[1];
int day = Integer.parseInt(ddMmYy[0]);
int month = Integer.parseInt(ddMmYy[1]);
int year = Integer.parseInt(ddMmYy[2]);
int hour = Integer.parseInt(hhSs.substring(0, 2));
int min = Integer.parseInt(hhSs.substring(2));
System.out.println(String.format("%d %d %d %d %d", day, month, year, hour, min));
return new Date(year - 1900, month - 1, day, hour, min);
}
}
1 change: 0 additions & 1 deletion src/main/java/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.Serializable;
import java.util.Date;
import java.util.Scanner;

public class Task implements Serializable {
protected String type;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/Todo.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package duke.task;

import java.util.Date;
import java.util.Scanner;

public class Todo extends Task {
public Todo(String description) {
Expand Down
Binary file modified src/main/java/duke/Main.class
Binary file not shown.
Binary file modified src/main/java/duke/Parser.class
Binary file not shown.
Binary file modified src/main/java/duke/command/AddCommand.class
Binary file not shown.
Binary file modified src/main/java/duke/command/DeleteCommand.class
Binary file not shown.
Binary file modified src/main/java/duke/command/DoneCommand.class
Binary file not shown.
Binary file modified src/main/java/duke/task/Deadline.class
Binary file not shown.
Binary file modified src/main/java/duke/task/Event.class
Binary file not shown.
Binary file modified src/main/java/duke/task/Task.class
Binary file not shown.
Binary file modified src/main/java/duke/task/Todo.class
Binary file not shown.
15 changes: 15 additions & 0 deletions src/test/java/duke/ParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import duke.Parser;
import duke.DukeException;
import org.junit.jupiter.api.Test;

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

public class ParserTest {
@Test
void parserTest() {
assertThrows(DukeException.class, () -> Parser.parse("asdfgh"));
}


}
Loading

0 comments on commit 6bd3400

Please sign in to comment.