-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToDoListApp.java
More file actions
102 lines (90 loc) · 3.78 KB
/
ToDoListApp.java
File metadata and controls
102 lines (90 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import java.util.Scanner;
public class ToDoListApp {
static String[][] tasks = new String[100][2]; // 2D Array to store task description and status
static int taskCount = 0; // Counter for number of tasks
public static String welcomeHeading() {
return "\n<<< Welcome to My To-Do List Application >>>\n" +
"____________________________________________\n";
}
public static String displayMenu() {
return "1. Add a Task\n" +
"2. View All Tasks\n" +
"3. Mark Task as Complete\n" +
"4. Delete a Task\n" +
"5. Exit\n";
}
public static String addTask(String taskDescription) {
if (taskCount < tasks.length) {
tasks[taskCount][0] = taskDescription; // Store task description
tasks[taskCount][1] = "Incomplete"; // Default status
taskCount++;
return "\n<<< Task added successfully >>>\n";
} else {
return "\nTask list is full! Cannot add more tasks.\n";
}
}
public static String viewTasks() {
if (taskCount == 0) {
return "No tasks recorded.\n";
}
String result = "\n<<< Tasks >>>\n";
for (int i = 0; i < taskCount; i++) {
String taskStatus = tasks[i][1].equals("Complete") ? "[X] " : "[ ] ";
result += (i + 1) + ". " + taskStatus + tasks[i][0] + "\n";
}
return result;
}
public static String markTaskComplete(int taskNumber) {
if (taskNumber >= 1 && taskNumber <= taskCount) {
tasks[taskNumber - 1][1] = "Complete";
return "Task marked as complete!\n";
} else {
return "Invalid task number.\n";
}
}
public static String deleteTask(int taskNumber) {
if (taskNumber >= 1 && taskNumber <= taskCount) {
for (int i = taskNumber - 1; i < taskCount - 1; i++) {
tasks[i][0] = tasks[i + 1][0];
tasks[i][1] = tasks[i + 1][1];
}
taskCount--;
return "Task deleted!\n";
} else {
return "Invalid task number.\n";
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(welcomeHeading());
while (true) {
System.out.println(displayMenu());
System.out.print("Enter your choice: ");
String choice = scanner.nextLine();
if (choice.equals("1")) {
System.out.print("Enter task description: ");
String taskDescription = scanner.nextLine();
System.out.println(addTask(taskDescription));
} else if (choice.equals("2")) {
System.out.println(viewTasks());
} else if (choice.equals("3")) {
System.out.println(viewTasks());
System.out.print("Enter task number to mark complete: ");
int taskNumber = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.println(markTaskComplete(taskNumber));
} else if (choice.equals("4")) {
System.out.println(viewTasks());
System.out.print("Enter task number to delete: ");
int taskNumber = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.println(deleteTask(taskNumber));
} else if (choice.equals("5")) {
System.out.println("Exiting application. Goodbye!");
break;
} else {
System.out.println("Invalid choice, please try again.");
}
}
}
}