📄 brokermodelnwimpl.java
字号:
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
*/
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;
}
/**-------------------------------------------------------------
* 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;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -