package onetomany.bargainingchipsgame; /** * Stack contains a number of {@link Chip} objects of the same color and price. * A stack=(chip, quantity). * 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. * Aggregation of a stack with the empty stack is itself. * * @author Faria Nassiri-Mofakham * */ public class Stack { private Chip chip; private int quantity; public Stack(Chip c, int q) { chip = c; quantity = q; } /** * @return the chip */ public Chip getChip() { return chip; } /** * @return the quantity */ public int getQuantity() { return quantity; } public double getPrice() { return chip.getPrice()*quantity; } public String getColor() { return chip.getColor(); } public boolean hasSameColorAs(Stack s) { return getColor().equals(s.getColor()); } public Stack aggregateWith(Stack s) { if (s!=null && chip.hasSameColorAs(s)) { String color = getChip().getColor(); double p = (getChip().getPrice()*quantity+s.getChip().getPrice()*s.getQuantity())/(quantity+s.getQuantity()); return new Stack(new Chip(color, p), quantity + s.getQuantity()); } else throw new IllegalStateException("\n\n[Warning] StackClass::aggregateWith(Stack). Different colors! Aggregating "+s+" into "+this+" is not possible."); } @Override public String toString() { return getQuantity()+" x "+getChip(); } }