/**
 * An address book View - MVC pattern
 *    
 * getUserInput()
 *     Open an input stream reader for (System.in), and buffer it
 *     Get data from user entry
 *     If nothing is entered, exit
 *     Call the controller search method 
 * display()
 *     Get results and display to the screen
 */
import java.io.*;

public class AddrBookView0 implements AddrBookView 
{
    private AddrBookControl control;            //A reference to the control object
    private String searchStr;                   //The last name to search for

    
    AddrBookView0(AddrBookControl c)            //constructor
    {
        this.control = c;
    }
        
    public void getUserInput()          //Called by controller object            
    {
        try
        {
            InputStreamReader keyboard    = new InputStreamReader(System.in);   
            BufferedReader    keyboardBuf = new BufferedReader(keyboard);

            while(true)
            {
                System.out.println();
                System.out.print("Enter (partial) last name: ");

                String lname = keyboardBuf.readLine();       //read a line

                if (lname.equals(""))            
                    System.exit(0);

                control.search(lname);              //call the controller search method
            }
        }
        catch (IOException e)
        {
            System.out.println(e);
        }
    }


    public void display(AddrBookEntry[] entry, int numMatches, String msg)    //Called by the controller object
    {       
        String str = msg + "\n";

        if (numMatches == 1)            //if one entry was found
            str = "";               //no need to display number of matches
                
        for (int i=0; i<numMatches; i++)        //loop and create a proper display
            str += entry[i] + "\n";
                    
        System.out.println(str);            //print the results                     
    }
}