|
ShoppingCart |
|
1 // joi/1/estore/ShoppingCart.java
2 //
3 //
4 // Copyright 2003 Bill Campbell and Ethan Bolker
5
6 /**
7 * A ShoppingCart keeps track of a customer's purchases.
8 *
9 * @see EStore
10 * @version 1
11 */
12
13 public class ShoppingCart
14 {
15 private int count; // number of Items in this ShoppingCart
16 private int cost; // cost of Items in this ShoppingCart
17
18 /**
19 * Construct a new empty ShoppingCart.
20 */
21
22 public ShoppingCart()
23 {
24 count = 0;
25 cost = 0;
26 }
27
28 /**
29 * When this ShoppingCart is asked to add an Item to itself
30 * it updates its count field and then updates its cost
31 * field by sending the Item a getCost message.
32 *
33 * @param purchase the Item being added to this ShoppingCart.
34 */
35
36 public void add( Item purchase )
37 {
38 count++; // Java idiom for count = count + 1;
39 cost = cost + purchase.getCost();
40 }
41
42 /**
43 * What happens when this ShoppingCart is asked how many
44 * Items it contains.
45 *
46 * @return the count of Items.
47 */
48
49 public int getCount()
50 {
51 return count;
52 }
53
54 /**
55 * What happens when this ShoppingCart is asked the total
56 * cost of the Items it contains.
57 *
58 * @return the total cost.
59 */
60
61 public int getCost()
62 {
63 return cost;
64 }
65 }
66
|
ShoppingCart |
|