diff --git a/src/main/java/seedu/duke/Duke.java b/src/main/java/seedu/duke/Duke.java index b9f678300e..99edff90a7 100644 --- a/src/main/java/seedu/duke/Duke.java +++ b/src/main/java/seedu/duke/Duke.java @@ -1,6 +1,8 @@ package seedu.duke; +import seedu.duke.command.AddCommand; import seedu.duke.command.Command; +import seedu.duke.command.LogInCommand; import seedu.duke.exception.DukeException; import seedu.duke.parser.Parser; import seedu.duke.task.TaskList; @@ -32,14 +34,18 @@ public Duke() { public void run() { ui.showWelcome(); boolean isExit = false; - User currentUser = null; + User nowUser = null; while (!isExit) { try { String fullCommand = ui.readCommand(); ui.showLine(); // show the divider line ("_______") Command c = Parser.parse(fullCommand); - c.execute(users, ui/*, storage*/); - currentUser = c.getCurrentUser(); + c.execute(users, ui, nowUser/*, storage*/); + + if (c.isLogIn() == true) { + nowUser = c.getCurrentUser(); + } + //System.out.println(nowUser.getName()); isExit = c.isExit(); } catch (DukeException e) { ui.showError(e.getMessage()); diff --git a/src/main/java/seedu/duke/command/AddCommand.java b/src/main/java/seedu/duke/command/AddCommand.java new file mode 100644 index 0000000000..6216cee9ac --- /dev/null +++ b/src/main/java/seedu/duke/command/AddCommand.java @@ -0,0 +1,69 @@ +package seedu.duke.command; + +import seedu.duke.exception.DukeException; +//import seedu.duke.storage.Storage; +import seedu.duke.task.Event; +import seedu.duke.task.TaskList; +import seedu.duke.timetable.Timetable; +import seedu.duke.ui.Ui; +import seedu.duke.user.User; +import seedu.duke.user.UserList; + +/** + * Adds an event to the task list. + */ +public class AddCommand extends Command { + + public AddCommand(String input) { + super(input); + } + + @Override + public void execute(UserList users, Ui ui, User nowUser/*, Storage storage*/) throws DukeException { + //Lec /day /time /location + if (nowUser != null) { + String[] parsedInputs = input.split(" /", 4); + String[] timeInputs = parsedInputs[2].split("-", 2); + + String date = parsedInputs[1]; + + for (int i = 0; i < users.getTotalUserCount(); i++) { + if ((users.getUser(i + 1).getName() == nowUser.getName())) { + Event newEvent = new Event(parsedInputs[0], parsedInputs[3], timeInputs[0], timeInputs[1]); + ui.printEvent(newEvent, date); + switch (date) { + case "mon": + (users.getUser(i + 1).getTimetable()).getMonTimetable().add(newEvent); + break; + case "tue": + (users.getUser(i + 1).getTimetable()).getTueTimetable().add(newEvent); + break; + case "wed": + (users.getUser(i + 1).getTimetable()).getWedTimetable().add(newEvent); + break; + case "thu": + (users.getUser(i + 1).getTimetable()).getThuTimetable().add(newEvent);; + break; + case "fri": + (users.getUser(i + 1).getTimetable()).getFriTimetable().add(newEvent); + break; + case "sat": + (users.getUser(i + 1).getTimetable()).getSatTimetable().add(newEvent); + break; + case "sun": + (users.getUser(i + 1).getTimetable()).getSunTimetable().add(newEvent); + break; + default: + throw new DukeException("Sorry! I don't know what day you mean :-("); + } + } + } + //((Timetable) currentUser.getTimetable()) + //tasks.addTask(newTask); + //ui.printEvent(newEvent, date); + //storage.write(tasks); + } else { + throw new DukeException("Sorry! You are not Logged in to any account :-("); + } + } +} \ No newline at end of file diff --git a/src/main/java/seedu/duke/command/ByeCommand.java b/src/main/java/seedu/duke/command/ByeCommand.java index 56310c1c69..4a183a85ad 100644 --- a/src/main/java/seedu/duke/command/ByeCommand.java +++ b/src/main/java/seedu/duke/command/ByeCommand.java @@ -3,6 +3,7 @@ //import seedu.duke.storage.Storage; import seedu.duke.task.TaskList; import seedu.duke.ui.Ui; +import seedu.duke.user.User; import seedu.duke.user.UserList; /** @@ -15,7 +16,7 @@ public ByeCommand() { } //@Override - public void execute(UserList users, Ui ui/*, Storage storage*/) { + public void execute(UserList users, Ui ui, User nowUser/*, Storage storage*/) { isExit = true; ui.showBye(); } diff --git a/src/main/java/seedu/duke/command/Command.java b/src/main/java/seedu/duke/command/Command.java index ff4d131af9..a9257b9b6a 100644 --- a/src/main/java/seedu/duke/command/Command.java +++ b/src/main/java/seedu/duke/command/Command.java @@ -10,7 +10,8 @@ public abstract class Command { protected String input; protected boolean isExit = false; - protected User currentUser; + protected User currentUser = null; + protected boolean isLogIn = false; /** * Creates a new command. @@ -28,7 +29,7 @@ public Command(String input) { * @param ui the corresponding messages based on the task. * @throws DukeException if execution encounters error. */ - public abstract void execute(UserList users, Ui ui/*, Storage storage*/) throws DukeException; + public abstract void execute(UserList users, Ui ui, User nowUser/*, Storage storage*/) throws DukeException; public boolean isExit() { return isExit; @@ -37,4 +38,8 @@ public boolean isExit() { public User getCurrentUser() { return currentUser; } + + public boolean isLogIn() { + return isLogIn; + } } diff --git a/src/main/java/seedu/duke/command/LogInCommand.java b/src/main/java/seedu/duke/command/LogInCommand.java index ecae89560e..463cecfa6c 100644 --- a/src/main/java/seedu/duke/command/LogInCommand.java +++ b/src/main/java/seedu/duke/command/LogInCommand.java @@ -18,12 +18,15 @@ public LogInCommand(String input) { } @Override - public void execute(UserList users, Ui ui/*, Storage storage*/) throws DukeException { + public void execute(UserList users, Ui ui, User nowUser/*, Storage storage*/) throws DukeException { String[] parsedInputs = input.split(" /", 2); User newUser = new User(parsedInputs[0], parsedInputs[1]); currentUser = newUser; users.addUser(newUser); ui.greetUser(newUser); + isLogIn = true; + + //storage.write(tasks); } } diff --git a/src/main/java/seedu/duke/parser/Parser.java b/src/main/java/seedu/duke/parser/Parser.java index 8c03bd9461..3e0a86c7fb 100644 --- a/src/main/java/seedu/duke/parser/Parser.java +++ b/src/main/java/seedu/duke/parser/Parser.java @@ -1,5 +1,6 @@ package seedu.duke.parser; +import seedu.duke.command.AddCommand; import seedu.duke.command.ByeCommand; import seedu.duke.command.Command; //import seedu.duke.command.DeleteCommand; @@ -22,6 +23,7 @@ public class Parser { //private static final String COMMAND_FIND = "find"; private static final String COMMAND_BYE = "bye"; private static final String COMMAND_LOGIN = "login"; + private static final String COMMAND_ADD = "add"; /** @@ -54,6 +56,9 @@ public static Command parse(String input) throws DukeException { case COMMAND_LOGIN: checkLogInValidity(parsedInputs); return new LogInCommand(parsedInputs[1]); + case COMMAND_ADD: + checkAddValidity(parsedInputs); + return new AddCommand(parsedInputs[1]); case COMMAND_BYE: return new ByeCommand(); default: @@ -61,6 +66,24 @@ public static Command parse(String input) throws DukeException { } } + private static void checkAddValidity(String[] input) throws DukeException { + if (input.length < 2) { + throw new DukeException("There is no description in your add command!"); + } else if (!input[1].contains("/")) { + throw new DukeException("An add command needs to be in a 'name /day /time /location' format!"); + } + String[] position = input[1].split(" /",4); + if (position[0].isEmpty()) { + throw new DukeException("There is no name in your add command!"); + } else if (position[1].isEmpty()) { + throw new DukeException("There is no day in your add command!"); + } else if (position[2].isEmpty()) { + throw new DukeException("There is no time in your add command!"); + } else { + throw new DukeException("There is no location in your add command!"); + } + + } /** @@ -104,9 +127,9 @@ private static void checkDeadlineValidity(String[] input) throws DukeException { throw new DukeException("A deadline task requires a '/by' to indicate time frame!"); } int byPosition = input[1].indexOf("/by"); - if (input[1].substring(0, byPosition).isBlank()) { + if (input[1].substring(0, byPosition).isEmpty()) { throw new DukeException("There is no description in your deadline command!"); - } else if (input[1].substring(byPosition + 3).isBlank()) { + } else if (input[1].substring(byPosition + 3).isEmpty()) { throw new DukeException("Please indicate time frame!"); } } @@ -124,9 +147,9 @@ private static void checkEventValidity(String[] input) throws DukeException { throw new DukeException("An event task requires an '/at' to indicate location!"); } int atPosition = input[1].indexOf("/at"); - if (input[1].substring(0, atPosition).isBlank()) { + if (input[1].substring(0, atPosition).isEmpty()) { throw new DukeException("There is no description in your event command!"); - } else if (input[1].substring(atPosition + 3).isBlank()) { + } else if (input[1].substring(atPosition + 3).isEmpty()) { throw new DukeException("An event task requires an '/at' to indicate location!"); } } @@ -138,9 +161,9 @@ private static void checkLogInValidity(String[] input) throws DukeException { throw new DukeException("An login requires an '/' to indicate password!"); } int atPosition = input[1].indexOf("/"); - if (input[1].substring(0, atPosition).isBlank()) { + if (input[1].substring(0, atPosition).isEmpty()) { throw new DukeException("There is no username in your login command!"); - } else if (input[1].substring(atPosition + 1).isBlank()) { + } else if (input[1].substring(atPosition + 1).isEmpty()) { throw new DukeException("An login requires a password!"); } } diff --git a/src/main/java/seedu/duke/task/Event.java b/src/main/java/seedu/duke/task/Event.java index a1890be746..3c35147849 100644 --- a/src/main/java/seedu/duke/task/Event.java +++ b/src/main/java/seedu/duke/task/Event.java @@ -11,16 +11,12 @@ public Event(String description, String location, String timeStart, String timeE //setTaskType("E"); } - /** - * Returns the event detail of the task. - * @return the event detail of the task. - */ /*public String getAt() { //return at; }*/ @Override public String toString() { - return super.toString(); + return description + " " + location + " " + " " + timeStart + "-" + timeEnd; } } diff --git a/src/main/java/seedu/duke/timetable/Timetable.java b/src/main/java/seedu/duke/timetable/Timetable.java index 8f93c3e401..ccf165bb10 100644 --- a/src/main/java/seedu/duke/timetable/Timetable.java +++ b/src/main/java/seedu/duke/timetable/Timetable.java @@ -1,6 +1,9 @@ package seedu.duke.timetable; +import seedu.duke.task.Event; + import java.util.ArrayList; +import java.util.List; public class Timetable { protected ArrayList monTimetable; @@ -20,4 +23,34 @@ public Timetable() { this.satTimetable = new ArrayList<>(); this.sunTimetable = new ArrayList<>(); } + + public ArrayList getMonTimetable() { + return monTimetable; + } + + public ArrayList getTueTimetable() { + return tueTimetable; + } + + public ArrayList getWedTimetable() { + return wedTimetable; + } + + public ArrayList getThuTimetable() { + return thuTimetable; + } + + public ArrayList getFriTimetable() { + return friTimetable; + } + + public ArrayList getSatTimetable() { + return satTimetable; + } + + public ArrayList getSunTimetable() { + return sunTimetable; + } + + } \ No newline at end of file diff --git a/src/main/java/seedu/duke/ui/Ui.java b/src/main/java/seedu/duke/ui/Ui.java index bd93310245..e180909393 100644 --- a/src/main/java/seedu/duke/ui/Ui.java +++ b/src/main/java/seedu/duke/ui/Ui.java @@ -1,7 +1,7 @@ package seedu.duke.ui; //import seedu.duke.task.Deadline; -//import seedu.duke.task.Event; +import seedu.duke.task.Event; import seedu.duke.task.Task; import seedu.duke.task.TaskList; import seedu.duke.user.User; @@ -81,14 +81,15 @@ public void printList(TaskList taskList) { /** * Prints out the event task given by the user. * - * @param task the task to be added to the array list. + * @param event the task to be added to the array list. */ - /*public void printEvent(TaskList taskList, Event task) { - System.out.println("Got it! I've added the following event in the list:\n" + task); - System.out.println("Now now have " + taskList.getTotalTaskCount() + " tasks in the list."); - }*/ + public void printEvent(Event event, String date) { + System.out.println("Got it! I've added the following event in " + date + "\n" + event); + //System.out.println("Now now have " + taskList.getTotalTaskCount() + " tasks in the list."); + } public void printDone(Task task) { + System.out.println("Nice! I have marked this task as done:\n" + task); } diff --git a/src/main/java/seedu/duke/user/UserList.java b/src/main/java/seedu/duke/user/UserList.java index b54cea9625..b57d58ff44 100644 --- a/src/main/java/seedu/duke/user/UserList.java +++ b/src/main/java/seedu/duke/user/UserList.java @@ -30,7 +30,7 @@ public User getUser(int index) throws DukeException { try { return users.get(index - 1); } catch (IndexOutOfBoundsException e) { - throw new DukeException("Invalid task number! Type 'list' to get an overview of your tasks."); + throw new DukeException("Invalid user number!"); } } diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index 3f022e4111..cfc58f9be8 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -1,2 +1,2 @@ login John Snow /123123 -bye +bye \ No newline at end of file