source: src/main/java/onetomany/bargainingchipsgame/Stack.java@ 265

Last change on this file since 265 was 265, checked in by Tim Baarslag, 5 years ago

Polished Stack

File size: 1.5 KB
Line 
1package onetomany.bargainingchipsgame;
2
3/**
4 * Stack contains a number of {@link Chip} objects of the same color and price.
5 * A stack=(chip, quantity).
6 * 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.
7 * Aggregation of a stack with the empty stack is itself.
8 *
9 * @author Faria Nassiri-Mofakham
10 *
11 */
12public class Stack
13{
14 private Chip chip;
15 private int quantity;
16
17 public Stack(Chip c, int q)
18 {
19 chip = c;
20 quantity = q;
21 }
22
23 /**
24 * @return the chip
25 */
26 public Chip getChip() {
27 return chip;
28 }
29
30 /**
31 * @return the quantity
32 */
33 public int getQuantity()
34 {
35 return quantity;
36 }
37
38 public double getPrice()
39 {
40 return chip.getPrice()*quantity;
41 }
42
43 public String getColor()
44 {
45 return chip.getColor();
46 }
47
48 public boolean hasSameColorAs(Stack s)
49 {
50 return getColor().equals(s.getColor());
51 }
52
53 public Stack aggregateWith(Stack s)
54 {
55 if (s!=null && chip.hasSameColorAs(s)) {
56 String color = getChip().getColor();
57 double p = (getChip().getPrice()*quantity+s.getChip().getPrice()*s.getQuantity())/(quantity+s.getQuantity());
58 return new Stack(new Chip(color, p), quantity + s.getQuantity());
59 } else
60 throw new IllegalStateException("\n\n[Warning] StackClass::aggregateWith(Stack). Different colors! Aggregating "+s+" into "+this+" is not possible.");
61 }
62
63 @Override
64 public String toString()
65 {
66 return getQuantity()+" x "+getChip();
67 }
68}
Note: See TracBrowser for help on using the repository browser.