-
Notifications
You must be signed in to change notification settings - Fork 116
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
base: master
Are you sure you want to change the base?
Changes from 5 commits
7b016a1
cc70201
c032aff
f4e5850
6599748
1117248
f6c8136
bc5cfc5
b63f451
2fb4a59
5da18af
209ab65
ae9fdba
3bbb7e6
d29c75c
2b989dd
f6b78eb
45f0d28
5525c53
ac5fa81
159670b
d7f22ce
afd9469
c5fba1d
0ef9fe0
fe17895
15d2a7c
8ef9d8d
fddddfb
f83bfa5
3aefa9b
b09cdf1
3c6d399
6299cc7
6da9289
70dd352
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,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 | ||
|
||
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(); | ||
} | ||
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. 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("_______________________________________"); | ||
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. Would it be better to store the greeting string as a constant instead of directly printing it? 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. 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) { | ||
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. 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() { | ||
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 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."); | ||
} | ||
} | ||
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. 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 { | ||
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. 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; | ||
} | ||
} | ||
} | ||
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. Would it be better to move your classes into their own files (AKA Task.java) to make your code more organized? |
This file was deleted.
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.
This can be renamed to taskList to be more descriptive.