Java实现简单待办事项应用

Implementing a Simple To-Do List Application in Java

In this article, we will explore how to implement a simple to-do list application using Java. This application will allow users to add, remove, and view tasks. We will focus on creating a console-based application to keep things straightforward and emphasize core Java concepts.

Functional Requirements

  1. Add Task: Users should be able to add a new task to the to-do list.
  2. Remove Task: Users should be able to remove a task from the list by specifying its index.
  3. View Tasks: Users should be able to view all tasks currently in the list.
  4. Exit: Users should be able to exit the application.

Solution Design

To achieve these functionalities, we will use the following components:

  • Task Class: A simple class to represent a task with a description.
  • ToDoList Class: This class will manage the list of tasks, providing methods to add, remove, and display tasks.
  • Main Class: The entry point of the application, which will handle user input and interact with the ToDoList class.

Implementation

Task Class

The Task class will have a single attribute for the task description and a constructor to initialize it.

public class Task {
    private String description;

    public Task(String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }
}

ToDoList Class

The ToDoList class will use an ArrayList to store tasks and provide methods to manipulate the list.

import java.util.ArrayList;
import java.util.List;

public class ToDoList {
    private List<Task> tasks;

    public ToDoList() {
        tasks = new ArrayList<>();
    }

    public void addTask(String description) {
        tasks.add(new Task(description));
    }

    public void removeTask(int index) {
        if (index >= 0 && index < tasks.size()) {
            tasks.remove(index);
        } else {
            System.out.println("Invalid task index.");
        }
    }

    public void viewTasks() {
        if (tasks.isEmpty()) {
            System.out.println("No tasks in the list.");
        } else {
            for (int i = 0; i < tasks.size(); i++) {
                System.out.println((i + 1) + ". " + tasks.get(i).getDescription());
            }
        }
    }
}

Main Class

The Main class will handle user interaction and control the flow of the application.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        ToDoList toDoList = new ToDoList();
        Scanner scanner = new Scanner(System.in);
        boolean running = true;

        while (running) {
            System.out.println("1. Add Task");
            System.out.println("2. Remove Task");
            System.out.println("3. View Tasks");
            System.out.println("4. Exit");
            System.out.print("Choose an option: ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // Consume newline

            switch (choice) {
                case 1:
                    System.out.print("Enter task description: ");
                    String description = scanner.nextLine();
                    toDoList.addTask(description);
                    break;
                case 2:
                    System.out.print("Enter task index to remove: ");
                    int index = scanner.nextInt();
                    toDoList.removeTask(index - 1);
                    break;
                case 3:
                    toDoList.viewTasks();
                    break;
                case 4:
                    running = false;
                    break;
                default:
                    System.out.println("Invalid option. Please try again.");
            }
        }

        scanner.close();
    }
}

Conclusion

This simple to-do list application demonstrates basic Java programming concepts such as classes, methods, and user input handling. While this implementation is console-based, it provides a foundation that can be expanded into a more complex application with a graphical user interface or persistent storage.