📄 ch07.htm
字号:
<TT>32: // Destructor.</TT><TT>33: AccountImpl::~AccountImpl() {</TT><TT>34: </TT><TT>35: free(myAccountNumber);</TT><TT>36: free(myCreationDate);</TT><TT>37: }</TT><TT>38: </TT><TT>39: char* AccountImpl::accountNumber() {</TT><TT>40: </TT><TT>41: return CORBA::strdup(myAccountNumber);</TT><TT>42: }</TT><TT>43: </TT><TT>44: char* AccountImpl::creationDate() {</TT><TT>45: </TT><TT>46: return CORBA::strdup(myCreationDate);</TT><TT>47: }</TT><TT>48: </TT><TT>49: CORBA::Float AccountImpl::balance() {</TT><TT>50: </TT><TT>51: return myBalance;</TT><TT>52: }</TT><TT>53: </TT><TT>54: CustomerList* AccountImpl::getCustomers() {</TT><TT>55: </TT><TT>56: return &myOwners;</TT><TT>57: }</TT><TT>58: </TT><B><TT>59:</TT></B><TT> CORBA::Float AccountImpl::withdraw(CORBA::Float amount) <B>throw</B></TT><B><TT>60:</TT></B><TT> <B>(InvalidAmountException, InsufficientFundsException)</B> {</TT><B><TT>61:</TT></B><TT> </TT><B><TT>62:</TT></B><TT> <B>// Disallow the withdrawal of negative amounts and throw an</B></TT><B><TT>63:</TT></B><TT> <B>// exception if this is attempted.</B></TT><B><TT>64:</TT></B><TT> <B>if (amount < 0.0) {</B></TT><B><TT>65:</TT></B><TT> <B>cout << "AccountImpl: Attempted to withdraw invalid "</B></TT><B><TT>66:</TT></B><TT> <B><< "amount." << endl;</B></TT><B><TT>67:</TT></B><TT> <B>throw InvalidAmountException();</B></TT><B><TT>68:</TT></B><TT> <B>}</B></TT><B><TT>69:</TT></B><TT> </TT><B><TT>70:</TT></B><TT> <B>// Disallow withdrawal of an amount greater than the current</B></TT><B><TT>71:</TT></B><TT> <B>// balance and throw an exception if this is attempted.</B></TT><B><TT>72:</TT></B><TT> <B>if (amount > myBalance) {</B></TT><B><TT>73:</TT></B><TT> <B>cout << "AccountImpl: Insufficient funds to withdraw "</B></TT><B><TT>74:</TT></B><TT> <B><< "specified amount." << endl;</B></TT><B><TT>75:</TT></B><TT> <B>throw InsufficientFundsException();</B></TT><B><TT>76:</TT></B><TT> <B>}</B></TT><TT>77: </TT><TT>78: myBalance -= amount;</TT><TT>79: </TT><TT>80: return myBalance;</TT><TT>81: }</TT><TT>82: </TT><B><TT>83:</TT></B><TT> CORBA::Float AccountImpl::deposit(CORBA::Float amount) <B>throw</B></TT><B><TT>84:</TT></B><TT> <B>(InvalidAmountException) </B>{</TT><TT>85: </TT><B><TT>86:</TT></B><TT> <B>// Disallow the deposit of negative amounts and throw an</B></TT><B><TT>87:</TT></B><TT> <B>// exception if this is attempted.</B></TT><B><TT>88:</TT></B><TT> <B>if (amount < 0.0) {</B></TT><B><TT>89:</TT></B><TT> <B>cout << "AccountImpl: Attempted to deposit invalid amount."</B></TT><B><TT>90:</TT></B><TT> <B><< endl;</B></TT><B><TT>91:</TT></B><TT> <B>throw InvalidAmountException();</B></TT><B><TT>92:</TT></B><TT> <B>}</B></TT><TT>93: </TT><TT>94: myBalance += amount;</TT><TT>95: </TT><TT>96: return myBalance;97: }</TT> </FONT></PRE><P>The modified <TT>withdraw()</TT> method in <TT>AccountImpl</TT> first checks theamount that the client wishes to withdraw. If the amount is negative, it is rejectedby the <TT>withdraw()</TT> method, and an <TT>InvalidAmountException</TT> is thrown.If the amount is non-negative, the <TT>Account</TT> balance is checked to see whethersufficient funds are available to withdraw the requested amount. (Recall that overdraftprotection is not a current feature of the system.) If there are insufficient fundsto cover the withdrawal, <TT>withdraw()</TT> throws an <TT>InsufficientFundsException</TT>.</P><P>The <TT>deposit()</TT> method of <TT>AccountImpl</TT> works similarly; an <TT>InvalidAmountException</TT>is thrown if the caller attempts to deposit a negative amount into an <TT>Account</TT>(which would really be a withdrawal). Any amount can be deposited into an <TT>Account</TT>,so there is no need for <TT>deposit()</TT> to ever throw an <TT>InsufficientFundsException</TT>.<H3><A NAME="Heading12"></A><FONT COLOR="#000077">CheckingAccountImpl</FONT></H3><P>Listings 7.18 and 7.19 contain further changes required to add exception capabilityto the <TT>Bank</TT> application.<H4><FONT COLOR="#000077">Listing 7.18. Modified CheckingAccountImpl.h.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // CheckingAccountImpl.h</TT><TT> 2: </TT><TT> 3: #ifndef CheckingAccountImpl_h</TT><TT> 4: #define CheckingAccountImpl_h</TT><TT> 5: </TT><TT> 6: #include "../CheckingAccount_s.h"</TT><TT> 7: #include "AccountImpl.h"</TT><TT> 8: </TT><TT> 9: class CheckingAccountImpl : public _sk_CheckingAccount {</TT><TT>10: </TT><TT>11: public:</TT><TT>12: </TT><TT>13: // Constructor.</TT><TT>14: //</TT><TT>15: // accountNumber - Account number.</TT><TT>16: // creationDate - Account creation date.</TT><TT>17: // initialBalance - Initial Account balance.</TT><TT>18: // customer - Initial Account owner.</TT><TT>19: CheckingAccountImpl(const char* accountNumber, const char*</TT><TT>20: creationDate, CORBA::Float initialBalance, Customer_ptr</TT><TT>21: customer);</TT><TT>22: </TT><TT>23: // Destructor.</TT><TT>24: ~CheckingAccountImpl();</TT><TT>25: </TT><TT>26: // These methods are described in Account.idl.</TT><TT>27: virtual char* accountNumber();</TT><TT>28: virtual char* creationDate();</TT><TT>29: virtual CORBA::Float balance();</TT><TT>30: virtual CustomerList* getCustomers();</TT><B><TT>31:</TT></B><TT> virtual CORBA::Float withdraw(CORBA::Float amount) <B>throw</B></TT><B><TT>32:</TT></B><TT> <B>(InvalidAmountException, InsufficientFundsException);</B></TT><B><TT>33:</TT></B><TT> virtual CORBA::Float deposit(CORBA::Float amount) <B>throw</B></TT><B><TT>34:</TT></B><TT> <B>(InvalidAmountException);</B></TT><TT>35: </TT><TT>36: private:</TT><TT>37: </TT><TT>38: // Default constructor.</TT><TT>39: CheckingAccountImpl();</TT><TT>40: </TT><TT>41: // My associated AccountImpl object.</TT><TT>42: AccountImpl myAccount;</TT><TT>43: };</TT><TT>44: 45: #endif</TT></FONT></PRE><H4><FONT COLOR="#000077">Listing 7.19. Modified CheckingAccountImpl.cpp.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // CheckingAccountImpl.cpp</TT><TT> 2: </TT><TT> 3: #include "CheckingAccountImpl.h"</TT><TT> 4: </TT><TT> 5: // Constructor.</TT><TT> 6: //</TT><TT> 7: // accountNumber - Account number.</TT><TT> 8: // creationDate - Account creation date.</TT><TT> 9: // initialBalance - Initial Account balance.</TT><TT>10: // customer - Initial Account owner.</TT><TT>11: CheckingAccountImpl::CheckingAccountImpl(const char* accountNumber,</TT><TT>12: const char* creationDate, CORBA::Float initialBalance,</TT><TT>13: Customer_ptr customer) : myAccount(accountNumber,</TT><TT>14: creationDate, initialBalance, customer) {</TT><TT>15: </TT><TT>16: }</TT><TT>17: </TT><TT>18: // Default constructor.</TT><TT>19: CheckingAccountImpl::CheckingAccountImpl() : myAccount(NULL, NULL,</TT><TT>20: 0.0, Customer::_nil()) {</TT><TT>21: </TT><TT>22: }</TT><TT>23: </TT><TT>24: // Destructor.</TT><TT>25: CheckingAccountImpl::~CheckingAccountImpl() {</TT><TT>26: </TT><TT>27: }</TT><TT>28: </TT><TT>29: char* CheckingAccountImpl::accountNumber() {</TT><TT>30: </TT><TT>31: return myAccount.accountNumber();</TT><TT>32: }</TT><TT>33: </TT><TT>34: char* CheckingAccountImpl::creationDate() {</TT><TT>35: </TT><TT>36: return myAccount.creationDate();</TT><TT>37: }</TT><TT>38: </TT><TT>39: CORBA::Float CheckingAccountImpl::balance() {</TT><TT>40: </TT><TT>41: return myAccount.balance();</TT><TT>42: }</TT><TT>43: </TT><TT>44: CustomerList* CheckingAccountImpl::getCustomers() {</TT><TT>45: </TT><TT>46: return myAccount.getCustomers();</TT><TT>47: }</TT><TT>48: </TT><TT>49: CORBA::Float CheckingAccountImpl::withdraw(CORBA::Float amount)</TT><B><TT>50:</TT></B><TT> <B>throw (InvalidAmountException, InsufficientFundsException)</B></TT><TT>51: {</TT><TT>52: </TT><TT>53: return myAccount.withdraw(amount);</TT><TT>54: }</TT><TT>55: </TT><TT>56: CORBA::Float CheckingAccountImpl::deposit(CORBA::Float amount)</TT><B><TT>57:</TT></B><TT> <B>throw (InvalidAmountException)</B> {</TT><TT>58: </TT><TT>59: return myAccount.deposit(amount);60: }</TT> </FONT></PRE><P>Again, the <TT>withdraw()</TT> and <TT>deposit()</TT> methods can throw the <TT>InvalidAmountException</TT>or <TT>InsufficientFundsException</TT>, or the <TT>InvalidAmountException</TT>, respectively.Note, however, that none of these exceptions are explicitly thrown within the methodsthemselves. Recall that the <TT>withdraw()</TT> and <TT>deposit()</TT> operationson the <TT>myAccount</TT> member (which is an <TT>AccountImpl</TT> object) can throwthese exceptions. Because these exceptions are not caught by the methods in <TT>CheckingAccountImpl</TT>,the exceptions are simply passed back to the caller of the <TT>CheckingAccountImpl</TT>method.<H3><A NAME="Heading13"></A><FONT COLOR="#000077">SavingsAccountImpl</FONT></H3><P>The changes for <TT>SavingsAccountImpl</TT>, as shown in Listings 7.20 and 7.21,closely resemble the changes made in <TT>CheckingAccountImpl</TT>.<H4><FONT COLOR="#000077">Listing 7.20. Modified SavingsAccountImpl.h.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // SavingsAccountImpl.h</TT><TT> 2: </TT><TT> 3: #ifndef SavingsAccountImpl_h</TT><TT> 4: #define SavingsAccountImpl_h</TT><TT> 5: </TT><TT> 6: #include "../SavingsAccount_s.h"</TT><TT> 7: #include "AccountImpl.h"</TT><TT> 8: </TT><TT> 9: class SavingsAccountImpl : public AccountImpl {</TT><TT>10: </TT><TT>11: public:</TT><TT>12: </TT><TT>13: // Constructor.</TT><TT>14: //</TT><TT>15: // accountNumber - Account number.</TT><TT>16: // creationDate - Account creation date.</TT><TT>17: // initialBalance - Initial Account balance.</TT><TT>18: // customer - Initial Account owner.</TT><TT>19: // interestRate - Initial Account interest rate.</TT><TT>20: SavingsAccountImpl(const char* accountNumber, const char*</TT><TT>21: creationDate, CORBA::Float initialBalance, Customer_ptr</TT><TT>22: customer, CORBA::Float interestRate);</TT><TT>23: </TT><TT>24: // Destructor.</TT><TT>25: ~SavingsAccountImpl();</TT><TT>26: </TT><TT>27: // These methods are described in Account.idl.</TT><TT>28: virtual char* accountNumber();</TT><TT>29: virtual char* creationDate();</TT><TT>30: virtual CORBA::Float balance();</TT><TT>31: virtual CustomerList* getCustomers();</TT><B><TT>32:</TT></B><TT> virtual CORBA::Float withdraw(CORBA::Float amount) <B>throw</B></TT><B><TT>33:</TT></B><TT> <B>(InvalidAmountException, InsufficientFundsException);</B></TT><B><TT>34:</TT></B><TT> virtual CORBA::Float deposit(CORBA::Float amount) <B>throw</B></TT><B><TT>35:</TT></B><TT> <B>(InvalidAmountException);</B></TT><TT>36: </TT><TT>37: // These methods are described in SavingsAccount.idl.</TT><TT>38: virtual CORBA::Float interestRate();</TT><B><TT>39:</TT></B><TT> virtual CORBA::Float setInterestRate(CORBA::Float rate) <B>throw</B></TT><B><TT>40:</TT></B><TT> <B>(InvalidAmountException);</B></TT><TT>41: </TT><TT>42: private:</TT><TT>43: </TT><TT>44: // Default constructor.</TT><TT>45: SavingsAccountImpl();</TT><TT>46: </TT><TT>47: // This Account's interest rate.</TT><TT>48: CORBA::Float myInterestRate;</TT><TT>49: </TT><TT>50: // My associated AccountImpl object.</TT><TT>51: AccountImpl myAccount;</TT><TT>52: };</TT><TT>53: 54: #endif</TT></FONT></PRE><H4><FONT COLOR="#000077">Listing 7.21. Modified SavingsAccountImpl.cpp.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // SavingsAccountImpl.cpp</TT><TT> 2: </TT><TT> 3: #include "SavingsAccountImpl.h"</TT><TT> 4: </TT><TT> 5: // Constructor.</TT><TT> 6: //</TT><TT> 7: // accountNumber - Account number.</TT><TT> 8: // creationDate - Account creation date.</TT><TT> 9: // initialBalance - Initial Account balance.</TT><TT>10: // customer - Initial Account owner.</TT><TT>11: // interestRate - Initial Account interest rate.</TT><TT>12: SavingsAccountImpl::SavingsAccountImpl(const char* accountNumber,</TT><TT>13: const char* creationDate, CORBA::Float initialBalance,</TT><TT>14: Customer_ptr customer, CORBA::Float interestRate) :</TT><TT>15: myAccount(accountNumber, creationDate, initialBalance,</TT><TT>16: customer), myInterestRate(interestRate) {</TT><TT>17: </TT><TT>18: }</TT><TT>19: </TT><TT>20: // Default constructor.</TT><TT>21: SavingsAccountImpl::SavingsAccountImpl() : myAccount(NULL, NULL,</TT><TT>22: 0.0, Customer::_nil()), myInterestRate(0.0) {</TT><TT>23: </TT><TT>24: }</TT><TT>25: </TT><TT>26: // Destructor.</TT><TT>27: SavingsAccountImpl::~SavingsAccountImpl() {</TT><TT>28: </TT><TT>29: }</TT><TT>30: </TT><TT>31: char* SavingsAccountImpl::accountNumber() {</TT><TT>32: </TT><TT>33: return myAccount.accountNumber();</TT><TT>34: }</TT><TT>35: </TT><TT>36: char* SavingsAccountImpl::creationDate() {</TT><TT>37: </TT><TT>38: return myAccount.creationDate();</TT>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -