-
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
[tiffanyliu0220] iP #185
base: master
Are you sure you want to change the base?
[tiffanyliu0220] iP #185
Changes from 3 commits
5067e11
c688996
1bfdf54
9c1c488
4dd35ac
ebc535d
16b5032
1d963eb
629cd1c
3fbfe84
4947b3a
386d459
00466b8
23fc241
8ed0d1c
0ab06aa
945178a
34fa31a
f1598b1
8794be4
ab1611b
391800e
00dce25
1e754db
db0c247
0ea728b
7a09af6
405b601
435b546
2ffffed
422a21d
b2ba144
6686ca4
2633fbb
fcf59b1
879520a
72e4710
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import java.util.Scanner; | ||
|
||
public class Sunny { | ||
public static void main(String[] args) { | ||
String chatBotName = "Sunny"; | ||
Task[] tasks = new Task[100]; //Fixed-size array to store tasks | ||
int counter = 0; //Counter to keep track of the number of tasks | ||
|
||
//Greets the user | ||
System.out.println("Hello! I'm " + chatBotName); | ||
System.out.println("What can I do for you?"); | ||
System.out.println(" "); | ||
|
||
//Initialises scanner for user input | ||
Scanner scanner = new Scanner(System.in); | ||
|
||
//Echoes commands after user says bye | ||
while (true) { | ||
//Gets user input | ||
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. Goodjob on indenting the comments correctly! |
||
String command = scanner.nextLine(); | ||
|
||
//If the user input is "bye" | ||
if (command.equalsIgnoreCase("bye")){ | ||
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. Add a space after before the opening brace. |
||
//Exit message | ||
System.out.println(" "); | ||
System.out.println("Bye. Hope to see you again soon!"); | ||
System.out.println(" "); | ||
break; | ||
} else if (command.equalsIgnoreCase("list")){ | ||
//Displays the list of tasks | ||
System.out.println("Here are the tasks in your list: "); | ||
for (int i = 0; i < counter; i ++) { | ||
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++ instead of i ++ |
||
// | ||
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. Clean up code to remove random comments |
||
System.out.println((i + 1) + ". [" + tasks[i].getStatusIcon() + "]" + tasks[i].getDescription()); | ||
} | ||
System.out.println(" "); | ||
} else if (command.startsWith("mark")){ | ||
//Mark a task as done | ||
int taskIndex = extractTaskIndex(command); | ||
if (taskIndex > 0 && taskIndex <= counter) { | ||
tasks[taskIndex - 1].markAsDone(); | ||
System.out.println("Nice! I've marked this task as done:"); | ||
System.out.println("[" + tasks[taskIndex - 1].getStatusIcon() + "] " + tasks[taskIndex - 1].getDescription()); | ||
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. Maybe can consider wrapping lines for longer codes :) |
||
} else { | ||
System.out.println("Invalid task index. Please provide a valid task index."); | ||
|
||
} | ||
System.out.println(" "); | ||
} else if (command.startsWith("unmark")){ | ||
// Mark a task as not done | ||
int taskIndex = extractTaskIndex(command); | ||
if (taskIndex > 0 && taskIndex <= counter) { | ||
tasks[taskIndex - 1].unmarkAsDone(); | ||
System.out.println("OK, I've marked this task as not done yet:"); | ||
System.out.println("[" + tasks[taskIndex - 1].getStatusIcon() + "] " + tasks[taskIndex - 1].getDescription()); | ||
} else { | ||
System.out.println(" Invalid task index. Please provide a valid task index."); | ||
} | ||
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. Perhaps you could separate each cases into different methods to make the method shorter. |
||
} else { | ||
//Display the added task message | ||
System.out.println("added: " + command); | ||
System.out.println(" "); | ||
|
||
//Add the command to the tasks array | ||
tasks[counter] = new Task(command); | ||
counter++; | ||
} | ||
} | ||
//Close the scanner | ||
scanner.close(); | ||
} | ||
|
||
// Helper method to extract the task index from commands like "mark 2" | ||
private static int extractTaskIndex(String command) { | ||
try { | ||
return Integer.parseInt(command.split(" ")[1]); | ||
} catch (NumberFormatException | ArrayIndexOutOfBoundsException e) { | ||
return -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. Good job in adding an error prompt so that the users know what goes wrong. |
||
|
||
// Task class to represent tasks | ||
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 unmarkAsDone() { | ||
this.isDone = false; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
} |
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.
Good job in adding comments so the readers know what they are reading!