brokermodelnwimpl.java

来自「java写的股票交易系统」· Java 代码 · 共 279 行

JAVA
279
字号
package trader.nw;
import trader.*;
import java.util.*;

public class BrokerModelNwImpl implements BrokerModel {
  // ArrayList changeListeners = new ArrayList(10);
  NwClient nwClient;

  /** Creates new BrokerNwImpl */
  public BrokerModelNwImpl(NwClient nwClient) {
    //** 1 Assign the parameter variable nwClient to
    //**   the attribute nwClient
    //**   Hint use this.nwClient
    this.nwClient = nwClient;
  }

// Broker model state change listener registration methods
  /**-----------------------------------------------------------
   * Adds requester to the list of objects to be notified when an
   * object(Customer, Portfolio or Stock) in the broker model 
   * alters state   
   */
  public void addChangeListener(BrokerView bv)
    throws BrokerException {
    //** 1 Leave this method empty for now.   
    //     It will be discussed later during the Advance Multi-Tier
    //     module
  }

// Iteration 1 Customer segment broker model methods
// Customer segment state change methods
  /**----------------------------------------------------------
   * Adds the Customer to the broker model 
   */ 
  public void addCustomer(Customer cust) 
    throws BrokerException {
    Command cmd;
    Object result;
    try {
      cmd = new AddCustomerCommand(cust);
      nwClient.send(cmd);
      cmd = (Command) nwClient.receive();
      result = cmd.getResult();
    } catch(Exception e) {
      throw new BrokerException(e.toString());
    }
  }

  /**-------------------------------------------------------------
   * deletes the customer from the broker model
   */
  public void deleteCustomer(Customer cust)
    throws BrokerException{
    Command cmd;
    Object result;
    try {
      //** 1 Create a DeleteCustomerCommand using cust and
      //**   assign it to cmd
      cmd = new DeleteCustomerCommand(cust);
      //** 2, 3, 4 Remaining 3 lines of code are identical to
      //**         the addCustomer method
      nwClient.send(cmd);
      cmd = (Command) nwClient.receive();
      result = cmd.getResult();
    } catch(Exception e) {
      throw new BrokerException(e.toString());
    }
  }

  /**-------------------------------------------------------------
   * Updates the customer in the broker model
   */
  public void updateCustomer(Customer cust)
    throws BrokerException {
    Command cmd;
    Object result;
    try {
      //** 1 Create a UpdateCustomerCommand using cust and
      //**   assign it to cmd
      cmd = new UpdateCustomerCommand(cust);
      //** 2, 3, 4 Remaining 3 lines of code are identical to
      //**         the addCustomer method
      nwClient.send(cmd);
      cmd = (Command) nwClient.receive();
      result = cmd.getResult();
    } catch(Exception e) {
      throw new BrokerException(e.toString());
    }
  }
  
// Customer segment state query methods
  /**-------------------------------------------------------------
   * Given an id, returns the Customer from the model
   */
   //modified  by  ourteam 051228
   //begin
   
  /*public Customer getCustomer(String id)
    throws BrokerException {
    Command cmd;
    Object result;
    Customer cust = null;
    try {
      //** 1 Create a GetCustomerCommand using id and
      //**   assign it to cmd
      cmd = new GetCustomerCommand(id);
      //** 2, 3, 4 Remaining 3 lines of code are identical to
      //**         the addCustomer method 
      nwClient.send(cmd);
      cmd = (Command) nwClient.receive();
      result =  cmd.getResult();
      //** 5 cast result to Customer and assign to cust
      cust = (Customer) result;
    } catch(Exception e) {
      throw new BrokerException(e.toString());
    }
    return cust;
  }*/
  
  //modified by Crane 
  public Customer getCustomer(String id,String name,String addr)
    throws BrokerException {
    Command cmd;
    Object result;
    Customer cust = null;
    try {
      //** 1 Create a GetCustomerCommand using id and
      //**   assign it to cmd
      cmd = new GetCustomerCommand(id,name,addr);
      //** 2, 3, 4 Remaining 3 lines of code are identical to
      //**         the addCustomer method 
      nwClient.send(cmd);
      cmd = (Command) nwClient.receive();
      result =  cmd.getResult();
      //** 5 cast result to Customer and assign to cust
      cust = (Customer) result;
    } catch(Exception e) {
      throw new BrokerException(e.toString());
    }
    return cust;
  }
  //end
  
