-
Notifications
You must be signed in to change notification settings - Fork 78
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
[Xu JiaChen]iP #60
base: master
Are you sure you want to change the base?
[Xu JiaChen]iP #60
Changes from 5 commits
ed3efe1
c6302e9
8d766a7
6eb2ef3
2cf44fb
79f86ed
c310414
dae8f4d
507e70b
47c336c
08a8f29
4c76995
317108a
898968d
564c8ee
6a62116
45ac2de
12bfeb7
c2b4e7c
63adef5
c9d74d9
7b95f42
3547e34
64b0893
85bb25c
0d42444
76e80b4
a1189ae
c7dbfd9
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 |
---|---|---|
@@ -1,10 +1,96 @@ | ||
import java.util.ArrayList; | ||
import java.util.Scanner; | ||
|
||
public class Duke { | ||
public static void main(String[] args) { | ||
String logo = " ____ _ \n" | ||
+ "| _ \\ _ _| | _____ \n" | ||
+ "| | | | | | | |/ / _ \\\n" | ||
+ "| |_| | |_| | < __/\n" | ||
+ "|____/ \\__,_|_|\\_\\___|\n"; | ||
|
||
// Method to draw horizontal lines | ||
public static void drawLine() { | ||
for (int i = 0; i < 30; i++) { | ||
System.out.print("_"); | ||
} | ||
System.out.println(); | ||
} | ||
|
||
// Method to print the logo and introductory message | ||
public static void hiDude() { | ||
// Logo string | ||
String logo = "### # \n" | ||
+ "# # # \n" | ||
+ "# # # # ### ## \n" | ||
+ "# # # # # # # ## \n" | ||
+ "# # # # # # ## \n" | ||
+ "### ### ### ## \n"; | ||
|
||
System.out.println("Hello from\n" + logo); | ||
drawLine(); | ||
System.out.println("Hello! I'm your best Dude :)"); | ||
System.out.println("What can I do for you?"); | ||
drawLine(); | ||
} | ||
|
||
// Method to handle the storage of tasks | ||
public static void storeDude() { | ||
// Initialize Scanner and ArrayList for tasks | ||
Scanner scan = new Scanner(System.in); | ||
String input = scan.nextLine(); | ||
ArrayList<Task> tasks = new ArrayList<>(); | ||
int curPos = 0; // Variable to keep track of the current task position | ||
|
||
// Main loop to process commands | ||
while (!(input.isEmpty())) { | ||
drawLine(); | ||
if (input.equals("bye")) { | ||
byeDude(); | ||
break; | ||
} else if (input.equals("list")) { | ||
System.out.println("Here are the tasks in your list:"); | ||
for (int i = 0; i < curPos; i++) { | ||
System.out.println((i + 1) + ". " + tasks.get(i).getStatusIcon() + " " + tasks.get(i).description); | ||
} | ||
} else if (input.startsWith("mark") || input.startsWith("unmark")) { | ||
// Split the input to separate command and task index | ||
String[] arrOfInput = input.split(" ", 2); | ||
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. naming "arrOfInput" can be clearer to indicate that it stores the split of the input |
||
if (arrOfInput.length < 2) { | ||
System.out.println("Please specify the task index."); | ||
} else { | ||
try { | ||
// Convert user input index to zero-based index for ArrayList | ||
int index = Integer.parseInt(arrOfInput[1]) - 1; | ||
if (index < 0 || index >= curPos) { | ||
System.out.println("Task index out of range."); | ||
} else { | ||
// Mark or unmark the task | ||
tasks.get(index).isDone = input.startsWith("mark"); | ||
String message = input.startsWith("mark") ? "Nice! I've marked this task as done:" : "OK, I've marked this task as not done yet:"; | ||
System.out.println(message); | ||
System.out.println(" " + tasks.get(index).getStatusIcon() + " " + tasks.get(index).description); | ||
} | ||
} catch (NumberFormatException e) { | ||
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. try-catch statementfollows coding standard |
||
System.out.println("Invalid task index format."); | ||
} | ||
} | ||
} else { | ||
// Add new task | ||
System.out.println("added: " + input); | ||
tasks.add(new Task(input)); | ||
curPos++; | ||
} | ||
drawLine(); | ||
input = scan.nextLine(); | ||
} | ||
// Close the Scanner to prevent resource leak | ||
scan.close(); | ||
} | ||
|
||
// Method to print the goodbye message | ||
public static void byeDude() { | ||
System.out.println("Bye. Hope to see you again soon!"); | ||
drawLine(); | ||
} | ||
|
||
// Main method | ||
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 use of encapsulation and methods of sufficient-length |
||
public static void main(String[] args) { | ||
hiDude(); | ||
storeDude(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
public class Task { | ||
// Instance variables to store task description and status | ||
protected String description; | ||
protected boolean isDone; | ||
|
||
// Constructor to initialize a task with a description | ||
public Task(String description) { | ||
this.description = description; | ||
this.isDone = false; | ||
} | ||
|
||
// Method to get the status icon based on whether the task is done or not | ||
public String getStatusIcon() { | ||
return (isDone ? "[X]" : "[ ]"); | ||
} | ||
} | ||
|
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.
sufficiently comprehensible comments