Skip to content
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
ad8e067
level 0,1,2,3 increments are done.
ayushi0803 Sep 4, 2024
07f59be
I have done the ToDos, Events, and Deadlines. Users can create a new …
ayushi0803 Sep 4, 2024
bfdbfe9
this commit adds all my current work to github again
ayushi0803 Sep 20, 2024
0698d9b
Merge branch 'branch-Level-5'
ayushi0803 Sep 20, 2024
7248062
level 6 commit that has additional spaces to test branching
ayushi0803 Sep 20, 2024
3ae2e49
this commit shows that the delete feature has been added so that if y…
ayushi0803 Sep 20, 2024
8f0e008
Merge branch 'branch-Level-6'
ayushi0803 Sep 20, 2024
a417128
this commit consists of a file with my stored inputs and the stored i…
ayushi0803 Sep 20, 2024
2e8c2f6
this commit makes the code able to handle errors and throw out approp…
ayushi0803 Sep 20, 2024
b8ae068
this commit is a continuation to the previous commit and it serves to…
ayushi0803 Sep 20, 2024
ac1aa44
no message
ayushi0803 Oct 9, 2024
b4e8976
pushing all branches to my fork
ayushi0803 Oct 9, 2024
40fc93a
Merge pull request #1 from ayushi0803/branch-Level-8
ayushi0803 Oct 9, 2024
75a9741
Refactor Duke to A-MoreOOP structure: extracted Ui, Storage, TaskList…
ayushi0803 Oct 10, 2024
3579aea
Merge branch 'A-MoreOOP'
ayushi0803 Oct 10, 2024
e3f4fc3
fixing errors and conducting tests
ayushi0803 Oct 10, 2024
8bd8b6d
Teach the chatbot how to understand dates and times—parsing the input…
ayushi0803 Oct 10, 2024
bafe749
Give users a way to find a task by searching for a keyword.
ayushi0803 Oct 10, 2024
2dfc562
Add JavaDoc comments to the code. Adding header comments to all class…
ayushi0803 Oct 10, 2024
bada908
adding header comments to methods and classes.
ayushi0803 Oct 10, 2024
2e1d503
Merge branch 'A-JavaDoc'
ayushi0803 Oct 10, 2024
2433b68
Update README.md
ayushi0803 Oct 10, 2024
7269ffa
Changing the name Duke to Ruhi - my chatbot
ayushi0803 Oct 10, 2024
dfc56c4
Merge branch 'master' of https://github.com/ayushi0803/ip
ayushi0803 Oct 10, 2024
5bd867c
Update README.md
ayushi0803 Oct 10, 2024
caa6b86
Update README.md
ayushi0803 Oct 10, 2024
be6ab9d
Update README.md
ayushi0803 Oct 10, 2024
7ca3341
Update README.md
ayushi0803 Oct 10, 2024
aba22b9
jar file upload release
ayushi0803 Oct 10, 2024
8bf1b20
Delete gradle/wrapper/gradle-wrapper.jar
ayushi0803 Oct 11, 2024
8a1825b
Update Parser.java to handle longer inputs
ayushi0803 Oct 11, 2024
bdafc37
Update Ruhi.java to handle 'Bye' command and longer inputs
ayushi0803 Oct 11, 2024
b837998
Update Storage.java to handle tasks marked as done and not done in a …
ayushi0803 Oct 11, 2024
519e612
Update Task.java to handle done and not done tasks
ayushi0803 Oct 11, 2024
5cdce35
Update Ruhi.java
ayushi0803 Oct 11, 2024
573b20e
Update Parser.java
ayushi0803 Oct 11, 2024
2bb52c2
Update Ruhi.java
ayushi0803 Oct 11, 2024
fe5010c
Update README.md
ayushi0803 Oct 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions data/duke.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
T | 0 | hi
T | 1 | hoisdjfhsd
T | 0 | dg
T | 0 | gwrs
T | 1 | grw
T | 0 | g
T | 0 | wrglisty
T | 0 | hiorgw
T | 0 | rg
T | 0 | wrg
T | 0 | wr
T | 0 | gwr
T | 0 | g
T | 0 | wrh
T | 0 | wrh
T | 0 | wrh
T | 0 | save
T | 0 | hi
T | 0 | sg
T | 0 | sr
T | 0 | gwr
T | 0 | g
T | 0 | rwg
T | 0 | rgw
T | 0 | wr
T | 0 | lisyt
T | 0 | todo
T | 0 | blah
T | 0 | todo
T | 0 | blah
T | 0 | print
177 changes: 168 additions & 9 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,169 @@
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class Duke {
public static void main(String[] args) {
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
System.out.println("Hello from\n" + logo);
}
}
private static final String FILE_PATH = "./data/duke.txt";
private static Task[] tasks = new Task[100];
private static int taskCount = 0;

