📄 ch08.htm
字号:
<TT>61: };</TT><TT>62: 63: #endif</TT></FONT></PRE><H4><FONT COLOR="#000077">Listing 8.9. BankImpl.cpp.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // BankImpl.cpp</TT><TT> 2: </TT><TT> 3: #include "BankImpl.h"</TT><TT> 4: </TT><TT> 5: #include <time.h></TT><TT> 6: #include <string.h></TT><TT> 7: #include <iostream.h></TT><TT> 8: #include <algorithm></TT><TT> 9: #include <functional></TT><TT> 10: </TT><TT> 11: #include "SavingsAccountImpl.h"</TT><TT> 12: #include "CheckingAccountImpl.h"</TT><B><TT> 13: #include "ATMCardImpl.h"</TT></B><TT> 14: </TT><TT> 15: extern CORBA::BOA_var boa;</TT><TT> 16: </TT><TT> 17: // STL-derived unary function which returns TRUE if Accounts are</TT><TT> 18: // equal.</TT><TT> 19: class IsAccountEqual : public std::unary_function<Account_ptr,</TT><TT> 20: bool> {</TT><TT> 21: public:</TT><TT> 22: IsAccountEqual(argument_type account) { myAccount = account; }</TT><TT> 23: result_type operator()(argument_type account) { return account-></TT><TT> 24: _is_equivalent(myAccount) != 0; }</TT><TT> 25: private:</TT><TT> 26: argument_type myAccount;</TT><TT> 27: };</TT><TT> 28: </TT><TT> 29: // Constructor.</TT><TT> 30: //</TT><TT> 31: // name - This Bank's name.</TT><TT> 32: BankImpl::BankImpl(const char* name) : _sk_Bank(name), myAccounts(),</TT><TT> 33: myName(strdup(name)), myAddress(strdup("123 Elm Street, "</TT><TT> 34: "Anyware USA 12345")), myNextAccountNumber(0) {</TT><TT> 35: </TT><TT> 36: }</TT><TT> 37: </TT><TT> 38: // Default constructor.</TT><TT> 39: BankImpl::BankImpl() : myAccounts(), myName(NULL), myAddress(NULL),</TT><TT> 40: myNextAccountNumber(0) {</TT><TT> 41: </TT><TT> 42: }</TT><TT> 43: </TT><TT> 44: // Destructor.</TT><TT> 45: BankImpl::~BankImpl() {</TT><TT> 46: </TT><TT> 47: cout << "Bank \"" << name() << "\" being destroyed." << endl;</TT><TT> 48: free(myName);</TT><TT> 49: free(myAddress);</TT><TT> 50: }</TT><TT> 51: </TT><TT> 52: char* BankImpl::name() {</TT><TT> 53: </TT><TT> 54: return CORBA::strdup(myName);</TT><TT> 55: }</TT><TT> 56: </TT><TT> 57: void BankImpl::name(const char* val) {</TT><TT> 58: </TT><TT> 59: free(myName);</TT><TT> 60: myName = strdup(val);</TT><TT> 61: }</TT><TT> 62: </TT><TT> 63: char* BankImpl::address() {</TT><TT> 64: </TT><TT> 65: return CORBA::strdup(myAddress);</TT><TT> 66: }</TT><TT> 67: </TT><TT> 68: void BankImpl::address(const char* val) {</TT><TT> 69: </TT><TT> 70: free(myAddress);</TT><TT> 71: myAddress = strdup(val);</TT><TT> 72: }</TT><TT> 73: </TT><TT> 74: Account_ptr BankImpl::createAccount(Customer_ptr customer,</TT><TT> 75: const char* accountType, CORBA::Float openingBalance) {</TT><TT> 76: </TT><TT> 77: Account_ptr newAccount;</TT><TT> 78: </TT><TT> 79: if (strcmp(accountType, "savings") == 0) {</TT><TT> 80: </TT><TT> 81: // Create a new SavingsAccountImpl object for the Account.</TT><TT> 82: cout << "BankImpl: Creating new SavingsAccount for "</TT><TT> 83: "Customer " << customer->name() << "." << endl;</TT><TT> 84: newAccount = new SavingsAccountImpl(getNextAccountNumber(),</TT><TT> 85: getCurrentDate(), openingBalance, customer, 10.0);</TT><TT> 86: } else if (strcmp(accountType, "checking") == 0) {</TT><TT> 87: </TT><TT> 88: // Create a new CheckingAccountImpl object for the Account.</TT><TT> 89: cout << "BankImpl: Creating new CheckingAccount for "</TT><TT> 90: "Customer " << customer->name() << "." << endl;</TT><TT> 91: newAccount = new CheckingAccountImpl(getNextAccountNumber(),</TT><TT> 92: getCurrentDate(), openingBalance, customer);</TT><TT> 93: } else {</TT><TT> 94: </TT><TT> 95: // Invalid Account type; do nothing.</TT><TT> 96: cout << "BankImpl: Customer " << customer->name() <<</TT><TT> 97: " requested invalid Account type \"" << accountType</TT><TT> 98: << "\"." << endl;</TT><TT> 99: return Account::_nil();</TT><TT>100: }</TT><TT>101: </TT><TT>102: // Add the created Account at the end of the list and return it.</TT><TT>103: ::boa->obj_is_ready(newAccount);</TT><TT>104: myAccounts.push_back(Account::_duplicate(newAccount));</TT><TT>105: return newAccount;</TT><TT>106: }</TT><TT>107: </TT><TT>108: void BankImpl::deleteAccount(Account_ptr account) throw</TT><TT>109: (InvalidAccountException) {</TT><TT>110: </TT><TT>111: std::vector<Account_ptr>::iterator first = myAccounts.begin();</TT><TT>112: std::vector<Account_ptr>::iterator last = myAccounts.end();</TT><TT>113: IsAccountEqual predicate(account);</TT><TT>114: </TT><TT>115: std::vector<Account_ptr>::iterator matchedAccount = std::</TT><TT>116: find_if(first, last, predicate);</TT><TT>117: if (matchedAccount == last) {</TT><TT>118: </TT><TT>119: // Invalid Account; throw an exception.</TT><TT>120: cout << "BankImpl: Attempted to delete invalid Account." <<</TT><TT>121: endl;</TT><TT>122: throw InvalidAccountException();</TT><TT>123: }</TT><TT>124: cout << "BankImpl: Deleting Account \"" << account-></TT><TT>125: accountNumber() << "\"." << endl;</TT><TT>126: </TT><TT>127: // Delete the given Account.</TT><TT>128: myAccounts.erase(matchedAccount);</TT><TT>129: account->_release();</TT><TT>130: }</TT><TT>131: </TT><TT>132: AccountList* BankImpl::getAccounts() {</TT><TT>133: </TT><TT>134: AccountList* list = new AccountList(myAccounts.size());</TT><TT>135: CORBA::Long i;</TT><TT>136: </TT><TT>137: for (i = 0; i < myAccounts.size(); i++) {</TT><TT>138: (*list)[i] = Account::_duplicate(myAccounts[i]);</TT><TT>139: }</TT><TT>140: </TT><TT>141: return list;</TT><TT>142: }</TT><TT>143: </TT><B><TT>144: ATMCard_ptr BankImpl::issueATMCard(CORBA::Short pin, Account_ptr</TT></B><B><TT>145: account) throw (InvalidAccountException) {</TT></B><B><TT>146: </TT></B><B><TT>147: // First check to see if the Account is with this Bank.</TT></B><B><TT>148: std::vector<Account_ptr>::iterator first = myAccounts.begin();</TT></B><B><TT>149: std::vector<Account_ptr>::iterator last = myAccounts.end();</TT></B><B><TT>150: IsAccountEqual predicate(account);</TT></B><B><TT>151: </TT></B><B><TT>152: std::vector<Account_ptr>::iterator matchedAccount = std::</TT></B><B><TT>153: find_if(first, last, predicate);</TT></B><B><TT>154: if (matchedAccount == last) {</TT></B><B><TT>155: </TT></B><B><TT>156: // Invalid Account; throw an exception.</TT></B><B><TT>157: throw InvalidAccountException();</TT></B><B><TT>158: }</TT></B><B><TT>159: </TT></B><B><TT>160: // If we got this far, the Account must exist with this Bank,</TT></B><B><TT>161: // so we can proceed.</TT></B><B><TT>162: ATMCard_ptr newCard = new ATMCardImpl(pin, account);</TT></B><B><TT>163: </TT></B><B><TT>164: return ATMCard::_duplicate(newCard);</TT></B><B><TT>165: }</TT></B><TT>166: </TT><TT>167: // Return the next available account number. The result is returned</TT><TT>168: // in a static buffer.</TT><TT>169: char* BankImpl::getNextAccountNumber() {</TT><TT>170: </TT><TT>171: static char accountNumber[16] = "Account ";</TT><TT>172: </TT><TT>173: sprintf(accountNumber + 7, "%08u", myNextAccountNumber++);</TT><TT>174: </TT><TT>175: return accountNumber;</TT><TT>176: }</TT><TT>177: </TT><TT>178: // Return the current date in the form "Mmm DD YYYY". The result is</TT><TT>179: // returned in a static buffer.</TT><TT>180: char* BankImpl::getCurrentDate() {</TT><TT>181: </TT><TT>182: static char currentDate[12] = " ";</TT><TT>183: </TT><TT>184: time_t ltime;</TT><TT>185: time(&ltime);</TT><TT>186: char* ctimeResult = ctime(&ltime);</TT><TT>187: </TT><TT>188: memcpy(currentDate, ctimeResult + 4, 3);</TT><TT>189: memcpy(currentDate + 4, ctimeResult + 8, 2);</TT><TT>190: memcpy(currentDate + 7, ctimeResult + 20, 4);</TT><TT>191: </TT><TT>192: return currentDate;193: }</TT> </FONT></PRE><P>The logic of <TT>ATMCardImpl</TT> (Listings 8.10 and 8.11) is very similar tothat in <TT>BankImpl</TT>; <TT>addAccount()</TT>, <TT>removeAccount()</TT>, and <TT>getAccounts()</TT>are implemented in exactly the same way as they are in <TT>BankImpl</TT> (with theexception that <TT>removeAccount()</TT> and <TT>addAccount()</TT> call <TT>isAuthorized()</TT>to determine whether the <TT>Account</TT> is in the list of <TT>Account</TT>s, ratherthan duplicate this functionality).<H4><FONT COLOR="#000077">Listing 8.10. ATMCardImpl.h.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // ATMCardImpl.h</TT><TT> 2: </TT><TT> 3: #ifndef ATMCardImpl_h</TT><TT> 4: #define ATMCardImpl_h</TT><TT> 5: </TT><TT> 6: #include <vector></TT><TT> 7: </TT><TT> 8: #include "../ATMCard_s.h"</TT><TT> 9: </TT><TT>10: class ATMCardImpl : public _sk_ATMCard {</TT><TT>11: </TT><TT>12: public:</TT><TT>13: // Constuctor.</TT><TT>14: //</TT><TT>15: // pin - the initial PIN to use for this ATMCard.</TT><TT>16: // initialAccount - the Account for which this ATMCard is</TT><TT>17: // initially authorized.</TT><TT>18: ATMCardImpl(CORBA::Short pin, Account_ptr initialAccount);</TT><TT>19: </TT><TT>20: // Destructor.</TT><TT>21: ~ATMCardImpl();</TT><TT>22: </TT><TT>23: // These methods are described in ATMCard.idl.</TT><TT>24: virtual CORBA::Boolean isAuthorized(Account_ptr account);</TT><TT>25: virtual CORBA::Short pin();</TT><TT>26: virtual void pin(CORBA::Short val);</TT><TT>27: virtual void removeAccount(Account_ptr account) throw</TT><TT>28: (InvalidAccountException);</TT><TT>29: virtual void addAccount(Account_ptr account) throw</TT><TT>30: (InvalidAccountException);</TT><TT>31: virtual AccountList* getAccounts();</TT><TT>32: </TT><TT>33: private:</TT><TT>34: </TT><TT>35: // Default constructor.</TT><TT>36: ATMCardImpl();</TT><TT>37: </TT><TT>38: // This ATMCard's PIN.</TT><TT>39: CORBA::Short myPIN;</TT><TT>40: </TT><TT>41: // A list of Accounts on which this ATM is authorized.</TT><TT>42: std::vector<Account_ptr> myAccounts;</TT><TT>43: };</TT><TT>44: 45: #endif</TT></FONT></PRE><H4><FONT COLOR="#000077">Listing 8.11. ATMCardImpl.cpp.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // ATMCardImpl.cpp</TT><TT> 2: </TT><TT> 3: #include "ATMCardImpl.h"</TT><TT> 4: </TT><TT> 5: #include <iostream.h></TT><TT> 6: #include <algorithm></TT><TT> 7: #include <functional></TT><TT> 8: </TT><TT> 9: // STL-derived unary function which returns TRUE if Accounts are</TT><TT> 10: // equal.</TT><TT> 11: class IsAccountEqual : public std::unary_function<Account_ptr,</TT><TT> 12: bool> {</TT><TT> 13: public:</TT><TT> 14: IsAccountEqual(argument_type account) { myAccount = account; }</TT><TT> 15: result_type operator()(argument_type account) { return account-></TT><TT> 16: _is_equivalent(myAccount) != 0; }</TT><TT> 17: private:</TT><TT> 18: argument_type myAccount;</TT><TT> 19: };</TT><TT> 20: </TT><TT> 21: // Constuctor.</TT><TT> 22: //</TT><TT> 23: // pin - the initial PIN to use for this ATMCard.</TT><TT> 24: // initialAccount - the Account for which this ATMCard is</TT><TT> 25: // initially authorized.</TT><TT> 26: ATMCardImpl::ATMCardImpl(CORBA::Short pin, Account_ptr</TT><TT> 27: initialAccount) : myPIN(pin), myAccounts() {</TT><TT> 28: </TT><TT> 29: // Add the Account to the authorized Account list.</TT><TT> 30: myAccounts.push_back(Account::_duplicate(initialAccount));</TT><TT> 31: }</TT><TT> 32: </TT><TT> 33: // Default constructor.</TT><TT> 34: ATMCardImpl::ATMCardImpl() : myPIN(0), myAccounts() {</TT><TT> 35: </TT><TT> 36: }</TT>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -