import java.util.Scanner;

public class Main {

    //global variables
    static Scanner kbin;

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        //initialise kbin
        kbin = new Scanner(System.in);

        //declare local variables
        String choice, tool;
        
        //the only tools that can be processed in this program
        final String[] validTools = {"wrench", "spanner", "screwdriver", "paintbrush","drill", "chainsaw", "fork", "spade"};
        
        //the state of the current toolshelf
        String[] currentToolShelf = {"wrench", "spanner", "screwdriver", "paintbrush","drill", "chainsaw", "fork", "spade"};

        
        //game loop
    	
        do {
        	
        	displayShelf(currentToolShelf);
        	//get the user's choice to borrow or return a tool
        	choice = borrowReturn();
        	
        	//process the user's choice
        	if(choice.equals("b")) {
        		
        		//attempt to borrow a tool and get the updated shelf
        		currentToolShelf = borrowTool(validTools, currentToolShelf);
        		
        		
        	}else if(choice.equals("r")){
        		
        		currentToolShelf = returnTool(validTools, currentToolShelf);
        		
        		
        	}else {
        		System.out.println("No borrow, no return?");
        	}
        	
        	choice = playAgain();

        }while(choice.equalsIgnoreCase("y"));
    }
    
    
    public static String borrowReturn() {
    	System.out.println("Do you want to borrow (b) or return (r) a tool, any other key to quit:");
    	String answer = kbin.nextLine();
    	return answer;
    }
    
    public static String playAgain() {
    	System.out.println("Play again (y), any other key to quit:");
    	String answer = kbin.nextLine();
    	return answer;
    }
    
    public static String[] borrowTool(String[] validTools, String[] shelf) {
    	System.out.println("Which tool do you want to borrow?");
    	String tool = kbin.nextLine();
    	
    	//is tool a valid tool and is it currently available?
    	if(valid(validTools, tool) && available(shelf, tool)) {
    		//search for tool on shelf and replace with null
    		for(int i = 0; i<shelf.length; i++) {
    			if(shelf[i].equals(tool)) {
    				shelf[i] = null;
    				System.out.println(tool+" borrowed!");
    			}
    		}
    	}else {
    		System.out.println("That tool cannot be borrowed. It is invalid, or already taken.");
    	}
    	return shelf;
    }
    
    public static boolean valid(String[] vTools, String t) {
    	//search through the valid set of tool names
    	for(int i = 0; i<vTools.length; i++) {
    		if(vTools[i].equals(t)) {
    			return true;
    		}
    	}
    	return false;
    }
    
    public static boolean available(String[] toolShelf, String tool) {
    	//see if the tool is in the current shelf
    	for(int i = 0; i<toolShelf.length; i++) {
    		if(toolShelf[i]!=null && toolShelf[i].equals(tool)) {
    			return true;
    		}
    	}
    	
    	return false;
    }
    
    /*This function asks the user to input the name of the tool to be returned. If there is an empty space on the
     * shelf and the name is valid, the tool is returned to the shelf at any empty space*/
    public static String[] returnTool(String[] vTools, String[] shelf){
    	System.out.println("Which tool do you want to return?");
    	String tool = kbin.nextLine();
    	
        if(!emptySpace(shelf)) {
        	System.out.println("There are no tools to return."); //ie there are no empty spaces
        }else if(valid(vTools, tool)&&!available(shelf, tool)){
        	//search for an empty space
        	int emptyIndex = 0;
        	while(shelf[emptyIndex]!=null) {
        		emptyIndex = emptyIndex + 1;
        	}
        	//assign the tool to the empty space
        	shelf[emptyIndex] = tool;
        	System.out.println(tool+" returned!");
        }else {
        	System.out.println("That tool cannot be returned! It may be invalid or not borrowed yet.");
        }
        
        return shelf;

    }
    
   
    public static boolean emptySpace(String[] shelf){

        for(int i = 0; i<shelf.length; i++) {
            for(int j = 0; j<shelf.length; j++) {
                if(shelf[i] == null) {
                    return true;
                }
            }
        }
        return false;
    }
    
    public static void displayShelf(String[] shelf) {

        System.out.println("------Current Shelf------");
        //loop through toolSet and display all tools
        for(int i = 0; i<shelf.length; i++) {
            System.out.print(shelf[i]+"\t");
        }
        System.out.println();
        System.out.println("----------------------");
    }

    
}