📄 ch09.htm
字号:
<TT>17: myName(strdup(name)),</TT><TT>18: mySocialSecurityNumber(strdup(socialSecurityNumber)),</TT><TT>19: myAddress(strdup(address)),</TT><TT>20: myMothersMaidenName(strdup(mothersMaidenName)),</TT><TT>21: myAccounts(), myATMCards() {</TT><TT>22: </TT><TT>23: }</TT><TT>24: </TT><TT>25: // Default constructor.</TT><TT>26: CustomerImpl::CustomerImpl() : myName(NULL),</TT><TT>27: mySocialSecurityNumber(NULL), myAddress(NULL),</TT><TT>28: myMothersMaidenName(NULL), myAccounts(), myATMCards() {</TT><TT>29: </TT><TT>30: }</TT><TT>31: </TT><TT>32: // Destructor.</TT><TT>33: CustomerImpl::~CustomerImpl() {</TT><TT>34: </TT><TT>35: free(myName);</TT><TT>36: free(mySocialSecurityNumber);</TT><TT>37: free(myAddress);</TT><TT>38: free(myMothersMaidenName);</TT><TT>39: }</TT><TT>40: </TT><TT>41: char* CustomerImpl::name() {</TT><TT>42: </TT><TT>43: return CORBA::strdup(myName);</TT><TT>44: }</TT><TT>45: </TT><TT>46: void CustomerImpl::name(const char* val) {</TT><TT>47: </TT><TT>48: free(myName);</TT><TT>49: myName = strdup(val);</TT><TT>50: }</TT><TT>51: </TT><TT>52: char* CustomerImpl::socialSecurityNumber() {</TT><TT>53: </TT><TT>54: return CORBA::strdup(mySocialSecurityNumber);</TT><TT>55: }</TT><TT>56: </TT><TT>57: char* CustomerImpl::address() {</TT><TT>58: </TT><TT>59: return CORBA::strdup(myAddress);</TT><TT>60: }</TT><TT>61: </TT><TT>62: void CustomerImpl::address(const char* val) {</TT><TT>63: </TT><TT>64: free(myAddress);</TT><TT>65: myAddress = strdup(val);</TT><TT>66: }</TT><TT>67: </TT><TT>68: char* CustomerImpl::mothersMaidenName() {</TT><TT>69: </TT><TT>70: return CORBA::strdup(myMothersMaidenName);</TT><TT>71: }</TT><TT>72: </TT><TT>73: AccountList* CustomerImpl::getAccounts() {</TT><TT>74: </TT><TT>75: return &myAccounts;</TT><TT>76: }</TT><TT>77: </TT><B><TT>78: void CustomerImpl::updateAccountBalance(Account_ptr account,</TT></B><B><TT>79: CORBA::Float balance) {</TT></B><B><TT>80: </TT></B><B><TT>81: cout << "CustomerImpl: Received account update:" << endl <<</TT></B><B><TT>82: " New balance is $" << balance << endl;</TT></B><TT>83: }</TT></FONT></PRE><H3><A NAME="Heading8"></A><FONT COLOR="#000077">Enhancing the ATMClient</FONT></H3><P>The modifications to <TT>ATMClientMain.cpp</TT> are easy to follow (see Listing9.7). The only additions are that the <TT>ATMClient</TT> now requests the accountupdate service from the <TT>Bank</TT> when the <TT>Account</TT> is created, and whenthe <TT>ATMClient</TT> is finished, it waits for two minutes to give the <TT>Bank</TT>a chance to call <TT>updateAccountBalance()</TT> once or twice before the <TT>ATMClient</TT>exits. (Like <TT>BankImpl</TT>, <TT>ATMClient</TT> uses the Win32 API to cause thecurrent thread to sleep; again, non-Windows developers need to substitute the appropriatemethod call here.)<H4><FONT COLOR="#000077">Listing 9.7. ATMClientMain.cpp.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // ATMClientMain.cpp</TT><TT> 2: </TT><TT> 3: #include <iostream.h></TT><TT> 4: #include <stdlib.h></TT><TT> 5: </TT><TT> 6: #include "../Customer/CustomerImpl.h"</TT><TT> 7: </TT><TT> 8: #include "../Bank_c.h"</TT><TT> 9: #include "../BankServer_c.h"</TT><TT> 10: #include "../ATM_c.h"</TT><TT> 11: </TT><TT> 12: int main(int argc, char *const *argv) {</TT><TT> 13: </TT><TT> 14: // Check the number of arguments; there should be exactly five</TT><TT> 15: // (six counting the executable name itself).</TT><TT> 16: if (argc != 6) {</TT><TT> 17: cout << "Usage: ATMClient <name> <social security number>"</TT><TT> 18: " <address> <mother's maiden name> <PIN>" << endl;</TT><TT> 19: return 1;</TT><TT> 20: }</TT><TT> 21: </TT><TT> 22: // Assign the command line arguments to the Customer attributes.</TT><TT> 23: const char* name = argv[1];</TT><TT> 24: const char* socialSecurityNumber = argv[2];</TT><TT> 25: const char* address = argv[3];</TT><TT> 26: const char* mothersMaidenName = argv[4];</TT><TT> 27: CORBA::Short pin = atoi(argv[5]);</TT><TT> 28: </TT><TT> 29: // Initialize the ORB and BOA.</TT><TT> 30: CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);</TT><TT> 31: CORBA::BOA_var boa = orb->BOA_init(argc, argv);</TT><TT> 32: </TT><TT> 33: // Create a Customer object.</TT><TT> 34: cout << "ATMClient: Creating new Customer:" << endl;</TT><TT> 35: cout << " name: " << name << endl;</TT><TT> 36: cout << " Social Security number: " << socialSecurityNumber <<</TT><TT> 37: endl;</TT><TT> 38: cout << " address: " << address << endl;</TT><TT> 39: cout << " mother's maiden name: " << mothersMaidenName << endl;</TT><TT> 40: CustomerImpl customer(name, socialSecurityNumber, address,</TT><TT> 41: mothersMaidenName);</TT><TT> 42: </TT><TT> 43: // Notify the BOA that the CustomerImpl object is ready.</TT><TT> 44: boa->obj_is_ready(&customer);</TT><TT> 45: </TT><TT> 46: // Locate a BankServer object and try to get a list of Banks</TT><TT> 47: // and ATMs from it.</TT><TT> 48: BankServer_var bankServer;</TT><TT> 49: try {</TT><TT> 50: bankServer = BankServer::_bind();</TT><TT> 51: } catch (const CORBA::Exception& ex) {</TT><TT> 52: </TT><TT> 53: // The bind attempt failed...</TT><TT> 54: cout << "ATMClient: Unable to bind to a BankServer:" <<</TT><TT> 55: endl;</TT><TT> 56: cout << ex << endl;</TT><TT> 57: return 1;</TT><TT> 58: }</TT><TT> 59: cout << "ATMClient: Successfully bound to a BankServer." <<</TT><TT> 60: endl;</TT><TT> 61: </TT><TT> 62: BankList_ptr banks;</TT><TT> 63: ATMList_ptr ATMs;</TT><TT> 64: </TT><TT> 65: try {</TT><TT> 66: banks = bankServer->getBanks();</TT><TT> 67: ATMs = bankServer->getATMs();</TT><TT> 68: } catch (const CORBA::Exception& ex) {</TT><TT> 69: </TT><TT> 70: // The attempt failed...</TT><TT> 71: cout << "ATMClient: Unable to get lists of Banks and ATMs:"</TT><TT> 72: << endl;</TT><TT> 73: cout << ex << endl;</TT><TT> 74: return 1;</TT><TT> 75: }</TT><TT> 76: </TT><TT> 77: // Use the first Bank and the first ATM that appear in the</TT><TT> 78: // lists.</TT><TT> 79: if (banks->length() == 0) {</TT><TT> 80: </TT><TT> 81: // No Banks were available.</TT><TT> 82: cout << "ATMClient: No Banks available." << endl;</TT><TT> 83: return 1;</TT><TT> 84: }</TT><TT> 85: if (ATMs->length() == 0) {</TT><TT> 86: </TT><TT> 87: // No ATMs were available.</TT><TT> 88: cout << "ATMClient: No ATMs available." << endl;</TT><TT> 89: return 1;</TT><TT> 90: }</TT><TT> 91: Bank_var bank = (*banks)[0];</TT><TT> 92: ATM_var atm = (*ATMs)[0];</TT><TT> 93: cout << "ATMClient: Using Bank \"" << bank->name() << "\" and"</TT><TT> 94: << " ATM \"" << atm->name() << "\"." << endl;</TT><TT> 95: </TT><TT> 96: // Do some cool stuff.</TT><TT> 97: </TT><TT> 98: Account_var account;</TT><TT> 99: ATMCard_var atmCard;</TT><TT>100: </TT><TT>101: try {</TT><TT>102: account = bank->createAccount(&customer, "checking", 0.0);</TT><TT>103: } catch (const CORBA::Exception& ex) {</TT><TT>104: </TT><TT>105: // The createAccount() attempt failed...</TT><TT>106: cout << "ATMClient: Unable to create Account." << endl;</TT><TT>107: cout << ex << endl;</TT><TT>108: return 1;</TT><TT>109: }</TT><TT>110: </TT><B><TT>111: try {</TT></B><B><TT>112: </TT></B><B><TT>113: // Request the automatic account update service from the</TT></B><B><TT>114: // Bank.</TT></B><B><TT>115: bank->requestUpdateService(account);</TT></B><B><TT>116: } catch (const CORBA::Exception& ex) {</TT></B><B><TT>117: </TT></B><B><TT>118: // The requestUpdateService() attempt failed...</TT></B><B><TT>119: cout << "ATMClient: Unable to create Account." << endl;</TT></B><B><TT>120: cout << ex << endl;</TT></B><B><TT>121: return 1;</TT></B><B><TT>122: }</TT></B><TT>123: </TT><TT>124: try {</TT><TT>125: </TT><TT>126: // Print out some Account statistics.</TT><TT>127: cout << "ATMClient: Opened new Account:" << endl;</TT><TT>128: cout << " account number: " << account->accountNumber() <<</TT><TT>129: endl;</TT><TT>130: cout << " creation date: " << account->creationDate() <<</TT><TT>131: endl;</TT><TT>132: cout << " account balance: " << account->balance() << endl;</TT><TT>133: </TT><TT>134: // Ask the Bank to issue an ATMCard for the newly-created</TT><TT>135: // Account.</TT><TT>136: cout << "ATMClient: Getting ATMCard from Bank." << endl;</TT><TT>137: try {</TT><TT>138: atmCard = bank->issueATMCard(pin, account);</TT><TT>139: } catch (const InvalidAccountException&) {</TT><TT>140: </TT><TT>141: // For some reason, the Account was invalid (this</TT><TT>142: // shouldn't happen).</TT><TT>143: cout << "ATMClient: Exception caught: Invalid Account"</TT><TT>144: << endl;</TT><TT>145: return 1;</TT><TT>146: }</TT><TT>147: </TT><TT>148: // Perform some transactions on the Account through the</TT><TT>149: // ATM.</TT><TT>150: cout << "ATMClient: Performing transactions." << endl;</TT><TT>151: try {</TT><TT>152: cout << " Depositing $250.00..." << endl;</TT><TT>153: cout << " New balance is $" << atm->deposit(atmCard,</TT><TT>154: account, pin, 250.00) << endl;</TT><TT>155: </TT><TT>156: // This will throw an exception since we're trying to</TT><TT>157: // withdraw too much.</TT><TT>158: cout << " Withdrawing $500.00..." << endl;</TT><TT>159: cout << " New balance is $" << atm->withdraw(atmCard,</TT><TT>160: account, pin, 500.00) << endl;</TT><TT>161: } catch (AuthorizationException&) {</TT><TT>162: cout << "ATMClient: Exception caught: Invalid PIN or "</TT><TT>163: << "No authorization (as expected)" << endl;</TT><TT>164: } catch (InvalidAmountException&) {</TT><TT>165: cout << "ATMClient: Exception caught: Invalid amount"</TT><TT>166: << endl;</TT><TT>167: } catch (InsufficientFundsException&) {</TT><TT>168: cout << "ATMClient: Exception caught: Insufficient " <<</TT><TT>169: "funds" << endl;</TT><TT>170: }</TT><TT>171: </TT><TT>172: // Perform some more transactions on the Account through</TT><TT>173: // the ATM.</TT><TT>174: cout << "ATMClient: Performing more transactions." << endl;</TT><TT>175: try {</TT><TT>176: cout << " Depositing $500.00..." << endl;</TT><TT>177: cout << " New balance is $" <<</TT><TT>178: atm->deposit(atmCard, account, pin, 500.00) <<</TT><TT>179: endl;</TT><TT>180: </TT><TT>181: // This will throw an exception since we're using the</TT><TT>182: // wrong PIN.</TT><TT>183: cout << " Withdrawing $250.00 with incorrect PIN..."</TT><TT>184: << endl;</TT><TT>185: cout << " New balance is $" << atm->withdraw(atmCard,</TT>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -