Question

Modify the Auction class according to these exercises: 4.48: close method 4.49: getUnsold method 4.51: Rewrite getLot method 4.52: removeLot method For help watch Textbook Video Note 4.2 which sho...

  1. Modify the Auction class according to these exercises:
    • 4.48: close method
    • 4.49: getUnsold method
    • 4.51: Rewrite getLot method
    • 4.52: removeLot method
  2. For help watch Textbook Video Note 4.2 which shows you how to use and test the auction project, as well as solving the getUnsold exercise.
  3. Document with javadoc and use good style (Appendix J) as usual.
  4. Test your code thoroughly as you go.
  5. Create a jar file of your project.
    1. From BlueJ, choose Project->Create Jar File...
    2. Check the "Include source" check box

import java.util.ArrayList;

/**
* A simple model of an auction.
* The auction maintains a list of lots of arbitrary length.
*
* @author David J. Barnes and Michael Kölling.
* @version 2011.07.31
*/
public class Auction
{
// The list of Lots in this auction.
private ArrayList<Lot> lots;
// The number that will be given to the next lot entered
// into this auction.
private int nextLotNumber;

/**
* Create a new auction.
*/
public Auction()
{
lots = new ArrayList<Lot>();
nextLotNumber = 1;
}

/**
* Enter a new lot into the auction.
* @param description A description of the lot.
*/
public void enterLot(String description)
{
lots.add(new Lot(nextLotNumber, description));
nextLotNumber++;
}

/**
* Show the full list of lots in this auction.
*/
public void showLots()
{
for(Lot lot : lots) {
System.out.println(lot.toString());
}
}
  
/**
* Make a bid for a lot.
* A message is printed indicating whether the bid is
* successful or not.
*
* @param lotNumber The lot being bid for.
* @param bidder The person bidding for the lot.
* @param value The value of the bid.
*/
public void makeABid(int lotNumber, Person bidder, long value)
{
Lot selectedLot = getLot(lotNumber);
if(selectedLot != null) {
Bid bid = new Bid(bidder, value);
boolean successful = selectedLot.bidFor(bid);
if(successful) {
System.out.println("The bid for lot number " +
lotNumber + " was successful.");
}
else {
// Report which bid is higher.
Bid highestBid = selectedLot.getHighestBid();
System.out.println("Lot number: " + lotNumber +
" already has a bid of: " +
highestBid.getValue());
}
}
}

/**
* Return the lot with the given number. Return null
* if a lot with this number does not exist.
* @param lotNumber The number of the lot to return.
*/
public Lot getLot(int lotNumber)
{
if((lotNumber >= 1) && (lotNumber < nextLotNumber)) {
// The number seems to be reasonable.
Lot selectedLot = lots.get(lotNumber - 1);
// Include a confidence check to be sure we have the
// right lot.
if(selectedLot.getNumber() != lotNumber) {
System.out.println("Internal error: Lot number " +
selectedLot.getNumber() +
" was returned instead of " +
lotNumber);
// Don't return an invalid lot.
selectedLot = null;
}
return selectedLot;
}
else {
System.out.println("Lot number: " + lotNumber +
" does not exist.");
return null;
}
}
}

Lot coding is

/**

* A class to model an item (or set of items) in an
* auction: a lot.
*
* @author David J. Barnes and Michael Kölling.
* @version 2011.07.31
*/
public class Lot
{
// A unique identifying number.
private final int number;
// A description of the lot.
private String description;
// The current highest bid for this lot.
private Bid highestBid;

/**
* Construct a Lot, setting its number and description.
* @param number The lot number.
* @param description A description of this lot.
*/
public Lot(int number, String description)
{
this.number = number;
this.description = description;
this.highestBid = null;
}

/**
* Attempt to bid for this lot. A successful bid
* must have a value higher than any existing bid.
* @param bid A new bid.
* @return true if successful, false otherwise
*/
public boolean bidFor(Bid bid)
{
if(highestBid == null) {
// There is no previous bid.
highestBid = bid;
return true;
}
else if(bid.getValue() > highestBid.getValue()) {
// The bid is better than the previous one.
highestBid = bid;
return true;
}
else {
// The bid is not better.
return false;
}
}
  
/**
* @return A string representation of this lot's details.
*/
public String toString()
{
String details = number + ": " + description;
if(highestBid != null) {
details += " Bid: " +
highestBid.getValue();
}
else {
details += " (No bid)";
}
return details;
}

/**
* @return The lot's number.
*/
public int getNumber()
{
return number;
}

/**
* @return The lot's description.
*/
public String getDescription()
{
return description;
}

/**
* @return The highest bid for this lot.
* This could be null if there is
* no current bid.
*/
public Bid getHighestBid()
{
return highestBid;
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1


Please let me know if you have any doubts or you want me to modify the answer. And if you find this answer useful then don't forget to rate my answer as thumps up. Thank you! :)


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;
        }
    }
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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;
    }
}


------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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;
    }
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class Person
{
    private final String name;

    public Person(String name)
    {
        this.name = name;
    }

    public String getName()
    {
        return name;
    }
}
Auction [-/ldeaProjects Auction] - .src/Auction.java [Auction] Auction-src Auction e IAuction javaLot java Bid java XPerson,j

