forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.java
127 lines (109 loc) · 3.61 KB
/
scanner.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
121
122
123
124
125
126
127
import java.util.ArrayList;
import java.util.Scanner;
public class ToDoListApp {
private static ArrayList<Task> taskList = new ArrayList<>();
private static int taskIdCounter = 1;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("To-Do List Application");
System.out.println("1. Add Task");
System.out.println("2. List Tasks");
System.out.println("3. Mark Task as Completed");
System.out.println("4. Remove Task");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume the newline
switch (choice) {
case 1:
addTask(scanner);
break;
case 2:
listTasks();
break;
case 3:
markTaskAsCompleted(scanner);
break;
case 4:
removeTask(scanner);
break;
case 5:
System.out.println("Goodbye!");
System.exit(0);
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
private static void addTask(Scanner scanner) {
System.out.print("Enter the task description: ");
String description = scanner.nextLine();
Task task = new Task(taskIdCounter, description);
taskList.add(task);
taskIdCounter++;
System.out.println("Task added successfully!");
}
private static void listTasks() {
if (taskList.isEmpty()) {
System.out.println("No tasks to display.");
} else {
System.out.println("Tasks:");
for (Task task : taskList) {
System.out.println(task);
}
}
}
private static void markTaskAsCompleted(Scanner scanner) {
System.out.print("Enter the task ID to mark as completed: ");
int taskId = scanner.nextInt();
scanner.nextLine(); // Consume the newline
for (Task task : taskList) {
if (task.getId() == taskId) {
task.setCompleted(true);
System.out.println("Task marked as completed.");
return;
}
}
System.out.println("Task not found.");
}
private static void removeTask(Scanner scanner) {
System.out.print("Enter the task ID to remove: ");
int taskId = scanner.nextInt();
scanner.nextLine(); // Consume the newline
for (Task task : taskList) {
if (task.getId() == taskId) {
taskList.remove(task);
System.out.println("Task removed.");
return;
}
}
System.out.println("Task not found.");
}
}
class Task {
private int id;
private String description;
private boolean completed;
public Task(int id, String description) {
this.id = id;
this.description = description;
this.completed = false;
}
public int getId() {
return id;
}
public String getDescription() {
return description;
}
public boolean isCompleted() {
return completed;
}
public void setCompleted(boolean completed) {
this.completed = completed;
}
@Override
public String toString() {
return id + ". [" + (completed ? "X" : " ") + "] " + description;
}
}