Skip to content

Commit

Permalink
A-MoreOOP
Browse files Browse the repository at this point in the history
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
rgonslayer committed Sep 8, 2022
1 parent 4f468dc commit c0a8d52
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 161 deletions.
46 changes: 0 additions & 46 deletions src/main/java/IOhandler.java

This file was deleted.

127 changes: 12 additions & 115 deletions src/main/java/Jarvis.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,134 +7,31 @@

public class Jarvis {
public static void main(String[] args) throws JarvisException {
Scanner inputScanner = new Scanner(System.in);

String filePath = "data/taskList.txt";

String userInput;
String introduction = "Hello. I am Jarvis \n"
+ "What can I do for you today?";
String farewell = "Goodbye, have a good day.";

List<Task> taskList = new ArrayList<>();
TaskList tasks;
Storage storage = new Storage(filePath);

try {
IOhandler.readFile(taskList);
tasks = new TaskList(storage.load());
} catch (IOException e) {
throw new RuntimeException(e);
tasks = new TaskList();
}

System.out.println(introduction);
try {
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 < taskList.size(); i++) {
if (taskList.get(i) == null) {
break;
}
System.out.println((i + 1) + ". " + taskList.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;
taskList.get(toMark).mark();
String markResponse = "Great. I have marked this task as done:\n ";
System.out.println(markResponse + taskList.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;
taskList.get(toMark).unmark();
String markResponse = "Ok, I have marked this task as not done yet:\n ";
System.out.println(markResponse + taskList.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 = taskList.get(toDelete);
taskList.remove(toDelete);
String deleteResponse = "Noted. I have removed this task:\n ";
System.out.println(deleteResponse + deleteTask.toString());
continue;
}
Parser parser = new Parser(tasks);

//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");
}
taskList.add(new ToDo(description));
System.out.println("Got it. I've added this task:\n " + taskList.get(Task.count - 1)
+ "\nNow you have " + (Task.count) + " tasks in the list.");
continue;
}
parser.introduction();

//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);
taskList.add(new Deadline(description, date));
System.out.println("Got it. I've added this task:\n " + taskList.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);
taskList.add(new Event(description, date));
System.out.println("Got it. I've added this task:\n " + taskList.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");
}
try {
parser.readCommand();
try {
File dir = new File("data");
File myFile = new File(dir, "taskList.txt");
myFile.createNewFile();
FileWriter myWriter = new FileWriter(myFile);
for (int i = 0; i < taskList.size(); i++ ) {
Task curr = taskList.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();
storage.write(tasks);
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
System.out.print(farewell);
parser.farewell();
}
catch (JarvisException e) {
System.out.println((e));
Expand Down
108 changes: 108 additions & 0 deletions src/main/java/Parser.java
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);
}
}

81 changes: 81 additions & 0 deletions src/main/java/Storage.java
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();
}
}
Loading

0 comments on commit c0a8d52

Please sign in to comment.