Skip to content

Commit

Permalink
Reformat code and added check file access code
Browse files Browse the repository at this point in the history
  • Loading branch information
wuzhzn committed Feb 14, 2023
1 parent 8f45a28 commit f362e1a
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 90 deletions.
65 changes: 37 additions & 28 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@

public class Duke {
public static ArrayList<Task> tasksList = new ArrayList<>();
public static final String filePath = "src/main/java/duke/storage/duke.txt";


public static void main(String[] args) {

try {
List<Task> taskLists = Storage.convertToArray(filePath);
Storage.checkFileAccess();
List<Task> taskLists = Storage.convertToList();
tasksList.addAll(taskLists);
} catch (FileNotFoundException e) {
} catch (FileNotFoundException err) {
System.out.println("File not found");
} catch (IOException err) {
System.out.println("Something went wrong: " + err.getMessage());
}

String logo = " ____ _ \n"
Expand All @@ -38,32 +39,34 @@ public static void main(String[] args) {
UI.showWelcomeMessage();
do {
input = in.nextLine();
if (DukeException.hasError(input)){
if (DukeException.hasError(input)) {
continue;
}
command(input);
run(input);
} while (!input.equals("bye"));
}

private static void command(String input) {
private static void run(String input) {
String[] inputWords = input.split(" ");
switch (inputWords[0]){
switch (inputWords[0]) {
case "todo":
input = input.replaceFirst("todo", "").trim();
UI.printTodo(input);
Todo t = new Todo(input, "T");
tasksList.add(t);
Todo todo = new Todo(input, "T");
tasksList.add(todo);
UI.printTaskList(tasksList.size());
updateFile();
break;
case "deadline":
input = input.replaceFirst("deadline", "").trim();
int indexOfSlash = input.indexOf("/");
String taskName = input.substring(0, indexOfSlash - 1);
String by = input.substring(indexOfSlash + 4);
UI.printDeadline(taskName, by);
Deadline d = new Deadline(taskName, "D", by);
tasksList.add(d);
Deadline deadline = new Deadline(taskName, "D", by);
tasksList.add(deadline);
UI.printTaskList(tasksList.size());
updateFile();
break;
case "event":
input = input.replaceFirst("event", "").trim();
Expand All @@ -73,14 +76,15 @@ private static void command(String input) {
String start = input.substring(indexOfSlash + 6, lastIndexOfSlash - 1);
String end = input.substring(lastIndexOfSlash + 4);
UI.printEvent(taskName, start, end);
Event e = new Event(taskName, "E", start, end);
tasksList.add(e);
Event event = new Event(taskName, "E", start, end);
tasksList.add(event);
UI.printTaskList(tasksList.size());
updateFile();
break;
case "list":
try{
try {
printList();
} catch (EmptyListError err){
} catch (EmptyListError err) {
UI.printMessage("There is nothing inside the list");
}
break;
Expand All @@ -89,40 +93,45 @@ private static void command(String input) {
UI.printMessage("Nice! I've marked this task as done:");
tasksList.get(taskNum).markAsDone();
System.out.println(" [" + tasksList.get(taskNum).getStatusIcon() + "] " + tasksList.get(taskNum).description);
updateFile();
break;
case "unmark":
taskNum = Integer.parseInt(inputWords[1]) - 1;
UI.printMessage("OK, I've marked this task as not done yet:");
tasksList.get(taskNum).markAsUndone();
System.out.println(" [" + tasksList.get(taskNum).getStatusIcon() + "] " + tasksList.get(taskNum).description);
updateFile();
break;
case "delete":
taskNum = Integer.parseInt(inputWords[1]) - 1;
UI.printMessage("Noted. I've removed this task:");
System.out.println(" " + tasksList.get(taskNum).toString());
tasksList.remove(taskNum);
UI.printTaskList(tasksList.size());
updateFile();
break;
case "bye":
UI.showByeMessage();
try {
Storage.writeToFile(filePath, "");
for (Task task_i : tasksList){
Storage.appendToFile(filePath, task_i.textToSave() + System.lineSeparator());
}
} catch (IOException err) {
System.out.println("Something went wrong: " + err.getMessage());
}
updateFile();
break;
default: //never reached
UI.printMessage("added: " + input);
Task u = new Task(input, "");
tasksList.add(u);
UI.printMessage("Never reached");
}
}

private static void updateFile() {
try {
Storage.writeToFile("");
for (Task task : tasksList) {
Storage.appendToFile(task.textToSave() + System.lineSeparator());
}
} catch (IOException err) {
System.out.println("Something went wrong: " + err.getMessage());
}
}

private static void printList() throws EmptyListError {
if (tasksList.isEmpty()){
if (tasksList.isEmpty()) {
throw new EmptyListError();
}
UI.printMessage("Here are the tasks in your list:");
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/duke/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,23 @@ public Task(String description, String taskType) {
this.isDone = false;
}

public void markAsDone(){
public void markAsDone() {
isDone = true;
}

public void markAsUndone(){
public void markAsUndone() {
isDone = false;
}

public String getStatusIcon() {
return (isDone ? "X" : " "); // mark done task with X
}

public String toString() {
return "[" + getStatusIcon() + "] " + description;
}

public String textToSave(){
public String textToSave() {
return "";
}
}
Expand Down
21 changes: 11 additions & 10 deletions src/main/java/duke/UI.java
Original file line number Diff line number Diff line change
@@ -1,43 +1,44 @@
package duke;

public class UI {
public static final String HORIZONTAL_LINE = "____________________________________________________________";
private static final String HORIZONTAL_LINE = "____________________________________________________________";

public static void printTodo(String message){
public static void printTodo(String message) {
System.out.println("Got it. I've added this task:");
System.out.println(" [T][ ] " + message);
}

public static void printDeadline(String taskName, String by){
public static void printDeadline(String taskName, String by) {
System.out.println("Got it. I've added this task:");
System.out.println(" [D][ ] " + taskName + " (by: " + by + ")");
}

public static void printEvent(String taskName, String start, String end){
public static void printEvent(String taskName, String start, String end) {
System.out.println("Got it. I've added this task:");
System.out.println(" [E][ ] " + taskName + " (from: " + start + " to: " + end + ")");
}

public static void printTaskList(int taskListLength){
public static void printTaskList(int taskListLength) {
System.out.println("Now you have " + taskListLength + " tasks in the list.");
}
public static void printMessage(String message){

public static void printMessage(String message) {
System.out.println(message);
}

public static void showWelcomeMessage(){
public static void showWelcomeMessage() {
printPicture(0);
System.out.println(HORIZONTAL_LINE);
System.out.println("Hello! I'm Duke\n" + "What can I do for you?");
System.out.println(HORIZONTAL_LINE);
}

public static void showByeMessage(){
public static void showByeMessage() {
System.out.println("Bye. Hope to see you again soon!");
}

public static void printPicture(int index){
switch(index){
private static void printPicture(int index) {
switch (index) {
case 1:
System.out.println(" ___\n" +
" _/ ..\\\n" +
Expand Down
26 changes: 12 additions & 14 deletions src/main/java/duke/exception/DukeException.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,31 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class DukeException extends Exception {
public static boolean hasError(String input){
try{
public class DukeException extends Exception {
public static boolean hasError(String input) {
try {
catchError(input);
} catch (UnknownInputFieldError e) {
} catch (UnknownInputFieldError err) {
return true;
} catch (EmptyTaskDescription e){
} catch (EmptyTaskDescription err) {
return true;
}
return false;
}

private static void catchError(String input) throws UnknownInputFieldError, EmptyTaskDescription {
String[] inputWords = input.split(" ");
String[] taskTypes = {"todo", "deadline", "event", "list", "mark", "unmark", "delete", "bye"};
ArrayList<String> taskTypeList = new ArrayList<>(Arrays.asList(taskTypes));
if (!taskTypeList.contains(inputWords[0])) {

String command = inputWords[0];
String[] commandArray = {"todo", "deadline", "event", "list", "mark", "unmark", "delete", "bye"};
ArrayList<String> commandList = new ArrayList<>(Arrays.asList(commandArray));
if (!commandList.contains(command)) {
UI.printMessage("☹ OOPS!!! I'm sorry, but I don't know what that means :-(");
throw new UnknownInputFieldError();
} else if (inputWords[0].equals("list") | inputWords[0].equals("bye")) {
} else if(inputWords.length == 1){
UI.printMessage("☹ OOPS!!! The description of a " + inputWords[0] + " cannot be empty");
} else if (command.equals("list") | command.equals("bye")) {
} else if (inputWords.length == 1) {
UI.printMessage("☹ OOPS!!! The description of a " + command + " cannot be empty");
throw new EmptyTaskDescription();
}
}

}
3 changes: 2 additions & 1 deletion src/main/java/duke/exception/EmptyListError.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package duke.exception;
public class EmptyListError extends Exception{

public class EmptyListError extends Exception {
}
2 changes: 1 addition & 1 deletion src/main/java/duke/exception/EmptyTaskDescription.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package duke.exception;

public class EmptyTaskDescription extends Exception{
public class EmptyTaskDescription extends Exception {
}
2 changes: 1 addition & 1 deletion src/main/java/duke/exception/UnknownInputFieldError.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package duke.exception;

public class UnknownInputFieldError extends Exception{
public class UnknownInputFieldError extends Exception {
}
46 changes: 31 additions & 15 deletions src/main/java/duke/storage/Storage.java
Original file line number Diff line number Diff line change
@@ -1,45 +1,50 @@
package duke.storage;

import duke.Task;
import duke.UI;
import duke.task.Deadline;
import duke.task.Event;
import duke.task.Todo;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Storage {

public static List<Task> convertToArray(String filePath) throws FileNotFoundException {
List<Task> taskList = new ArrayList<>();
private static final String dirPath = "src/main/java/duke/storage";
private static final String filePath = "src/main/java/duke/storage/duke.txt";

File f = new File(filePath); // create a File for the given file path
Scanner s = new Scanner(f); // create a Scanner using the File as the source
public static List<Task> convertToList() throws FileNotFoundException {
List<Task> taskList = new ArrayList<>();
File file = new File(filePath); // create a File for the given file path
Scanner s = new Scanner(file); // create a Scanner using the File as the source
while (s.hasNext()) {
String textLine = s.nextLine();
String[] textLineArray = textLine.split(" ");
switch (textLineArray[0]) {
case "T":
String taskName = textLine.substring(8);
Todo t = new Todo(taskName, "T");
Todo todo = new Todo(taskName, "T");
if (textLineArray[2].equals("1")) {
t.markAsDone();
todo.markAsDone();
}
taskList.add(t);
taskList.add(todo);
break;
case "D":
textLine = textLine.substring(8);
int indexOfBoundary = textLine.indexOf("|");
taskName = textLine.substring(0, indexOfBoundary - 1);
String by = textLine.substring(indexOfBoundary + 2);
Deadline d = new Deadline(taskName, "D", by);
Deadline deadline = new Deadline(taskName, "D", by);
if (textLineArray[2].equals("1")) {
d.markAsDone();
deadline.markAsDone();
}
taskList.add(d);
taskList.add(deadline);
break;
case "E":
textLine = textLine.substring(8);
Expand All @@ -48,11 +53,11 @@ public static List<Task> convertToArray(String filePath) throws FileNotFoundExce
taskName = textLine.substring(0, indexOfBoundary - 1);
String from = textLine.substring(indexOfBoundary + 2, lastIndexOfBoundary - 1);
String to = textLine.substring(lastIndexOfBoundary + 2);
Event e = new Event(taskName, "E", from, to);
Event event = new Event(taskName, "E", from, to);
if (textLineArray[2].equals("1")) {
e.markAsDone();
event.markAsDone();
}
taskList.add(e);
taskList.add(event);
break;
default:
UI.printMessage("finish converting");
Expand All @@ -61,16 +66,27 @@ public static List<Task> convertToArray(String filePath) throws FileNotFoundExce
return taskList;
}

public static void writeToFile(String filePath, String textToAdd) throws IOException {
public static void writeToFile(String textToAdd) throws IOException {
FileWriter fw = new FileWriter(filePath);
fw.write(textToAdd);
fw.close();
}

public static void appendToFile(String filePath, String textToAppend) throws IOException {
public static void appendToFile(String textToAppend) throws IOException {
FileWriter fw = new FileWriter(filePath, true); // create a FileWriter in append mode
fw.write(textToAppend);
fw.close();
}

public static void checkFileAccess() throws IOException {
File dir = new File(dirPath);
if (!dir.exists()) {
dir.mkdir();
}
File f = new File(filePath);
if (!f.exists()) {
f.createNewFile();
}
}

}
Loading

0 comments on commit f362e1a

Please sign in to comment.