Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 57 additions & 6 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,61 @@
import duke.command.Command;
import duke.DukeException;
import duke.Storage;
import duke.command.CommandResult;
import duke.command.Exit;
import duke.parser.Parser;
import duke.task.TaskList;
import duke.Ui.UserInterface;


public class Duke {
UserInterface ui;
TaskList taskList;
Storage storage;

public Duke() {
this.ui = new UserInterface();
this.taskList = new TaskList();
this.storage = new Storage();
}

public void run() {
ui.logo();
ui.showWelcomeMessage();
this.userCommand();
ui.showGoodbyeMessage();
System.exit(0);
}

public void userCommand() {
boolean isExit = false;
while (!isExit) {
String fullCommand = ui.getInput();
Command c = (new Parser()).parseCommand(fullCommand);
CommandResult result = execute(c);
ui.printRespond(result);
isExit = Exit.isExitCommand(c);
}
}


public CommandResult execute (Command command) {
command.inputData(taskList);
storage.save(taskList);
return command.execute();
}


public String getResponse(String userInput) {
Command command = (new Parser()).parseCommand(userInput);
CommandResult result = execute(command);
return ui.getResponse(result);
}

public static void main(String[] args) {
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
System.out.println("Hello from\n" + logo);
Duke duke = new Duke();
duke.run();

}

}
11 changes: 11 additions & 0 deletions src/main/java/duke/DukeException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package duke;

/**
* Represents exception class
*/

public class DukeException extends Exception {
public DukeException(String e){
super(e);
}
}
109 changes: 109 additions & 0 deletions src/main/java/duke/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package duke;

import duke.task.*;

import java.nio.charset.Charset;
import java.nio.file.Path;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
* Stores the TaskList
*/

public class Storage {
Path filePath;
Path folderPath;

public Storage() {
String home = System.getProperty("user.home");
filePath = Paths.get(home, "dukeData", "duke.txt");
folderPath = Paths.get(home, "dukeData");

}
public void fileLocation() throws IOException {
if (Files.notExists(folderPath)) {
Files.createDirectory(folderPath);
}
if (Files.notExists(filePath)) {
Files.createFile(filePath);
}
}
/**
* Read the task list
* @return task list
*/

public TaskList read() {
TaskList taskList = null;
try {
fileLocation();
List<String> tasks = Files.readAllLines(filePath, Charset.defaultCharset());
taskList = this.TaskListDecoder(tasks);
} catch (IOException e) {
e.printStackTrace();
}
return taskList;
}

/**
* Stores a task list in a filePath
* @param taskList to store task list
*/

public void save(TaskList taskList) {
try {
List<String> encodedTasks = this.TaskListEncoder(taskList);
Files.write(filePath, encodedTasks);
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* Returns an encoded String task list to store.
* @return a String task list
*/

public List<String> TaskListEncoder (TaskList taskList) {
List<String> TaskEncoder = new ArrayList<>();
for (Task t : taskList.getList()) {
TaskEncoder.add(t.taskEncode());
}
return TaskEncoder;
}

/**
* Returns a decoded task list to use
* If the text is an empty String, an empty ArrayList will be returned as expected.
* @param TaskEncoder the encoded task list
* @return encoded task list
*/


public TaskList TaskListDecoder(List<String> TaskEncoder) {
List<Task> TaskDecoder = new ArrayList<>();
for (String s : TaskEncoder) {
switch(s.charAt(0)) {
case ('T'):
TaskDecoder.add(Todo.decode(s));
break;
case ('D'):
TaskDecoder.add(Deadlines.decode(s));
break;
case ('E'):
TaskDecoder.add(Event.decode(s));
break;
default:
break;
}
}
return new TaskList(TaskDecoder);
}

}


50 changes: 50 additions & 0 deletions src/main/java/duke/Ui/UserInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package duke.Ui;

import duke.command.CommandResult;

import java.util.Scanner;

public class UserInterface {

Scanner s;
public UserInterface() {
this.s = new Scanner(System.in);
}

public String getInput() {
return s.nextLine();
}


public String getResponse(CommandResult result) {
return result.getRespondToUser();
}

public void printRespond(CommandResult result) {
System.out.println(result.getRespondToUser());
}

public void goodBye() {
System.out.println("Bye");
}


public void showWelcomeMessage() {
System.out.println("Hello! I'm Duke\n"
+ "What can I do for you?");
}

public void logo() {
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
System.out.println("Hello from\n" + logo);
}

public void showGoodbyeMessage() {
System.out.println("Bye :)");
}

}
37 changes: 37 additions & 0 deletions src/main/java/duke/command/Command.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package duke.command;

import duke.task.Task;
import duke.task.TaskList;
/**
* Represents a command
*/
public abstract class Command {
public TaskList taskList;
/**
* Executes the command and returns the result.
*/

public abstract CommandResult execute();

/**
* Input data so command can work
* @param taskList the single taskList used in Duke
*/

public void inputData(TaskList taskList) {
this.taskList = taskList;
}

/**
* Return the successful message
* @param task
* @return answers to the user
*/

public String addTaskSuccess(Task task) {
assert(taskList != null);
return "Got it. I have added the task!\n" + task
+ "Now you have " + taskList.getSize() + " tasks in the list.";

}
}
22 changes: 22 additions & 0 deletions src/main/java/duke/command/CommandResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package duke.command;

import duke.task.TaskList;
/**
* Represents a command result
*/
public class CommandResult {
String respondToUser;
TaskList taskList;

public CommandResult(String respondToUser){
this.respondToUser = respondToUser;
}

public String getRespondToUser() {
return respondToUser;
}

public TaskList getTaskList() {
return taskList;
}
}
41 changes: 41 additions & 0 deletions src/main/java/duke/command/DeadlinesCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package duke.command;

import duke.task.Deadlines;
import duke.DukeException;
import duke.task.Task;

import java.time.LocalDate;
import java.time.format.DateTimeParseException;

/**
* Adds a deadline to the taskList
*/

public class DeadlinesCommand extends Command {
public static final String COMMAND_WORD = "deadline";
String taskName;
LocalDate on;

/**
* creates a DeadlinesCommand
* @param Text input by the user
* @throws DukeException if the command is empty
*/

public DeadlinesCommand(String Text) throws DukeException, DateTimeParseException {
String[] deadParts = Text.split(" /by ");
if (deadParts[0].equals("") || deadParts[1].equals("")) {
throw new DukeException("The description of an deadline cannot be empty");
} else {
this.taskName = deadParts[0];
this.on = LocalDate.parse(deadParts[1]);
}
}

@Override
public CommandResult execute() {
Task newDeadlines = new Deadlines(taskName, false, on);
taskList.addTasks(newDeadlines);
return new CommandResult(addTaskSuccess(newDeadlines));
}
}
26 changes: 26 additions & 0 deletions src/main/java/duke/command/Delete.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package duke.command;
import duke.task.Task;

/**
* Delete tasks from the taskList
*/

public class Delete extends Command {
public static final String COMMAND_WORD = "delete";
int taskNumToDelete;


public Delete(String[] s) {
this.taskNumToDelete = Integer.parseInt(s[1]);
}
@Override
public CommandResult execute() {
Task removeTask = taskList.deleteTasks(taskNumToDelete);
return new CommandResult(Message(removeTask));
}
public String Message(Task removeTask) {
return "Noted! I've removed this task:\n"
+ removeTask + "\n"
+ "Now you have " + taskList.getSize() + " tasks in the list.";
}
}
Loading