Skip to content
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

[andreusxcarvalho] iP #108

Open
wants to merge 36 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
7b016a1
Updated chatbot name and greeting
andreusxcarvalho Aug 30, 2024
cc70201
Added echo function
andreusxcarvalho Aug 30, 2024
c032aff
List addition and viewing
andreusxcarvalho Aug 30, 2024
f4e5850
Added MarkAsDone functionality for list
andreusxcarvalho Aug 31, 2024
6599748
Amend code to comply with coding standards
andreusxcarvalho Aug 31, 2024
1117248
Add ToDo,Deadline,Event Functionality
andreusxcarvalho Sep 4, 2024
f6c8136
Improve Code Quality
andreusxcarvalho Sep 4, 2024
bc5cfc5
refactor code to multiple class files
andreusxcarvalho Sep 10, 2024
b63f451
add error-handling
andreusxcarvalho Sep 10, 2024
2fb4a59
Merged 'branch-Level-5' into 'master' to add the Level-5 functionality.
andreusxcarvalho Sep 11, 2024
5da18af
Add delete functionality
andreusxcarvalho Sep 12, 2024
209ab65
Completed Level-7
andreusxcarvalho Sep 18, 2024
ae9fdba
Merge branch 'branch-Level-6'
andreusxcarvalho Sep 18, 2024
3bbb7e6
Merge branch 'branch-Level-7'
andreusxcarvalho Sep 18, 2024
d29c75c
Updated .gitignore to ignore JAR files
andreusxcarvalho Oct 4, 2024
2b989dd
Completed A-MoreOOP increment
andreusxcarvalho Oct 4, 2024
f6b78eb
Implement Level-9: Find feature
andreusxcarvalho Oct 4, 2024
45f0d28
Merge pull request #1 from andreusxcarvalho/Level-9
andreusxcarvalho Oct 4, 2024
5525c53
Added Javadoc comments to all relevant classes
andreusxcarvalho Oct 4, 2024
ac5fa81
Merge pull request #2 from andreusxcarvalho/javadoc-branch
andreusxcarvalho Oct 4, 2024
159670b
Update README.md
andreusxcarvalho Oct 4, 2024
d7f22ce
Update README.md
andreusxcarvalho Oct 5, 2024
afd9469
Update README.md
andreusxcarvalho Oct 5, 2024
c5fba1d
Update README.md
andreusxcarvalho Oct 5, 2024
0ef9fe0
Update README.md
andreusxcarvalho Oct 5, 2024
fe17895
Update README.md
andreusxcarvalho Oct 5, 2024
15d2a7c
Update README.md
andreusxcarvalho Oct 5, 2024
8ef9d8d
Update README.md
andreusxcarvalho Oct 5, 2024
fddddfb
Update README.md
andreusxcarvalho Oct 5, 2024
f83bfa5
Update README.md
andreusxcarvalho Oct 5, 2024
3aefa9b
Update README.md
andreusxcarvalho Oct 23, 2024
b09cdf1
fix bugs from peer review
andreusxcarvalho Oct 23, 2024
3c6d399
Merge remote-tracking branch 'origin/master'
andreusxcarvalho Oct 23, 2024
6299cc7
Update User Guide
andreusxcarvalho Oct 23, 2024
6da9289
Fix nesting and long methods issue
andreusxcarvalho Oct 26, 2024
70dd352
Edit user guide
andreusxcarvalho Oct 26, 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
136 changes: 136 additions & 0 deletions src/main/java/Andy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Andy {

private static List<Task> itemList = new ArrayList<>(); // List to store tasks
Copy link

@joshuan98 joshuan98 Sep 16, 2024

Choose a reason for hiding this comment

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

This can be renamed to taskList to be more descriptive.


public static void main(String[] args) {
greetUser(); // Method to greet the user

Scanner scanner = new Scanner(System.in); // Use the imported Scanner class
String input = "";

while (!input.equals("bye")) {
input = scanner.nextLine();

if (input.startsWith("list")) {
handleListInput(input);
} else if (input.startsWith("mark ")) {
markTaskAsDone(input);
} else if (input.startsWith("unmark ")) {
markTaskAsNotDone(input);
} else {
echo(input); // Call echo method to repeat the input
}
}

exit();
}

Choose a reason for hiding this comment

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

I like how your statements are at the same level of abstraction


private static void greetUser() {
System.out.println("_______________________________________");
System.out.println("Hello! I'm ANDY");
System.out.println("What can I do for you?");
System.out.println("Type 'list' followed by an item to add it to the list.");
System.out.println("Type 'list show' to display all items in the list.");
System.out.println("Type 'mark <number>' to mark a task as done.");
System.out.println("Type 'unmark <number>' to mark a task as not done.");
System.out.println("_______________________________________");
Copy link

Choose a reason for hiding this comment

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

Would it be better to store the greeting string as a constant instead of directly printing it?
In the event we need to change the System.out.println function to something else it would be easier + avoid magic number scenario

Choose a reason for hiding this comment

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

Would it be better to use a single println() function with concatenation instead of having multiple println() functions?

}

private static void handleListInput(String input) {
String[] parts = input.split(" ", 2); // Split the input into command and item

if (parts.length > 1) {
Copy link

Choose a reason for hiding this comment

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

I think you can avoid arrowhead style code by using guard clauses. In each if statement, you return after the function was run. This way, you don't need an if else if chain

String command = parts[1];

if (command.equals("show")) {
showList(); // Correctly handle 'list show' to display the list
} else {
addItemToList(command); // Add the item to the list
}
} else {
System.out.println("Please provide a valid list command or item.");
}
}

private static void addItemToList(String item) {
itemList.add(new Task(item));
System.out.println("Item added to the list: " + item);
}

private static void showList() {
Copy link

Choose a reason for hiding this comment

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

The abstraction of the various features into various functions made the code easier to read. Well done!

System.out.println("_______________________________________");
System.out.println("Items in your list:");
if (itemList.isEmpty()) {
System.out.println("The list is empty.");
} else {
for (int i = 0; i < itemList.size(); i++) {
System.out.println((i + 1) + "." + itemList.get(i));
}
}
System.out.println("_______________________________________");
}

private static void markTaskAsDone(String input) {
try {
int taskNumber = Integer.parseInt(input.split(" ")[1]) - 1;
itemList.get(taskNumber).setDone(true);
System.out.println("Nice! I've marked this task as done:");
System.out.println("[X] " + itemList.get(taskNumber).getDescription());
} catch (Exception e) {
System.out.println("Invalid task number. Please try again.");
}
}

Choose a reason for hiding this comment

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

I like how you use try and catch statements to catch errors and prevent your code from crashing


private static void markTaskAsNotDone(String input) {
try {
int taskNumber = Integer.parseInt(input.split(" ")[1]) - 1;
itemList.get(taskNumber).setDone(false);
System.out.println("OK, I've marked this task as not done yet:");
System.out.println("[ ] " + itemList.get(taskNumber).getDescription());
} catch (Exception e) {
System.out.println("Invalid task number. Please try again.");
}
}

private static void echo(String input) {
System.out.println("You said: " + input);
}

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

// Task class to represent each task in the list
static class Task {
Copy link

Choose a reason for hiding this comment

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

Should multiple classes be separated into different files for readability?

private String description;
private boolean isDone;

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

public String getDescription() {
return description;
}

public boolean isDone() {
return isDone;
}

public void setDone(boolean done) {
isDone = done;
}

@Override
public String toString() {
return (isDone ? "[X] " : "[ ] ") + description;
}
}
}

Choose a reason for hiding this comment

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

Would it be better to move your classes into their own files (AKA Task.java) to make your code more organized?

10 changes: 0 additions & 10 deletions src/main/java/Duke.java

This file was deleted.