/** * Stack class */ package onetomany.bargainingchipsgame; /** * Stack contains a number of Chips 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() { setChip(null); setQuantity(-1); } public Stack(Chip c, int q) { setChip(c); setQuantity(q); } /** * @return the chip */ public Chip getChip() { return chip; } /** * @param chip the chip to set */ public void setChip(Chip chip) { this.chip = chip; } /** * @return the quantity */ public int getQuantity() { return quantity; } /** * @param quantity the quantity to set */ public void setQuantity(int quantity) { this.quantity = quantity; } public double getPrice() { return chip.getPrice()*quantity; } public String getColor() { return chip.getColor(); } public Stack aggregateWith(Stack s) { if (this!=null && s!=null && chip.getColor()==s.getChip().getColor()) return new Stack(new Chip(getChip().getColor(), (getChip().getPrice()*quantity+s.getChip().getPrice()*s.getQuantity())/(quantity+s.getQuantity())),quantity+s.getQuantity()); else { System.out.println("\n\n[Warning] StackClass::aggregateWith(Stack). Different colors! Aggregating "+s+" into "+this+" is not possible; stack "+this+" remained unchanged!"); return this; } } @Override public String toString() { return getQuantity()+" x "+getChip(); // two other representations: //return this.getClass().getSimpleName()+"("+getChip()+","+getQuantity()+")" //return this.getClass().getSimpleName()+"("+getQuantity()+" x "+getChip()+")"; } }