forked from nus-cs2103-AY2223S1/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.
Most of the code is placed in the main method Abstract most of the code in the main method out to OOP classes.
- Loading branch information
1 parent
4f468dc
commit c0a8d52
Showing
6 changed files
with
221 additions
and
161 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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,108 @@ | ||
import java.util.List; | ||
import java.util.ArrayList; | ||
import java.io.FileWriter; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.Scanner; | ||
|
||
public class Parser { | ||
private String userInput; | ||
private Scanner inputScanner = new Scanner(System.in); | ||
private static String introduction = "Hello. I am Jarvis \n" | ||
+ "What can I do for you today?"; | ||
private static String farewell = "Goodbye, have a good day."; | ||
private TaskList tasks; | ||
public Parser(TaskList tasks) { | ||
this.tasks = tasks; | ||
} | ||
|
||
public void readCommand() throws JarvisException { | ||
while (true) { | ||
//receive user input | ||
userInput = inputScanner.nextLine(); | ||
//check if userinput is bye, break if true | ||
if (userInput.equals("bye")) { | ||
break; | ||
} | ||
//if userinput equals list, return task list | ||
if (userInput.equals("list")) { | ||
System.out.println("Here are the tasks in your list:\n"); | ||
for (int i = 0; i < tasks.getList().size(); i++) { | ||
if (tasks.getList().get(i) == null) { | ||
break; | ||
} | ||
System.out.println((i + 1) + ". " + tasks.getList().get(i).toString()); | ||
} | ||
continue; | ||
} | ||
//if userinput equals mark, check which task and mark it | ||
if (userInput.length() > 4 && userInput.substring(0, 4).equals("mark")) { | ||
int toMark = Integer.parseInt(userInput.substring(5)) - 1; | ||
tasks.getList().get(toMark).mark(); | ||
String markResponse = "Great. I have marked this task as done:\n "; | ||
System.out.println(markResponse + tasks.getList().get(toMark).toString()); | ||
continue; | ||
} | ||
//if userinput equals unmark, check which task and unmark | ||
if (userInput.length() > 6 && userInput.substring(0, 6).equals("unmark")) { | ||
int toMark = Integer.parseInt(userInput.substring(7)) - 1; | ||
tasks.getList().get(toMark).unmark(); | ||
String markResponse = "Ok, I have marked this task as not done yet:\n "; | ||
System.out.println(markResponse + tasks.getList().get(toMark).toString()); | ||
continue; | ||
} | ||
//if userinput equals delete, check which task and delete | ||
if (userInput.length() > 6 && userInput.substring(0, 6).equals("delete")) { | ||
int toDelete = Integer.parseInt(userInput.substring(7)) - 1; | ||
Task deleteTask = tasks.getList().get(toDelete); | ||
tasks.getList().remove(toDelete); | ||
String deleteResponse = "Noted. I have removed this task:\n "; | ||
System.out.println(deleteResponse + deleteTask.toString()); | ||
continue; | ||
} | ||
|
||
//if userinput equals to do add new to do task to list | ||
if (userInput.length() > 3 && userInput.substring(0, 4).equals("todo")) { | ||
String description = userInput.substring(4); | ||
if (description.equals("")) { | ||
throw new JarvisException("The description of a todo cannot be empty"); | ||
} | ||
tasks.getList().add(new ToDo(description)); | ||
System.out.println("Got it. I've added this task:\n " + tasks.getList().get(Task.count - 1) | ||
+ "\nNow you have " + (Task.count) + " tasks in the list."); | ||
continue; | ||
} | ||
|
||
//if userinput equals deadline add new deadline task to list | ||
if (userInput.length() > 8 && userInput.substring(0, 8).equals("deadline")) { | ||
int divisor = userInput.indexOf("/by"); | ||
String description = userInput.substring(9, divisor - 1); | ||
String date = userInput.substring(divisor + 4); | ||
tasks.getList().add(new Deadline(description, date)); | ||
System.out.println("Got it. I've added this task:\n " + tasks.getList().get(Task.count - 1) | ||
+ "\nNow you have " + (Task.count) + " tasks in the list."); | ||
continue; | ||
} | ||
//if userinput equals event add new event task to list | ||
if (userInput.length() > 7 && userInput.substring(0, 5).equals("event")) { | ||
int divisor = userInput.indexOf("/at"); | ||
String description = userInput.substring(6, divisor - 1); | ||
String date = userInput.substring(divisor + 4); | ||
tasks.getList().add(new Event(description, date)); | ||
System.out.println("Got it. I've added this task:\n " + tasks.getList().get(Task.count - 1) | ||
+ "\nNow you have " + (Task.count) + " tasks in the list."); | ||
continue; | ||
} | ||
throw new JarvisException("I'm sorry, but I don't know what that means"); | ||
} | ||
} | ||
|
||
public void introduction() { | ||
System.out.println(introduction); | ||
} | ||
|
||
public void farewell() { | ||
System.out.print(farewell); | ||
} | ||
} | ||
|
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,81 @@ | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.io.File; | ||
import java.util.Scanner; | ||
public class Storage { | ||
private String filePath; | ||
public Storage(String filePath) { | ||
this.filePath = filePath; | ||
} | ||
public List<Task> load() throws IOException { | ||
File myFile = new File(filePath); | ||
List<Task> taskList = new ArrayList<>(); | ||
if (!myFile.createNewFile()) { | ||
Scanner sc = new Scanner(myFile); | ||
while (sc.hasNextLine()) { | ||
String next = sc.nextLine(); | ||
int divisor = next.lastIndexOf("|"); | ||
|
||
if (next.charAt(0) == 'D') { | ||
String description = next.substring(8, divisor - 1); | ||
String date = next.substring((divisor + 2)); | ||
taskList.add(new Deadline(description, date)); | ||
|
||
if (next.charAt(4) == '1') { | ||
taskList.get(taskList.size() - 1).mark(); | ||
} | ||
|
||
} | ||
if (next.charAt(0) == 'E') { | ||
String description = next.substring(8, divisor - 1); | ||
String date = next.substring((divisor + 2)); | ||
taskList.add(new Event(description, date)); | ||
|
||
if (next.charAt(4) == '1') { | ||
taskList.get(taskList.size() - 1).mark(); | ||
} | ||
} | ||
if (next.charAt(0) == 'T') { | ||
String description = next.substring(8); | ||
taskList.add(new ToDo(description)); | ||
|
||
if (next.charAt(4) == '1') { | ||
taskList.get(taskList.size() - 1).mark(); | ||
} | ||
} | ||
} | ||
} | ||
return taskList; | ||
} | ||
|
||
public void write(TaskList tasks) throws IOException { | ||
File myFile = new File(filePath); | ||
myFile.createNewFile(); | ||
FileWriter myWriter = new FileWriter(myFile); | ||
for (int i = 0; i < tasks.getList().size(); i++ ) { | ||
Task curr = tasks.getList().get(i); | ||
if (curr instanceof Deadline) { | ||
if (curr.isDone) { | ||
myWriter.write("D" + " | 1 | " + curr.description + " | " + ((Deadline) curr).by + "\n"); | ||
} else { | ||
myWriter.write("D" + " | 0 | " + curr.description + " | " + ((Deadline) curr).by + "\n"); | ||
} | ||
} else if (curr instanceof Event) { | ||
if (curr.isDone) { | ||
myWriter.write("E" + " | 1 | " + curr.description + " | " + ((Event) curr).at + "\n"); | ||
} else { | ||
myWriter.write("E" + " | 0 | " + curr.description + " | " + ((Event) curr).at + "\n"); | ||
} | ||
} else { | ||
if (curr.isDone) { | ||
myWriter.write("T" + " | 1 | " + curr.description + "\n"); | ||
} else { | ||
myWriter.write("T" + " | 0 | " + curr.description + "\n"); | ||
} | ||
} | ||
} | ||
myWriter.close(); | ||
} | ||
} |
Oops, something went wrong.