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
RevLine 
[253]1package onetomany.bargainingchipsgame;
[233]2
3/**
[265]4 * Stack contains a number of {@link Chip} objects of the same color and price.
[233]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 */
[239]12public class Stack
13{
[233]14 private Chip chip;
15 private int quantity;
16
17 public Stack(Chip c, int q)
18 {
[265]19 chip = c;
20 quantity = q;
[233]21 }
22
23 /**
24 * @return the chip
25 */
26 public Chip getChip() {
27 return chip;
28 }
29
30 /**
31 * @return the quantity
32 */
[239]33 public int getQuantity()
34 {
[233]35 return quantity;
36 }
37
38 public double getPrice()
39 {
40 return chip.getPrice()*quantity;
41 }
42
[243]43 public String getColor()
44 {
45 return chip.getColor();
46 }
47
[257]48 public boolean hasSameColorAs(Stack s)
49 {
50 return getColor().equals(s.getColor());
51 }
52
[233]53 public Stack aggregateWith(Stack s)
54 {
[265]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 }
[233]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.