⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 brokerviewimpl.java

📁 this is a trade sale system realized by java. It can run some easy functions and has a good design p
💻 JAVA
字号:
package trader;
import trader.gui.BrokerGui;
import java.util.*;
import java.io.Serializable;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class BrokerViewImpl implements BrokerView, Serializable{
  private transient BrokerGui gui;
  private BrokerModel brokerModel;
  private ArrayList brokerControllers  = new ArrayList(10);
  
  // Important note - For mod05_view exercises you are supplied
  // with the BrokerGui class. You should treat the GUI as a 
  // blackbox. You should focus on the services it provides and
  // not on how it provides the services. For the API (public 
  // methods) of the BrokerGui class, refer to Module 5 of 
  // the student guide. 

  public BrokerViewImpl(BrokerModel model) {
    System.out.println("Creating BrokerViewImpl");
    try {
      //** 1 Assign model to the attribute brokerModel
      brokerModel = model;
      //** 2 Invoke the addChangeListener method with this
      //     instance of BrokerViewImpl as the input parameter
      model.addChangeListener(this);
    } catch (Exception e) {
      System.out.println("BrokerViewImpl constructor " + e);
    }
    // Create and assign a BrokerGui object to gui 
    gui = new BrokerGui();
    // Pass the array selectionPanelListeners containing the
    // event listener objects that handle button clicks on
    // the selection panel of the gui. The array 
    // selectionPanelListeners is declared later in this class.
    // The buttons on the selection panel of the gui are
    // "Customer Details", "Portfolio", "All Customers" and
    // "Stocks".
    gui.addSelectionPanelListeners(selectionPanelListeners);
    // Pass the array custPanelListeners containing the
    // event listener objects that handle button clicks on
    // the customer details panel of the gui. The array 
    // custPanelListeners is declared later in this class.
    // The buttons on the customer details panel of the gui are
    // "Get Customer", "Add Customer", "Update Customer" and
    // "Delete Customer".
    gui.addCustPanelListeners(custPanelListeners);
  }
      
//user gesture listener registration methods
  /* ---------------------------------------------------------------
   * adds requester to the list of objects to be notified of user 
   * gestures entered through a user interface such as a GUI.
   * User getsures for the customer segment are add, delete, update
   * get and getAll customers. There are similar user gestures for 
   * portfolio and stock segments
   */
  public void addUserGestureListener(BrokerController b)
    throws BrokerException {
    System.out.println("BrokerViewImpl.addUserGestureListener " +b);
    //** 1 add b to brokerControllers using the add method   
    brokerControllers.add(b);
  }

//display selection request service methods  
  /* ------------------------------------------------------------
   * shows the display page specified by the broker controller
   */
  public void showDisplay(Object display) throws BrokerException {
    System.out.println("BrokerViewImpl.showDisplay " + display);
    // General hints: Use the instanceof operator to determine
    // the display parameter variable's class type.
    //** 1 If display variable's the class type is Customer then
    //**   1.1 Invoke refreshCustPan method on the gui passing
    //**       display as the invoked methods input parameter.
    //**       Note a cast of display to Customer type may be
    //**       required.
    //**   1.2 Invoke the showCard method on the gui passing the
    //**       string "customer"  as the invoked methods input 
    //**       parameter.
    if (display instanceof Customer) {
      gui.refreshCustPan((Customer) display);
      gui.showCard("customer");
    }
    //** 2 If display variable's the class type is Customer[] then
    //**   2.1 Invoke refreshAllCustPan method on the gui passing
    //**       display as the invoked methods input parameter.
    //**       Note a cast of display to Customer[] type may be
    //**       required.
    //**   2.2 Invoke the showCard method on the gui passing the
    //**       string "allcustomers"  as the invoked methods input 
    //**       parameter.
    if (display instanceof Customer[]) {
      gui.refreshAllCustPan((Customer[])display);
      gui.showCard("allcustomers");
    }
    if (display instanceof Portfolio) {
      //TBD
    }
    if (display instanceof Stock[]) {
      //TBD
    }    
    //** 3 If display variable's the class type is Exception then
    //**   3.1 Invoke updateLog method on the gui passing
    //**       display.toString() as the invoked methods input
    //**       parameter.
    if (display instanceof Exception) {
      // One way to show exceptions 
      gui.updateLog(display.toString());
    }    
  }
  
// iteration 1 Customer segment broker view methods
  /* ---------------------------------------------------------------
   * callback method to handle customer state change notification
   * from the broker model
   */
  public void handleCustomerChange(Customer cust)
    throws BrokerException{
    System.out.println("BrokerViewImpl.processCustomer " + cust);
    // get the cust id currently on the customer details display
    String cId;
    //** 1 Assign cId to customer id on the gui's customer details
    //**   panel. Use the gui's getCustIdOnCustPan() method.
    cId = gui.getCustIdOnCustPan();
    // refresh customer details panel if required
    //** 2 Use an if statement to test if cust.getId().equals(cId)
    //**   is true. 
    //** 2.1 If true:
    //**     Invoke refreshCustPan method on the gui passing
    //**     cust as the invoked methods input parameter. 
    if (cust.getId().equals(cId)) { 
        gui.refreshCustPan(cust);
    }
    
    // refresh all customers Panel
    try {
      Customer custs[];
      //** 1 Assign custs with the Customer[] returned by invoking
      //**   the getAllCustomers() on the attribute brokerModel.
      custs = brokerModel.getAllCustomers();
      //** 2 Invoke the refreshAllCustPan on gui with custs as
      //**   the refreshAllCustPan methods input parameter.
      gui.refreshAllCustPan(custs);
    } catch (Exception e) {
      System.out.println("BrokerViewImpl processCustomer " + e);
    }
  }


//event handler methods--------------------------------------
  // Following is an anonymous inner class declaration
  // The attribute custGetHandler is registered as the action
  // event listener with the "Get Customer" button of the gui,
  // by the constructor of this class.
  transient ActionListener custGetHandler =
    new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        // This method is called when the "Get Customer" button
        // is clicked by the user
        System.out.println("BrokerViewImpl: custGetHandler");
        BrokerController bc;
        String custId;
        //  Assign custId with the value of the customer id on
        //  the gui. Use the getCustIdOnCustPan() method of gui
        custId = gui.getCustIdOnCustPan();
        // Create a for loop: 
        // For every object in the ArrayList brokerControllers:
        // -Use get method to get the object and assign to bc.
        // -Invoke the handleGetCustomerGesture method on bc.
        for (int i=0; i<brokerControllers.size(); i++) {
          bc = (BrokerController) brokerControllers.get(i);
          bc.handleGetCustomerGesture(custId);
        }
      }
    };

  // Following is an anonymous inner class declaration
  // The attribute custAddHandler is registered as the action
  // event listener with the "Add Customer" button of the gui,
  // by the constructor of this class.
  transient ActionListener custAddHandler =
    new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        // This method is called when the "Add Customer" button
        // is clicked by the user
        System.out.println("BrokerViewImpl: custAddHandler");
        BrokerController bc;
        Customer cust;
        //** 1 Assign to cust a customer object that represents
        //**   the customer information on the gui. To get this 
        //**   object use the getCustomerOnCustPan() method of gui
        cust = gui.getCustomerOnCustPan();
        //** 2 Create a for loop: 
        //**   Hint: See actionPerformed method of the
        //**         custGetHandler object.
        //**   Invoke the handleAddCustomerGesture method on 
        //**   every object in the ArrayList brokerControllers.
        for (int i=0; i<brokerControllers.size(); i++) {
          bc = (BrokerController) brokerControllers.get(i);
          bc.handleAddCustomerGesture(cust);
        }
      }
    };

  // Following is an anonymous inner class declaration
  // The attribute custDeleteHandler is registered as the action
  // event listener with the "Delete Customer" button of the 
  // gui, by the constructor of this class.
  transient  ActionListener custDeleteHandler =
    new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        // This method is called when the "Delete Customer"
        //  button is clicked by the user
        System.out.println("BrokerViewImpl: custDeleteHandler");
        BrokerController bc;
        Customer cust;
        //** 1 Assign to cust a customer object that represents
        //**   the customer information on the gui. To get this 
        //**   object Use the getCustomerOnCustPan() method of gui.
        cust = gui.getCustomerOnCustPan();
        //** 2 Create a for loop: 
        //**   Hint: See actionPerformed method of the
        //**         custGetHandler object.
        //**   Invoke the handleDeleteCustomerGesture method on 
        //**   every object in the ArrayList brokerControllers.
        for (int i=0; i<brokerControllers.size(); i++) {
          bc = (BrokerController) brokerControllers.get(i);
          bc.handleDeleteCustomerGesture(cust);
        }
      }
    };

  // Following is an anonymous inner class declaration
  // The attribute custUpdateHandler is registered as the action
  // event listener with the "Update Customer" button of the 
  // gui, by the constructor of this class.
  transient  ActionListener custUpdateHandler =
    new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        System.out.println("BrokerViewImpl: custUpdateHandler");
        BrokerController bc;
        Customer cust;
        //** 1 Assign to cust a customer object that represents
        //**   the customer information on the gui. To get this 
        //**   object Use the getCustOnCustPan() method of gui.
        cust = gui.getCustomerOnCustPan();
        //** 2 Create a for loop: 
        //**   Hint: See actionPerformed method of the
        //**         custGetHandler object.
        //**   Invoke the handleUpdateCustomerGesture method on 
        //**   every object in the ArrayList brokerControllers.
        for (int i=0; i<brokerControllers.size(); i++) {
          bc = (BrokerController) brokerControllers.get(i);
          bc.handleUpdateCustomerGesture(cust);
        }
      }
    };

  // Following is an anonymous inner class declaration
  // The attribute custDetailsPageHandler is registered as the 
  // action event listener with the "Customer Details" button of  
  // the gui, by the constructor of this class.
  transient  ActionListener custDetailsPageHandler =
    new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        // This method is called when the "Customer Details" 
        // button is clicked by the user
        System.out.println("BrokerViewImpl: " +
          "custDetailsPageHandler");
        BrokerController bc;
        String custId;
        //** 1  Assign custId with the value of the customer id
        //** on the gui. Use getCustIdOnCustPan() method of gui.
        custId = gui.getCustIdOnCustPan();
        //** 2 Create a for loop: 
        //**   Hint: See actionPerformed method of the
        //**         custGetHandler object.
        //**   Invoke the handleGetCustomerGesture method on 
        //**   every object in the ArrayList brokerControllers.
        for (int i=0; i<brokerControllers.size(); i++) {
          bc = (BrokerController) brokerControllers.get(i);
          bc.handleGetCustomerGesture(custId);
        }
      }
    };

  // Following is an anonymous inner class declaration
  // The attribute allCustsPageHandler is registered as the action
  // event listener with the "All Customers" button of the 
  // gui, by the constructor of this class.
  transient ActionListener allCustsPageHandler =
    new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        // This method is called when the "All Customers" 
        // button is clicked by the user
        System.out.println("BrokerViewImpl: "
          + "allCustsPageHandler");
        //showCard("allcustomers");
        BrokerController bc;
        //** 1 Create a for loop: 
        //**   Hint: See actionPerformed method of the
        //**         custGetHandler object.
        //**   Invoke the handleGetAllCustomersGesture method on 
        //**   every object in the ArrayList brokerControllers.
        for (int i=0; i<brokerControllers.size(); i++) {
          bc = (BrokerController) brokerControllers.get(i);
          bc.handleGetAllCustomersGesture();
        }
      }
    };

  // No action required for this method
  // This method supports the showing of the portfolio page.
  // The portfolio page is a final iteration action item.
  transient ActionListener portfolioPageHandler =
    new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        System.out.println("BrokerViewImpl: portfolioPageHandler");
      }
    };

  // No action required for this method
  // This method supports the showing of the stock page.
  // The stock page is a final iteration action item.
  transient ActionListener stockPageHandler =
    new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        System.out.println("BrokerViewImpl: stockPageHandler");
      }
    };

  transient ActionListener custPanelListeners[] = {custGetHandler,
    custAddHandler, custDeleteHandler, custUpdateHandler};

  transient ActionListener selectionPanelListeners[] = {
    custDetailsPageHandler, allCustsPageHandler, portfolioPageHandler,
    stockPageHandler};

  public static void main(String args[]){
    try {
      BrokerModel model = new trader.db.BrokerModelDbImpl("localhost");
      BrokerViewImpl view = new BrokerViewImpl(model);
    } catch (Exception e) {
      System.out.println(e.toString());
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -