source: src/main/java/negotiator/onetomany/domain/Stack.java@ 239

Last change on this file since 239 was 239, checked in by Faria Nassiri Mofakham, 5 years ago

Commented code in Bundle and Utility

File size: 1.9 KB
Line 
1/**
2 * Stack class
3 */
4package negotiator.onetomany.domain;
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 */
15public 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 Stack aggregateWith(Stack s)
71 {
72 if (this!=null && s!=null && chip.getColor()==s.getChip().getColor())
73 return new Stack(new Chip(getChip().getColor(), (getChip().getPrice()*quantity+s.getChip().getPrice()*s.getQuantity())/(quantity+s.getQuantity())),quantity+s.getQuantity());
74 else
75 {
76 System.out.println("\n\n[Warning] StackClass::aggregateWith(Stack). Different colors! Aggregating "+s+" into "+this+" is not possible; stack "+this+" remained unchanged!");
77 return this;
78 }
79 }
80
81
82 @Override
83 public String toString()
84 {
85 return getQuantity()+" x "+getChip();
86 // two other representations:
87 //return this.getClass().getSimpleName()+"("+getChip()+","+getQuantity()+")"
88 //return this.getClass().getSimpleName()+"("+getQuantity()+" x "+getChip()+")";
89 }
90}
Note: See TracBrowser for help on using the repository browser.