import java.util.*;
import java.io.*;

abstract class Book {
    private String bookId;
    private String title;
    private String author;
    private double price;
    private String publisher;

    public Book (String bookId, String title, String author, double price, String publisher) {
        this.bookId=bookId;
        this.title=title;
        this.author=author;
        this.price=price;
        this.publisher=publisher;
    }

    public String getBookId()
    {return bookId;}
    public String getTitle()
    {return title;}
    public String getAuthor()
    {return author;}
    public double getPrice()
    {return price;}
    public String getPublisher()
    {return publisher;}

    /*public void getBookId()
	{ System.out.println("Book ID: " +bookId); }
	public String getTitle()
	{ System.out.println("Title of book: " +title); }
	public String getAuthor()
	{ System.out.println("Author of book: " +author); }
	public double getPrice()
	{ System.out.println("Price of book: RM" +price); }
	public String getPublisher()
	{ System.out.println("Publisher of book: " +publisher); }*/


    public String toString()
    {
    return "Book Details" + "\nBook ID: " + bookId +
    "\nTitle: " + title + "\nAuthor: " + author + "\nPrice: " + price + "\nPublisher: " + publisher;
    }
}

class Journals extends Book {
    private int edision;
    private int yearPublished;

    public Journals(String bookId, String title, String author, double price, String publisher, int edision, int yearPublished){
        super(bookId, title, author, price, publisher);
        this.edision=edision;
        this.yearPublished=yearPublished;
    }

    public int getEdision()
    {return edision;}

    public int getYearPublished()
    {return yearPublished;}

    public String toString() {
        return super.toString() + "\nEdision: " + edision + "\nYear Published: " + yearPublished;
    }
}

class Magazines extends Book{
    private String monthPublished;

    public Magazines(String bookId, String title, String author, double price, String publisher, String monthPublished){
        super(bookId, title, author, price, publisher);
        this.monthPublished=monthPublished;
    }

    public String getMonthPublished()
    {return monthPublished;}

    public String toString() {
        return super.toString() + "\nMonth Published: " + monthPublished;
    }
}

class StudyBooks extends Book{
    private String course;
    private int yearPublished;

    public StudyBooks(String bookId, String title, String author, double price, String publisher, String course, int yearPublished){
        super(bookId, title, author, price, publisher);
        this.course=course;
        this.yearPublished=yearPublished;
    }

    public String getCourse()
    {return course;}

    public int getYearPublished()
    {return yearPublished;}

    public String toString() {
        return super.toString()  + "\nCourse: " + course + "\nYear Published: " + yearPublished;
    }
}

class Account {
    private String memberShipDate;
    private String contactNo;
    private String address;

    public Account(String m, String c, String a) {
        memberShipDate=m;
        contactNo=c;
        address=a;
    }

    public String getMemberShipDate() {
        return memberShipDate;
    }

    public String getContactNo() {
        return contactNo;
    }

    public String getAddress() {
        return address;
    }
}

class User {
    private String id;
    private String password;
    private Account acct;		////composition with Account class
    Book bk;

    public User(String i, String p, String m, String c, String a) {
        id=i;
        password=p;
        acct = new Account(m, c, a);
    }

    public String getID() {
        return id;
    }

    public String getPassword() {
        return password;
    }

    public String toString()
	{
		return "User Details"
		+ "\nUser ID: " +id + "\nUser password: " +password
		+ "\nMembership date: " +acct.getMemberShipDate()
		+ "\nContact number: " +acct.getContactNo()
		+ "\nAddress: " +acct.getAddress();
	}

    public static void getBookDetails(Vector<Book> book)
    {
        for (int i=0; i<book.size(); i++) {
            System.out.println(book.get(i).toString());
            System.out.println();
        }
	}

