[316] | 1 | package bargainingchips.actions;
|
---|
[315] | 2 |
|
---|
[316] | 3 | import bargainingchips.Bundle;
|
---|
| 4 | import bargainingchips.BundleBuilder;
|
---|
[315] | 5 |
|
---|
| 6 | /**
|
---|
[318] | 7 | * An offer has two parts: (1) a bundle or null, (2) a message code: `bid', `accept', `breakoff', or `end'.
|
---|
[315] | 8 | *
|
---|
| 9 | * Immutable.
|
---|
| 10 | */
|
---|
| 11 | public class Offer
|
---|
| 12 | {
|
---|
| 13 | /**
|
---|
| 14 | * Message codes:
|
---|
| 15 | * (1) `bid' [body contains a bundle],
|
---|
| 16 | * (2) `accept' [agree with the deal (based on the rules); null body],
|
---|
[318] | 17 | * (3) `breakoff' [propose to quit the negotiation; null body]
|
---|
| 18 | *
|
---|
| 19 | * Special:
|
---|
| 20 | * (4) `end' [poison message to signal the agents that the negotiation is over;
|
---|
| 21 | * null body for no agreement, bundle body for agreement]
|
---|
[315] | 22 | */
|
---|
| 23 | private final OfferType type;
|
---|
| 24 | private final Bundle bundle;
|
---|
| 25 |
|
---|
| 26 | /**
|
---|
| 27 | * Creates an offer. Bundle and OfferType are immutable and therefore so is Offer.
|
---|
| 28 | */
|
---|
| 29 | public Offer(Bundle b, OfferType t)
|
---|
| 30 | {
|
---|
| 31 | bundle = b;
|
---|
| 32 | type = t;
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | public Offer(Offer o)
|
---|
| 36 | {
|
---|
| 37 | this(o.bundle, o.type);
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | /**
|
---|
| 41 | * Creates a bid. Bundle is immutable and therefore so is Offer.
|
---|
| 42 | */
|
---|
| 43 | public Offer(Bundle b)
|
---|
| 44 | {
|
---|
| 45 | this(b, OfferType.BID);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | /**
|
---|
| 49 | * @return the proposal
|
---|
| 50 | */
|
---|
| 51 | public Bundle getBundle()
|
---|
| 52 | {
|
---|
| 53 | return bundle;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | @Override
|
---|
| 57 | public String toString()
|
---|
| 58 | {
|
---|
| 59 | if (type != OfferType.BID)
|
---|
| 60 | return type.toString();
|
---|
| 61 | else
|
---|
| 62 | return type.toString() + ": " + bundle.toString();
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | public static Offer getSampleOffer(int qty)
|
---|
| 66 | {
|
---|
| 67 | return getSampleOffer("Red", qty);
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | /**
|
---|
| 71 | * Just a sample example of an offer
|
---|
| 72 | */
|
---|
| 73 | public static Offer getSampleOffer(String color, int qty)
|
---|
| 74 | {
|
---|
| 75 | Bundle bundle = new BundleBuilder()
|
---|
| 76 | .addStack(color, 7.0, qty)
|
---|
| 77 | .build();
|
---|
| 78 | return new Offer(bundle);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | public boolean isBid()
|
---|
| 82 | {
|
---|
| 83 | return type == OfferType.BID;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[318] | 86 | public boolean isBreakoff()
|
---|
[315] | 87 | {
|
---|
[318] | 88 | return type == OfferType.BREAKOFF;
|
---|
[315] | 89 | }
|
---|
| 90 |
|
---|
| 91 | public boolean isAccept()
|
---|
| 92 | {
|
---|
| 93 | return type == OfferType.ACCEPT;
|
---|
| 94 | }
|
---|
[318] | 95 |
|
---|
| 96 | public boolean isEnd()
|
---|
| 97 | {
|
---|
| 98 | return type == OfferType.END;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | public boolean isAgreement()
|
---|
| 102 | {
|
---|
| 103 | return isEnd() && bundle != null;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | public boolean isDisagreement()
|
---|
| 107 | {
|
---|
| 108 | return isEnd() && bundle == null;
|
---|
| 109 | }
|
---|
[315] | 110 | } |
---|