[316] | 1 | package bargainingchips.utilityfunctions;
|
---|
[315] | 2 |
|
---|
[316] | 3 | import bargainingchips.Bundle;
|
---|
| 4 | import bargainingchips.BundleBuilder;
|
---|
| 5 | import bargainingchips.Chip;
|
---|
[340] | 6 | import bargainingchips.wishlist.WishList;
|
---|
| 7 | import bargainingchips.wishlist.WishListBuilder;
|
---|
[315] | 8 |
|
---|
| 9 | /**
|
---|
| 10 | * If all requested quantities at the total requested price (or less) would be satisfied,
|
---|
| 11 | * then the bundle is evaluated as 1.0, otherwise as 0.0;
|
---|
[338] | 12 | *
|
---|
| 13 | *
|
---|
| 14 | * @author Faria Nassiri-Mofakham
|
---|
| 15 | *
|
---|
[315] | 16 | */
|
---|
| 17 | public class UF_AllOrNothing implements UtilityFunction
|
---|
| 18 | {
|
---|
| 19 | private WishList wishlist;
|
---|
| 20 | private double maxprice;
|
---|
| 21 |
|
---|
| 22 |
|
---|
| 23 | public UF_AllOrNothing(WishList w, double max)
|
---|
| 24 | {
|
---|
| 25 | this.wishlist = w;
|
---|
| 26 | this.maxprice = max;
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 |
|
---|
| 30 | @Override
|
---|
| 31 | public Double getUtility(Bundle b)
|
---|
| 32 | {
|
---|
| 33 | if (b.getTotalPrice() > maxprice)
|
---|
| 34 | return 0.0;
|
---|
| 35 |
|
---|
| 36 | for (Chip c : wishlist)
|
---|
| 37 | {
|
---|
| 38 | int desired = wishlist.getQuantity(c);
|
---|
| 39 | Integer offered = b.getQuantity(c);
|
---|
| 40 |
|
---|
| 41 | if (offered == null || desired != offered)
|
---|
| 42 | return 0.0;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | return 1.0;
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | @Override
|
---|
| 49 | public String toString()
|
---|
| 50 | {
|
---|
| 51 | return this.getClass().getSimpleName() + ": " + wishlist.toString() + " for <= $" + maxprice;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | public static void main(String[] args)
|
---|
| 55 | {
|
---|
| 56 | WishList wishlist = new WishListBuilder().addWish("Red", 3).addWish("Green", 2).build();
|
---|
| 57 | UF_AllOrNothing u = new UF_AllOrNothing(wishlist, 10.0);
|
---|
| 58 | System.out.println(u);
|
---|
| 59 |
|
---|
| 60 | Bundle offer = new BundleBuilder()
|
---|
| 61 | .addStack("Red", 1.0, 3)
|
---|
| 62 | .addStack("Green", 3.0, 2)
|
---|
| 63 | .addStack("Purple", 0.10, 10)
|
---|
| 64 | .build();
|
---|
| 65 | System.out.println(u.getUtility(offer));
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | }
|
---|
| 69 |
|
---|