0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Todoアプリ(Java)

Last updated at Posted at 2024-04-21

chatGPTにJavaで作らせたTodoアプリです。以下の3つのソースファイルを作ります。

TodoItem.java
public class TodoItem {
    private String task;
    private boolean completed;

    public TodoItem(String task) {
        this.task = task;
        this.completed = false;
    }

    public void complete() {
        this.completed = true;
    }

    public boolean isCompleted() {
        return completed;
    }

    @Override
    public String toString() {
        return (completed ? "[完了] " : "[未完了] ") + task;
    }
}
TodoList.java
import java.util.ArrayList;
import java.util.List;

public class TodoList {
    private List<TodoItem> items;

    public TodoList() {
        this.items = new ArrayList<>();
    }

    public void addTodoItem(String task) {
        TodoItem newItem = new TodoItem(task);
        items.add(newItem);
    }

    public void completeTodoItem(int index) {
        if (index >= 0 && index < items.size()) {
            items.get(index).complete();
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < items.size(); i++) {
            sb.append(i).append(": ").append(items.get(i).toString()).append("\n");
        }
        return sb.toString();
    }
}
TodoApp.java
import java.util.Scanner;

public class TodoApp {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        TodoList todoList = new TodoList();
        System.out.println("ToDoアプリへようこそ!");

        while (true) {
            System.out.println("\n[操作] 1: タスク追加, 2: タスク完了, 3: リスト表示, 0: 終了");
            System.out.print("操作を選択: ");
            int operation = scanner.nextInt();
            scanner.nextLine(); // 改行文字を消費

            switch (operation) {
                case 1:
                    System.out.print("追加するタスク: ");
                    String task = scanner.nextLine();
                    todoList.addTodoItem(task);
                    break;
                case 2:
                    System.out.print("完了するタスクの番号: ");
                    int index = scanner.nextInt();
                    todoList.completeTodoItem(index);
                    break;
                case 3:
                    System.out.println("\nToDoリスト:");
                    System.out.println(todoList);
                    break;
                case 0:
                    System.out.println("アプリを終了します。");
                    return;
                default:
                    System.out.println("無効な操作です。");
                    break;
            }
        }
    }
}

ファイルが作れたらターミナルで以下のコマンドを実行すればいいです。

javac TodoItem.java TodoList.java TodoApp.java
java TodoApp
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?