From 2fea7cd2417c8eb3a45c239898911f6a11acb206 Mon Sep 17 00:00:00 2001 From: Sumitha Nair <61596065+sumitha1998@users.noreply.github.com> Date: Sat, 21 Oct 2023 18:20:33 +0530 Subject: [PATCH] Create ToDoList.java --- JAVA/ToDoList.java | 70 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 JAVA/ToDoList.java diff --git a/JAVA/ToDoList.java b/JAVA/ToDoList.java new file mode 100644 index 0000000..414158d --- /dev/null +++ b/JAVA/ToDoList.java @@ -0,0 +1,70 @@ +// This program allows users to add tasks, view tasks, mark tasks as completed, and exit the program. + +import java.util.ArrayList; +import java.util.Scanner; + +class Task { + String description; + boolean isCompleted; + + Task(String description) { + this.description = description; + this.isCompleted = false; + } +} + +public class ToDoList { + private static ArrayList tasks = new ArrayList<>(); + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + while (true) { + System.out.println("1. Add Task"); + System.out.println("2. View Tasks"); + System.out.println("3. Mark Task as Completed"); + System.out.println("4. Exit"); + System.out.print("Enter your choice: "); + int choice = scanner.nextInt(); + scanner.nextLine(); // Consume the newline character after nextInt() + + switch (choice) { + case 1: + System.out.print("Enter task description: "); + String description = scanner.nextLine(); + Task task = new Task(description); + tasks.add(task); + System.out.println("Task added successfully!"); + break; + case 2: + if (tasks.isEmpty()) { + System.out.println("No tasks found."); + } else { + System.out.println("Tasks:"); + for (int i = 0; i < tasks.size(); i++) { + Task currentTask = tasks.get(i); + System.out.println((i + 1) + ". " + (currentTask.isCompleted ? "[Completed] " : "") + currentTask.description); + } + } + break; + case 3: + System.out.print("Enter the task number to mark as completed: "); + int taskNumber = scanner.nextInt(); + if (taskNumber >= 1 && taskNumber <= tasks.size()) { + Task selectedTask = tasks.get(taskNumber - 1); + selectedTask.isCompleted = true; + System.out.println("Task marked as completed!"); + } else { + System.out.println("Invalid task number."); + } + break; + case 4: + System.out.println("Exiting program. Goodbye!"); + System.exit(0); + break; + default: + System.out.println("Invalid choice. Please try again."); + break; + } + } + } +}