/**
 * An address book Controller - MVC pattern
 *    
 * Instantiate a View object and a Model object 
 * Call the view object getUserInput()  
 * Loop through the found entries, count them and produce success/fail message
 * Call the appropriate view object display method  
 */

public class AddrBookControl
{
    private String view;                        //The UI type '0' cmdLine, or '1','2','3' for swing
    private String model;                       //The data source 'file', 'db' or 'xml'

    private AddrBookView  v;                    //a view object
    private AddrBookModel m;                    //a model object

    private AddrBookEntry[] found;              //A reference to the array of found entries
    private int    numMatches;                  //Number of matches
    private String message;                     //Client message

    AddrBookControl(String view, String model)  //constructor
    {
        this.view  = view;
        this.model = model;
        
        start();
    }
    
    void start()                                //start the application
    {        
        startClientInterface();
        startDataModel();
        
        v.getUserInput();                       //call the view getUserInput() method
    }

    private void startClientInterface()         //start the "view" client interface                          
    {
        if (view.equals("0"))
            v = new AddrBookView0(this);        //create a client interface, and pass it the control object 
        if (view.equals("1"))
            v = new AddrBookView1(this);          
        if (view.equals("2"))
            v = new AddrBookView2(this);      
        if (view.equals("3"))
            v = new AddrBookView3(this);      
        if (view.equals("4"))
            v = new AddrBookView4(this);      
    }

    private void startDataModel()               //start the "model" data source                          
    {
        if (model.equals("file"))
            m = new AddrBookModelFile(this);    //create a file model object, and pass it the control object 
        if (model.equals("db"))
            m = new AddrBookModelDB(this);     
        if (model.equals("xml"))
            m = new AddrBookModelXML(this);   
    }

    void search(String lastname)                //called by the view object                                             
    {
        found = m.search(lastname);             //call the appropriate model object method

        numMatches = 0;                            
        for (AddrBookEntry entry : found)       //loop through the matches
            if(entry != null)
                numMatches++;
                                             
        if (numMatches > 0)
            message = numMatches + " " + lastname + " were found";
        else 
            message = " Name " + lastname + " was not found";

        display();
    }

    private void display()                                               
    {
        v.display(found, numMatches, message);    //call the appropriate view object to display
    }
}