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 (97 loc) · 3.37 KB
/
ToDoList.java
File metadata and controls
113 lines (97 loc) · 3.37 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 completed;
public Task(String description) {
this.description = description;
this.completed = false;
}
public String getDescription() {
return description;
}
public boolean isCompleted() {
return completed;
}
public void markAsCompleted() {
this.completed = true;
}
@Override
public String toString() {
return (completed ? "[X] " : "[ ] ") + description;
}
}
public class ToDoList {
private ArrayList<Task> tasks;
public ToDoList() {
this.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 index");
}
}
public void markTaskAsCompleted(int index) {
if (index >= 0 && index < tasks.size()) {
tasks.get(index).markAsCompleted();
} else {
System.out.println("Invalid index");
}
}
public void displayTasks() {
if (tasks.isEmpty()) {
System.out.println("No tasks");
} 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();
while (true) {
System.out.println("\n---- ToDo List Menu ----");
System.out.println("1. Add Task");
System.out.println("2. Delete Task");
System.out.println("3. Mark Task as Completed");
System.out.println("4. Display Tasks");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline character
switch (choice) {
case 1:
System.out.print("Enter task description: ");
String description = scanner.nextLine();
todoList.addTask(description);
break;
case 2:
System.out.print("Enter index of task to delete: ");
int deleteIndex = scanner.nextInt();
todoList.deleteTask(deleteIndex - 1);
break;
case 3:
System.out.print("Enter index of task to mark as completed: ");
int completeIndex = scanner.nextInt();
todoList.markTaskAsCompleted(completeIndex - 1);
break;
case 4:
System.out.println("\n---- Tasks ----");
todoList.displayTasks();
break;
case 5:
System.out.println("Exiting...");
scanner.close();
System.exit(0);
default:
System.out.println("Invalid choice");
}
}
}
}