[233] | 1 | /**
|
---|
| 2 | * Bundle class
|
---|
| 3 | */
|
---|
[253] | 4 | package onetomany.bargainingchipsgame;
|
---|
[233] | 5 |
|
---|
[240] | 6 | import java.util.List;
|
---|
| 7 |
|
---|
[233] | 8 | import java.util.ArrayList;
|
---|
[236] | 9 | import java.util.Arrays;
|
---|
[240] | 10 | import java.util.Iterator;
|
---|
[233] | 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.
|
---|
[243] | 15 | * A stack can be aggregated into a bundle, if at the same color of one of the Bundle's stacks.
|
---|
[233] | 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 | */
|
---|
[240] | 22 | public class Bundle implements Iterable<Stack>
|
---|
[239] | 23 | {
|
---|
[233] | 24 | private List<Stack> bundle;
|
---|
| 25 |
|
---|
| 26 | public Bundle()
|
---|
| 27 | {
|
---|
| 28 | this.bundle = new ArrayList<Stack>();
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | /**
|
---|
| 32 | * @return the bundle
|
---|
| 33 | */
|
---|
[239] | 34 | public List<Stack> getBundle()
|
---|
| 35 | {
|
---|
[233] | 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 |
|
---|
[240] | 65 | public Iterator<Stack> iterator()
|
---|
[233] | 66 | {
|
---|
[240] | 67 | return bundle.iterator();
|
---|
[233] | 68 | }
|
---|
| 69 |
|
---|
[236] | 70 |
|
---|
[243] | 71 | public Double getPrice()
|
---|
| 72 | {
|
---|
| 73 | double sum=0.0;
|
---|
| 74 | for (Stack t: bundle)
|
---|
| 75 | sum += t.getPrice();
|
---|
| 76 | return sum;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[236] | 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 | */
|
---|
[243] | 89 | public Bundle aggregateWith(Bundle b) //This WORKED WELL although it is not in nice programming style (without calling aggregate with stack, below !
|
---|
[236] | 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);
|
---|
[243] | 107 | if (!f1[i] && !f2[j] && t.getColor()==s.getColor())
|
---|
[236] | 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 |
|
---|
[239] | 125 |
|
---|
[243] | 126 | // previous try for calling two methods both with using iterators
|
---|
[239] | 127 |
|
---|
[240] | 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 | */
|
---|
[243] | 137 | public Bundle aggregateWith333(Bundle b)
|
---|
| 138 | {
|
---|
| 139 | if (b!=null && this!=null)
|
---|
| 140 | {
|
---|
[263] | 141 | //Bundle l=new Bundle();
|
---|
| 142 |
|
---|
[243] | 143 | for (Stack s : b) //loop over bundle b stacks
|
---|
[263] | 144 | this.aggregateWith333(s); //aggregate stacks of b into `this' bundle by calling aggregateWith(stack), below
|
---|
| 145 | }
|
---|
[243] | 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 | {
|
---|
[263] | 163 | Bundle b=new Bundle();
|
---|
| 164 |
|
---|
[243] | 165 | if (s!=null && this!=null)
|
---|
| 166 | {
|
---|
| 167 | Boolean f=Boolean.FALSE; //flag for stack s added
|
---|
[263] | 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
|
---|
[243] | 172 | {
|
---|
[263] | 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);
|
---|
[243] | 176 | f=true;
|
---|
| 177 | }
|
---|
| 178 | else
|
---|
[263] | 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 | }
|
---|
[243] | 183 | }
|
---|
| 184 | if (!f) //s not aggregated with any stack in `this' bundle
|
---|
[263] | 185 | {b.addStack(s); // so put stack s directly into the new bundle
|
---|
| 186 | System.out.println("\n hey3 it is b "+b);
|
---|
| 187 | }
|
---|
[243] | 188 | return b;
|
---|
| 189 | }
|
---|
[263] | 190 | return b;
|
---|
[243] | 191 | }
|
---|
| 192 |
|
---|
| 193 |
|
---|
| 194 |
|
---|
[233] | 195 | }
|
---|