import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// ==========================================
// 1. 学生クラス (Student)
// ==========================================
class Student {
    private final String studentId; // 学籍番号
    private final String name;      // 名前

    public Student(String studentId, String name) {
        this.studentId = studentId;
        this.name = name;
    }

    public String getStudentId() {
        return studentId;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return name + " (学籍番号: " + studentId + ")";
    }
}

// ==========================================
// 2. 本クラス (Book)
// ==========================================
class Book {
    private final String bookId; // 書籍ID (ISBNなど)
    private final String title;  // タイトル

    public Book(String bookId, String title) {
        this.bookId = bookId;
        this.title = title;
    }

    public String getBookId() {
        return bookId;
    }

    public String getTitle() {
        return title;
    }

    @Override
    public String toString() {
        return "『" + title + "』 (ID: " + bookId + ")";
    }
}

// ==========================================
// 3. 貸出記録クラス (LendingRecord)
// ==========================================
class LendingRecord {
    private final Book book;
    private final Student student;

    public LendingRecord(Book book, Student student) {
        this.book = book;
        this.student = student;
    }

    public Book getBook() {
        return book;
    }

    public Student getStudent() {
        return student;
    }
}

// ==========================================
// 4. 図書館クラス (Library)
// ==========================================
class Library {
    // 登録された本の一覧 (キー: 書籍ID)
    private final Map<String, Book> books = new HashMap<>();
    // 登録された学生の一覧 (キー: 学籍番号)
    private final Map<String, Student> students = new HashMap<>();
    // 現在の貸出状況 (キー: 書籍ID, 値: 貸出記録)
    private final Map<String, LendingRecord> activeLendings = new HashMap<>();

    // 本の登録
    public void registerBook(Book book) {
        books.put(book.getBookId(), book);
    }

    // 学生の登録
    public void registerStudent(Student student) {
        students.put(student.getStudentId(), student);
    }

    // 貸出処理
    public void lendBook(String bookId, String studentId) {
        Book book = books.get(bookId);
        Student student = students.get(studentId);

        if (book == null) {
            System.out.println("エラー: ID " + bookId + " の本は図書館に登録されていません。");
            return;
        }
        if (student == null) {
            System.out.println("エラー: 学籍番号 " + studentId + " の学生は登録されていません。");
            return;
        }
        if (activeLendings.containsKey(bookId)) {
            System.out.println("エラー: " + book.getTitle() + " は既に他の学生に貸出中です。");
            return;
        }

        // 貸出実行
        LendingRecord record = new LendingRecord(book, student);
        activeLendings.put(bookId, record);
        System.out.println("【貸出完了】 " + student.getName() + " さんが " + book.getTitle() + " を借りました。");
    }

    // 返却処理
    public void returnBook(String bookId) {
        if (!activeLendings.containsKey(bookId)) {
            System.out.println("エラー: ID " + bookId + " の本は現在貸出中ではありません。");
            return;
        }

        LendingRecord removed = activeLendings.remove(bookId);
        System.out.println("【返却完了】 " + removed.getBook().getTitle() + " が返却されました。");
    }

    // 現在の貸出状況を一覧表示する
    public void showLendingStatus() {
        System.out.println("\n--- 現在の貸出状況一覧 ---");
        if (activeLendings.isEmpty()) {
            System.out.println("現在、貸出中の本はありません。");
            return;
        }
        for (LendingRecord record : activeLendings.values()) {
            System.out.println("・" + record.getBook() + "  => 貸出先: " + record.getStudent());
        }
        System.out.println("--------------------------\n");
    }
}

// ==========================================
// メインクラス (ideone.com の仕様に合わせ Main クラスにする)
// ==========================================
public class Main {
    public static void main(String[] args) {
        Library library = new Library();

        // 1. 本の登録
        Book book1 = new Book("B001", "Javaオブジェクト指向開発入門");
        Book book2 = new Book("B002", "アルゴリズムとデータ構造");
        Book book3 = new Book("B003", "データベース設計の基本");
        library.registerBook(book1);
        library.registerBook(book2);
        library.registerBook(book3);

        // 2. 学生の登録 (学籍番号で識別)
        Student student1 = new Student("S2026001", "アリス");
        Student student2 = new Student("S2026002", "ボブ");
        library.registerStudent(student1);
        library.registerStudent(student2);

        // --- 初期状態の確認 ---
        library.showLendingStatus();

        // 3. 貸出操作のシミュレーション
        library.lendBook("B001", "S2026001"); // アリスがJavaの本を借りる
        library.lendBook("B002", "S2026002"); // ボブがアルゴリズムの本を借りる

        // 状況確認
        library.showLendingStatus();

        // 重複貸出のエラーチェック
        library.lendBook("B001", "S2026002"); // ボブが既にアリスに貸出中の本を借りようとする (エラー)

        // 4. 返却と貸出状況の変化
        library.returnBook("B001"); // アリスがJavaの本を返却
        
        // 最終的な状況確認
        library.showLendingStatus();
    }
}