Hey, I'm working through Objects First with Java and using bluej. I've just gotten to where I call other classes and methods, and sometimes I can get it to work. Other times, like the example below, I'm completely suck. I've tried every combination I can think of to get the bidder's name called, but nothing works. Is it because of the 'this'? I think this is all the relevant code, but if I should post more just let me know.

Any help would be appreciated, thanks!

public String closeAuction()
{
    String details = number + ": " + description;
    if(highestBid != null) {
        details += "    Bid: " + 
                   highestBid.getValue() + " by " + //here's where I'm stuck, I want to get the bidder's name

    }
    else {
        details += "    (No bid)";
    }
    return details;
}


public Bid(Person bidder, long value)
{
    this.bidder = bidder;
    this.value = value;
}

get the bidder's name called

What variable is the bidder's name in? I don't see it in the code you posted.
Is it in the Person object reference by bidder?

What variable is the bidder's name in? I don't see it in the code you posted.
Is it in the Person object reference by bidder?

Yes.

Ok, what next? Try to say a bid more or this could take forever.

Also notice that you tried to use non-matching CODE-tags. That cannot work. Use (snip) at the end if you use (code) at the beginning, or use (snip) at the end if you use (snip) at the beginning. (Case does not matter).

You need to keep both the highest bid and the highest bidder around. Then you can do something like highestBidder.getName() . You can, of course have a Bid class that has a bidder and a bid, and set them both in the course of the Bid() method... um, I see a naming problem:

It is usually considered bad form to capitalize member data and method names. We usually make a Capitalized class name and a lower_case or lowerCamelCase method name. This kind of detail is not important to the compiler, but it makes it easier for mere humans to quickly see what is going on. Assuming you believe programmers are human of course. ;) Here's the "One True Style Guide" which of course is widely extended, interpreted, changed, and otherwise maligned by actual practitioners...

Ah, I'm learning already. I'll pour through the code again, but in the meantime here is the complete code I believe is relevant.

import java.util.ArrayList;
public class Auction
{
    private ArrayList<Lot> lots;
    private int nextLotNumber;
    private int index;

    public Auction()
    {
        lots = new ArrayList<Lot>();
        nextLotNumber = 1;
    }
    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 bidFor(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 {
                // Report which bid is higher.
                Bid highestBid = selectedLot.getHighestBid();
                System.out.println("Lot number: " + lotNumber +
                                   " already has a bid of: " +
                                   highestBid.getValue());
            }
        }
    }
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;
        }
    }
    public Lot removeLot( int number)
    {
            if((number >= 1) && (number < nextLotNumber)) 
            {
                Lot selectedLot = lots.get(number - 1);
                lots.remove(selectedLot);
                return selectedLot;
            }
            else 
            {
                System.out.println("Lot number: " + number +
                               " does not exist.");
                return null;
            }
    }
    public void close()
    {
        for(Lot lot : lots) 
        {
            System.out.println(lot.closeAuction());
        }
    }
}


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;
    public boolean bidFor(Bid bid)
    {
        if((highestBid == null) ||
               (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 String bidOn()
    {
        String details = number + ": " + description;
        {
            details += "    Bid: " + highestBid.getValue() + " by ";
        }
        return details;
    }
    public String notBidOn()
    {
        String details = number + ": " + description + "     (No bid)";
        return details;
    }
    public int getNumber()
    {
        return number;
    }
    public String getDescription()
    {
        return description;
    }
    public Bid getHighestBid()
    {
        return highestBid;
    }
     public String closeAuction()
    {
        String details = number + ": " + description;
        if(highestBid != null) {
            details += "    Bid: " + 
                       highestBid.getValue() +
                       " by ";/** bidder.getBidder;**/
        }
        else {
            details += "    (No bid)";
        }
        return details;
    }
}

public class Person
{
    private final String name;

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


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

}

This is out of a book for a class I took for awhile a couple months ago. I'm in the middle of working through it so the code probably looks strange, but if you could explain how I could make a method in the auction class that calls the bidder's name, that'd be great. Thanks!

Get a reference to the Person object (say refPerson) and call its getName method:

String theName = refPerson.getName(); // get the Person's name

The compiler says it cannot find symbol for the line.

String theName = refPerson.getName()

I remember when I was trying to get this to work I saw this error several times. Also, when I tried to call my other classes it said there was no instance of the class to reference, but isn't the whole point that there wouldn't be an instance of the class until the code is being used? I'm confused.

At the time when the code is running, when you need the name, by then you have to have an instance Person (here NormR1 suggested the name refPerson). In your case, I think line 30 becomes System.out.println("The bid by " + bidder.getName() + " for lot number " + lotNumber + " was successful.");

Sorry, I'm talking about a different section of the code. I've redownloaded the original code for the practice problem, here it is

import java.util.ArrayList;
public class Auction
{
    private ArrayList<Lot> lots;
    private int nextLotNumber;
    
    public Auction()
    {
        lots = new ArrayList<Lot>();
        nextLotNumber = 1;
    }

    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 bidFor(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 getLot(int lotNumber)
    {
        if((lotNumber >= 1) && (lotNumber < nextLotNumber)) {
            Lot selectedLot = lots.get(lotNumber - 1);
            if(selectedLot.getNumber() != lotNumber) {
                System.out.println("Internal error: Lot number " +
                                   selectedLot.getNumber() +
                                   " was returned instead of " +
                                   lotNumber);
                selectedLot = null;
            }
            return selectedLot;
        }
        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;
    }

    public boolean bidFor(Bid bid)
    {
        if((highestBid == null) ||
               (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;
    }
}

I want to add a close method for the Auction class, which goes through all my lots--if a lot is sold it prints out what person bought it, if unsold it says that it's unsold. Can you please explain how I would make the close method do this?

The way to think about this is to work it backward in your mind. You want a method Auction.close() ... and in that method you need to be able to find the lots that were sold. Where is that information? It is in the ArrayList<Lot> lots. Cool. So we need the highest bid for that lot. Do we have it? We do. It is a Bid object, which has both the amount and the person. That's it, then. You can fix this one of two places (or both): You can do the job with this in the close() method: lot.getHighestBid().getBidder().getName() or you can do something similar in method lot.toString() and just use the string value of the lot when you print out the list in the close() method.

Wow, thanks so much!
I really appreciate the help (and patience). Now I've got.

public void close()
    {
        for( Lot lot : lots )
        {
            String details = lot.getNumber() + ": " + lot.getDescription();
            if (lot.getHighestBid() != null)
            {
                System.out.println( details + " sold to " + lot.getHighestBid().getBidder().getName() + " for " + lot.getHighestBid() );
            }
            else
            {
                System.out.println( details + " remains unsold.");
            }
        }
    }

Which both compiles and works exactly how I want it to. I'll step into it now and try and really understand it from every aspect, there are still a few parts that elude me as to why they worked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.