source: src/main/java/bargainingchips/Bundle.java@ 338

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

An extended version of Buyer (BuyerExtended), which can be constructed using one among many utility functions. UF_LessPrice and seven complex utility functions based on price+quantity and two types of weights: UF_LessPriceCloseToQuantity, UF_IntensifiedLessPriceCloseToQuantity, UF_PeakedPricePeakedQuantity, UF_PeakedFlatPricePeakedFlatQuantity, UF_PeakedFlatCurvePriceGaussianQuantity, UF_BezierPriceBezierQuantity, and UF_BezierPriceGaussianQuantity. Two more fundamental helper classes: ChipIssueValue, ChipIssueValueBuilder. Adding a validity check to Stack. An update in Bundle main. Adding getSampleBid function to Bid. An update in k variable in Agent. A UF class and UtilityHelperMethods for individual passing utility functions. Adding some more tests in UF_CloseToQuantity main. An update on BundleTest.

File size: 3.7 KB
Line 
1package bargainingchips;
2
3import java.util.List;
4
5import java.util.ArrayList;
6import java.util.Collections;
7import java.util.Iterator;
8
9/**
10 * Offers are exchanged in Bundle formats.
11 * A Bundle is a set of several stacks, {s_1, ..., s_n}, where no two stacks are of the same color.
12 * A stack can be aggregated into a bundle, if at the same color of one of the Bundle's stacks.
13 * Two bundles can also be aggregated into a new bundle, where every two stacks of the same colors from both bundles are aggregated into one stack in the new bundle.
14 * Aggregation of a bundle with the empty stack or empty bundle is itself.
15 *
16 * Immutable.
17 */
18public class Bundle implements Iterable<Stack>
19{
20 private final List<Stack> bundle;
21
22 /**
23 * Makes sure bundle remains unmodifiable.
24 */
25 public Bundle(List<Stack> list)
26 {
27 List<Stack> tmpListOfHolding = new ArrayList<Stack>();
28 tmpListOfHolding.addAll(list);
29 this.bundle = Collections.unmodifiableList(tmpListOfHolding);
30 }
31
32 /**
33 * @return the bundle
34 */
35 public List<Stack> getBundle()
36 {
37 return bundle;
38 }
39
40 @Override
41 public String toString()
42 {
43 return bundle.toString();
44 }
45
46 public int size()
47 {
48 return getBundle().size();
49 }
50
51 public Iterator<Stack> iterator()
52 {
53 return bundle.iterator();
54 }
55
56
57 public Double getTotalPrice()
58 {
59 double sum=0.0;
60 for (Stack t: bundle)
61 sum += t.getTotalPrice();
62 return sum;
63 }
64
65 /**
66 * Gets the quantity of the right stack in this bundle
67 */
68 public Integer getQuantity(Chip c)
69 {
70 for (Stack s : this)
71 if (s.getChip().equals(c))
72 return s.getQuantity();
73 return null;
74 }
75
76 /**
77 * Gets the unit price of the chip in the right stack in this bundle
78 */
79 public double getUnitPrice(Chip c)
80 {
81 for (Stack s : this)
82 if (s.getChip().equals(c))
83 return s.getUnitPrice();
84 return 0.0;
85 }
86
87 /**
88 *
89 * bundle "+" bundle
90 *
91 * @param b, this method adds bundle b to `this' bundle
92 * @return the aggregated bundle
93 * where, any two stacks of the same colors are aggregated as a new stack into the new bundle, otherwise, they are just added into the new bundle.
94 *
95 * This method just calls stack '+' stack aggregation operation (has no need to bundle "+" stack aggregation operation.
96 */
97 public Bundle aggregateWith(Bundle b)
98 {
99 // Start with this bundle
100 Bundle agg = new Bundle(this.getBundle());
101 // And add all stacks from b
102 for (Stack t : b)
103 agg = agg.aggregateWith(t);
104
105 return agg;
106 }
107
108
109
110
111 /**
112 * bundle '+' stack
113 *
114 * @param s, this method adds stack s to `this' bundle
115 * @return the aggregated bundle
116 * where, stack s is aggregated with a stack of the same color in `this' bundle and the new stack is added into the new bundle;
117 * otherwise, stack s is just added into the new bundle.
118 */
119 public Bundle aggregateWith(Stack s)
120 {
121 ArrayList<Stack> agg = new ArrayList<Stack>();
122 boolean sameColorFound = false;
123 for (Stack t : bundle)
124 if (!t.hasSameColorAs(s))
125 agg.add(t);
126 else
127 {
128 agg.add(s.aggregateWith(t));
129 sameColorFound = true;
130 }
131 if (!sameColorFound)
132 agg.add(s);
133 return new Bundle(agg);
134 }
135
136 public static void main(String[] args)
137 {
138 Bundle b = new BundleBuilder()
139 .addStack("Red", 7.0, 3)
140 .addStack("Green", 3.0, 5)
141 .addStack("Purple", 2.0, 7)
142 .build();
143
144 Bundle c = new BundleBuilder()
145 .addStack("Red", 5.0, 3)
146 .addStack("Yellow", 9.0, 1)
147 .addStack("Green", 9.0, 1)
148 .build();
149
150 System.out.println(b);
151 System.out.println(b.aggregateWith(new Stack("Green", 3.0, 2)));
152 System.out.println(b.aggregateWith(c)+ ": total price "+b.getTotalPrice());
153 }
154
155
156
157}
Note: See TracBrowser for help on using the repository browser.