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

Last change on this file since 298 was 290, checked in by Faria Nassiri Mofakham, 5 years ago

UF_Peaked class created, by creating a class for ChipMappingType (more functionality than ChipMapping), and adding a method for getUnitPrice to Bundle class. Some comments added to UtilityFunction interface.

File size: 3.7 KB
RevLine 
[233]1/**
2 * Bundle class
3 */
[253]4package onetomany.bargainingchipsgame;
[233]5
[240]6import java.util.List;
7
[233]8import java.util.ArrayList;
[272]9import java.util.Collections;
[240]10import 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]21public 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
[282]60 public Double getTotalPrice()
[243]61 {
62 double sum=0.0;
63 for (Stack t: bundle)
[282]64 sum += t.getTotalPrice();
[243]65 return sum;
66 }
[282]67
68 /**
69 * Gets the quantity of the right stack in this bundle
70 */
71 public Integer getQuantity(Chip c)
72 {
73 for (Stack s : this)
74 if (s.getChip().equals(c))
75 return s.getQuantity();
76 return null;
77 }
[243]78
[236]79 /**
[290]80 * Gets the unit price of the chip in the right stack in this bundle
81 */
82 public double getUnitPrice(Chip c)
83 {
84 for (Stack s : this)
85 if (s.getChip().equals(c))
86 return s.getUnitPrice();
87 return 0.0;
88 }
89
90 /**
[236]91 *
92 * bundle "+" bundle
93 *
94 * @param b, this method adds bundle b to `this' bundle
95 * @return the aggregated bundle
96 * 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.
97 *
98 * This method just calls stack '+' stack aggregation operation (has no need to bundle "+" stack aggregation operation.
99 */
[272]100 public Bundle aggregateWith(Bundle b)
[236]101 {
[272]102 // Start with this bundle
103 Bundle agg = new Bundle(this.getBundle());
104 // And add all stacks from b
105 for (Stack t : b)
106 agg = agg.aggregateWith(t);
[263]107
[272]108 return agg;
[243]109 }
110
111
[272]112
[243]113
114 /**
115 * bundle '+' stack
116 *
117 * @param s, this method adds stack s to `this' bundle
118 * @return the aggregated bundle
119 * 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;
120 * otherwise, stack s is just added into the new bundle.
121 */
[272]122 public Bundle aggregateWith(Stack s)
[243]123 {
[272]124 ArrayList<Stack> agg = new ArrayList<Stack>();
125 boolean sameColorFound = false;
126 for (Stack t : bundle)
127 if (!t.hasSameColorAs(s))
128 agg.add(t);
129 else
130 {
131 agg.add(s.aggregateWith(t));
132 sameColorFound = true;
133 }
134 if (!sameColorFound)
135 agg.add(s);
136 return new Bundle(agg);
137 }
[263]138
[272]139 public static void main(String[] args)
140 {
141 Bundle b = new BundleBuilder()
142 .addStack("Red", 7.0, 3)
143 .addStack("Green", 3.0, 5)
144 .addStack("Purple", 2.0, 7)
145 .build();
[263]146
[272]147 Bundle c = new BundleBuilder()
148 .addStack("Red", 5.0, 3)
149 .addStack("Yellow", 9.0, 1)
150 .addStack("Green", 9.0, 1)
151 .build();
152
153 System.out.println(b);
154 System.out.println(b.aggregateWith(new Stack("Green", 3.0, 2)));
155 System.out.println(b.aggregateWith(c));
[243]156 }
157
158
159
[233]160}
Note: See TracBrowser for help on using the repository browser.