[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 |
|
---|
| 54 | public Stack getStack(int i)
|
---|
| 55 | {
|
---|
| 56 | return getBundle().get(i);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[240] | 59 | public Iterator<Stack> iterator()
|
---|
[233] | 60 | {
|
---|
[240] | 61 | return bundle.iterator();
|
---|
[233] | 62 | }
|
---|
| 63 |
|
---|
[236] | 64 |
|
---|
[243] | 65 | public Double getPrice()
|
---|
| 66 | {
|
---|
| 67 | double sum=0.0;
|
---|
| 68 | for (Stack t: bundle)
|
---|
| 69 | sum += t.getPrice();
|
---|
| 70 | return sum;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[236] | 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 | */
|
---|
[272] | 83 | public Bundle aggregateWith(Bundle b)
|
---|
[236] | 84 | {
|
---|
[272] | 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);
|
---|
[263] | 90 |
|
---|
[272] | 91 | return agg;
|
---|
[243] | 92 | }
|
---|
| 93 |
|
---|
| 94 |
|
---|
[272] | 95 |
|
---|
[243] | 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 | */
|
---|
[272] | 105 | public Bundle aggregateWith(Stack s)
|
---|
[243] | 106 | {
|
---|
[272] | 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 | }
|
---|
[263] | 121 |
|
---|
[272] | 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();
|
---|
[263] | 129 |
|
---|
[272] | 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));
|
---|
[243] | 139 | }
|
---|
| 140 |
|
---|
| 141 |
|
---|
| 142 |
|
---|
[233] | 143 | }
|
---|