In: Computer Science
source: /** * 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; } } Question: Implement the method batchDetail() in the BeerBatch class. Below you see the details of the first three batches created in the BearAuction constructor. For your string use the same format as below! 1: 1892 Traditional Bid: 14 by Juliet (female, 27) 2: Iceberg Lager No bid 3: Rhinegold Altbier Bid: 17 by William (male, 22).
Auction.java
import java.util.ArrayList;
import java.util.Iterator;
public class Auction
{
private ArrayList<Lot> lots;
private int nextLotNumber;
private Auction auction1;
private Lot chindo;
private Lot keyfi;
private Lot taco;
public Auction()
{
lots = new ArrayList<>();
nextLotNumber = 1;
}
public void createScenario()
{
int n = 1;
Person jack = new Person("John");
Person jill = new Person("Bob");
Lot abc = new Lot((n++), "Used Abc");
Lot def = new Lot((n++), "Def");
Lot ghijklm = new Lot((n++), "Ghi Jklm");
auction1.enterLot("abc");
auction1.enterLot("def");
auction1.enterLot("ghijklm");
auction1.makeABid(1, jack, 569);
auction1.makeABid(3, jill, 4545);
close();
}
public ArrayList<Lot> getUnsold()
{
ArrayList<Lot> unsold = new ArrayList<Lot>();
for(Lot lot: lots){
Bid bid = lot.getHighestBid();
if(bid == null){
unsold.add(lot);
}
}
return unsold;
}
public void close()
{
ArrayList noBid = new ArrayList<Lot>();
System.out.println("The results of the auction are as follows:");
System.out.println("-----------------------------------------------------");
for(Lot lot : lots){
Bid bid = lot.getHighestBid();
if(bid != null){
System.out.println("Item Number: " + lot.getNumber());
System.out.println("Item Description: " + lot.getDescription());
System.out.println("We have a winner!");
System.out.println("Bidder: " + bid.getBidder());
System.out.println("-----------------------------------------------------");
}
}
System.out.println("The following items received no bids:");
System.out.println(getUnsold());
}
public void enterLot(String description)
{
lots.add(new Lot(nextLotNumber, description));
nextLotNumber++;
}
public void showLots()
{
for(Lot lot : lots) {
System.out.println(lot.toString());
}
}
public void makeABid(int lotNumber, Person bidder, long value)
{
Lot selectedLot = getLot(lotNumber);
if(selectedLot != null) {
boolean successful = selectedLot.bidFor(new Bid(bidder,value));
if(successful) {
System.out.println("The bid for lot number " +
lotNumber + " was successful.");
}
else {
Bid highestBid = selectedLot.getHighestBid();
System.out.println("Lot number: " + lotNumber + " already has a bid of: " + highestBid.getValue());
}
}
}
public Lot removeLot(int lotToRemove)
{
int index = 0;
int rmvLot = lotToRemove;
boolean found = false;
if(lotToRemove >= lots.size()){
return null;
}
else{
while(index <= lots.size() && index <= (lotToRemove - 1) && found == false){
Lot lot = lots.get(index);
if((lots.get(index) == lots.get(rmvLot -1))){
found = true;
}
else{
index += 1;
}
}
}
return lots.get(index);
}
public Lot getLot(int lotNumber)
{
if((lotNumber >= 1) && (lotNumber < nextLotNumber)) {
return lots.get((lotNumber-1));
}
else {
System.out.println("Lot number: " + lotNumber +
" does not exist.");
return null;
}
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------
Lot.java
public class Lot
{
private final int number;
private String description;
private Bid highestBid;
public Lot(int number, String description)
{
this.number = number;
this.description = description;
this.highestBid = null;
}
public boolean bidFor(Bid bid)
{
if(highestBid == null) {
highestBid = bid;
return true;
}
else if(bid.getValue() > highestBid.getValue()) {
highestBid = bid;
return true;
}
else {
return false;
}
}
public String toString()
{
String details = number + ": " + description;
if(highestBid != null) {
details += " Bid: " +
highestBid.getValue();
}
else {
details += " (No bid)";
}
return details;
}
public int getNumber()
{
return number;
}
public String getDescription()
{
return description;
}
public Bid getHighestBid()
{
return highestBid;
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------
Bid.java
public class Bid
{
private final Person bidder;
private final long value;
public Bid(Person bidder, long value)
{
this.bidder = bidder;
this.value = value;
}
public Person getBidder()
{
return bidder;
}
public long getValue()
{
return value;
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------
Person.java
public class Person
{
private final String name;
public Person(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------
Note: I hope the above-provided solution is as per your question. If you find any error in the code then please do let me know through comments. I'll try to resolve your issues. And one request is to please upload the question and code giving space. Thank You!!
We try harder to provide you the best solutions, UpVote!!Please!! Have a Nice Day......