In: Computer Science
Part 2
BeerBatch Class
/**
* A class to model an item (or set of items) in an
* auction: a batch.
*/
public class BeerBatch
{
// A unique identifying number.
private final int number;
// A description of the batch.
private String description;
// The current highest offer for this
batch.
private Offer highestOffer;
/**
* Construct a BeerBatch, setting its
number and description.
* @param number The batch number.
* @param description A description of this
batch.
*/
public BeerBatch(int number, String
description)
{
this.number =
number;
this.description =
description;
this.highestOffer =
null;
}
/**
* Attempt an offer for this batch. A
successful offer
* must have a value higher than any
existing offer.
* @param offer A new offer.
* @return true if successful, false
otherwise
*/
public boolean bidFor(Offer offer)
{
if(highestOffer == null)
{
// There is no previous bid.
highestOffer = offer;
return true;
}
else
if(offer.getAmount() > highestOffer.getAmount()) {
// The bid is better than the previous one.
highestOffer = offer;
return true;
}
else {
// The bid is not better.
return false;
}
}
/**
* @return A string representation of this
batch's details.
*/
public String batchDetail()
{
return "TO DO";
}
/**
* @return The batch's number.
*/
public int getNumber()
{
return number;
}
/**
* @return The batch's description.
*/
public String getDescription()
{
return
description;
}
/**
* @return The highest offer for this
lot.
* This could be
null if there is
* no current
bid.
*/
public Offer getHighestOffer()
{
return
highestOffer;
}
}
Offer Class
/**
* A class that models an offer.
* It contains a reference to the Person bidding and the amount of
the offer.
*/
public class Offer
{
// The person making the bid.
private final Bidder bidder;
// The amount of the offer.
private final int amount;
/**
* Create an offer.
* @param bidder Who is bidding for the
batch.
* @param x The amount of the offer.
*/
public Offer(int x, Bidder b)
{
this.bidder = b;
this.amount = x;
}
/**
* @return The bidder.
*/
public Bidder getBidder()
{
return bidder;
}
/**
* @return The amount of the offer.
*/
public int getAmount()
{
return amount;
}
}
/**
* A class to model an item (or set of items) in an
* auction: a batch.
*/
public class BeerBatch
{
// A unique identifying number.
private final int number;
// A description of the batch.
private String description;
// The current highest offer for this batch.
private Offer highestOffer;
/**
* Construct a BeerBatch, setting its number and description.
* @param number The batch number.
* @param description A description of this batch.
*/
public BeerBatch(int number, String description)
{
this.number = number;
this.description = description;
this.highestOffer = null;
}
/**
* Attempt an offer for this batch. A successful offer
* must have a value higher than any existing offer.
* @param offer A new offer.
* @return true if successful, false otherwise
*/
public boolean bidFor(Offer offer)
{
if(highestOffer == null) {
// There is no previous bid.
highestOffer = offer;
return true;
}
else if(offer.getAmount() > highestOffer.getAmount()) {
// The bid is better than the previous one.
highestOffer = offer;
return true;
}
else {
// The bid is not better.
return false;
}
}
/**
* @return A string representation of this batch's details.
*/
public String batchDetail()
{
return "BeerBatch [number=" + number + ", description=" + description + ", highestOffer=" + highestOffer + "]";
}
/**
* @return The batch's number.
*/
public int getNumber()
{
return number;
}
/**
* @return The batch's description.
*/
public String getDescription()
{
return description;
}
/**
* @return The highest offer for this lot.
* This could be null if there is
* no current bid.
*/
public Offer getHighestOffer()
{
return highestOffer;
}
}
/**
* A class that models an offer.
* It contains a reference to the Person bidding and the amount of the offer.
*/
class Offer
{
// The person making the bid.
private final Bidder bidder;
// The amount of the offer.
private final int amount;
/**
* Create an offer.
* @param bidder Who is bidding for the batch.
* @param x The amount of the offer.
*/
public Offer(int x, Bidder b)
{
this.bidder = b;
this.amount = x;
}
/**
* @return The bidder.
*/
public Bidder getBidder()
{
return bidder;
}
/**
* @return The amount of the offer.
*/
public int getAmount()
{
return amount;
}
}