1 | /**
|
---|
2 | * Stack class
|
---|
3 | */
|
---|
4 | package onetomany.bargainingchipsgame;
|
---|
5 |
|
---|
6 | /**
|
---|
7 | * Stack contains a number of Chips of the same color and price.
|
---|
8 | * A stack=(chip, quantity).
|
---|
9 | * Aggregation of a two stacks of the same colors, creates another stack with the total quantity but its chip in a new price equal to weighted average of the both unit prices.
|
---|
10 | * Aggregation of a stack with the empty stack is itself.
|
---|
11 | *
|
---|
12 | * @author Faria Nassiri-Mofakham
|
---|
13 | *
|
---|
14 | */
|
---|
15 | public class Stack
|
---|
16 | {
|
---|
17 |
|
---|
18 | private Chip chip;
|
---|
19 | private int quantity;
|
---|
20 |
|
---|
21 |
|
---|
22 | public Stack()
|
---|
23 | {
|
---|
24 | setChip(null);
|
---|
25 | setQuantity(-1);
|
---|
26 | }
|
---|
27 |
|
---|
28 | public Stack(Chip c, int q)
|
---|
29 | {
|
---|
30 | setChip(c);
|
---|
31 | setQuantity(q);
|
---|
32 | }
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * @return the chip
|
---|
36 | */
|
---|
37 | public Chip getChip() {
|
---|
38 | return chip;
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * @param chip the chip to set
|
---|
43 | */
|
---|
44 | public void setChip(Chip chip)
|
---|
45 | {
|
---|
46 | this.chip = chip;
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * @return the quantity
|
---|
51 | */
|
---|
52 | public int getQuantity()
|
---|
53 | {
|
---|
54 | return quantity;
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * @param quantity the quantity to set
|
---|
59 | */
|
---|
60 | public void setQuantity(int quantity)
|
---|
61 | {
|
---|
62 | this.quantity = quantity;
|
---|
63 | }
|
---|
64 |
|
---|
65 | public double getPrice()
|
---|
66 | {
|
---|
67 | return chip.getPrice()*quantity;
|
---|
68 | }
|
---|
69 |
|
---|
70 | public String getColor()
|
---|
71 | {
|
---|
72 | return chip.getColor();
|
---|
73 | }
|
---|
74 |
|
---|
75 | public boolean hasSameColorAs(Stack s)
|
---|
76 | {
|
---|
77 | return getColor().equals(s.getColor());
|
---|
78 | }
|
---|
79 |
|
---|
80 | public Stack aggregateWith(Stack s)
|
---|
81 | {
|
---|
82 | if (this!=null && s!=null && chip.getColor()==s.getChip().getColor())
|
---|
83 | return new Stack(new Chip(getChip().getColor(), (getChip().getPrice()*quantity+s.getChip().getPrice()*s.getQuantity())/(quantity+s.getQuantity())),quantity+s.getQuantity());
|
---|
84 | else
|
---|
85 | {
|
---|
86 | System.out.println("\n\n[Warning] StackClass::aggregateWith(Stack). Different colors! Aggregating "+s+" into "+this+" is not possible; stack "+this+" remained unchanged!");
|
---|
87 | return this;
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 |
|
---|
93 | @Override
|
---|
94 | public String toString()
|
---|
95 | {
|
---|
96 | return getQuantity()+" x "+getChip();
|
---|
97 | // two other representations:
|
---|
98 | //return this.getClass().getSimpleName()+"("+getChip()+","+getQuantity()+")"
|
---|
99 | //return this.getClass().getSimpleName()+"("+getQuantity()+" x "+getChip()+")";
|
---|
100 | }
|
---|
101 | }
|
---|