-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTaskManager
103 lines (92 loc) · 3.44 KB
/
TaskManager
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
//importing the Scanner and the ArrayList class and the List interface from the java.util package
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
//creating TasksMenu class to manage a user interface, the constructor of the TasksMenu contains a list of objects of class Task
public class TasksMenu {
private final List<Task> tasks;
public TasksMenu(List<Task> tasks) {
this.tasks = tasks;
}
//creating viewTasks() method
public void viewTasks() {
System.out.println("Your tasks:");
for (int i = 0; i < tasks.size(); i++) {
Task task = tasks.get(i);
System.out.println((i + 1) + ". " + task.getName() + " - Done: " + task.isDone());
}
}
//creating addTask() method
public void addTask(String taskName) {
Task newTask = new Task(taskName);
tasks.add(newTask);
System.out.println("Task added: " + taskName);
}
//creating markTaskAsDone() method
public void markTaskAsDone(int taskIndex) {
if (taskIndex >= 0 && taskIndex < tasks.size()) {
Task task = tasks.get(taskIndex);
task.setDone(true);
System.out.println("Task marked as done: " + task.getName());
} else {
System.out.println("Invalid task index.");
}
}
//main method
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Task> tasks = new ArrayList<>();
TasksMenu tasksMenu = new TasksMenu(tasks);
//creating the menu interface
while (true) {
System.out.println("Options:");
System.out.println("1. View tasks");
System.out.println("2. Add task ");
System.out.println("3. Mark task as done");
System.out.println("4. Exit");
System.out.print("Enter your choice (1-4): ");
int choice = scanner.nextInt();
//using the switch method for interacting with user
switch (choice) {
case 1:
tasksMenu.viewTasks();
break;
case 2:
System.out.print("Enter the task: ");
scanner.nextLine(); // Consume the newline character left by nextInt()
String taskName = scanner.nextLine();
tasksMenu.addTask(taskName);
break;
case 3:
System.out.print("Enter the index of the task to mark as done: ");
int taskIndex = scanner.nextInt();
tasksMenu.markTaskAsDone(taskIndex - 1); // Adjust index to match the displayed list
break;
case 4:
System.out.println("Exiting the program. Goodbye!");
scanner.close(); // Close only the scanner, not System.in
System.exit(0);
default:
System.out.println("Invalid choice. Please enter a number between 1 and 4.");
}
}
}
}
//creating a Task class with relevant methods
class Task {
private final String name;
private boolean isDone;
public Task(String name) {
this.name = name;
this.isDone = false; // Tasks are initially not done
}
public String getName() {
return name;
}
public boolean isDone() {
return isDone;
}
public void setDone(boolean done) {
isDone = done;
}
}