Last change
on this file since 256 was 253, checked in by Faria Nassiri Mofakham, 5 years ago |
Commit #1 of:
+ negotiator.onetomany package refactored into onetomany package.
+ info created for all contained the packages.
+ current work defined under onetomany.bargainingchipsgame.
+ in this package, onetomany.bargainingchipsgame.players package includes packages for utility function, coordinator, negotiatior, and buyer and seller.
+ negotiator.onetomany package now contains nothing, can be deleted.
+ Interaction thread
|
File size:
2.0 KB
|
Rev | Line | |
---|
[233] | 1 | /**
|
---|
| 2 | * Stack class
|
---|
| 3 | */
|
---|
[253] | 4 | package onetomany.bargainingchipsgame;
|
---|
[233] | 5 |
|
---|
| 6 | /**
|
---|
| 7 | * Stack contains a number of Chips of the same color and price.
|
---|
| 8 | * A stack=(chip, quantity).
|
---|
| 9 | * Aggregation of a two stacks of the same colors, creates another stack with the total quantity but its chip in a new price equal to weighted average of the both unit prices.
|
---|
| 10 | * Aggregation of a stack with the empty stack is itself.
|
---|
| 11 | *
|
---|
| 12 | * @author Faria Nassiri-Mofakham
|
---|
| 13 | *
|
---|
| 14 | */
|
---|
[239] | 15 | public class Stack
|
---|
| 16 | {
|
---|
[233] | 17 |
|
---|
| 18 | private Chip chip;
|
---|
| 19 | private int quantity;
|
---|
| 20 |
|
---|
| 21 |
|
---|
| 22 | public Stack()
|
---|
| 23 | {
|
---|
| 24 | setChip(null);
|
---|
| 25 | setQuantity(-1);
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | public Stack(Chip c, int q)
|
---|
| 29 | {
|
---|
| 30 | setChip(c);
|
---|
| 31 | setQuantity(q);
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | /**
|
---|
| 35 | * @return the chip
|
---|
| 36 | */
|
---|
| 37 | public Chip getChip() {
|
---|
| 38 | return chip;
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | /**
|
---|
| 42 | * @param chip the chip to set
|
---|
| 43 | */
|
---|
[239] | 44 | public void setChip(Chip chip)
|
---|
| 45 | {
|
---|
[233] | 46 | this.chip = chip;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | /**
|
---|
| 50 | * @return the quantity
|
---|
| 51 | */
|
---|
[239] | 52 | public int getQuantity()
|
---|
| 53 | {
|
---|
[233] | 54 | return quantity;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | /**
|
---|
| 58 | * @param quantity the quantity to set
|
---|
| 59 | */
|
---|
[239] | 60 | public void setQuantity(int quantity)
|
---|
| 61 | {
|
---|
[233] | 62 | this.quantity = quantity;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | public double getPrice()
|
---|
| 66 | {
|
---|
| 67 | return chip.getPrice()*quantity;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
[243] | 70 | public String getColor()
|
---|
| 71 | {
|
---|
| 72 | return chip.getColor();
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[233] | 75 | public Stack aggregateWith(Stack s)
|
---|
| 76 | {
|
---|
[236] | 77 | if (this!=null && s!=null && chip.getColor()==s.getChip().getColor())
|
---|
| 78 | return new Stack(new Chip(getChip().getColor(), (getChip().getPrice()*quantity+s.getChip().getPrice()*s.getQuantity())/(quantity+s.getQuantity())),quantity+s.getQuantity());
|
---|
| 79 | else
|
---|
| 80 | {
|
---|
| 81 | System.out.println("\n\n[Warning] StackClass::aggregateWith(Stack). Different colors! Aggregating "+s+" into "+this+" is not possible; stack "+this+" remained unchanged!");
|
---|
| 82 | return this;
|
---|
| 83 | }
|
---|
[233] | 84 | }
|
---|
[243] | 85 |
|
---|
| 86 |
|
---|
[233] | 87 |
|
---|
| 88 | @Override
|
---|
| 89 | public String toString()
|
---|
| 90 | {
|
---|
| 91 | return getQuantity()+" x "+getChip();
|
---|
| 92 | // two other representations:
|
---|
| 93 | //return this.getClass().getSimpleName()+"("+getChip()+","+getQuantity()+")"
|
---|
| 94 | //return this.getClass().getSimpleName()+"("+getQuantity()+" x "+getChip()+")";
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.