  //modified  by  ourteam 051228
  //begin
  public Portfolio getPortfolio(String id,String name,Share[] shares)
    throws BrokerException {
    Command cmd;
    Object result;
    Portfolio port = null;
    try {
      //** 1 Create a GetCustomerCommand using id and
      //**   assign it to cmd
      cmd = new GetPortfolioCommand(id,name,shares);
      //** 2, 3, 4 Remaining 3 lines of code are identical to
      //**         the addCustomer method 
      nwClient.send(cmd);
      cmd = (Command) nwClient.receive();
      result =  cmd.getResult();
      //** 5 cast result to Customer and assign to cust
      port = (Portfolio) result;
    } catch(Exception e) {
      throw new BrokerException(e.toString());
    }
    return port;
  }
  
  public void addPortfolio(Portfolio port) 
    throws BrokerException {
    Command cmd;
    Object result;
    try {
      cmd = new AddPortfolioCommand(port);
      nwClient.send(cmd);
      cmd = (Command) nwClient.receive();
      result = cmd.getResult();
    } catch(Exception e) {
      throw new BrokerException(e.toString());
    }
  }

  /**-------------------------------------------------------------
   * deletes the customer from the broker model
   */
  public void deletePortfolio(Portfolio port)
    throws BrokerException{
    Command cmd;
    Object result;
    try {
      //** 1 Create a DeleteCustomerCommand using cust and
      //**   assign it to cmd
      cmd = new DeletePortfolioCommand(port);
      //** 2, 3, 4 Remaining 3 lines of code are identical to
      //**         the addCustomer method
      nwClient.send(cmd);
      cmd = (Command) nwClient.receive();
      result = cmd.getResult();
    } catch(Exception e) {
      throw new BrokerException(e.toString());
    }
  }
  //end

  /**-------------------------------------------------------------
   * Returns all customers in the broker model
   */
  public Customer[] getAllCustomers()
    throws BrokerException{
    Command cmd;
    Object result;
    Customer[] customers = null;
    try {
      //** 1 Create a getAllCustomersCommand and
      //**   assign it to cmd
      cmd = new GetAllCustomersCommand();
      //** 2, 3, 4 Remaining 3 lines of code are identical to
      //**         the addCustomer method
      nwClient.send(cmd);
      cmd = (Command) nwClient.receive();
      result =  cmd.getResult();
      //** 5 cast result to Customer[] and assign to cust
      customers = (Customer[]) result;
    } catch(Exception e) {
      throw new BrokerException(e.toString());
    }
    return customers;
  }
  
  //modified  by  ourteam 051228
  //begin
  public Stock getStock(String symbol, String price)
    throws BrokerException {
    Command cmd;
    Object result;
    Stock cust = null;
    try {
      
      cmd = new GetStockCommand(symbol);
      //** 2, 3, 4 Remaining 3 lines of code are identical to
      //**         the addCustomer method 
      nwClient.send(cmd);
      cmd = (Command) nwClient.receive();
      result =  cmd.getResult();
      //** 5 cast result to Customer and assign to cust
      cust = (Stock) result;
    } catch(Exception e) {
      throw new BrokerException(e.toString());
    }
    return cust;	
    	
  }
  //end



//modified  by  ourteam 051228
//begin
public Stock[] getAllStocks()
    throws BrokerException{
    Command cmd;
    Object result;
    Stock[] stocks = null;
    try {
      
      cmd = new GetAllStocksCommand();
      //** 2, 3, 4 Remaining 3 lines of code are identical to
      //**         the addCustomer method
      nwClient.send(cmd);
      cmd = (Command) nwClient.receive();
      result =  cmd.getResult();
      //** 5 cast result to Customer[] and assign to cust
      stocks = (Stock[]) result;
    } catch(Exception e) {
      throw new BrokerException(e.toString());
    }
    return stocks;
  }
  //end
}

⌨️ 快捷键说明

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