package com.twu.biblioteca;

import java.util.List;
import java.util.Scanner;

public class BibliotecaApp {
    private List<Book> bookList;

    public BibliotecaApp(List<Book> bookList) {
        this.bookList = bookList;
    }

    public void showWelcomePage() {
        System.out.println("Welcome to Biblioteca Library!\n");
        showSelectMenuOptions();
    }

    private void showSelectMenuOptions() {
        System.out.println("Please Select an Option:");
        System.out.println("---------------------------------------------");
        System.out.println("1.List Books");
        System.out.println("2.Check Out Books");
        System.out.println("3.Return Books");
        System.out.println("4.Quit");
        System.out.println("---------------------------------------------");
    }


    public void executeSelectedOption() {
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNextLine()){
            String option = scanner.next();
            switch (option) {
                case "1":
                    showAvailableBookList();
                    break;
                case "2":
                    showCheckOutOptions();
                    break;
                case "3":
                    showReturnBookOptions();
                    break;
                case "4":
                    quit();
                    break;
                default:
                    showErroMessage();
            }
        }
    }

    public void showAvailableBookList(){
        System.out.println("Here is the book list:");
        System.out.println("---------------------------------------------");
        System.out.printf("%-5s", "Id");
        System.out.printf("%-20s", "Name");
        System.out.printf("%-20s", "Author");
        System.out.printf("%-20s", "Year");
        System.out.printf("\n");
        System.out.println("---------------------------------------------");

        for(Book book : bookList){
            if(book.getBookState().equals(BookState.CHECKED_IN)){
                System.out.printf("%-5s", book.getBookId());
                System.out.printf("%-20s", book.getBookName());
                System.out.printf("%-20s", book.getBookAuthor());
                System.out.printf("%-20s", book.getPublishedYear());
                System.out.println();;
            }
        }
        System.out.println("---------------------------------------------");
    }

    private void showCheckOutOptions() {
        showAvailableBookList();

        System.out.println("Please Select Book Number You Want To Check Out:");
        System.out.println("---------------------------------------------");

        Scanner scanner = new Scanner(System.in);
        checkOutTheSelectedBook(scanner.next());

    }

    private void showReturnBookOptions() {
        showReturnBookList();

        System.out.println("Please Select Book Number You Want To Return:");
        System.out.println("---------------------------------------------");

        Scanner scanner = new Scanner(System.in);
        returnTheSelectedBook(scanner.next());
    }

    private void showReturnBookList() {
        System.out.println("Here is the book list:");
        System.out.println("---------------------------------------------");
        System.out.printf("%-5s", "Id");
        System.out.printf("%-20s", "Name");
        System.out.printf("%-20s", "Author");
        System.out.printf("%-20s", "Year");
        System.out.printf("\n");
        System.out.println("---------------------------------------------");

        for(Book book : bookList){
            if(book.getBookState().equals(BookState.CHECKED_OUT)){
                System.out.printf("%-5s", book.getBookId());
                System.out.printf("%-20s", book.getBookName());
                System.out.printf("%-20s", book.getBookAuthor());
                System.out.printf("%-20s", book.getPublishedYear());
                System.out.println();;
            }
        }
        System.out.println("---------------------------------------------");
    }

    private void checkOutTheSelectedBook(String option) {
        for(Book book : bookList){
            if(book.getBookId().equals(option)){
                book.setBookState(BookState.CHECKED_OUT);
                System.out.println("Thank you! Enjoy the book.");
                System.out.println();
                showWelcomePage();
                return;
            }
        }

        System.out.println("That book is not available.\n");
        showCheckOutOptions();
    }

    private void returnTheSelectedBook(String option) {
        for(Book book : bookList){
            if(book.getBookId().equals(option)){
                book.setBookState(BookState.CHECKED_IN);
                System.out.println("Thank you for returning the book.");
                System.out.println();
                showWelcomePage();
                return;
            }
        }

        System.out.println("That is not a valid book to return.\n");
        showReturnBookOptions();
    }

    public void showErroMessage() {
        System.out.println("Select a valid option!");
    }


    private void quit() {
        System.exit(0);
    }

    public static void main(String[] args) {
        BibliotecaApp bibliotecaApp = new BibliotecaApp(new BookList().getBookList());
        bibliotecaApp.showWelcomePage();
        bibliotecaApp.executeSelectedOption();
    }
}

@@ -22,16 +22,44 @@
 oteca;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -148,25 +148,77 @@
  List%3CBook%3E 
-b
+availableBookList;%0A    private List%3CBook%3E checkedOutB
 ookList;%0A%0A  
@@ -251,17 +251,26 @@
 t%3CBook%3E 
-b
+availableB
 ookList)
@@ -289,27 +289,98 @@
 his.
-bookList = bookList
+availableBookList = availableBookList;%0A        this.checkedOutBookList = new ArrayList%3C%3E()
 ;%0A  
@@ -2108,33 +2108,42 @@
 for(Book book : 
-b
+availableB
 ookList)%7B%0A      
@@ -3755,33 +3755,43 @@
 for(Book book : 
-b
+checkedOutB
 ookList)%7B%0A      
@@ -4332,33 +4332,42 @@
 for(Book book : 
-b
+availableB
 ookList)%7B%0A      
@@ -4467,16 +4467,110 @@
 D_OUT);%0A
+                checkedOutBookList.add(book);%0A                availableBookList.remove(book);%0A
         
@@ -4928,17 +4928,27 @@
  book : 
-b
+checkedOutB
 ookList)
@@ -5055,16 +5055,110 @@
 ED_IN);%0A
+                checkedOutBookList.remove(book);%0A                availableBookList.add(book);%0A
         

add availableBookList and checkedOutBookList attribute, and fix bug for return books