-
Notifications
You must be signed in to change notification settings - Fork 187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Praneet-25] iP #191
base: master
Are you sure you want to change the base?
[Praneet-25] iP #191
Changes from 12 commits
f54347d
d74699a
27e84eb
8085bde
6fd9175
a150b93
8df137b
1f78bd4
bca0bcf
51704c7
8064db9
618c9f2
6ee2390
8aa350c
8a04325
d1403e6
f119c00
b9ad83f
224c57d
6a351f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
T| laundry|0 | ||
E| project meeting (from: 5pm to: 8pm)))|0| 5pm | 8pm)) | ||
D| assignment (by: today)))|0| today)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
public class Deadline extends Task { | ||
protected String dateOfDeadline; | ||
|
||
public Deadline(String description) { | ||
super(description); | ||
typeOfTask = "D"; | ||
int dividerIndex = description.indexOf("by"); | ||
|
||
if (dividerIndex == -1) { | ||
this.description = description; | ||
setDateOfDeadline(null); | ||
} | ||
|
||
else { | ||
String endDate = description.substring(dividerIndex + 3); | ||
String descriptionWithoutDate = description.substring(0, (dividerIndex - 1)).replace("deadline", ""); | ||
setDateOfDeadline(endDate); | ||
this.description = descriptionWithoutDate + " (by: " + getDateOfDeadline() + ")"; | ||
} | ||
} | ||
|
||
public String getDateOfDeadline() { | ||
return dateOfDeadline; | ||
} | ||
|
||
public void setDateOfDeadline(String dateOfDeadline) { | ||
this.dateOfDeadline = dateOfDeadline; | ||
} | ||
|
||
@Override | ||
public String toFileString() { | ||
return "D|" + super.toFileString() + "|" + dateOfDeadline; // Prefix with "D" to indicate Deadline | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,206 @@ | ||
import java.io.*; | ||
import java.util.Arrays; | ||
import java.util.Scanner; | ||
import java.util.ArrayList; | ||
|
||
public class Duke { | ||
private static final String FILE_PATH = "./data/taskCategory.txt"; | ||
|
||
static ArrayList<Task> listOfItems = new ArrayList<>(); | ||
|
||
//variable stores the number of tasks being added | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. redundant comment |
||
static int sizeOfAddedItems = 0; | ||
|
||
public static void main(String[] args) { | ||
String logo = " ____ _ \n" | ||
+ "| _ \\ _ _| | _____ \n" | ||
+ "| | | | | | | |/ / _ \\\n" | ||
+ "| |_| | |_| | < __/\n" | ||
+ "|____/ \\__,_|_|\\_\\___|\n"; | ||
System.out.println("Hello from\n" + logo); | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The main method is doing a lot of things. please follow the instructions in A-MoreOOP and refactor the code accordingly. |
||
System.out.println(logo); | ||
System.out.println("____________________________________________________________"); | ||
System.out.println("Hello! I'm Brennan!"); | ||
System.out.println("What can I do for you?\n"); | ||
|
||
loadTasksFromFile(); | ||
|
||
//String that stores the input entered by the user | ||
String input; | ||
|
||
|
||
Scanner in = new Scanner(System.in); | ||
|
||
while (true) { | ||
input = in.nextLine(); | ||
|
||
try { | ||
|
||
if (input.equals("bye")) { | ||
break; | ||
} else if (input.equals("list")) { | ||
System.out.println("Here are the tasks in your list: "); | ||
System.out.println("____________________________________________________________"); | ||
for (int i = 0; i < sizeOfAddedItems; i++) { | ||
|
||
System.out.println((i + 1) + ". " + " " + "[" + listOfItems.get(i).typeOfTask + "]" + "[" + listOfItems.get(i).getStatusIcon() + "]" + listOfItems.get(i).description); | ||
} | ||
System.out.println("____________________________________________________________"); | ||
} else if (Arrays.asList(input.split(" ")).contains("mark")) { | ||
String[] splitInput = input.split(" "); | ||
|
||
if (splitInput.length < 2 || splitInput[1].isEmpty()) { | ||
throw new StringIndexOutOfBoundsException(); | ||
} | ||
//Finding the index of the task that the user wants to mark | ||
|
||
int indexTask = Integer.parseInt(splitInput[1]); | ||
|
||
listOfItems.get(indexTask - 1).markAsCompleted(); | ||
|
||
System.out.println("____________________________________________________________"); | ||
System.out.println("Nice! I've marked this task as done:"); | ||
System.out.println((indexTask) + ". " + "[" + listOfItems.get(indexTask - 1).getStatusIcon() + "]" + listOfItems.get(indexTask - 1).description); | ||
System.out.println("____________________________________________________________"); | ||
} else if (Arrays.asList(input.split(" ")).contains("unmark")) { | ||
String[] splitInput = input.split(" "); | ||
|
||
if (splitInput.length < 2 || splitInput[1].isEmpty()) { | ||
throw new StringIndexOutOfBoundsException(); | ||
} | ||
//Finding the index of the task that the user wants to mark | ||
|
||
int indexTask = Integer.parseInt(splitInput[1]); | ||
|
||
listOfItems.get(indexTask - 1).markAsNotCompleted(); | ||
|
||
System.out.println("____________________________________________________________"); | ||
System.out.println(" OK, I've marked this task as not done yet:"); | ||
System.out.println((indexTask) + ". " + "[" + listOfItems.get(indexTask - 1).getStatusIcon() + "]" + listOfItems.get(indexTask - 1).description); | ||
System.out.println("____________________________________________________________"); | ||
} else if (Arrays.asList(input.split(" ")).contains("todo")) { | ||
String[] splitInput = input.split(" "); | ||
if (splitInput.length < 2 || splitInput[1].isEmpty()) { | ||
throw new StringIndexOutOfBoundsException(); | ||
} else if (sizeOfAddedItems >= 100) { | ||
throw new ArrayIndexOutOfBoundsException(); | ||
} | ||
listOfItems.add(sizeOfAddedItems, new ToDo(input)); | ||
sizeOfAddedItems += 1; | ||
|
||
indicateNewTask(listOfItems.get(sizeOfAddedItems - 1), sizeOfAddedItems); | ||
} else if (Arrays.asList(input.split(" ")).contains("deadline")) { | ||
String[] splitInput = input.split(" "); | ||
if (splitInput.length < 2 || splitInput[1].isEmpty()) { | ||
throw new StringIndexOutOfBoundsException(); | ||
} else if (sizeOfAddedItems >= 100) { | ||
throw new ArrayIndexOutOfBoundsException(); | ||
} | ||
listOfItems.add(sizeOfAddedItems, new Deadline(input)); | ||
sizeOfAddedItems += 1; | ||
|
||
indicateNewTask(listOfItems.get(sizeOfAddedItems - 1), sizeOfAddedItems); | ||
} else if (Arrays.asList(input.split(" ")).contains("event")) { | ||
String[] splitInput = input.split(" "); | ||
if (splitInput.length < 2 || splitInput[1].isEmpty()) { | ||
throw new StringIndexOutOfBoundsException(); | ||
} else if (sizeOfAddedItems >= 100) { | ||
throw new ArrayIndexOutOfBoundsException(); | ||
} | ||
listOfItems.add(sizeOfAddedItems, new Event(input)); | ||
sizeOfAddedItems += 1; | ||
|
||
indicateNewTask(listOfItems.get(sizeOfAddedItems - 1), sizeOfAddedItems); | ||
} else if (Arrays.asList(input.split(" ")).contains("delete")) { | ||
String[] splitInput = input.split(" "); | ||
deleteTask(listOfItems,splitInput); | ||
sizeOfAddedItems--; | ||
} | ||
else { | ||
throw new IllegalArgumentException(); | ||
} | ||
|
||
} catch (Exception e) { | ||
DukeException.handleException(e, input); | ||
} | ||
|
||
saveTasksToFile(); | ||
} | ||
System.out.println("____________________________________________________________"); | ||
System.out.println("Bye. Hope to see you again soon!"); | ||
System.out.println("____________________________________________________________"); | ||
} | ||
|
||
|
||
public static void indicateNewTask(Task newTask, int currentNumberOfTasks) { | ||
System.out.println("____________________________________________________________"); | ||
System.out.println("Well done, you've added a new task: "); | ||
System.out.println("[" + newTask.typeOfTask + "]" + "[" + newTask.getStatusIcon() + "]" + newTask.description); | ||
System.out.println("Currently you have " + currentNumberOfTasks + " task(s) in your list!"); | ||
System.out.println("____________________________________________________________"); | ||
} | ||
|
||
private static void deleteTask(ArrayList<Task> listOfItems, String[] splitInput) { | ||
int indexTask = Integer.parseInt(splitInput[1]) ; | ||
System.out.println("This task has been deleted:"); | ||
System.out.println((indexTask) + ". " + "[" + listOfItems.get(indexTask - 1).getStatusIcon() + "]" + listOfItems.get(indexTask - 1).description); | ||
System.out.println("Your roster now contains " + (listOfItems.size() - 1) + " endeavors."); | ||
|
||
listOfItems.remove(indexTask-1); | ||
} | ||
|
||
public static void saveTasksToFile() { | ||
try { | ||
File file = new File(FILE_PATH); | ||
if (!file.getParentFile().exists()) { | ||
file.getParentFile().mkdirs(); // Create directories if they don't exist | ||
} | ||
file.createNewFile(); // Create the file if it doesn't exist | ||
try (FileWriter writer = new FileWriter(file)) { | ||
for (int i = 0; i < sizeOfAddedItems; i++) { | ||
writer.write(listOfItems.get(i).toFileString() + "\n"); | ||
} | ||
} | ||
} catch (IOException e) { | ||
System.err.println("Error saving tasks to file: " + e.getMessage()); | ||
} | ||
} | ||
|
||
// Method to load tasks from a file | ||
public static void loadTasksFromFile() { | ||
try (BufferedReader reader = new BufferedReader(new FileReader(FILE_PATH))) { | ||
String line; | ||
while ((line = reader.readLine()) != null) { | ||
Task task = Task.fromString(line); | ||
if (task != null) { | ||
listOfItems.add(sizeOfAddedItems++, task); | ||
} | ||
} | ||
} catch (FileNotFoundException e) { | ||
// Handle the case where the file doesn't exist | ||
System.err.println("File not found. Creating a new file..."); | ||
System.err.println("File created. Please enter your commands."); | ||
File file = new File(FILE_PATH); | ||
try { | ||
if (!file.getParentFile().exists()) { | ||
file.getParentFile().mkdirs(); // Create directories if they don't exist | ||
} | ||
file.createNewFile(); | ||
} catch (IOException ioException) { | ||
System.err.println("Error creating a new file: " + ioException.getMessage()); | ||
} | ||
} catch (IOException e) { | ||
System.err.println("Error loading tasks from file: " + e.getMessage()); | ||
} | ||
} | ||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unnecessary blank space |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
public class DukeException extends Exception { | ||
|
||
public static void handleException(Exception exception, String input) { | ||
final String SEPARATOR = "====================================================================================================================="; | ||
|
||
String[] splitInput = input.split(" "); | ||
|
||
System.out.println(SEPARATOR); | ||
|
||
if (exception instanceof ArrayIndexOutOfBoundsException) { | ||
System.out.println("You have exceeded the task limit of 100 tasks."); | ||
|
||
} else if (exception instanceof StringIndexOutOfBoundsException) { | ||
|
||
if (input.startsWith("mark") || input.startsWith("unmark")) { | ||
System.out.println("The task to marked or unmarked is not stated clearly."); | ||
} | ||
else if (input.startsWith("todo") || input.startsWith("deadline") || input.startsWith("event")) { | ||
System.out.println("Incomplete " + splitInput[0] + " detected. " + | ||
"Your statement is not clear. Please fix this."); | ||
} | ||
|
||
} else if (exception instanceof IllegalArgumentException) { | ||
System.out.println("Unfamiliar commands cannot be accepted by the system."); | ||
|
||
} else { | ||
System.out.println("Unknown error detected. Try to fix this immediately."); | ||
} | ||
|
||
System.out.println(SEPARATOR); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
public class Event extends Task { | ||
|
||
protected String startingDate; | ||
protected String endingDate; | ||
|
||
public Event(String description) { | ||
super(description); | ||
typeOfTask = "E"; | ||
|
||
int dividerIndexFrom = description.indexOf("from"); | ||
int dividerIndexTo = description.lastIndexOf("to"); | ||
|
||
setStartingDate(description.substring(dividerIndexFrom + 5, dividerIndexTo - 1)); | ||
setEndingDate(description.substring(dividerIndexTo + 3)); | ||
|
||
if (dividerIndexFrom == -1) { | ||
setStartingDate(null); | ||
} | ||
|
||
else if (dividerIndexTo == -1) { | ||
setEndingDate(null); | ||
} | ||
|
||
String descriptionWithoutDate = description.substring(0, (dividerIndexFrom - 1)).replace("event", ""); | ||
|
||
this.description = descriptionWithoutDate + " (from: " + getStartingDate() + " to: " + getEndingDate() + ")"; | ||
} | ||
|
||
public String getStartingDate() { | ||
return startingDate; | ||
} | ||
|
||
public void setStartingDate(String startingDate) { | ||
this.startingDate = startingDate; | ||
} | ||
|
||
public String getEndingDate() { | ||
return endingDate; | ||
} | ||
|
||
public void setEndingDate(String endingDate) { | ||
this.endingDate = endingDate; | ||
} | ||
|
||
@Override | ||
public String toFileString() { | ||
return "E|" + super.toFileString() + "|" + startingDate + "|" + endingDate; // Prefix with "E" to indicate Event | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Manifest-Version: 1.0 | ||
Main-Class: Duke | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
public class Task { | ||
protected String description; | ||
protected boolean isDone; | ||
protected String typeOfTask; | ||
|
||
public Task(String description) { | ||
this.description = description; | ||
this.isDone = false; | ||
typeOfTask = ""; | ||
} | ||
|
||
public String getStatusIcon() { | ||
return (isDone ? "X" : " "); | ||
} | ||
|
||
public void markAsCompleted() { | ||
isDone = true; | ||
} | ||
|
||
public void markAsNotCompleted() { | ||
isDone = false; | ||
} | ||
|
||
public String toFileString() { | ||
return description + "|" + (isDone ? "1" : "0"); // Example format: "description|status" | ||
} | ||
|
||
public static Task fromString(String fileString) { | ||
String[] parts = fileString.split("\\|"); | ||
String taskType = parts[0]; // Extract task type | ||
String description = parts[1]; | ||
boolean isDone = parts[2].equals("1"); // Extract status | ||
|
||
Task task; | ||
switch (taskType) { | ||
case "D": | ||
task = new Deadline(description); // Create Deadline task | ||
break; | ||
case "E": | ||
task = new Event(description); // Create Event task | ||
break; | ||
case "T": | ||
task = new ToDo(description); // Create Todo task | ||
break; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. redundant comments |
||
default: | ||
// Handle unknown task type | ||
task = null; | ||
break; | ||
} | ||
|
||
if (task != null && isDone) { | ||
task.markAsCompleted(); // Mark task as done if status is '1' | ||
} | ||
|
||
return task; | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
coding standard violation here.