Skip to content

Commit

Permalink
Removed static variable numOfTask
Browse files Browse the repository at this point in the history
  • Loading branch information
imaginarys996 committed Sep 20, 2023
2 parents 0321771 + 5526242 commit a608f54
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 95 deletions.
3 changes: 3 additions & 0 deletions src/main/java/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: chatbot.Chatbot

258 changes: 163 additions & 95 deletions src/main/java/chatbot/Chatbot.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,141 +2,209 @@

import chatbot.*;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.sql.Array;
import java.util.ArrayList;
import java.util.Scanner;

public class Chatbot {
public static void saveToFile(String input) {
String str = input + System.lineSeparator();
try {
Path path = Paths.get("./tasklist.txt");
//System.out.println("fullpath: " + path.toAbsolutePath().toString());
if(Files.exists(path)) {
Files.write(path, str.getBytes(), StandardOpenOption.APPEND);
} else {
Files.write(path, str.getBytes(), StandardOpenOption.CREATE);
}

public static void main(String[] args) throws Exception {
//Task[] tasks = new Task[100];
ArrayList<Task> tasks = new ArrayList<Task>(100);
int numOfTasks = 0;
} catch (IOException e) {
//exception handling left as an exercise for the reader
}
}

String greetingMsg = "____________________________________________________________\n" +
" Hello! I'm Chatbot\n" +
" What can I do for you?\n" +
"____________________________________________________________\n";
String byeMsg = "____________________________________________________________\n" +
" Bye. Hope to see you again soon!\n" +
"____________________________________________________________\n";
System.out.println(greetingMsg);
public static void parseFile(ArrayList<Task> tasks) throws IOException {
Path path = Paths.get("./tasklist.txt");
if(!Files.exists(path)) {
return;
}
try (BufferedReader br = new BufferedReader(new FileReader(path.toAbsolutePath().toString()))) {
String line;
while ((line = br.readLine()) != null) {
// process the line.
//System.out.println(line);
if(line.trim().isEmpty()) {
continue;
}
parseCommand(tasks, line, false);

Scanner in = new Scanner(System.in);
String input = in.nextLine();
while(!input.equals("bye")) {
try {
if (input.equals("list")) {
System.out.println("____________________________________________________________");
System.out.println(" Here are the tasks in your list:");
for (int i = 0; i < numOfTasks; i++) {
//System.out.println(" " + (i + 1) + ".[" + tasks[i].getTypeIcon() + "][" + tasks[i].getStatusIcon() + "] " + tasks[i].getDescription());
System.out.println(" " + (i + 1) + "." + tasks.get(i));
}
System.out.println("____________________________________________________________");
} else if (input.startsWith("mark ")) {
String number = input.replace("mark ", "").trim();
int markTaskNo = Integer.parseInt(number);
if (markTaskNo > 0) {
tasks.get(markTaskNo - 1).markAsDone();
}
}
}
}

public static void parseCommand(ArrayList<Task> tasks, String input, boolean isUserInput) {
try {
if (input.equals("list")) {
System.out.println("____________________________________________________________");
System.out.println(" Here are the tasks in your list:");
for (int i = 0; i < tasks.size(); i++) {
//System.out.println(" " + (i + 1) + ".[" + tasks[i].getTypeIcon() + "][" + tasks[i].getStatusIcon() + "] " + tasks[i].getDescription());
System.out.println(" " + (i + 1) + "." + tasks.get(i));
}
System.out.println("____________________________________________________________");
} else if (input.startsWith("mark ")) {
String number = input.replace("mark ", "").trim();
int markTaskNo = Integer.parseInt(number);
if (markTaskNo > 0) {
tasks.get(markTaskNo - 1).markAsDone();
}
if (isUserInput) {
saveToFile(input);
System.out.println("____________________________________________________________");
System.out.println(" Nice! I've marked this task as done:");
System.out.println(" [" + tasks.get(markTaskNo - 1).getStatusIcon() + "] " + tasks.get(markTaskNo - 1).getDescription());
System.out.println("____________________________________________________________");
} else if (input.startsWith("unmark ")) {
String number = input.replace("unmark ", "").trim();
int unmarkedTaskNo = Integer.parseInt(number);
if (unmarkedTaskNo > 0) {
tasks.get(unmarkedTaskNo - 1).markAsUndone();
}
}

} else if (input.startsWith("unmark ")) {
String number = input.replace("unmark ", "").trim();
int unmarkedTaskNo = Integer.parseInt(number);
if (unmarkedTaskNo > 0) {
tasks.get(unmarkedTaskNo - 1).markAsUndone();
}
if (isUserInput) {
saveToFile(input);
System.out.println("____________________________________________________________");
System.out.println(" OK, I've marked this task as not done yet:");
System.out.println(" [" + tasks.get(unmarkedTaskNo - 1).getStatusIcon() + "] " + tasks.get(unmarkedTaskNo - 1).getDescription());
System.out.println("____________________________________________________________");
} else if (input.startsWith("todo")) {
String msg = input.replace("todo", "").trim();
if(msg.isEmpty()) {
throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of a todo cannot be empty.");
}
}

Task task = new Todo(msg);
//tasks.set(numOfTasks, task);
tasks.add(task);
numOfTasks++;
} else if (input.startsWith("todo")) {
String msg = input.replace("todo", "").trim();
if (msg.isEmpty()) {
throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of a todo cannot be empty.");
}

Task task = new Todo(msg);
tasks.add(task);
numOfTasks++;

if (isUserInput) {
saveToFile(input);
System.out.println("____________________________________________________________");
System.out.println(" Got it. I've added this task:");
System.out.println(" " + task);
System.out.println(" Now you have " + String.valueOf(numOfTasks) + " tasks in the list.");
System.out.println(" Now you have " + String.valueOf(tasks.size()) + " tasks in the list.");
System.out.println("____________________________________________________________");
} else if (input.startsWith("deadline")) {
String msg = input.replace("deadline", "").trim();
if(msg.isEmpty()) {
throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of a deadline cannot be empty.");
}
}

String byDate = msg.substring(msg.indexOf("/by ") + 4).trim(); // will contain the byDate
String desc = msg.substring(0, msg.indexOf("/by")); // will contain the deadline description
} else if (input.startsWith("deadline")) {
String msg = input.replace("deadline", "").trim();
if (msg.isEmpty()) {
throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of a deadline cannot be empty.");
}

String byDate = msg.substring(msg.indexOf("/by ") + 4).trim(); // will contain the byDate
String desc = msg.substring(0, msg.indexOf("/by")); // will contain the deadline description

Task task = new Deadline(desc, byDate);
//tasks.set(numOfTasks, task);
tasks.add(task);
numOfTasks++;
Task task = new Deadline(desc, byDate);
tasks.add(task);
numOfTasks++;

if (isUserInput) {
saveToFile(input);
System.out.println("____________________________________________________________");
System.out.println(" Got it. I've added this task:");
System.out.println(" " + task);
System.out.println(" Now you have " + String.valueOf(numOfTasks) + " tasks in the list.");
System.out.println(" Now you have " + String.valueOf(tasks.size()) + " tasks in the list.");
System.out.println("____________________________________________________________");
} else if (input.startsWith("event")) {
String msg = input.replace("event", "").trim();
if(msg.isEmpty()) {
throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of a event cannot be empty.");
}

String dateRange = msg.substring(msg.indexOf("/from ") + 6).trim();
String fromDate = dateRange.substring(0, dateRange.indexOf("/to ")).trim();
String toDate = dateRange.substring(dateRange.indexOf("/to ") + 4).trim();
String desc = msg.substring(0, msg.indexOf("/from ")); // will contain the deadline description

Task task = new Event(desc, fromDate, toDate);
//tasks.set(numOfTasks, task);
tasks.add(task);
numOfTasks++;
}

} else if (input.startsWith("event")) {
String msg = input.replace("event", "").trim();
if (msg.isEmpty()) {
throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of a event cannot be empty.");
}

String dateRange = msg.substring(msg.indexOf("/from ") + 6).trim();
String fromDate = dateRange.substring(0, dateRange.indexOf("/to ")).trim();
String toDate = dateRange.substring(dateRange.indexOf("/to ") + 4).trim();
String desc = msg.substring(0, msg.indexOf("/from ")); // will contain the deadline description

Task task = new Event(desc, fromDate, toDate);
tasks.add(task);
numOfTasks++;

if (isUserInput) {
saveToFile(input);
System.out.println("____________________________________________________________");
System.out.println(" Got it. I've added this task:");
System.out.println(" " + task);
System.out.println(" Now you have " + String.valueOf(numOfTasks) + " tasks in the list.");
System.out.println(" Now you have " + String.valueOf(tasks.size()) + " tasks in the list.");
System.out.println("____________________________________________________________");
} else if (input.startsWith("delete")) {
String parameter = input.replace("delete", "").trim();
if(parameter.isEmpty()) {
throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of delete cannot be empty.");
}
int indexToRemove = Integer.parseInt(parameter);
Task task = tasks.get(indexToRemove-1);
tasks.remove(indexToRemove-1);
}

} else if (input.startsWith("delete")) {
String msg = input.replace("delete", "").trim();
if (msg.isEmpty()) {
throw new ChatbotEmptyDescException(" ☹ OOPS!!! The description of delete cannot be empty.");
}
int indexToRemove = Integer.parseInt(msg);
if( indexToRemove > 0 ) {
indexToRemove -= 1;
}
Task task = tasks.get(indexToRemove);
tasks.remove(indexToRemove);
if (isUserInput) {
saveToFile(input);
System.out.println("____________________________________________________________");
System.out.println(" Noted. I've removed this task: ");
System.out.println(" " + task);
System.out.println(" Now you have " + String.valueOf(tasks.size()) + " tasks in the list.");
System.out.println("____________________________________________________________");
} else {
// unknown command
throw new ChatbotUnknownCommandException(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-(");
}
} catch(ChatbotUnknownCommandException e) {
System.out.println("____________________________________________________________");
System.out.println(e.getMessage());
System.out.println("____________________________________________________________");
} catch(ChatbotEmptyDescException e) {
System.out.println("____________________________________________________________");
System.out.println(e.getMessage());
System.out.println("____________________________________________________________");
} catch (Exception e) {
System.out.println("Unknown exception. Error message: " + e.getMessage());
} else {
// unknown command
throw new ChatbotUnknownCommandException(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-(");
}
} catch(ChatbotUnknownCommandException e) {
System.out.println("____________________________________________________________");
System.out.println(e.getMessage());
System.out.println("____________________________________________________________");
} catch(ChatbotEmptyDescException e) {
System.out.println("____________________________________________________________");
System.out.println(e.getMessage());
System.out.println("____________________________________________________________");
} catch (Exception e) {
System.out.println("Unknown exception. Error message: " + e.getMessage());
}
}
public static void main(String[] args) throws Exception {
ArrayList<Task> tasks = new ArrayList<Task>(100);

parseFile(tasks);

String greetingMsg = "____________________________________________________________\n" +
" Hello! I'm Chatbot\n" +
" What can I do for you?\n" +
"____________________________________________________________\n";
String byeMsg = "____________________________________________________________\n" +
" Bye. Hope to see you again soon!\n" +
"____________________________________________________________\n";
System.out.println(greetingMsg);

Scanner in = new Scanner(System.in);
String input = in.nextLine();
while(!input.equals("bye")) {
parseCommand(tasks, input, true);

input = in.nextLine();
}
Expand Down
3 changes: 3 additions & 0 deletions tasklist.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
event jeremy attend CS2113 lecture Friday 22 Sep 2023 /from 4 /to 6pm
deadline jeremy submit UG draft /by today 2359
delete 2

0 comments on commit a608f54

Please sign in to comment.