Skip to content

Commit

Permalink
Merge branch 'branch-Level-7'
Browse files Browse the repository at this point in the history
Resolved conflicts
# Conflicts:
#	src/main/java/Duke.java
  • Loading branch information
BenjaminPoh committed Feb 11, 2023
2 parents 078a99f + 0d3a57d commit ed44907
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 51 deletions.
234 changes: 184 additions & 50 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@
import duke.Task;
import duke.Todo;

import java.io.FileWriter;
import java.util.Scanner;
import java.util.ArrayList;
import java.io.File;

public class Duke {

public static final String GREETING_MESSAGE = "Hai, Ningensama-tachi! Kon-Nakiri!";
public static final String MESSAGE_DATA_LOADED_ON_STARTUP = "Hai, Ningensama-tachi! Loading your previous data...";
public static final String WELCOME_BACK_MESSAGE = "Welcome back, Ningensama! Kon-Nakiri!";
public static final String GREETING_MESSAGE_NEW_USER = "Hai, Ningensama-tachi! I see you're a new user, Kon-Nakiri!";
public static final String GOODBYE_MESSAGE = "Otsu-Nakiri!";
public static final String DATA_PATH = "data.txt";
//Instructions Strings
public static final String ACTION_LIST = "list";
public static final String ACTION_DELETE = "delete";
public static final String ACTION_MARK_COMPLETE = "mark";
public static final String ACTION_MARK_INCOMPLETE = "unmark";
public static final String ACTION_NEW_TODO = "todo";
Expand All @@ -32,7 +38,9 @@ public class Duke {
public static final String ERROR_BAD_FORMATTING = "That's some terrible formatting.";
public static final String ERROR_TOO_MUCH_TASKS = "Your free trial has concluded. " +
"To unlock more tasks, please purchase the DLC for $9.99/mth";
public static final String GOODBYE_WITH_NO_TASK_MESSAGE = "" +
public static final String ERROR_WITH_DATA_FILE = "An error occurred with the data file...\n";
public static final String ERROR_LOADING_FILE = "Why did you edit the data file?";
public static final String GOODBYE_WITH_NOTHING_DONE = "" +
"⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢿⣇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀\n" +
"⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠰⣶⣆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠺⣟⣶⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀\n" +
"⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⣿⣆⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⡈⣇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀\n" +
Expand Down Expand Up @@ -85,25 +93,77 @@ public class Duke {
"⠀⠀⣼⣿⡿⣛⣭⣶⣿⡿⢟⣫⣵⣶⣿⣿⣿⣿⣿⣿⣿⣷⣌⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀⡰⠁⠀⠀⢀⣤⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀⠀⡰⠙⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀\n" +
"⠀⣼⡿⣻⣾⣿⡿⢛⣥⣾⣿⣿⣿⣿⣿⠿⣋⣴⡙⣿⣿⣿⣿⣷⣍⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠀⠀⠀⠀⠀⠘⠁⠀⣤⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡀⠀⠀⠀⠀⠰⠁⠀⠹⣿⣿⣿⣿⣿⣧⣽⣿⢸⠀\n" +
"You must think you're funny, Ningen";
public static final String ACTION_DELETE = "delete";


public static String[] processInputMessage(Scanner in) {
String input = in.nextLine();
return input.split(" ", 2);
}

public static File initialiseData(ArrayList<Task> todoList) {
File data = new File(DATA_PATH);
try {
if (data.createNewFile()) {
System.out.println(GREETING_MESSAGE_NEW_USER);
} else {
System.out.println(MESSAGE_DATA_LOADED_ON_STARTUP);
Scanner in = new Scanner(data);
boolean brokenFile = false;
while (in.hasNextLine()) {
String[] inputMessage = processInputMessage(in);
switch (inputMessage[0]) {
case ACTION_MARK_COMPLETE:
int taskIndex = checkActionInputValidity(inputMessage, todoList.size());
Task currentTodo = todoList.get(taskIndex);
currentTodo.setComplete();
break;
case ACTION_MARK_INCOMPLETE:
taskIndex = checkActionInputValidity(inputMessage, todoList.size());
currentTodo = todoList.get(taskIndex);
currentTodo.setComplete();
break;
case ACTION_NEW_TODO:
String task = processToDoMessage(inputMessage);
Todo newTodo = new Todo(task);
todoList.add(newTodo);
break;
case ACTION_NEW_DEADLINE:
inputMessage = processDeadlineMessage(inputMessage);
Deadline newDeadline = new Deadline(inputMessage[0], inputMessage[1]);
todoList.add(newDeadline);
break;
case ACTION_NEW_EVENT:
inputMessage = processEventMessage(inputMessage);
Event newEvent = new Event(inputMessage[0], inputMessage[1], inputMessage[2]);
todoList.add(newEvent);
break;
default:
brokenFile = true;
}
if (brokenFile) {
System.out.println(ERROR_LOADING_FILE);
//TODO: introduce clear feature to wipe everything, to force-use here + use as feature.
}
}
System.out.println(WELCOME_BACK_MESSAGE);
}
} catch (Exception e) {
System.out.println(ERROR_WITH_DATA_FILE);
e.printStackTrace();
}
return data;
}

public static String[] processEventMessage(String[] input) {
String[] eventArray = new String[3];
try {
String[] inputArray = input[1].split(" /from ");
if (inputArray.length > 2) {
if (inputArray.length != 2) {
System.out.println(ERROR_BAD_FORMATTING);
return new String[0];
}
eventArray[0] = inputArray[0];
inputArray = inputArray[1].split(" /to ");
if (inputArray.length > 2) {
if (inputArray.length != 2) {
System.out.println(ERROR_BAD_FORMATTING);
return new String[0];
}
Expand All @@ -125,7 +185,7 @@ public static String[] processDeadlineMessage(String[] input) {
String[] deadlineArray;
try {
deadlineArray = input[1].split(" /by ");
if (deadlineArray.length > 2) {
if (deadlineArray.length != 2) {
System.out.println(ERROR_BAD_FORMATTING);
return new String[0];
}
Expand Down Expand Up @@ -182,82 +242,156 @@ private static void printRemovedMessage(int sizeOfTodoList, Task currentTodo) {
System.out.println("You have " + sizeOfTodoList + " tasks left");
}

public static void main(String[] args) {
System.out.println(GREETING_MESSAGE);
private static void addNewEventTask(ArrayList<Task> todoList, String[] inputMessage) {
Event newEvent = new Event(inputMessage[0], inputMessage[1], inputMessage[2]);
todoList.add(newEvent);
System.out.println(ADDED_TASK_MESSAGE + newEvent);
}

private static void addNewDeadlineTask(ArrayList<Task> todoList, String[] inputMessage) {
Deadline newDeadline = new Deadline(inputMessage[0], inputMessage[1]);
todoList.add(newDeadline);
System.out.println(ADDED_TASK_MESSAGE + newDeadline);
}

private static void addNewTodoTask(ArrayList<Task> todoList, String task) {
Todo newTodo = new Todo(task);
todoList.add(newTodo);
System.out.println(ADDED_TASK_MESSAGE + newTodo);
}

private static void markTaskIncomplete(ArrayList<Task> todoList, int taskIndex) {
Task currentTodo;
currentTodo = todoList.get(taskIndex);
currentTodo.setIncomplete();
System.out.println(INCOMPLETE_TASK_MESSAGE);
System.out.println(currentTodo);
}

private static void markTaskComplete(ArrayList<Task> todoList, int taskIndex) {
Task currentTodo;
currentTodo = todoList.get(taskIndex);
currentTodo.setComplete();
System.out.println(COMPLETED_TASK_MESSAGE);
System.out.println(currentTodo);
}

private static void listAllTasks(ArrayList<Task> todoList) {
Task currentTodo;
int sizeOfTodoList = todoList.size();
for (int i = 0; i < sizeOfTodoList; i += 1) {
currentTodo = todoList.get(i);
String printedMessage = String.format("%d.%s", i + 1, currentTodo);
System.out.println(printedMessage);
}
}

private static void updateData(ArrayList<Task> todoList) {
Task currentTodo;
try {
FileWriter writer = new FileWriter(DATA_PATH);
int sizeOfTodoList = todoList.size();
for (int i = 0; i < sizeOfTodoList; i += 1) {
currentTodo = todoList.get(i);
String type = currentTodo.getType();
String task = currentTodo.getTask();
String instruction;
switch (type) {
case ACTION_NEW_TODO:
instruction = String.format("%s %s\n", type, task);
break;
case ACTION_NEW_DEADLINE:
Deadline deadline = (Deadline) currentTodo;
String by = deadline.getDetails();
instruction = String.format("%s %s /by %s\n", type, task, by);
break;
case ACTION_NEW_EVENT:
Event event = (Event) currentTodo;
String[] details = event.getDetails();
instruction = String.format("%s %s /from %s /to %s\n", type, task, details[0], details[1]);
break;
default:
instruction = "Something is wrong I can feel it\n";

}
writer.write(instruction);
if (currentTodo.getComplete() == 'X') {
instruction = String.format("mark %d\n", i + 1);
writer.write(instruction);
}
}
writer.close();
} catch (Exception e) {
System.out.println(ERROR_WITH_DATA_FILE);
e.printStackTrace();
}
}

public static void main(String[] args) {
ArrayList<Task> todoList = new ArrayList<Task>();
int sizeOfTodoList = 0;
File data = initialiseData(todoList);
Task currentTodo;
boolean madeAValidInstruction = false;

Scanner in = new Scanner(System.in);
String[] inputMessage = processInputMessage(in);
while (!inputMessage[0].equals(ACTION_GOODBYE) && sizeOfTodoList < 100) {
while (!inputMessage[0].equals(ACTION_GOODBYE) && todoList.size() < 100) {
switch (inputMessage[0]) {
case ACTION_LIST:
for (int i = 0; i < sizeOfTodoList; i += 1) {
currentTodo = todoList.get(i);
String printedMessage = String.format("%d.%s", i + 1, currentTodo);
System.out.println(printedMessage);
}
listAllTasks(todoList);
inputMessage = processInputMessage(in);
break;
case ACTION_DELETE:
int taskIndex = checkActionInputValidity(inputMessage, sizeOfTodoList);
int taskIndex = checkActionInputValidity(inputMessage, todoList.size());
if(taskIndex >= 0) {
currentTodo = todoList.get(taskIndex);
todoList.remove(taskIndex);
sizeOfTodoList -= 1;
printRemovedMessage(sizeOfTodoList, currentTodo);
printRemovedMessage(todoList.size(), currentTodo);
}
inputMessage = processInputMessage(in);
break;
case ACTION_MARK_COMPLETE:
taskIndex = checkActionInputValidity(inputMessage, sizeOfTodoList);
taskIndex = checkActionInputValidity(inputMessage, todoList.size());
if (taskIndex >= 0) {
currentTodo = todoList.get(taskIndex);
currentTodo.setComplete();
System.out.println(COMPLETED_TASK_MESSAGE);
System.out.println(currentTodo);
markTaskComplete(todoList, taskIndex);
updateData(todoList);
madeAValidInstruction = true;
}
inputMessage = processInputMessage(in);
break;
case ACTION_MARK_INCOMPLETE:
taskIndex = checkActionInputValidity(inputMessage, sizeOfTodoList);
taskIndex = checkActionInputValidity(inputMessage, todoList.size());
if (taskIndex >= 0) {
currentTodo = todoList.get(taskIndex);
currentTodo.setIncomplete();
System.out.println(INCOMPLETE_TASK_MESSAGE);
System.out.println(currentTodo);
markTaskIncomplete(todoList, taskIndex);
updateData(todoList);
madeAValidInstruction = true;
}
inputMessage = processInputMessage(in);
break;
case ACTION_NEW_TODO:
String task = processToDoMessage(inputMessage);
if (!task.equals("")) {
Todo newTodo = new Todo(task);
todoList.add(newTodo);
sizeOfTodoList += 1;
System.out.println(ADDED_TASK_MESSAGE + newTodo);
String todoTask = processToDoMessage(inputMessage);
if (!todoTask.equals("")) {
addNewTodoTask(todoList, todoTask);
updateData(todoList);
madeAValidInstruction = true;
}
inputMessage = processInputMessage(in);
break;
case ACTION_NEW_DEADLINE:
inputMessage = processDeadlineMessage(inputMessage);
if (inputMessage.length == 2) {
sizeOfTodoList += 1;
Deadline newDeadline = new Deadline(inputMessage[0], inputMessage[1]);
todoList.add(newDeadline);
System.out.println(ADDED_TASK_MESSAGE + newDeadline);
String[] deadlineTask = processDeadlineMessage(inputMessage);
if (deadlineTask.length == 2) {
addNewDeadlineTask(todoList, deadlineTask);
updateData(todoList);
madeAValidInstruction = true;
}
inputMessage = processInputMessage(in);
break;
case ACTION_NEW_EVENT:
inputMessage = processEventMessage(inputMessage);
if (inputMessage.length == 3) {
sizeOfTodoList += 1;
Event newEvent = new Event(inputMessage[0], inputMessage[1], inputMessage[2]);
todoList.add(newEvent);
System.out.println(ADDED_TASK_MESSAGE + newEvent);
String[] eventTask = processEventMessage(inputMessage);
if (eventTask.length == 3) {
addNewEventTask(todoList, eventTask);
updateData(todoList);
madeAValidInstruction = true;
}
inputMessage = processInputMessage(in);
break;
Expand All @@ -266,9 +400,9 @@ public static void main(String[] args) {
inputMessage = processInputMessage(in);
}
}
if (sizeOfTodoList == 0) {
System.out.println(GOODBYE_WITH_NO_TASK_MESSAGE);
} else if (sizeOfTodoList == 100) {
if (!madeAValidInstruction) {
System.out.println(GOODBYE_WITH_NOTHING_DONE);
} else if (todoList.size() == 100) {
System.out.println(ERROR_TOO_MUCH_TASKS);
} else {
System.out.println(GOODBYE_MESSAGE);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/duke/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ public class Deadline extends Task {

public Deadline(String description, String by) {
super(description);
this.type= "deadline";
this.by = by;
}

public String getDetails() {
return this.by;
}

@Override
public String toString() {
return "[D][" + getComplete() + "] " + getTask() + " (By: " + by + ")";
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/duke/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@ public class Event extends Task {

public Event(String description, String from, String to) {
super(description);
this.type= "event";
this.from = from;
this.to = to;
}

public String[] getDetails() {
String[] details = new String[2];
details[0] = this.from;
details[1] = this.to;
return details;
}

@Override
public String toString() {
return "[E][" + getComplete() + "] " + getTask() + " (From: " + from + " To: " + to + ")";
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/duke/Task.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package duke;

public class Task {
private String task;
private final String task;
private boolean isComplete;
protected String type = "";

public Task(String name) {
this.task = name;
Expand All @@ -27,4 +28,9 @@ public void setComplete() {
public void setIncomplete() {
this.isComplete = false;
}

public String getType() {
return this.type;
}

}
1 change: 1 addition & 0 deletions src/main/java/duke/Todo.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public class Todo extends Task {

public Todo(String description) {
super(description);
this.type = "todo";
}

@Override
Expand Down

0 comments on commit ed44907

Please sign in to comment.