// Online Java Compiler
// Use this editor to write, compile and run your Java code online
import java.util.Scanner; 
import java.util.Arrays;

public class Library {
    
    public static String[][] addBook(String[][] db ){
        
        // initiate scanner object 
        Scanner sc = new Scanner(System.in);
        
        // create a new larger database 
        String[][] newDB = new String[db.length + 1][db[0].length];

        for (int i=0; i<db.length; i++){
            for (int j=0; j < db[i].length; j++){
                newDB[i][j] = db[i][j];
                }
            }
        
        // ask user input for last row        
        System.out.println("What is the book title?");
        String newTitle = sc.nextLine().toLowerCase();
        
        System.out.println("Who is the Author?");
        String newAuthor = sc.nextLine().toLowerCase();
        
        System.out.println("How many books are you adding?");
        String newAvailability = sc.nextLine();
        
        String[] newRow = {newTitle, newAuthor, newAvailability};
        newDB[db.length] = newRow;
        
        // find if book already exist
        for (int i = 0; i < db.length; i++) {
            if (newTitle.equals(db[i][0]) && newAuthor.equals(db[i][1])) {
                db[i][2] = Integer.toString(Integer.parseInt(db[i][2]) + Integer.parseInt(newAvailability));
                newDB = db;
                //break;
                }
            }
        
        return newDB; 
    }
    
    public static String[][] borrowBook(String[][] db ) {
        
        // initiate scanner object 
        Scanner sc = new Scanner(System.in);
        
        // ask user input for last row        
        System.out.println("What is the book title you want to borrow?");
        String borrowTitle = sc.nextLine().toLowerCase();
        
        System.out.println("Who is the Author of the boow you want to borrow?");
        String borrowAuthor = sc.nextLine().toLowerCase();
        
        System.out.println("How many books are you borrowing?");
        String borrowAvailability = sc.nextLine();
        
        
        // find if book already exist
        
        
        // check out a book
        try{
            for (int i = 0; i < db.length; i++) {
                if (borrowTitle.equals(db[i][0]) && borrowAuthor.equals(db[i][1])) {
                    if (Integer.parseInt(db[i][2]) - Integer.parseInt(borrowAvailability) >     0 ) {
                        db[i][2] = Integer.toString(Integer.parseInt(db[i][2]) - Integer.parseInt(borrowAvailability));
                        break;
                        }else{
                            System.out.println("Not enough books in stock");
                            break;
                            }
                        }else{
                            System.out.println("No such book in stock");
                            break;
                        }
                    }
            }catch (Exception e) {
			    // Default exception handler
			    System.out.println(“An unexpected exception occurred! Only number inputs are accepted in availibity”);
		    }
		
		
        return db;
        
    }
    
    public static String[][] returnBook(String[][] db ) {
        
        // initiate scanner object 
        Scanner sc = new Scanner(System.in);
        
        // ask user input for last row        
        System.out.println("What is the book title you want to return?");
        String returnTitle = sc.nextLine().toLowerCase();
        
        System.out.println("Who is the Author of the boow you want to return?");
        String returnAuthor = sc.nextLine().toLowerCase();
        
        System.out.println("How many books are you returning?");
        String returnAvailability = sc.nextLine();
        
        // find if book already exist
        // check out a book
        
        for (int i = 0; i < db.length; i++) {

            if (returnTitle.equals(db[i][0]) && returnAuthor.equals(db[i][1]) ) {
                db[i][2] = Integer.toString(Integer.parseInt(db[i][2]) + Integer.parseInt(returnAvailability));
                break;
                }else{
                    System.out.println("No such book in stock or availibity not a number");
                    break;
                    }
        }    
		    
		 return db;
    
        
    }
    
    public static void main(String[] args) {
        
        // initiate scanner object 
        Scanner sc = new Scanner(System.in);
        
        // database of books 
        String[][] db = {
            {"title", "author", "quantity" },
            {"harry potter", "jk rowling", "5"}
        };
        
        System.out.println("Welcome to the Library System!");
        System.out.println("\n");
        
        System.out.println("What would you wish to do?");
        System.out.println("\n");
        System.out.println("A. Add books?");
        System.out.println("B. Borrow books?");
        System.out.println("C. Return books?");
        
        System.out.println("\n");

        // type in an answer 
        String answer = sc.next().toLowerCase();
        
        try{
            if(answer.equals("a")){
                db = addBook(db);
            }else if(answer.equals("b")){
                db = borrowBook(db);
            }else if(answer.equals("c")){
                db = returnBook(db);
            }else{
                System.out.println("Please choose a correct letter from the choices provided.");
            }
        }catch(Exception e){
            // Default exception handler
            System.out.println("An unexpected exception occurred!");
        }
        
        System.out.println("\n");
        
    }
}