/* * This source file was generated by the Gradle 'init' task */package com.example.minitl;import java.util.*;
public class Main {    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);        PostStore store = new PostStore();
        while (true) {            System.out.println("===== minitl cli =====");            System.out.println("1) post");            System.out.println("2) list");            System.out.println("3) like");            System.out.println("4) delete");            System.out.println("5) quit");
            int choice = Integer.parseInt(sc.nextLine().trim());
            try {                switch (choice) {                    case 1 -> {                        System.out.print("---write author: ");                        String author = sc.nextLine();
                        System.out.print("---write body: ");                        String body = sc.nextLine();
                        Post p = store.add(author, body);                        System.out.println("created: " + p);                    }                    case 2 -> {                        for (Post p : store.list()) {                            System.out.println(p);                        }                    }                    case 3 -> {                        System.out.print("---choose id: ");                        long id = Long.parseLong(sc.nextLine());                        store.like(id);                        System.out.println("liked!");                    }                    case 4 -> {                        System.out.print("---choose id: ");                        long id = Long.parseLong(sc.nextLine());                        store.delete(id);                        System.out.println("deleted!");                    }                    case 5 -> {                        System.out.println("quited");                        return;                    }                    default -> System.out.println("invalid");                }            } catch (PostNotFoundException e) {                System.out.println(e.getMessage());            }        }    }}

PostNotFoundExeption

package com.example.minitl;
public class PostNotFoundException extends RuntimeException {    public PostNotFoundException(long id) {        super("Post not found: " + id);    }}

PostStore

package com.example.minitl;import java.util.*;
public class PostStore {
    private final List<Post> posts = new ArrayList<>();    private long nextId = 1;    //add(auther, body)->新しいpostをつくって先頭に追加し、返す    public Post add(String author, String body) {        Post p = new Post(nextId++, author, body, 0);        posts.add(0, p); // 先頭に追加        return p;    }
    //list()ー>全件を新しい順でかえす    public List<Post> list() {        return new ArrayList<>(posts); // コピー返す    }
    //like(id)->likeCountを+1した新しいpostに差し替える。なければ例外
    public void like(long id) {        for (int i = 0; i < posts.size(); i++) {            Post p = posts.get(i);            if (p.id() == id) {                Post updated = new Post(p.id(), p.author(), p.body(), p.likeCount() + 1);                posts.set(i, updated);                return;            }        }        throw new PostNotFoundException(id);    }
        // delete(id)->さくじょする。存在しなければ例外    public void delete(long id) {        boolean removed = posts.removeIf(p -> p.id() == id);        if (!removed) {            throw new PostNotFoundException(id);        }    }}

Post

package com.example.minitl;
public record Post(long id, String author, String body, int likeCount) {}

build.gradle

tasks.named('run') {    standardInput = System.in}