From 3ee5b9d21a72e3d1b644deda157cd4b31cb4c163 Mon Sep 17 00:00:00 2001 From: Saksham Bhugra <85192629+sh4d0wy@users.noreply.github.com> Date: Sun, 15 Oct 2023 22:27:01 +0530 Subject: [PATCH] Added To do app --- todoapp.java | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 todoapp.java diff --git a/todoapp.java b/todoapp.java new file mode 100644 index 00000000..5014d325 --- /dev/null +++ b/todoapp.java @@ -0,0 +1,85 @@ +import java.util.ArrayList; +import java.util.Scanner; + +class Task { + private String description; + + public Task(String description) { + this.description = description; + } + + public String getDescription() { + return description; + } +} + +class TodoList { + private ArrayList tasks = new ArrayList<>(); + + public void addTask(String description) { + Task task = new Task(description); + tasks.add(task); + System.out.println("Task added successfully!"); + } + + public void listTasks() { + if (tasks.isEmpty()) { + System.out.println("No tasks found."); + } else { + System.out.println("Task List:"); + for (int i = 0; i < tasks.size(); i++) { + System.out.println((i + 1) + ". " + tasks.get(i).getDescription()); + } + } + } + + public void removeTask(int index) { + if (index >= 0 && index < tasks.size()) { + tasks.remove(index); + System.out.println("Task removed successfully!"); + } else { + System.out.println("Invalid task index."); + } + } +} + +public class TodoApp { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + TodoList todoList = new TodoList(); + + while (true) { + System.out.println("\nTo-Do List Application"); + System.out.println("1. Add Task"); + System.out.println("2. List Tasks"); + System.out.println("3. Remove Task"); + System.out.println("4. Exit"); + System.out.print("Enter your choice: "); + + int choice = scanner.nextInt(); + scanner.nextLine(); // Consume the newline character + + switch (choice) { + case 1: + System.out.print("Enter task description: "); + String description = scanner.nextLine(); + todoList.addTask(description); + break; + case 2: + todoList.listTasks(); + break; + case 3: + System.out.print("Enter the task index to remove: "); + int index = scanner.nextInt(); + todoList.removeTask(index - 1); + break; + case 4: + System.out.println("Exiting the To-Do List Application. Goodbye!"); + scanner.close(); + System.exit(0); + default: + System.out.println("Invalid choice. Please try again."); + } + } + } +}