source: src/main/java/bargainingchips/utilityfunctions/UF_AllOrNothing.java@ 337

Last change on this file since 337 was 316, checked in by Tim Baarslag, 5 years ago

new packages complete

File size: 1.5 KB
Line 
1package bargainingchips.utilityfunctions;
2
3import bargainingchips.Bundle;
4import bargainingchips.BundleBuilder;
5import bargainingchips.Chip;
6import bargainingchips.WishList;
7import bargainingchips.WishListBuilder;
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;
12 */
13public class UF_AllOrNothing implements UtilityFunction
14{
15 private WishList wishlist;
16 private double maxprice;
17
18
19 public UF_AllOrNothing(WishList w, double max)
20 {
21 this.wishlist = w;
22 this.maxprice = max;
23 }
24
25
26 @Override
27 public Double getUtility(Bundle b)
28 {
29 if (b.getTotalPrice() > maxprice)
30 return 0.0;
31
32 for (Chip c : wishlist)
33 {
34 int desired = wishlist.getQuantity(c);
35 Integer offered = b.getQuantity(c);
36
37 if (offered == null || desired != offered)
38 return 0.0;
39 }
40
41 return 1.0;
42 }
43
44 @Override
45 public String toString()
46 {
47 return this.getClass().getSimpleName() + ": " + wishlist.toString() + " for <= $" + maxprice;
48 }
49
50 public static void main(String[] args)
51 {
52 WishList wishlist = new WishListBuilder().addWish("Red", 3).addWish("Green", 2).build();
53 UF_AllOrNothing u = new UF_AllOrNothing(wishlist, 10.0);
54 System.out.println(u);
55
56 Bundle offer = new BundleBuilder()
57 .addStack("Red", 1.0, 3)
58 .addStack("Green", 3.0, 2)
59 .addStack("Purple", 0.10, 10)
60 .build();
61 System.out.println(u.getUtility(offer));
62 }
63
64}
65
Note: See TracBrowser for help on using the repository browser.