/** * Bundle class */ package onetomany.bargainingchipsgame; import java.util.List; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; /** * Offers are exchanged in Bundle formats. * A Bundle is a set of several stacks, {s_1, ..., s_n}, where no two stacks are of the same color. * A stack can be aggregated into a bundle, if at the same color of one of the Bundle's stacks. * 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. * Aggregation of a bundle with the empty stack or empty bundle is itself. * * @author Faria Nassiri-Mofakham * */ public class Bundle implements Iterable { private List bundle; public Bundle() { this.bundle = new ArrayList(); } /** * @return the bundle */ public List getBundle() { return bundle; } /** * @param b the bundle to set */ public void setBundle(List b) { this.bundle = b; } public void addStack(Stack s) { bundle.add(s); } @Override public String toString() { return bundle.toString(); } public int size() { if (bundle != null) { return getBundle().size(); } else return 0; } public Stack getStack(int i) { return getBundle().get(i); } public Iterator iterator() { return bundle.iterator(); } public Double getPrice() { double sum=0.0; for (Stack t: bundle) sum += t.getPrice(); return sum; } /** * * bundle "+" bundle * * @param b, this method adds bundle b to `this' bundle * @return the aggregated bundle * 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. * * This method just calls stack '+' stack aggregation operation (has no need to bundle "+" stack aggregation operation. */ public Bundle aggregateWith(Bundle b) //This WORKED WELL although it is not in nice programming style (without calling aggregate with stack, below ! { if (b!=null && this!=null) { Bundle l=new Bundle(); Boolean[] f1 = new Boolean[b.size()]; //flag for b bundle stack added Arrays.fill(f1, Boolean.FALSE); Boolean[] f2 = new Boolean[this.size()]; //flag for `this' bundle stack added Arrays.fill(f2, Boolean.FALSE); for (int i=0;i