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

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

Bug in classes Chip, Stack and operations, Bundle and operations fixed; they work well. Old versions of aggregation operators and tests copied into an old file.

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 private Chip chip;
18 private int quantity;
19
20
21 public Stack()
22 {
23 setChip(null);
24 setQuantity(-1);
25 }
26
27 public Stack(Chip c, int q)
28 {
29 setChip(c);
30 setQuantity(q);
31 }
32
33 /**
34 * @return the chip
35 */
36 public Chip getChip() {
37 return chip;
38 }
39
40 /**
41 * @param chip the chip to set
42 */
43 public void setChip(Chip chip) {
44 this.chip = chip;
45 }
46
47 /**
48 * @return the quantity
49 */
50 public int getQuantity() {
51 return quantity;
52 }
53
54 /**
55 * @param quantity the quantity to set
56 */
57 public void setQuantity(int quantity) {
58 this.quantity = quantity;
59 }
60
61 public double getPrice()
62 {
63 return chip.getPrice()*quantity;
64 }
65
66 public Stack aggregateWith(Stack s)
67 {
68 if (this!=null && s!=null && chip.getColor()==s.getChip().getColor())
69 return new Stack(new Chip(getChip().getColor(), (getChip().getPrice()*quantity+s.getChip().getPrice()*s.getQuantity())/(quantity+s.getQuantity())),quantity+s.getQuantity());
70 else
71 {
72 System.out.println("\n\n[Warning] StackClass::aggregateWith(Stack). Different colors! Aggregating "+s+" into "+this+" is not possible; stack "+this+" remained unchanged!");
73 return this;
74 }
75 }
76
77
78 @Override
79 public String toString()
80 {
81 return getQuantity()+" x "+getChip();
82 // two other representations:
83 //return this.getClass().getSimpleName()+"("+getChip()+","+getQuantity()+")"
84 //return this.getClass().getSimpleName()+"("+getQuantity()+" x "+getChip()+")";
85 }
86}
Note: See TracBrowser for help on using the repository browser.