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

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

Agent class. Utility_Function interface defined. 8 sample utility functions created and all tested. Now, Bundle has getPrice() and Stack has getColor(). Still, aggregation in Bundle class is without iterators (iterators could not work out yet).

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