📄 ch13.htm
字号:
<TT>37: public float balance() {</TT><TT>38: </TT><TT>39: return myAccount.balance();</TT><TT>40: }</TT><TT>41: </TT><TT>42: public Customer[] getCustomers() {</TT><TT>43: </TT><TT>44: return myAccount.getCustomers();</TT><TT>45: }</TT><TT>46: </TT><TT>47: public float withdraw(float amount) throws</TT><TT>48: InvalidAmountException, InsufficientFundsException {</TT><TT>49: </TT><TT>50: return myAccount.withdraw(amount);</TT><TT>51: }</TT><TT>52: </TT><TT>53: public float deposit(float amount) throws</TT><TT>54: InvalidAmountException {</TT><TT>55: </TT><TT>56: return myAccount.deposit(amount);</TT><TT>57: }</TT></FONT></PRE><P><TT>58: }</TT><H4><FONT COLOR="#000077">Listing 13.7. SavingsAccountImpl.java.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // SavingsAccountImpl.java</TT><TT> 2: </TT><TT> 3: import idlGlobal.Customer;</TT><TT> 4: </TT><TT> 5: import idlGlobal.InsufficientFundsException;</TT><TT> 6: import idlGlobal.InvalidAmountException;</TT><TT> 7: </TT><TT> 8: public class SavingsAccountImpl extends idlGlobal.</TT><TT> 9: _SavingsAccountImplBase {</TT><TT>10: </TT><TT>11: // The AccountImpl to which most of this SavingsAccountImpl's</TT><TT>12: // functionality is delegated.</TT><TT>13: private AccountImpl myAccount;</TT><TT>14: </TT><TT>15: // This account's interest rate.</TT><TT>16: private float myInterestRate;</TT><TT>17: </TT><TT>18: public SavingsAccountImpl(String accountNumber, String</TT><TT>19: creationDate, float initialBalance, Customer customer,</TT><TT>20: float interestRate) {</TT><TT>21: </TT><TT>22: myAccount = new AccountImpl(accountNumber, creationDate,</TT><TT>23: initialBalance, customer);</TT><TT>24: myInterestRate = interestRate;</TT><TT>25: }</TT><TT>26: </TT><TT>27: protected SavingsAccountImpl() {</TT><TT>28: </TT><TT>29: }</TT><TT>30: </TT><TT>31: public String accountNumber() {</TT><TT>32: </TT><TT>33: return myAccount.accountNumber();</TT><TT>34: }</TT><TT>35: </TT><TT>36: public String creationDate() {</TT><TT>37: </TT><TT>38: return myAccount.creationDate();</TT><TT>39: }</TT><TT>40: </TT><TT>41: public float balance() {</TT><TT>42: </TT><TT>43: return myAccount.balance();</TT><TT>44: }</TT><TT>45: </TT><TT>46: public Customer[] getCustomers() {</TT><TT>47: </TT><TT>48: return myAccount.getCustomers();</TT><TT>49: }</TT><TT>50: </TT><TT>51: public float withdraw(float amount) throws</TT><TT>52: InvalidAmountException, InsufficientFundsException {</TT><TT>53: </TT><TT>54: return myAccount.withdraw(amount);</TT><TT>55: }</TT><TT>56: </TT><TT>57: public float deposit(float amount) throws</TT><TT>58: InvalidAmountException {</TT><TT>59: </TT><TT>60: return myAccount.deposit(amount);</TT><TT>61: }</TT><TT>62: </TT><TT>63: public float interestRate() {</TT><TT>64: </TT><TT>65: return myInterestRate;</TT><TT>66: }</TT><TT>67: </TT><TT>68: public float setInterestRate(float rate) throws</TT><TT>69: InvalidAmountException {</TT><TT>70: </TT><TT>71: // Disallow negative interest rates and throw an exception</TT><TT>72: // if this is attempted.</TT><TT>73: if (rate < 0.0) {</TT><TT>74: </TT><TT>75: throw new InvalidAmountException();</TT><TT>76: }</TT><TT>77: </TT><TT>78: float oldInterestRate = myInterestRate;</TT><TT>79: myInterestRate = rate;</TT><TT>80: return oldInterestRate;</TT><TT>81: }82: }</TT></FONT></PRE><H4><FONT COLOR="#000077">Listing 13.8. BankImpl.java.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // BankImpl.java</TT><TT> 2: </TT><TT> 3: import java.text.SimpleDateFormat;</TT><TT> 4: import java.util.Date;</TT><TT> 5: import java.util.Enumeration;</TT><TT> 6: import java.util.Vector;</TT><TT> 7: </TT><TT> 8: import idlGlobal.Account;</TT><TT> 9: import idlGlobal.ATMCard;</TT><TT> 10: import idlGlobal.Customer;</TT><TT> 11: </TT><TT> 12: import idlGlobal.InvalidAccountException;</TT><TT> 13: </TT><TT> 14: import util.CORBAAlgorithms;</TT><TT> 15: </TT><TT> 16: public class BankImpl extends idlGlobal._BankImplBase {</TT><TT> 17: </TT><TT> 18: // This Bank's name.</TT><TT> 19: private String myName;</TT><TT> 20: </TT><TT> 21: // This Bank's address.</TT><TT> 22: private String myAddress;</TT><TT> 23: </TT><TT> 24: // This Bank's list of Accounts.</TT><TT> 25: Vector myAccounts;</TT><TT> 26: </TT><TT> 27: // This Bank's list of Accounts that are subscribed to the</TT><TT> 28: // automatic account update service.</TT><TT> 29: Vector mySubscribedAccounts;</TT><TT> 30: </TT><TT> 31: // This Bank's next Account number to assign.</TT><TT> 32: int myNextAccountNumber;</TT><TT> 33: </TT><TT> 34: public BankImpl(String name) {</TT><TT> 35: </TT><TT> 36: myName = new String(name);</TT><TT> 37: myAddress = new String("123 Elm Street, Anywhere USA "</TT><TT> 38: + "12345");</TT><TT> 39: myNextAccountNumber = 0;</TT><TT> 40: myAccounts = new Vector();</TT><TT> 41: mySubscribedAccounts = new Vector();</TT><TT> 42: </TT><TT> 43: Thread updateThread = new</TT><TT> 44: UpdateAccountThread(mySubscribedAccounts);</TT><TT> 45: updateThread.start();</TT><TT> 46: }</TT><TT> 47: </TT><TT> 48: private BankImpl() {</TT><TT> 49: </TT><TT> 50: myName = new String();</TT><TT> 51: myAddress = new String();</TT><TT> 52: myAccounts = new Vector();</TT><TT> 53: mySubscribedAccounts = new Vector();</TT><TT> 54: myNextAccountNumber = 0;</TT><TT> 55: }</TT><TT> 56: </TT><TT> 57: public void name(String name) {</TT><TT> 58: </TT><TT> 59: myName = new String(name);</TT><TT> 60: }</TT><TT> 61: </TT><TT> 62: public String name() {</TT><TT> 63: </TT><TT> 64: return myName;</TT><TT> 65: }</TT><TT> 66: </TT><TT> 67: public void address(String address) {</TT><TT> 68: </TT><TT> 69: myAddress = new String(address);</TT><TT> 70: }</TT><TT> 71: </TT><TT> 72: public String address() {</TT><TT> 73: </TT><TT> 74: return myAddress;</TT><TT> 75: }</TT><TT> 76: </TT><TT> 77: public Account createAccount(Customer customer, String</TT><TT> 78: accountType, float openingBalance) {</TT><TT> 79: </TT><TT> 80: Account newAccount;</TT><TT> 81: </TT><TT> 82: if (accountType.compareTo("savings") == 0) {</TT><TT> 83: </TT><TT> 84: // Create a new SavingsAccountImpl object for the</TT><TT> 85: // Account.</TT><TT> 86: System.out.println("BankImpl: Creating new " +</TT><TT> 87: "SavingsAccount for Customer " + customer.</TT><TT> 88: name() + ".");</TT><TT> 89: newAccount = new</TT><TT> 90: SavingsAccountImpl(getNextAccountNumber(),</TT><TT> 91: getCurrentDate(), openingBalance, customer,</TT><TT> 92: 10.0f);</TT><TT> 93: } else if (accountType.compareTo("checking") == 0) {</TT><TT> 94: </TT><TT> 95: // Create a new CheckingAccountImpl object for the</TT><TT> 96: // Account.</TT><TT> 97: System.out.println("BankImpl: Creating new " +</TT><TT> 98: "CheckingAccount for Customer " + customer.</TT><TT> 99: name() + ".");</TT><TT>100: newAccount = new</TT><TT>101: CheckingAccountImpl(getNextAccountNumber(),</TT><TT>102: getCurrentDate(), openingBalance, customer);</TT><TT>103: } else {</TT><TT>104: </TT><TT>105: // Invalid Account type; do nothing.</TT><TT>106: System.out.println("BankImpl: Customer " + customer.</TT><TT>107: name() + " requested invalid Account type \"" +</TT><TT>108: accountType + "\".");</TT><TT>109: return null;</TT><TT>110: }</TT><TT>111: </TT><TT>112: // Add the created Account at the end of the list and</TT><TT>113: // return it.</TT><TT>114: BankMain.myBOA.obj_is_ready(newAccount);</TT><TT>115: myAccounts.addElement(newAccount._duplicate());</TT><TT>116: return newAccount;</TT><TT>117: }</TT><TT>118: </TT><TT>119: public void deleteAccount(Account account) throws</TT><TT>120: InvalidAccountException {</TT><TT>121: </TT><TT>122: if (!CORBAAlgorithms.contains(myAccounts, account)) {</TT><TT>123: </TT><TT>124: // Invalid Account; throw an exception.</TT><TT>125: System.out.println("BankImpl: Attempted to delete " +</TT><TT>126: "invalid Account.");</TT><TT>127: throw new InvalidAccountException();</TT><TT>128: }</TT><TT>129: </TT><TT>130: System.out.println("BankImpl: Deleting Account \"" +</TT><TT>131: account.accountNumber() + "\".");</TT><TT>132: </TT><TT>133: // Delete the given Account.</TT><TT>134: CORBAAlgorithms.removeElement(myAccounts, account);</TT><TT>135: account._release();</TT><TT>136: }</TT><TT>137: </TT><TT>138: public Account[] getAccounts() {</TT><TT>139: </TT><TT>140: Account[] list = new Account[myAccounts.size()];</TT><TT>141: myAccounts.copyInto(list);</TT><TT>142: </TT><TT>143: Enumeration e = myAccounts.elements();</TT><TT>144: while (e.hasMoreElements()) {</TT><TT>145: ((Account)e.nextElement())._duplicate();</TT><TT>146: }</TT><TT>147: </TT><TT>148: return list;</TT><TT>149: }</TT><TT>150: </TT><TT>151: public ATMCard issueATMCard(short pin, Account account) throws</TT><TT>152: InvalidAccountException {</TT><TT>153: </TT><TT>154: // First check to see if the Account is with this Bank.</TT><TT>155: if (!CORBAAlgorithms.contains(myAccounts, account)) {</TT><TT>156: </TT><TT>157: // Invalid Account; throw an exception.</TT><TT>158: System.out.println("BankImpl: Attempted to delete " +</TT><TT>159: "invalid Account.");</TT><TT>160: throw new InvalidAccountException();</TT><TT>161: }</TT><TT>162: </TT><TT>163: // If we got this far, the Account must exist with this</TT><TT>164: // Bank, so we can proceed.</TT><TT>165: ATMCard newCard = new ATMCardImpl(pin, account);</TT><TT>166: BankMain.myBOA.obj_is_ready(newCard);</TT><TT>167: </TT><TT>168: System.out.println("BankImpl: Issuing new ATMCard on " +</TT><TT>169: "Account " + account.accountNumber());</TT><TT>170: newCard._duplicate();</TT><TT>171: return newCard;</TT><TT>172: }</TT><TT>173: </TT>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -