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

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

Bargaining Chips packages cleanup

File size: 5.1 KB
Line 
1/**
2 * Bundle class
3 */
4package onetomany.bargainingchipsgame;
5
6import java.util.List;
7
8import java.util.ArrayList;
9import java.util.Arrays;
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 * @author Faria Nassiri-Mofakham
20 *
21 */
22public class Bundle implements Iterable<Stack>
23{
24 private List<Stack> bundle;
25
26 public Bundle()
27 {
28 this.bundle = new ArrayList<Stack>();
29 }
30
31 /**
32 * @return the bundle
33 */
34 public List<Stack> getBundle()
35 {
36 return bundle;
37 }
38
39
40 public void addStack(Stack s)
41 {
42 bundle.add(s);
43 }
44
45 @Override
46 public String toString()
47 {
48 return bundle.toString();
49 }
50
51 public int size()
52 {
53 if (bundle != null)
54 {
55 return getBundle().size();
56 }
57 else return 0;
58 }
59
60 public Stack getStack(int i)
61 {
62 return getBundle().get(i);
63 }
64
65 public Iterator<Stack> iterator()
66 {
67 return bundle.iterator();
68 }
69
70
71 public Double getPrice()
72 {
73 double sum=0.0;
74 for (Stack t: bundle)
75 sum += t.getPrice();
76 return sum;
77 }
78
79 /**
80 *
81 * bundle "+" bundle
82 *
83 * @param b, this method adds bundle b to `this' bundle
84 * @return the aggregated bundle
85 * 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.
86 *
87 * This method just calls stack '+' stack aggregation operation (has no need to bundle "+" stack aggregation operation.
88 */
89 public Bundle aggregateWith(Bundle b) //This WORKED WELL although it is not in nice programming style (without calling aggregate with stack, below !
90 {
91 if (b!=null && this!=null)
92 {
93 Bundle l=new Bundle();
94 Boolean[] f1 = new Boolean[b.size()]; //flag for b bundle stack added
95 Arrays.fill(f1, Boolean.FALSE);
96 Boolean[] f2 = new Boolean[this.size()]; //flag for `this' bundle stack added
97 Arrays.fill(f2, Boolean.FALSE);
98
99
100 for (int i=0;i<b.size();i++) //loop over bundle b stacks
101 {
102 Stack s=b.getStack(i);
103
104 for (int j=0; j<this.size();j++) //loop over `this' bundle stacks
105 {
106 Stack t= this.getStack(j);
107 if (!f1[i] && !f2[j] && t.getColor()==s.getColor())
108 {
109 l.addStack(t.aggregateWith(s)); //aggregate these two stacks (b's i-th and this j-th) into the new bundle l
110 f1[i]=Boolean.TRUE;
111 f2[j]=Boolean.TRUE;
112 }
113 }
114 if (!f1[i])
115 l.addStack(s); // so put i-th stack of bundle b directly into the new bundle
116 }
117 for (int j=0;j<this.size();j++)
118 if (!f2[j])
119 l.addStack(this.getStack(j)); // so put j-th stack of `this' bundle directly into the new bundle
120 return l;
121 }
122 return this;
123 }
124
125
126 // previous try for calling two methods both with using iterators
127
128 /**
129 * bundle "+" bundle : second method (nested iterators by the other method), doesn't work correctly.
130 *
131 * @param b, this method adds bundle b to `this' bundle
132 * @return the aggregated bundle
133 * 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.
134 *
135 * This method calls bundle "+" stack aggregation operation.
136 */
137 public Bundle aggregateWith333(Bundle b)
138 {
139 if (b!=null && this!=null)
140 {
141 //Bundle l=new Bundle();
142
143 for (Stack s : b) //loop over bundle b stacks
144 this.aggregateWith333(s); //aggregate stacks of b into `this' bundle by calling aggregateWith(stack), below
145 }
146 return this;
147 }
148
149
150
151 /**
152 * bundle '+' stack
153 *
154 * @param s, this method adds stack s to `this' bundle
155 * @return the aggregated bundle
156 * 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;
157 * otherwise, stack s is just added into the new bundle.
158 *
159 * This method is called by
160 */
161 public Bundle aggregateWith333(Stack s)
162 {
163 Bundle b=new Bundle();
164
165 if (s!=null && this!=null)
166 {
167 Boolean f=Boolean.FALSE; //flag for stack s added
168
169 for (Stack t : bundle) //loop over `this' bundle stacks
170 {
171 if (t.hasSameColorAs(s)) //if the stacks t and s are of the same colors
172 {
173 System.out.println("\n hey it is t "+t+" and it is s "+s);
174 b.addStack(s.aggregateWith(t)); //aggregate stacks t and s and then add to the new bundle
175 System.out.println("\n hey1 it is b "+b);
176 f=true;
177 }
178 else
179 {b.addStack(t); // so put the stack of `this' bundle directly into the new bundle
180 System.out.println("\n hey2 it is b "+b);
181
182 }
183 }
184 if (!f) //s not aggregated with any stack in `this' bundle
185 {b.addStack(s); // so put stack s directly into the new bundle
186 System.out.println("\n hey3 it is b "+b);
187 }
188 return b;
189 }
190 return b;
191 }
192
193
194
195}
Note: See TracBrowser for help on using the repository browser.