This repository was archived by the owner on Jun 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathToDoList.java
More file actions
113 lines (96 loc) · 3.32 KB
/
ToDoList.java
File metadata and controls
113 lines (96 loc) · 3.32 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
103
104
105
106
107
108
109
110
111
112
113
import java.util.ArrayList;
import java.util.Scanner;
class Task {
private String description;
private boolean isCompleted;
public Task(String description) {
this.description = description;
this.isCompleted = false;
}
public String getDescription() {
return description;
}
public boolean isCompleted() {
return isCompleted;
}
public void markAsCompleted() {
this.isCompleted = true;
}
@Override
public String toString() {
return (isCompleted ? "[X] " : "[ ] ") + description;
}
}
public class ToDoList {
private ArrayList<Task> tasks;
public ToDoList() {
tasks = new ArrayList<>();
}
public void addTask(String description) {
tasks.add(new Task(description));
}
public void deleteTask(int index) {
if (index >= 0 && index < tasks.size()) {
tasks.remove(index);
} else {
System.out.println("Invalid task number");
}
}
public void markTaskAsCompleted(int index) {
if (index >= 0 && index < tasks.size()) {
tasks.get(index).markAsCompleted();
} else {
System.out.println("Invalid task number");
}
}
public void displayTasks() {
if (tasks.isEmpty()) {
System.out.println("No tasks in the list.");
} else {
for (int i = 0; i < tasks.size(); i++) {
System.out.println((i + 1) + ". " + tasks.get(i));
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ToDoList toDoList = new ToDoList();
boolean exit = false;
while (!exit) {
System.out.println("\nTo-Do List:");
toDoList.displayTasks();
System.out.println("\nOptions:");
System.out.println("1. Add Task");
System.out.println("2. Delete Task");
System.out.println("3. Mark Task as Completed");
System.out.println("4. Exit");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume the newline
switch (choice) {
case 1:
System.out.print("Enter task description: ");
String description = scanner.nextLine();
toDoList.addTask(description);
break;
case 2:
System.out.print("Enter task number to delete: ");
int deleteIndex = scanner.nextInt() - 1;
toDoList.deleteTask(deleteIndex);
break;
case 3:
System.out.print("Enter task number to mark as completed: ");
int completeIndex = scanner.nextInt() - 1;
toDoList.markTaskAsCompleted(completeIndex);
break;
case 4:
exit = true;
break;
default:
System.out.println("Invalid choice. Please try again.");
}
}
scanner.close();
System.out.println("To-Do List application closed.");
}
}