Add a comment
Know the answer?
Add Answer to:
Modify the Auction class according to these exercises: 4.48: close method 4.49: getUnsold method 4.51: Rewrite getLot method 4.52: removeLot method For help watch Textbook Video Note 4.2 which sho...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In java 8. Modify the averageTopSpeed() method (in the class FormulaOne) using only lambdas and streams...

    In java 8. Modify the averageTopSpeed() method (in the class FormulaOne) using only lambdas and streams and print it to the terminal via the driver class: import java.util.*; public class Racer implements Comparable { private final String name; private final int year; private final int topSpeed;    public Racer(String name, int year, int topSpeed){    this.name = name; this.year = year; this.topSpeed = topSpeed;    }    public String toString(){    return name + "-" + year + ", Top...

  • Define a toString method in Post and override it in EventPost. EventPost Class: /** * This...

    Define a toString method in Post and override it in EventPost. EventPost Class: /** * This class stores information about a post in a social network news feed. * The main part of the post consists of events. * Other data, such as author and type of event, are also stored. * * @author Matthieu Bourbeau * @version (1.0) March 12, 2020 */ public class EventPost extends Post { // instance variables - replace the example below with your own...

  • Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a...

    Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a string s as a parameter and returns true if the title of the string is equal to s. Create the same method for your library material copies. Note that it will need to be abstract in the LibraryMaterialCopy class MY CODE **************************************************************** LibraryCard: import java.util.List; import java.util.ArrayList; import java.time.LocalDate; import    java.time.temporal.ChronoUnit; public class LibraryCard {    private String id;    private String cardholderName;   ...

  • How to build Java test class? I am supposed to create both a recipe class, and...

    How to build Java test class? I am supposed to create both a recipe class, and then a class tester to test the recipe class. Below is what I have for the recipe class, but I have no idea what/or how I am supposed to go about creating the test class. Am I supposed to somehow call the recipe class within the test class? if so, how? Thanks in advance! This is my recipe class: package steppingstone5_recipe; /** * *...

  • Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java...

    Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java //package simple; public class PartTest { public static void main(String[] args) { ArrayList<ExpendablePart> system=new ArrayList<ExpendablePart>();   // creating Part Object Part part1 = new ExpendablePart("Last, First"); part1.setNumber("AX-34R"); part1.setNcage("MN34R"); part1.setNiin("ABCD-RF-WDE-KLJM"); // printing information System.out.println(part1.toString());       //Create a part2 object of class Part Part part2=new ConsumablePart("Widget, purple"); part2.setNumber("12345"); part2.setNcage("OU812"); part2.setNiin("1234-12-123-1234");    // printing information System.out.println(part2.toString());    //checking equality of two Part class objects if(part1.equals(part2)) System.out.println("part1 and...

  • JAVA programming 9.9 Ch 9, Part 2: ArrayList Searching import java.util.ArrayList; public class ArrayListSet {    ...

    JAVA programming 9.9 Ch 9, Part 2: ArrayList Searching import java.util.ArrayList; public class ArrayListSet {     /**      * Searches through the ArrayList arr, from the first index to the last, returning an ArrayList      * containing all the indexes of Strings in arr that match String s. For this method, two Strings,      * p and q, match when p.equals(q) returns true or if both of the compared references are null.      *      * @param s The string...

  • Need help with this Java. I need help with the "to do" sections. Theres two parts...

    Need help with this Java. I need help with the "to do" sections. Theres two parts to this and I added the photos with the entire question Lab14 Part 1: 1) change the XXX to a number in the list, and YYY to a number // not in the list 2.code a call to linearSearch with the item number (XXX) // that is in the list; store the return in the variable result 3. change both XXX numbers to the...

  • I need help with adding comments to my code and I need a uml diagram for...

    I need help with adding comments to my code and I need a uml diagram for it. PLs help.... Zipcodeproject.java package zipcodesproject; import java.util.Scanner; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Zipcodesproject { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input=new Scanner(System.in); BufferedReader reader; int code; String state,town; ziplist listOFCodes=new ziplist(); try { reader = new BufferedReader(new FileReader("C:UsersJayDesktopzipcodes.txt")); String line = reader.readLine(); while (line != null) { code=Integer.parseInt(line); line =...

  • DataSetEmployee Can you please help me the JAVA program? Here is the requirement. Rewrite DataSetBook to...

    DataSetEmployee Can you please help me the JAVA program? Here is the requirement. Rewrite DataSetBook to be DataSetEmployee. DataSetEmployee should extend ArrayList<Employee> and have no instance variables. You'll have to decide what replaces the getPages concept for getMax/getMin. As a hint, what method of Employee returns a numeric value? Make a package com.acme.midmanager. This package should contain the Manager class. Make a package com.acme.personnel. This package should contain the DataSetEmployee class. Make a package com.acme.topmanagement. This package should contain the...

  • in java coorect this code & screenshoot your output ---------------------------------------------------------------------- public class UNOGame {     /**...

    in java coorect this code & screenshoot your output ---------------------------------------------------------------------- public class UNOGame {     /**      * @param args the command line arguments      */         public static void main(String[] args) {       Scanner input=new Scanner(System.in);          Scanner input2=new Scanner(System.in);                             UNOCard c=new UNOCard ();                UNOCard D=new UNOCard ();                 Queue Q=new Queue();                           listplayer ll=new listplayer();                           System.out.println("Enter Players Name :\n Click STOP To Start Game..");        String Name = input.nextLine();...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT