forked from nus-cs2113-AY2425S1/ip
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5f04586
commit 7d3ce0d
Showing
21 changed files
with
636 additions
and
591 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package Command; | ||
|
||
import Entity.Deadline; | ||
import Entity.Event; | ||
import Entity.TaskList; | ||
import Entity.Todo; | ||
|
||
import java.io.IOException; | ||
|
||
import static Storage.Save.writeToFile; | ||
import static Utils.Converter.convertToDate; | ||
|
||
public class Add { | ||
/** | ||
* Add new item to message list | ||
* | ||
* <p>This method is to extract information | ||
* from the user input and add them to the message list</p> | ||
* @param input The input to be added. | ||
* @param list The message list to add to. | ||
*/ | ||
|
||
public static void addList(TaskList list, String input) throws ArrayIndexOutOfBoundsException { | ||
try { | ||
if (input.contains("todo")) { | ||
String eventName = input.substring(8).trim(); | ||
Todo todo = new Todo(eventName); | ||
list.addTask(todo); | ||
System.out.println("-----------------------------------"); | ||
System.out.println("added:" + eventName); | ||
} else if (input.contains("deadline")) { | ||
String[] strings = input.split("/"); | ||
String endTime = strings[strings.length - 1].split(" ")[1]; | ||
String eventName = strings[strings.length - 2].split(" ")[2]; | ||
Deadline deadline = new Deadline(eventName, convertToDate(endTime)); | ||
list.addTask(deadline); | ||
System.out.println("-----------------------------------"); | ||
System.out.println("added:" + eventName); | ||
} else if (input.contains("event")) { | ||
String[] strings = input.split("/"); | ||
String startTime = strings[strings.length - 2].split(" ")[1]; | ||
String endTime = strings[strings.length - 1].split(" ")[1]; | ||
String eventName = strings[strings.length - 3].split(" ")[2]; | ||
|
||
Event event = new Event(eventName, convertToDate(startTime), convertToDate(endTime)); | ||
list.addTask(event); | ||
System.out.println("-----------------------------------"); | ||
System.out.println("added:" + eventName); | ||
} | ||
writeToFile(list); | ||
int taskNumber = list.getTaskListSize(); | ||
System.out.println("Now you have " + taskNumber + " tasks"); | ||
System.out.println("-----------------------------------\n"); | ||
} catch (ArrayIndexOutOfBoundsException | IOException e) { | ||
e.printStackTrace(); | ||
System.out.println("Sorry, you have entered an invalid command"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package Command; | ||
|
||
import Entity.Task; | ||
import Entity.TaskList; | ||
import Storage.InitFile; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import static Storage.Save.writeToFile; | ||
|
||
public class Delete { | ||
/** | ||
* Delete an existing item to message list | ||
* | ||
* <p>This method is to extract information | ||
* from the user input and delete corresponding | ||
* item from the message list</p> | ||
* @param input The input to be deleted. | ||
* @param list The message list to delete from. | ||
* @throws IOException If an error occurs. | ||
*/ | ||
|
||
public static void delete(TaskList list, String input) throws IOException { | ||
String[] sentences = input.split(" "); | ||
List<Task> tasks = list.getTaskList(); | ||
int i = 0; | ||
for(i = 0; i < list.getTaskListSize(); i++) { | ||
if(tasks.get(i).getDescription().equals(sentences[1])) { | ||
list.removeTask(tasks.get(i)); | ||
break; | ||
} | ||
} | ||
if(tasks.size() == 0 || i > tasks.size() - 1) { | ||
System.out.println("Sorry, you are deleting an event that has not been added"); | ||
} | ||
else { | ||
System.out.println("------------------------------------\n"); | ||
System.out.println("You have successfully deleted this task"); | ||
System.out.println("------------------------------------\n"); | ||
} | ||
writeToFile(list); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package Command; | ||
|
||
import Entity.Task; | ||
import Entity.TaskList; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import static Utils.Print.printToTerminal; | ||
|
||
public class Find { | ||
|
||
/** | ||
* Find existing items in message list | ||
* | ||
* <p>This method is to extract information | ||
* from the user input and find items | ||
* containing the input from the message list</p> | ||
* @param input The input to be searched. | ||
* @param list The message list to search from. | ||
*/ | ||
|
||
public static void find(TaskList list, String input) { | ||
List<Task> tasks = list.getTaskList(); | ||
String target = input.substring(4).trim(); | ||
int numberFound = 0; | ||
List<Task> foundTasks = new ArrayList<>(); | ||
for(int i = 0; i < tasks.size(); i++) { | ||
Task task = tasks.get(i); | ||
String description = task.getDescription(); | ||
if(description.contains(target)) { | ||
numberFound++; | ||
foundTasks.add(task); | ||
} | ||
} | ||
if(numberFound == 0) { | ||
System.out.println("Sorry, no task is found"); | ||
} else { | ||
System.out.println("I found " + numberFound + " task(s) for you:"); | ||
for (Task task : foundTasks) { | ||
printToTerminal(task, foundTasks.indexOf(task) + 1); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package Command; | ||
|
||
import Entity.Task; | ||
import Entity.TaskList; | ||
|
||
import java.util.List; | ||
|
||
public class Mark { | ||
/** | ||
* Mark existing item in the list | ||
* | ||
* <p>This method is to mark existing items | ||
* in the list by analysing the input</p> | ||
* @param input The input to recognize. | ||
* @param list The message list to use. | ||
*/ | ||
|
||
public static void mark(TaskList list, String input) { | ||
List<Task> tasks = list.getTaskList(); | ||
String[] sentences = input.split(" "); | ||
int number = Integer.parseInt(sentences[1]); | ||
if(number > tasks.size()) { | ||
System.out.println("Sorry, you are marking a task that has not been added"); | ||
return; | ||
} | ||
|
||
if(input.contains("unmark")){ | ||
tasks.get(number-1).setDone(false); | ||
System.out.println("-----------------------------------\n"); | ||
System.out.println("I have marked this task as not done!\n" + "[X] " + tasks.get(number-1).getDescription()); | ||
System.out.println("-----------------------------------\n"); | ||
} | ||
else { | ||
tasks.get(number - 1 ).setDone(true); | ||
System.out.println("-----------------------------------\n"); | ||
System.out.println("I have marked this task as done!\n" + "[X] " + tasks.get(number-1).getDescription()); | ||
System.out.println("-----------------------------------\n"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package Command; | ||
|
||
import Entity.TaskList; | ||
import java.io.IOException; | ||
import java.util.Scanner; | ||
|
||
import static Command.Add.addList; | ||
import static Command.Delete.delete; | ||
import static Command.Find.find; | ||
import static Command.Mark.mark; | ||
import static Command.Show.listShow; | ||
import static java.lang.System.exit; | ||
|
||
|
||
public class PreHandle { | ||
public static void preHandle(TaskList list) throws IOException { | ||
Scanner scanner = new Scanner(System.in); | ||
String input = null; | ||
while(true) { | ||
input = scanner.nextLine(); | ||
if (input.equals("bye")) { | ||
System.out.println("-----------------------------------\n"); | ||
System.out.println("Bye! Hope to see you soon!\n"); | ||
System.out.println("-----------------------------------\n"); | ||
break; | ||
} | ||
else if (input.equals("list")) { | ||
listShow(list); | ||
} | ||
else if (input.contains("mark") || input.contains("unmark")) { | ||
mark(list, input); | ||
} | ||
else if (input.contains("delete")) { | ||
delete(list, input); | ||
} | ||
else if (input.contains("add")) { | ||
addList(list, input); | ||
} | ||
else if (input.contains("find")) { | ||
find(list, input); | ||
} | ||
else if (input.contains("exit")) { | ||
System.out.println("Bye! Hope to see you soon!\n"); | ||
exit(0); | ||
} | ||
else { | ||
System.out.println("Sorry, your command is not recognized!"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package Command; | ||
|
||
import Entity.Task; | ||
import Entity.TaskList; | ||
|
||
import java.util.List; | ||
|
||
import static Utils.Print.printToTerminal; | ||
|
||
public class Show { | ||
/** | ||
* Show list | ||
* | ||
* <p>This method is to show or to list down the message list</p> | ||
* @param list The message list to show. | ||
*/ | ||
|
||
public static void listShow(TaskList list) { | ||
System.out.println("-----------------------------------\n"); | ||
int i = 1; | ||
List<Task> messages = list.getTaskList(); | ||
while(i <= messages.size()) { | ||
Task task = messages.get(i - 1); | ||
printToTerminal(task, i); | ||
i++; | ||
} | ||
if(messages.size() == 0) { | ||
System.out.println("No task found"); | ||
} | ||
System.out.println("-----------------------------------\n"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package Entity; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
import static Utils.Converter.convertToString; | ||
|
||
public class Deadline extends Task { | ||
|
||
private LocalDateTime endTime; | ||
|
||
public Deadline(String description, LocalDateTime endTime) { | ||
super(description); | ||
this.endTime = endTime; | ||
} | ||
|
||
public Deadline(String description, LocalDateTime endTime, Boolean isDone) { | ||
super(description, isDone); | ||
this.endTime = endTime; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[D]" + super.toString() + " by: " + endTime; | ||
} | ||
|
||
@Override | ||
public String toFile() { | ||
return "D" + " | " + super.toFile() + " | " + convertToString(endTime); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package Entity; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
import static Utils.Converter.convertToString; | ||
|
||
public class Event extends Task { | ||
|
||
private LocalDateTime startTime; | ||
private LocalDateTime endTime; | ||
|
||
public Event(String description, LocalDateTime startTime, LocalDateTime endTime) { | ||
super(description); | ||
this.startTime = startTime; | ||
this.endTime = endTime; | ||
} | ||
|
||
public Event(String description, LocalDateTime startTime, LocalDateTime endTime, boolean isDone) { | ||
super(description, isDone); | ||
this.startTime = startTime; | ||
this.endTime = endTime; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[E]" + super.toString() | ||
+ " from: " + startTime + " by: " + endTime; | ||
} | ||
|
||
@Override | ||
public String toFile() { | ||
return "E" + " | " + super.toFile() + " | " + convertToString(startTime) + " | " + convertToString(endTime); | ||
} | ||
} |
Oops, something went wrong.