[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;
|
---|
[272] | 9 | import java.util.Collections;
|
---|
[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 | *
|
---|
[272] | 19 | * Immutable.
|
---|
[233] | 20 | */
|
---|
[240] | 21 | public class Bundle implements Iterable<Stack>
|
---|
[239] | 22 | {
|
---|
[272] | 23 | private final List<Stack> bundle;
|
---|
| 24 |
|
---|
| 25 | /**
|
---|
| 26 | * Makes sure bundle remains unmodifiable.
|
---|
| 27 | */
|
---|
| 28 | public Bundle(List<Stack> list)
|
---|
[233] | 29 | {
|
---|
[272] | 30 | List<Stack> tmpListOfHolding = new ArrayList<Stack>();
|
---|
| 31 | tmpListOfHolding.addAll(list);
|
---|
| 32 | this.bundle = Collections.unmodifiableList(tmpListOfHolding);
|
---|
| 33 | }
|
---|
[233] | 34 |
|
---|
| 35 | /**
|
---|
| 36 | * @return the bundle
|
---|
| 37 | */
|
---|
[239] | 38 | public List<Stack> getBundle()
|
---|
| 39 | {
|
---|
[233] | 40 | return bundle;
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | @Override
|
---|
| 44 | public String toString()
|
---|
| 45 | {
|
---|
| 46 | return bundle.toString();
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | public int size()
|
---|
| 50 | {
|
---|
[272] | 51 | return getBundle().size();
|
---|
[233] | 52 | }
|
---|
| 53 |
|
---|
[240] | 54 | public Iterator<Stack> iterator()
|
---|
[233] | 55 | {
|
---|
[240] | 56 | return bundle.iterator();
|
---|
[233] | 57 | }
|
---|
| 58 |
|
---|
[236] | 59 |
|
---|
[243] | 60 | public Double getPrice()
|
---|
| 61 | {
|
---|
| 62 | double sum=0.0;
|
---|
| 63 | for (Stack t: bundle)
|
---|
| 64 | sum += t.getPrice();
|
---|
| 65 | return sum;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
[236] | 68 | /**
|
---|
| 69 | *
|
---|
| 70 | * bundle "+" bundle
|
---|
| 71 | *
|
---|
| 72 | * @param b, this method adds bundle b to `this' bundle
|
---|
| 73 | * @return the aggregated bundle
|
---|
| 74 | * 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.
|
---|
| 75 | *
|
---|
| 76 | * This method just calls stack '+' stack aggregation operation (has no need to bundle "+" stack aggregation operation.
|
---|
| 77 | */
|
---|
[272] | 78 | public Bundle aggregateWith(Bundle b)
|
---|
[236] | 79 | {
|
---|
[272] | 80 | // Start with this bundle
|
---|
| 81 | Bundle agg = new Bundle(this.getBundle());
|
---|
| 82 | // And add all stacks from b
|
---|
| 83 | for (Stack t : b)
|
---|
| 84 | agg = agg.aggregateWith(t);
|
---|
[263] | 85 |
|
---|
[272] | 86 | return agg;
|
---|
[243] | 87 | }
|
---|
| 88 |
|
---|
| 89 |
|
---|
[272] | 90 |
|
---|
[243] | 91 |
|
---|
| 92 | /**
|
---|
| 93 | * bundle '+' stack
|
---|
| 94 | *
|
---|
| 95 | * @param s, this method adds stack s to `this' bundle
|
---|
| 96 | * @return the aggregated bundle
|
---|
| 97 | * 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;
|
---|
| 98 | * otherwise, stack s is just added into the new bundle.
|
---|
| 99 | */
|
---|
[272] | 100 | public Bundle aggregateWith(Stack s)
|
---|
[243] | 101 | {
|
---|
[272] | 102 | ArrayList<Stack> agg = new ArrayList<Stack>();
|
---|
| 103 | boolean sameColorFound = false;
|
---|
| 104 | for (Stack t : bundle)
|
---|
| 105 | if (!t.hasSameColorAs(s))
|
---|
| 106 | agg.add(t);
|
---|
| 107 | else
|
---|
| 108 | {
|
---|
| 109 | agg.add(s.aggregateWith(t));
|
---|
| 110 | sameColorFound = true;
|
---|
| 111 | }
|
---|
| 112 | if (!sameColorFound)
|
---|
| 113 | agg.add(s);
|
---|
| 114 | return new Bundle(agg);
|
---|
| 115 | }
|
---|
[263] | 116 |
|
---|
[272] | 117 | public static void main(String[] args)
|
---|
| 118 | {
|
---|
| 119 | Bundle b = new BundleBuilder()
|
---|
| 120 | .addStack("Red", 7.0, 3)
|
---|
| 121 | .addStack("Green", 3.0, 5)
|
---|
| 122 | .addStack("Purple", 2.0, 7)
|
---|
| 123 | .build();
|
---|
[263] | 124 |
|
---|
[272] | 125 | Bundle c = new BundleBuilder()
|
---|
| 126 | .addStack("Red", 5.0, 3)
|
---|
| 127 | .addStack("Yellow", 9.0, 1)
|
---|
| 128 | .addStack("Green", 9.0, 1)
|
---|
| 129 | .build();
|
---|
| 130 |
|
---|
| 131 | System.out.println(b);
|
---|
| 132 | System.out.println(b.aggregateWith(new Stack("Green", 3.0, 2)));
|
---|
| 133 | System.out.println(b.aggregateWith(c));
|
---|
[243] | 134 | }
|
---|
| 135 |
|
---|
| 136 |
|
---|
| 137 |
|
---|
[233] | 138 | }
|
---|