Skip to content

Commit

Permalink
fix: level-6 and level-7 merge conflict resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
Geeeetyx committed Feb 15, 2023
1 parent 3518b2d commit 2b77d92
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 42 deletions.
2 changes: 2 additions & 0 deletions src/main/java/duke.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
T|X|read book
T| |return book
146 changes: 107 additions & 39 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,51 @@
import duke.exception.EventException;
import duke.exception.KeywordException;
import duke.exception.TodoException;

import duke.task.Task;

import static duke.print.Print.*;

import java.io.*;

import java.util.ArrayList;
import java.util.Scanner;


public class Duke {
static final int USER_INPUT_EXPECTED_SIZE = 2;
static final String FILE_PATH = "src/main/java/duke.txt";

public static void main(String[] args) {
showWelcomeMessage();

File F = new File(FILE_PATH);
if (!F.exists()) {
new File("duke.txt");
}

ArrayList<Task> taskList = new ArrayList<>();

try {
loadFileContent(taskList);
} catch (FileNotFoundException e) {
println("A save file was not found. I shall create a new one.");
} catch (IOException e) {
println("Sorry, I could not read file.");
}

Scanner input = new Scanner(System.in);

while (true) {

String text = input.nextLine();

if ("bye".equalsIgnoreCase(text)) {
try {
writeToFile(taskList);
} catch (IOException e) {
System.out.println("Something broke " + e.getMessage());
}
break;
}

Expand Down Expand Up @@ -110,6 +132,82 @@ public static void main(String[] args) {
showExitMessage();
}

private static void loadFileContent(ArrayList<Task> taskList) throws IOException{
File f = new File(FILE_PATH);
Scanner scanner = new Scanner(f);

while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] task = line.split("\\|");
String taskType = task[0];
String taskDoneStatus = task[1];
String taskDescription = task[2];

switch (taskType) {
case "T":
Todo new_Todo = new Todo(taskDescription);
if (taskDoneStatus.equalsIgnoreCase("X")) {
new_Todo.markDone();
}
taskList.add(new_Todo);

break;

case "D":
String taskBy = task[3];
Deadline new_deadline = new Deadline(taskDescription, taskBy);
if (taskDoneStatus.equalsIgnoreCase("X")) {
new_deadline.markDone();
}

taskList.add(new_deadline);
break;

case "E":
String[] taskDuration = task[3].split(" ",2);
Event new_event = new Event(taskDescription, taskDuration[0], taskDuration[1]);
if (taskDoneStatus.equalsIgnoreCase("X")) {
new_event.markDone();
}

taskList.add(new_event);
break;
}
}
}

/**
* Writes the task list to a save file
*
* @param taskList The taskList to update the save file
*/
private static void writeToFile(ArrayList<Task> taskList) throws IOException {
FileWriter fw = new FileWriter(FILE_PATH);

for (Task taskToAppend : taskList) {
String toWrite;

String taskType = taskToAppend.getTypeIcon();
String taskDone = taskToAppend.getDoneIcon();
String taskDescription = taskToAppend.getDescription();

toWrite = taskType + "|" + taskDone + "|" + taskDescription;

if (taskToAppend.isDeadline()) {
String taskDeadline = taskToAppend.getBy();
toWrite = toWrite + "|" + taskDeadline;
} else if (taskToAppend.isEvent()) {
String taskFrom = taskToAppend.getFrom();
String taskTo = taskToAppend.getTo();
toWrite = toWrite + "|" + taskFrom + " " + taskTo;
}

toWrite = toWrite + '\n';
fw.write(toWrite);
}
fw.close();
}

private static void deleteOneTask(ArrayList<Task> taskList, String taskNumber) {
int taskNumberToDelete = Integer.parseInt(taskNumber);
taskNumberToDelete -= 1;
Expand All @@ -126,11 +224,11 @@ private static void deleteOneTask(ArrayList<Task> taskList, String taskNumber) {
* @param taskList The list to insert the task into
* @param userInput The details of the task to be added
*/
private static void addTodo(ArrayList<Task> taskList, String[] userInput) {
private static void addTodo(ArrayList<Task> taskList, String[] userInput) throws TodoException {
Todo new_todo = new Todo(userInput[1]);
taskList.add(new_todo);

printAddingOneTask(taskList, new_todo);
printAddingOneTask(new_todo, taskList);
}

/**
Expand All @@ -140,13 +238,13 @@ private static void addTodo(ArrayList<Task> taskList, String[] userInput) {
* @param userInput The details of the task to be added
*/
private static void addEvent(ArrayList<Task> taskList, String[] userInput) {
String[] eventDetails = userInput[1].split("/from | /to");
String[] eventDetails = userInput[1].split(" /from | /to ");

Event new_event = new Event(eventDetails[0], eventDetails[1], eventDetails[2]);

taskList.add(new_event);

printAddingOneTask(taskList, new_event);
printAddingOneTask(new_event, taskList);
}

/**
Expand All @@ -156,7 +254,7 @@ private static void addEvent(ArrayList<Task> taskList, String[] userInput) {
* @param userInput The details of the task to be added
*/
private static void addDeadline(ArrayList<Task> taskList, String[] userInput) {
String[] taskDetails = userInput[1].split(" /by", 2);
String[] taskDetails = userInput[1].split(" /by ", 2);

String taskName = taskDetails[0];
String taskDueDate = taskDetails[1];
Expand All @@ -165,7 +263,7 @@ private static void addDeadline(ArrayList<Task> taskList, String[] userInput) {

taskList.add(new_deadline);

printAddingOneTask(taskList, new_deadline);
printAddingOneTask(new_deadline, taskList);
}

/**
Expand All @@ -185,24 +283,10 @@ private static void unmarkSelectedTask(ArrayList<Task> taskList, String taskNumb
printOneLine();
println(" OK, I've marked this task as not done yet:");

printTypeAndStatus(selectedTask);
print(selectedTask.getDescription());

switch (selectedTask.getTypeIcon()) {
case "D":
println(" (by:" + selectedTask.getBy() + ")");
break;

case "E":
println("(from: " + selectedTask.getFrom() + " to:" + selectedTask.getTo() + ")");
break;
printMarkingOrUnmarkingOneTask(selectedTask);
}

default:
println("");
}

printOneLine();
}

/**
* Sets a specified task as done
Expand All @@ -220,22 +304,6 @@ private static void markSelectedTask(ArrayList<Task> taskList, String taskNumber
printOneLine();
println(" Nice! I've marked this task as done:");

printTypeAndStatus(selectedTask);
print(selectedTask.getDescription());

switch (selectedTask.getTypeIcon()) {
case "D":
println(" (by:" + selectedTask.getBy() + ")");
break;

case "E":
println("(from: " + selectedTask.getFrom() + " to:" + selectedTask.getTo() + ")");
break;

default:
println("");
}

printOneLine();
printMarkingOrUnmarkingOneTask(selectedTask);
}
}
30 changes: 27 additions & 3 deletions src/main/java/duke/print/Print.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ public static void printTaskList(ArrayList<Task> taskList) {
/**
* Prints the details of a newly added task
*
* @param taskList The list of tasks, to print the size (i.e. how many tasks in the list)
* @param taskToAdd The task to print the details of.
* @param taskToAdd The newly added task to print the details of.
* @param taskList The list of tasks where the newly added task is found
*/
public static void printAddingOneTask(ArrayList<Task> taskList, Task taskToAdd) {
public static void printAddingOneTask(Task taskToAdd, ArrayList<Task> taskList) {
printOneLine();
println(" Got it. I've added this task:");
printTypeAndStatus(taskToAdd);
Expand All @@ -163,6 +163,30 @@ public static void printAddingOneTask(ArrayList<Task> taskList, Task taskToAdd)
printOneLine();
}

/**
* Prints a task when it has been marked on unmarked as DONE.
* @param selectedTask The task that has been marked or unmarked.
*/
public static void printMarkingOrUnmarkingOneTask(Task selectedTask) {
printTypeAndStatus(selectedTask);
print(selectedTask.getDescription());

switch (selectedTask.getTypeIcon()) {
case "D":
println(" (by:" + selectedTask.getBy() + ")");
break;

case "E":
println("(from: " + selectedTask.getFrom() + " to:" + selectedTask.getTo() + ")");
break;

default:
println("");
}

printOneLine();
}

/**
* Prints the details of a deleted task
*
Expand Down

0 comments on commit 2b77d92

Please sign in to comment.