-
Notifications
You must be signed in to change notification settings - Fork 454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bernard Wan De Yuan] iP #461
base: master
Are you sure you want to change the base?
Changes from 17 commits
d839859
f38a22a
b98c16f
feac61e
83381b8
3c1011c
7551481
2565300
5ff4233
0266c78
33921a6
48d97f1
09721a2
e4d0342
c3e9b99
6db07e9
fe6833c
5f6bf33
9dbf7dd
06609b4
d093898
d2d6805
bcefd1c
679bfa8
341051c
5dcd832
62293f6
c786acd
9d41498
e710891
f787e9d
879b551
558ea60
fceed2d
fd12c10
a57ca3c
d45e077
6e830cf
4714d9b
0d11fb8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Manifest-Version: 1.0 | ||
Main-Class: duke.Duke | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package duke.Command; | ||
|
||
import duke.*; | ||
|
||
public class AddCommand extends Command{ | ||
private String type; | ||
private String description; | ||
|
||
public AddCommand(String type, String description) { | ||
this.type = type; | ||
this.description = description; | ||
} | ||
|
||
@Override | ||
public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException { | ||
Task task; | ||
if(type.equals("todo")) { | ||
task = new Task.Todo(description, false); | ||
|
||
} else if(type.equals("deadline")) { | ||
String[] temp = description.split("by ", 2); | ||
task = new Task.Deadline(temp[0], false, temp[1]); | ||
|
||
} else if(type.equals("event")) { | ||
String[] temp = description.split("at ", 2); | ||
task = new Task.Event(temp[0], false, temp[1]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe can consider using |
||
|
||
} else { | ||
throw new DukeException("Sorry, I don't understand what that means. :("); | ||
} | ||
|
||
tasks.addTask(task); | ||
ui.print("Added: " + task.getTaskType() + task.getStatusIcon() + " " + task.getDescription()); | ||
ui.print("There are " + tasks.size() + " tasks in the list"); | ||
storage.save(tasks); | ||
} | ||
|
||
@Override | ||
public boolean isExit() { | ||
return false; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package duke.Command; | ||
|
||
import duke.DukeException; | ||
import duke.Storage; | ||
import duke.TaskList; | ||
import duke.Ui; | ||
|
||
public abstract class Command { | ||
public abstract void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException; | ||
|
||
public abstract boolean isExit(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package duke.Command; | ||
|
||
import duke.*; | ||
|
||
public class DeleteCommand extends Command{ | ||
|
||
private int index; | ||
|
||
public DeleteCommand(int index) { | ||
this.index = index; | ||
} | ||
|
||
@Override | ||
public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException { | ||
Task task = tasks.deleteTask(index); | ||
ui.print("Deleted:\n" + task.getDescription()); | ||
ui.print("There are " + tasks.size() + " tasks remaining in the list"); | ||
storage.save(tasks); | ||
} | ||
|
||
@Override | ||
public boolean isExit() { | ||
return false; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package duke.Command; | ||
|
||
import duke.*; | ||
|
||
public class DoneCommand extends Command{ | ||
|
||
private int index; | ||
|
||
public DoneCommand(int index) { | ||
this.index = index; | ||
} | ||
|
||
@Override | ||
public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException { | ||
Task task = tasks.doneTask(this.index); | ||
ui.print("Marked as done:\n" + task.getDescription()); | ||
storage.save(tasks); | ||
} | ||
|
||
@Override | ||
public boolean isExit() { | ||
return false; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package duke.Command; | ||
|
||
import duke.Storage; | ||
import duke.TaskList; | ||
import duke.Ui; | ||
|
||
public class ExitCommand extends Command { | ||
|
||
@Override | ||
public void execute(TaskList tasks, Ui ui, Storage storage) { | ||
System.out.println("Bye. Hope to see you again soon!"); | ||
} | ||
|
||
@Override | ||
public boolean isExit() { | ||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package duke.Command; | ||
|
||
import duke.*; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you should import package by package, for example |
||
|
||
import java.util.ArrayList; | ||
|
||
public class FindCommand extends Command{ | ||
String keyword; | ||
|
||
public FindCommand(String keyword) { | ||
this.keyword = keyword; | ||
} | ||
|
||
@Override | ||
public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException { | ||
ArrayList<Task> list = tasks.getList(); | ||
list.removeIf(task -> !task.getDescription().contains(this.keyword)); | ||
if (list.size() == 0) { | ||
ui.print("No matching tasks found."); | ||
} else { | ||
ui.print(list.size() + " matching task(s):"); | ||
ui.print(new TaskList(list).allTasks()); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isExit() { | ||
return false; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package duke.Command; | ||
|
||
import duke.Command.Command; | ||
import duke.Storage; | ||
import duke.TaskList; | ||
import duke.Ui; | ||
|
||
public class ListCommand extends Command { | ||
|
||
@Override | ||
public void execute(TaskList tasks, Ui ui, Storage storage) { | ||
ui.print("All tasks:"); | ||
ui.print(tasks.allTasks()); | ||
} | ||
|
||
@Override | ||
public boolean isExit() { | ||
return false; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package duke; | ||
|
||
import duke.Command.Command; | ||
|
||
public class Duke { | ||
|
||
private Storage storage; | ||
private TaskList tasks; | ||
private Ui ui; | ||
|
||
public Duke() { | ||
ui = new Ui(); | ||
storage = new Storage("data/duke.txt"); | ||
try { | ||
tasks = new TaskList(storage.load()); | ||
} catch (DukeException e) { | ||
tasks = new TaskList(); | ||
} | ||
} | ||
|
||
public void run() { | ||
ui.greet(); | ||
boolean isExit = false; | ||
while (!isExit) { | ||
try { | ||
String input = ui.readCommand(); | ||
Command c = Parser.parse(input); | ||
c.execute(tasks, ui, storage); | ||
isExit = c.isExit(); | ||
} catch (DukeException e) { | ||
ui.showError(e.getMessage()); | ||
} | ||
} | ||
|
||
|
||
} | ||
|
||
|
||
public static void main(String[] args) { | ||
new Duke().run(); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package duke; | ||
|
||
public class DukeException extends Exception{ | ||
public DukeException(String errorMessage) { | ||
super(errorMessage); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package duke; | ||
|
||
import duke.Command.*; | ||
|
||
public class Parser { | ||
|
||
public static Command parse(String input) throws DukeException{ | ||
String first_word = input.split(" ")[0]; | ||
|
||
|
||
if (input.equals("bye")) { | ||
return new ExitCommand(); | ||
} else if (input.equals("list")) { | ||
return new ListCommand(); | ||
} else if (first_word.equals("done")) { | ||
int index; | ||
try { | ||
index = Integer.parseInt(input.split(" ")[1]) - 1; | ||
} catch (ArrayIndexOutOfBoundsException e) { | ||
throw new DukeException("Sorry, please enter an integer after 'done'. (e.g. done 2)"); | ||
} | ||
return new DoneCommand(index); | ||
} else if (first_word.equals("delete")) { | ||
int index; | ||
try { | ||
index = Integer.parseInt(input.split(" ")[1]) - 1; | ||
} catch (ArrayIndexOutOfBoundsException e) { | ||
throw new DukeException("Sorry, please enter an integer after 'delete'. (e.g. delete 2)"); | ||
} | ||
return new DeleteCommand(index); | ||
} else if (first_word.equals("find")){ | ||
String remaining; | ||
try { | ||
remaining = input.split(" ", 2)[1]; | ||
} catch (ArrayIndexOutOfBoundsException e) { | ||
throw new DukeException("Sorry, please enter a keyword after 'find'."); | ||
} | ||
return new FindCommand(remaining); | ||
} else { | ||
String remaining; | ||
try { | ||
remaining = input.split(" ", 2)[1]; | ||
} catch (ArrayIndexOutOfBoundsException e) { | ||
throw new DukeException("Sorry, tasks must include descriptions."); | ||
} | ||
return new AddCommand(first_word, remaining); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps we can use |
||
|
||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package duke; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
import java.util.Scanner; | ||
|
||
public class Storage { | ||
private String filepath; | ||
|
||
public Storage(String filepath) { | ||
this.filepath = filepath; | ||
} | ||
|
||
public ArrayList<Task> load() throws DukeException { | ||
ArrayList<Task> list = new ArrayList<>(); | ||
File list_data = new File(filepath); | ||
Scanner reader; | ||
try { | ||
reader = new Scanner(list_data); | ||
} catch (FileNotFoundException e) { | ||
throw new DukeException("File not found"); | ||
} | ||
|
||
while (reader.hasNextLine()) { | ||
String line = reader.nextLine(); | ||
String[] data = line.split(" \\| "); | ||
switch (data[0]) { | ||
case "todo": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should |
||
list.add(new Task.Todo(data[2], data[1].equals("0") ? false : true)); | ||
break; | ||
case "deadline": | ||
list.add(new Task.Deadline(data[2], data[1].equals("0") ? false : true, data[3])); | ||
break; | ||
case "event": | ||
list.add(new Task.Event(data[2], data[1].equals("0") ? false : true, data[3])); | ||
break; | ||
} | ||
} | ||
reader.close(); | ||
return list; | ||
} | ||
|
||
public void save(TaskList list) { | ||
try { | ||
File list_data = new File(filepath); | ||
Files.createDirectories(Paths.get("data/")); | ||
FileWriter myWriter = new FileWriter(list_data); | ||
myWriter.write(list.toString()); | ||
myWriter.close(); | ||
} catch (IOException e) { | ||
System.out.println("File not found"); | ||
} | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think perhaps can consider importing individual classes instead of using wildcard
import
!