forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTask List Application.java
120 lines (103 loc) · 3.62 KB
/
Task List Application.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import java.util.ArrayList;
import java.util.Scanner;
class Task {
private String title;
private String description;
private boolean isCompleted;
public Task(String title, String description) {
this.title = title;
this.description = description;
this.isCompleted = false;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public boolean isCompleted() {
return isCompleted;
}
public void markAsCompleted() {
isCompleted = true;
}
}
public class TaskListApp {
private static ArrayList<Task> tasks = new ArrayList<>();
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
while (true) {
displayMenu();
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline character
switch (choice) {
case 1:
addTask();
break;
case 2:
viewTasks();
break;
case 3:
markTaskCompleted();
break;
case 4:
deleteTask();
break;
case 5:
System.out.println("Goodbye!");
System.exit(0);
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
private static void displayMenu() {
System.out.println("\nTask List Application");
System.out.println("1. Add a new task");
System.out.println("2. View tasks");
System.out.println("3. Mark a task as completed");
System.out.println("4. Delete a task");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
}
private static void addTask() {
System.out.print("Enter task title: ");
String title = scanner.nextLine();
System.out.print("Enter task description: ");
String description = scanner.nextLine();
Task task = new Task(title, description);
tasks.add(task);
System.out.println("Task added successfully!");
}
private static void viewTasks() {
System.out.println("\nTask List:");
for (int i = 0; i < tasks.size(); i++) {
Task task = tasks.get(i);
System.out.println(i + 1 + ". " + task.getTitle() + " - " + task.getDescription() +
(task.isCompleted() ? " (Completed)" : ""));
}
}
private static void markTaskCompleted() {
System.out.print("Enter the task number to mark as completed: ");
int taskNumber = scanner.nextInt();
scanner.nextLine(); // Consume newline character
if (taskNumber >= 1 && taskNumber <= tasks.size()) {
Task task = tasks.get(taskNumber - 1);
task.markAsCompleted();
System.out.println("Task marked as completed!");
} else {
System.out.println("Invalid task number. Please try again.");
}
}
private static void deleteTask() {
System.out.print("Enter the task number to delete: ");
int taskNumber = scanner.nextInt();
scanner.nextLine(); // Consume newline character
if (taskNumber >= 1 && taskNumber <= tasks.size()) {
Task task = tasks.remove(taskNumber - 1);
System.out.println("Task deleted: " + task.getTitle());
} else {
System.out.println("Invalid task number. Please try again.");
}
}
}