    public static void borrowBook(Vector<Book> book) {
        try {
            FileWriter brwbook=new FileWriter("booksBorrowed.txt");
            Scanner input=new Scanner(System.in);
            System.out.println("\nBORROW BOOK PAGE");
            System.out.print("Enter the book ID that wanted to borrow -> ");
            String bookId=input.nextLine();
            for (int b=0; b<book.size(); b++) {
                if(book.get(b).getBookId().equals(bookId)) {
                    brwbook.write(book.get(b).toString());
                    System.out.print("Date borrow -> ");
                    String borrowDate=input.nextLine();
                    System.out.print("Date give back -> ");
                    String giveDate=input.nextLine();
                    brwbook.write("\nDate borrow: "+borrowDate);
                    brwbook.write("\nDate give back: "+giveDate);
                }
            }
            brwbook.close();
            System.out.println("Borrowed book has been recorded");
        }

        catch (IOException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}

class Librarian {
    private String name;
    private String password;

    public Librarian(String n, String p) {
        name=n;
        password=p;
    }

    public String getName() {
        return name;
    }

    public String getPassword() {
        return password;
    }

    //public static void searchBook() {
    public static void searchBook(Vector<Book> book) {
        Scanner input=new Scanner(System.in);
        String keyword;
        System.out.println("\nSEARCH PAGE");
        System.out.print("Search by book ID -> ");
        keyword=input.nextLine();
        for (int i=0; i<book.size(); i++) {
            if(book.get(i).getBookId().equals(keyword)) {
                System.out.println(book.get(i).toString());
                break;
            }
        }
    }

    public static Vector<Book> issueBook(Vector<Book> book) {
        String id, title, author, publisher, monthPublished, course;
        double price;
        int edision, yearPublished;
        Scanner input=new Scanner(System.in);
        try{
        int kind;
        do {
        System.out.println("\nADD BOOK PAGE");
        System.out.print("(1)Journals (2)Magazines (3)Studybook (4)Quit adding -> ");
        kind=input.nextInt();
        switch (kind) {
            case 1:
                System.out.print("Book ID -> ");
                input.nextLine();
                id=input.nextLine();
                System.out.print("Title -> ");
                title=input.nextLine();
                System.out.print("Author -> ");
                author=input.nextLine();
                System.out.print("Price -> ");
                price=input.nextDouble();
                System.out.print("Publisher -> ");
                input.nextLine();
                publisher=input.nextLine();
                System.out.print("Edision -> ");
                edision=input.nextInt();
                System.out.print("Year Published -> ");
                yearPublished=input.nextInt();
                book.add(new Journals(id, title, author, price, publisher, edision, yearPublished));
                System.out.println("Book added.");
                break;

            case 2:
                System.out.print("Book ID -> ");
                input.nextLine();
                id=input.nextLine();
                System.out.print("Title -> ");
                title=input.nextLine();
                System.out.print("Author -> ");
                author=input.nextLine();
                System.out.print("Price -> ");
                price=input.nextDouble();
                System.out.print("Publisher -> ");
                input.nextLine();
                publisher=input.nextLine();
                System.out.print("Month Published -> ");
                monthPublished=input.nextLine();
                book.add(new Magazines(id, title, author, price, publisher, monthPublished));
                System.out.println("Book added.");
                break;

            case 3:
                System.out.print("Book ID -> ");
                input.nextLine();
                id=input.nextLine();
                System.out.print("Title -> ");
                title=input.nextLine();
                System.out.print("Author -> ");
                author=input.nextLine();
                System.out.print("Price -> ");
                price=input.nextDouble();
                System.out.print("Publisher -> ");
                input.nextLine();
                publisher=input.nextLine();
                System.out.print("Course -> ");
                course=input.nextLine();
                System.out.print("Year Published -> ");
                yearPublished=input.nextInt();
                book.add(new StudyBooks(id, title, author, price, publisher, course, yearPublished));
                System.out.println("Book added.");
                break;

            case 4:
                System.out.println("Going back to Librarian Work Page...");
                break;

            default:
                System.out.println("INVALID! Enter a valid input.");
                continue; 
          } }while(kind==1||kind==2||kind==3);
       }
     catch (InputMismatchException e)
     {System.out.println("You input a wrong data type");}
     return book;
    }
}

public class LibrarySystem {
    public static void main(String[] args) {
        int librarianNo, libWork, userOptions, kind;
        String libName, libPassw, keyword;
        String userID, userPassw;
        Scanner input=new Scanner(System.in);

        Vector<Book> book=new Vector<Book>();
        book.add(new StudyBooks("bk001", "Object-Oriented Programming", "Jailani Y.", 20.30, "Sasbadi", "Programming", 2013));
        book.add(new Journals("bk002", "Human's Energy", "Joseph Paul", 40.00, "Nat Geo", 2, 2012));

        Vector<Librarian> librarian=new Vector<Librarian>();
        librarian.add(new Librarian("Srivani", "lib001"));
        librarian.add(new Librarian("Aqilah", "lib002"));
        librarian.add(new Librarian("Syamira", "lib003"));
        
        Vector<User> user=new Vector<User>();
        user.add(new User("NL001", "user001", "18 June 2019", "0123456789", "Shah Alam, Selangor"));
        user.add(new User("NL002", "user002", "13 May 2019", "0104567434", "Klang, Selangor"));
        user.add(new User("NL003", "user003", "8 August 2019", "0108732197", "Bangi, Selangor"));

        try{
        int login;
        do {
        System.out.println("\nLOGIN PAGE");
        System.out.print("Log In as (1)Librarian or (2)Non-librarian or (3)Quit -> ");
        login=input.nextInt();

        switch (login) {
            //Log in as Librarian
            case 1:
                System.out.print("Enter name -> ");
                input.nextLine();
                libName=input.nextLine();
                System.out.print("Enter password -> ");
                libPassw=input.nextLine();
                //verify librarian
                for (int l=0; l<librarian.size(); l++) {
                    if(librarian.get(l).getName().equals(libName)&&librarian.get(l).getPassword().equals(libPassw)) {
                        System.out.println("Librarian logged in.");
                        //librarian work
                        do {
                            System.out.println("\nLIBRARIAN WORK PAGE");
                            System.out.print("(1)Search or (2)Add book or (3)Back to Login Page-> ");
                            libWork=input.nextInt();
                            switch (libWork) {
                                case 1:
                                    //search book
                                    librarian.get(l).searchBook(book);
                                    continue;
        
                                case 2:
                                    //add book
                                    book=librarian.get(l).issueBook(book);
                                    continue;
        
                                case 3:
                                    System.out.println("Going back to Login Page...");
                                    break;
        
                                default:
                                    System.out.println("INVALID! Enter a valid input.");
                                    continue;
                            }
                        }while(libWork==1||libWork==2);
                    }
                }
                continue;

            //Log in as Non-Librarian
            case 2:
                System.out.print("Enter ID -> ");
                input.nextLine();
				userID = input.nextLine();
				System.out.print("Enter password -> ");
				userPassw = input.nextLine();

				//verify user
                for (int u=0; u<user.size(); u++) {
                    if (user.get(u).getID().equals(userID)&&user.get(u).getPassword().equals(userPassw)) {
                        System.out.println("User logged in.");
                        do {
                            System.out.println("\nUSER MAIN PAGE");
                            System.out.print("(1)Book details or (2)User details or (3)Borrow Book (4)Back to Login Page -> ");
                            userOptions=input.nextInt();
                            switch (userOptions)
                            {
                                case 1: //get book details
                                user.get(u).getBookDetails(book);
                                continue;

                                case 2: //get user details
                                System.out.println(user.get(u).toString());
                                continue;

                                case 3: //borrow book
                                user.get(u).borrowBook(book);
                                continue;

                                case 4: //back to login page
                                System.out.println("Going back to Login Page...");
                                break;

                                default:
                                System.out.println("INVALID! Enter a valid input.");
                                continue;
                            }
                        }while(userOptions==1||userOptions==2||userOptions==3);
                    }
                }
                continue;
            
            //Quit program
            case 3:
                System.out.println("Thank you!");
                break;

            //invalid input
            default:
                System.out.println("INVALID! Enter a valid input");
                continue;
            } }while(login==1||login==2);
        }
      catch (InputMismatchException e)
     {System.out.println("You input a wrong data type");}
    }
}