public static void main(String[] args) {
// Print the welcome message
System.out.println("____________________________________________________________");
System.out.println(" Hello! I'm Ruhi.");
System.out.println(" What can I do for you?");
System.out.println("____________________________________________________________");

// Load existing tasks from file
loadTasksFromFile();

// Initialize a scanner to read user input
Scanner scanner = new Scanner(System.in);

while (true) {
try {
String input = scanner.nextLine();
System.out.println("____________________________________________________________");

if (input.equals("bye")) {
System.out.println(" Bye. Hope to see you again soon!");
System.out.println("____________________________________________________________");
break;
} else if (input.equals("list")) {
// Display the list of tasks
System.out.println(" Here are the tasks in your list:");
for (int i = 0; i < taskCount; i++) {
System.out.println((i + 1) + "." + tasks[i]);
}
System.out.println("____________________________________________________________");
} else if (input.startsWith("mark")) {
handleMarkCommand(input);
} else if (input.startsWith("unmark")) {
handleUnmarkCommand(input);
} else if (input.startsWith("delete")) {
handleDeleteCommand(input);
} else if (input.trim().isEmpty()) {
throw new DukeException("Oops! Task description cannot be empty.");
} else {
handleAddCommand(input);
}
} catch (DukeException e) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the variable name "e" informative enough?

System.out.println(e.getMessage());
System.out.println("____________________________________________________________");
} catch (Exception e) {
System.out.println("Oopsy daisy! Something went wrong, check again.");
System.out.println("____________________________________________________________");
}
}

scanner.close();
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider extracting parts of the code the a separate method to avoid having long methods


// Methods for handling different commands

private static void handleMarkCommand(String input) throws DukeException {
try {
int taskIndex = Integer.parseInt(input.split(" ")[1]) - 1;
if (taskIndex < 0 || taskIndex >= taskCount) {
throw new DukeException("Invalid task number. Please provide a valid task number to mark.");
}
tasks[taskIndex].markAsDone();
System.out.println(" Nice! I've marked this task as done:");
System.out.println(" " + tasks[taskIndex]);
saveTasksToFile();
} catch (ArrayIndexOutOfBoundsException | NumberFormatException e) {
throw new DukeException("Please specify a valid task number after 'mark'.");
}
}

private static void handleUnmarkCommand(String input) throws DukeException {
try {
int taskIndex = Integer.parseInt(input.split(" ")[1]) - 1;
if (taskIndex < 0 || taskIndex >= taskCount) {
throw new DukeException("Invalid task number. Please provide a valid task number to unmark.");
}
tasks[taskIndex].markAsNotDone();
System.out.println(" OK, I've marked this task as not done yet:");
System.out.println(" " + tasks[taskIndex]);
saveTasksToFile();
} catch (ArrayIndexOutOfBoundsException | NumberFormatException e) {
throw new DukeException("Please specify a valid task number after 'unmark'.");
}
}

private static void handleDeleteCommand(String input) throws DukeException {
try {
int taskIndex = Integer.parseInt(input.split(" ")[1]) - 1;
if (taskIndex < 0 || taskIndex >= taskCount) {
throw new DukeException("Invalid task number. Please provide a valid task number to delete.");
}
System.out.println(" Noted. I've removed this task:");
System.out.println(" " + tasks[taskIndex]);
for (int i = taskIndex; i < taskCount - 1; i++) {
tasks[i] = tasks[i + 1];
}
tasks[taskCount - 1] = null;
taskCount--;
saveTasksToFile();
System.out.println(" Now you have " + taskCount + " tasks in the list.");
} catch (ArrayIndexOutOfBoundsException | NumberFormatException e) {
throw new DukeException("Please specify a valid task number after 'delete'.");
}
}

private static void handleAddCommand(String input) throws DukeException {
if (input.trim().isEmpty()) {
throw new DukeException("The description of a task cannot be empty.");
}
tasks[taskCount] = new Task(input);
taskCount++;
System.out.println("added: " + input);
saveTasksToFile();
}

// Load and Save Tasks
private static void loadTasksFromFile() {
try {
File file = new File(FILE_PATH);
if (!file.exists()) {
File dir = new File("./data");
if (!dir.exists()) {
dir.mkdirs();
}
file.createNewFile();
}

Scanner fileScanner = new Scanner(file);
while (fileScanner.hasNextLine()) {
String line = fileScanner.nextLine();
String[] parts = line.split(" \\| ");
String type = parts[0];
boolean isDone = parts[1].equals("1");
String description = parts[2];
Task task = new Task(description);
if (isDone) {
task.markAsDone();
}
tasks[taskCount] = task;
taskCount++;
}
fileScanner.close();
} catch (IOException e) {
System.out.println("Error loading tasks from file: " + e.getMessage());
}
}

private static void saveTasksToFile() {
try {
FileWriter writer = new FileWriter(FILE_PATH);
for (int i = 0; i < taskCount; i++) {
Task task = tasks[i];
writer.write("T | " + (task.isDone ? "1" : "0") + " | " + task.description + "\n");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to assign magic strings to a named constant instead

}
writer.close();
} catch (IOException e) {
System.out.println("Error saving tasks to file: " + e.getMessage());
}
}
}
6 changes: 6 additions & 0 deletions src/main/java/DukeException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Custom Exception for Duke-related errors
public class DukeException extends Exception {
public DukeException(String message) {
super(message);
}
}
27 changes: 27 additions & 0 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
public class Task {
protected String description;
protected boolean isDone;

public Task(String description) {
this.description = description;
this.isDone = false;
}

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

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

public void markAsNotDone() {
this.isDone = false;
}

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