|
|
|
Sponsored Link •
|
|
Advertisement
|
java.util.LinkedList is a "Linked list implementation of the List interface."
Talk about how you alot responsibilities to different objects and then give them names, method signatures, and semantic contracts.
Account ObjectAccount means to offer three services:
getBalance(), withdraw(), and deposit()
1 package com.artima.examples.account.ex1;
2
3 /**
4 * Represents a bank account. Money is stored in this account
5 * in integral units. Clients can use this account to store
6 * any kind of value, such as money or points, etc. The meaning
7 * of the integral units stored in this account is a decision
8 * of the client that instantiates the account. The maximum
9 * amount of units that can be stored as the current balance of
10 * an <code>Account</code> is Long.MAX_VALUE.
11 */
12 public class Account {
13
14 /**
15 * The current balance
16 */
17 private long balance;
18
19 /**
20 * Withdraws exactly the passed amount from the
21 * <code>Account</code>. Subclasses must withdraw
22 * at least the passed amount, but may effectively withdraw more.
23 * For example, if a subclass includes the notion of
24 * a withrawal fee, the subclass's implementation of
25 * this method may charge that fee by decrementing it
26 * from the account at the time of withdrawal.
27 *
28 * @param amount amount to withdraw
29 * @returns amount withdrawn from the <code>Account</code>
30 * @throws InsufficientFundsException if the <code>Account</code>
31 * contains insufficient funds for the requested withdrawal
32 */
33 public long withdraw(long amount)
34 throws InsufficientFundsException {
35
36 if (amount > balance) {
37 throw new InsufficientFundsException(
38 amount - balance);
39 }
40
41 balance -= amount;
42 return amount;
43 }
44
45 /**
46 * Deposits exactly the passed amount into the <code>Account</code>.
47 * Subclasses may effectively deposit more or less than the passed
48 * amount into the <code>Account</code>. For example, if a subclass
49 * includes the notion of funds matching, the subclass implementation
50 * of this method may match some or all of the deposited amount at
51 * the time of deposit, effectively increasing the deposited amount.
52 * Or, if a subclass includes the notion of
53 * a deposit fee, the subclass's implementation of
54 * this method may charge that fee by decrementing it
55 * from the account at the time of withdrawal, effectively reducing
56 * the deposited amount.
57 *
58 * @param amount amount to deposit
59 * @throws ArithmeticException if requested deposit would cause the
60 * balance of this <code>Account</code> to exceed Long.MAX_VALUE.
61 */
62 public void deposit(long amount) {
63
64 balance += amount;
65 }
66
67 /**
68 * Gets the current balance of this <code>Account</code>
69 *
70 * @returns the current balance
71 */
72 public long getBalance() {
73 return balance;
74 }
75 }
76
Account object on the heap:
Type safety means that the data on the heap that represents an Account object
can only be accessed in ways compatible with its Accountness.
Igor said: "There is less money in the world than pennies in long."
|
Sponsored Links
|