forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTask_Tracker.java
97 lines (79 loc) · 2.77 KB
/
Task_Tracker.java
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
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 markCompleted() {
completed = true;
}
}
class TaskTracker {
private ArrayList<Task> tasks;
public TaskTracker() {
tasks = new ArrayList<>();
}
public void addTask(String description) {
Task newTask = new Task(description);
tasks.add(newTask);
System.out.println("Task added: " + description);
}
public void markTaskCompleted(int index) {
if (index >= 0 && index < tasks.size()) {
Task task = tasks.get(index);
task.markCompleted();
System.out.println("Task marked as completed: " + task.getDescription());
} else {
System.out.println("Invalid task index.");
}
}
public void displayTasks() {
System.out.println("Task List:");
for (int i = 0; i < tasks.size(); i++) {
Task task = tasks.get(i);
System.out.println((i + 1) + ". " + task.getDescription() +
(task.isCompleted() ? " - Completed" : ""));
}
}
}
public class TaskTrackerApp {
public static void main(String[] args) {
TaskTracker taskTracker = new TaskTracker();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("\n1. Add Task\n2. Mark Task as Completed\n3. Display Tasks\n4. Exit");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
switch (choice) {
case 1:
System.out.print("Enter task description: ");
String description = scanner.nextLine();
taskTracker.addTask(description);
break;
case 2:
System.out.print("Enter the index of the task to mark as completed: ");
int index = scanner.nextInt();
taskTracker.markTaskCompleted(index - 1); // Adjust index for user input
break;
case 3:
taskTracker.displayTasks();
break;
case 4:
System.out.println("Exiting the Task Tracker. Goodbye!");
System.exit(0);
default:
System.out.println("Invalid choice. Please choose a valid option.");
}
}
}
}