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

Last change on this file since 272 was 272, checked in by Tim Baarslag, 5 years ago

Bundle immutable. Made aggregation work.

File size: 3.3 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 Stack getStack(int i)
55 {
56 return getBundle().get(i);
57 }
58
59 public Iterator<Stack> iterator()
60 {
61 return bundle.iterator();
62 }
63
64
65 public Double getPrice()
66 {
67 double sum=0.0;
68 for (Stack t: bundle)
69 sum += t.getPrice();
70 return sum;
71 }
72
73 /**
74 *
75 * bundle "+" bundle
76 *
77 * @param b, this method adds bundle b to `this' bundle
78 * @return the aggregated bundle
79 * 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.
80 *
81 * This method just calls stack '+' stack aggregation operation (has no need to bundle "+" stack aggregation operation.
82 */
83 public Bundle aggregateWith(Bundle b)
84 {
85 // Start with this bundle
86 Bundle agg = new Bundle(this.getBundle());
87 // And add all stacks from b
88 for (Stack t : b)
89 agg = agg.aggregateWith(t);
90
91 return agg;
92 }
93
94
95
96
97 /**
98 * bundle '+' stack
99 *
100 * @param s, this method adds stack s to `this' bundle
101 * @return the aggregated bundle
102 * 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;
103 * otherwise, stack s is just added into the new bundle.
104 */
105 public Bundle aggregateWith(Stack s)
106 {
107 ArrayList<Stack> agg = new ArrayList<Stack>();
108 boolean sameColorFound = false;
109 for (Stack t : bundle)
110 if (!t.hasSameColorAs(s))
111 agg.add(t);
112 else
113 {
114 agg.add(s.aggregateWith(t));
115 sameColorFound = true;
116 }
117 if (!sameColorFound)
118 agg.add(s);
119 return new Bundle(agg);
120 }
121
122 public static void main(String[] args)
123 {
124 Bundle b = new BundleBuilder()
125 .addStack("Red", 7.0, 3)
126 .addStack("Green", 3.0, 5)
127 .addStack("Purple", 2.0, 7)
128 .build();
129
130 Bundle c = new BundleBuilder()
131 .addStack("Red", 5.0, 3)
132 .addStack("Yellow", 9.0, 1)
133 .addStack("Green", 9.0, 1)
134 .build();
135
136 System.out.println(b);
137 System.out.println(b.aggregateWith(new Stack("Green", 3.0, 2)));
138 System.out.println(b.aggregateWith(c));
139 }
140
141
142
143}
Note: See TracBrowser for help on using the repository browser.