source: src/main/java/onetomany/bargainingchipsgame/Bundle.java@ 290

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

UF_Peaked class created, by creating a class for ChipMappingType (more functionality than ChipMapping), and adding a method for getUnitPrice to Bundle class. Some comments added to UtilityFunction interface.

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