AUCTION.JAVA

package English_Auction;

/**
 *
 * 
 */

public class Auction
{
    // A unique identifying number.
    private final int number;
    // A description of the lot.
    private String description;
    //
    private int startPrice;
    //
    private int reserve;
    // The current highest bid for this lot.
    private Bid highbid;
    //
    private Bid highestBid;

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

    /**
     * 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((highbid == null) ||
               (bid.getValue() > highbid.getValue())) {
            // This bid is the best so far.
            highbid = bid;
            return true;
        }
        else {
            return false;
        }
    }

    /**
     * @return A string representation of this lot's details.
     */
    public String toString()
    {
        String details = number + ": " + description + " Start " + startPrice + " Reserve "+ reserve;
        if(highbid != null) {
            details += "    Bid: " +
                       highbid.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 gethighbid()
    {
        return highbid;
    }




    public int findHighestBidder(Bid bid)
    {
        if((highestBid == null) ||
               (bid.getValue() > highestBid.getValue())) {
            // This bid is the best so far.
            highestBid = bid;
            return (int)(highestBid.getValue());
        }
        else {
            //return 0;
            return (int)(highestBid.getValue());
        }
    }
}

BID.JAVA

package English_Auction;

/**
 *
 * 
 */
//public class Bid {
public class Bid
{
    // The person making the bid.
    private final Bidder bidder;
    // The value of the bid. This could be a large number so
    // the long type has been used.
    private final long value;

    /**
     * Create a bid.
     * @param bidder Who is bidding for the lot.
     * @param value The value of the bid.
     */
    public Bid(Bidder bidder, long value)
    {
        this.bidder = bidder;
        this.value = value;
    }

    /**
     * @return The bidder.
     */
    public Bidder getBidder()
    {
        return bidder;
    }

    /**
     * @return The value of the bid.
     */
    public long getValue()
    {
        return value;
    }
}

BIDDER.JAVA

package English_Auction;

/**
 *
 * 
 */
//public class Bidder {
public class Bidder
{
    // The name of this person.
    private final String name;
    private int strategy;
    /**
     * Create a new person with the given name.
     * @param name The person's name.
     */
    public Bidder(String name, int strategy)
    {
        this.name = name;
        this.strategy = strategy;

    }

    /**
     * @return The person's name.
     */
    public String getName()
    {
        return name;
    }
}


//}

HIGHESTBIDDER.java

package English_Auction;

/**
 *
 * 
 */
public class HigherBidder {
//public class HighestBidder {

    Bid highestBid;

    public int findHighestBidder(Bid bid)
    {
        if((highestBid == null) ||
               (bid.getValue() > highestBid.getValue())) {
            // This bid is the best so far.
            highestBid = bid;
            return (int)(highestBid.getValue());
        }
        else {
            return 0;
        }
    }
}

//}

runauction.java

package English_Auction;

/**
 *
 * 
 */
public class RunAuction {
   //public class RunAution {
    public static void main(String[] args) {

        Bidder B1= new Bidder("John",1);
        Bidder B2= new Bidder("Paul",1);
        Bidder B3= new Bidder("Peter",1);

        Auction ac = new Auction(1,"Ferrari", 10000, 30000);

        //HighestBidder HB1= new HighestBidder();
        Bid b1=new Bid(B2,5000);
        Bid b2=new Bid(B1,10000);
        Bid b3=new Bid(B3,15000);

        System.out.println(ac.findHighestBidder(b1));
        System.out.println(ac.findHighestBidder(b3));
        System.out.println(ac.findHighestBidder(b1));
    }
}
//}

I am trying to get the highest bid from the bids that have been passed.
when someone bids, I would like to get the bid value into an array of n size and then do bubble sort to get the highest bid value. I could not get this work..could anyone please give me some hints. Any help much appreciated.

Many thanks
Dee

What do you mean, you couldn't get it to work? What did you expect to see, and what happened instead?
(It's easier to diagnose the fault if you give us something to look for!)

I wanted to know how do i pass the bid values into an array and do bubble sort?

Any program code help much appreciated.

For the sort part: by "do bubble sort" do you mean you need help writing the code for a bubble sort, or do you mean you have a bubble sort that you don't know how to apply here?

Passing the values into an array is not hard, as long as you know how many you have. Suppose you're getting the values from the command line - ask the user how many they're going to enter, and declare an array of that length. Then, in a for loop from i=0 to i<n, array[n]= the user input.

That's simplified, of course, but it's the essentials.

thanks for your reply. I have bubble sort code that goes something like below-
int temp, counter, index;

    for(counter=0; counter<length-1; counter++) { //Loop once for each element in the array.
        for(index=0; index<length-1-counter; index++) { //Once for each element, minus the counter.
            if(unsortedArray[index] > unsortedArray[index+1]) { //Test if need a swap or not.
                temp = unsortedArray[index]; //These three lines just swap the two elements:
                unsortedArray[index] = unsortedArray[index+1];
                unsortedArray[index+1] = temp;
            }

But how do I apply it here in my program?

I have created 3 new objects for the bid class as below -

Bid b1=new Bid(B2,5000);
Bid b2=new Bid(B1,10000);
Bid b3=new Bid(B3,15000);

I would like to pass the value 5000, 10000, 15000 into an array of size n

then bubblesort() program should be called. Upon when the highest value should be returned(which would be highest bid in my english auction program)

Hope this is clear. Any program code hints would be very much appreciated.

Okay, let's rephrase this:

I have created 3 new objects for the bid class as below

as this:

I have a collection of objects of the bid class.

Instead of a handful of objects which happen to be of the same type, all rolling around loose and maybe getting lost under the sofa, let's have one object which consists of a collection of objects of that type. Perhaps they're in a basket or something.
This collection we'll call an array.

So, instead of

Bid b1=new Bid(B2,5000);
Bid b2=new Bid(B1,10000);
Bid b3=new Bid(B3,15000);

we'll have

Bid[] bids = new Bid[3];
bids[0]=new Bid(B2,5000);
bids[1]=new Bid(B1,10000);
bids[2]=new Bid(B3,15000);

Now you've got an array. Probably you'll want to make this a bit more flexible - let the user input values, however many they want - but this will do for now.

So - how would you change your bubble sort to make it work for the array here? You'll have to get the values by which the array is to be ordered, which means that you'll have to know what sort of array you're sorting. Go ahead and see if you can make that method work.


(there's a further trick, and if you want we can talk about the Comparable interface, which will make your sorting much more flexible, but let's do first things first)

Thanks for your reply. Please could you help me in sorting the values by using bubble sort? or comparable interface

You've got an array, and you've got the code for a bubble sort. Give it a try. Really, if you understand that code you posted you can figure out how to make this work.

p

ackage English_Auction;

/**
 *
 * @author Deepu
 */
public class RunAuction {
   //public class RunAution {

    private static void bubbleSort(int[] Bid, int length) {
        int temp, counter, index;

        for(counter=0; counter<length-1; counter++) { //Loop once for each element in the array.
            for(index=0; index<length-1-counter; index++) { //Once for each element, minus the counter.
                if(Bid[index] > Bid[index+1]) { //Test if need a swap or not.
                    temp = Bid[index]; //These three lines just swap the two elements:
                    Bid[index] = Bid[index+1];
                    Bid[index+1] = temp;
                }
            }
        }
    }
    public static void main(String[] args) {

        Bidder B1= new Bidder("John",1);
        Bidder B2= new Bidder("Paul",1);
        Bidder B3= new Bidder("Peter",1);

        Auction ac = new Auction(1,"Ferrari", 10000, 30000);

        //HighestBidder HB1= new HighestBidder();
        //Bid b1=new Bid(B2,5000);
        //Bid b2=new Bid(B1,10000);
    //Bid b3=new Bid(B3,15000);
        Bid[] bids = new Bid[3];
bids[0]=new Bid(B2,5000);
bids[1]=new Bid(B1,10000);
bids[2]=new Bid(B3,15000);
        



    

//System.out.println("value =" + bids[2]);
        //System.out.println(ac.findHighestBidder(bids[0]));
        //System.out.println(ac.findHighestBidder(bids[2]));
       // System.out.println(ac.findHighestBidder(bids[1]));

int Bid[] = {10000, 5000, 15000};


        bubbleSort(Bid, Bid.length); //Pass the array to be sorted and its length.

        System.out.println("The highest bid for Round 1 is : "+Bid[2]); //Just to show you it worked. :)

        
            
        }
    }

Hi , Thanks for your reply. Sorry, i am a complete newbie to java. I did amend my code, it only works when I manually pass the values into the array.

int Bid[] = {5000, 15000, 10000};

When I initiate an object called bids to bid array, I wanted the second parameter of the method("Value" in this case) to be assigned to the first element of bid array. Then I would like to call bubblesort method, which would arrange the values in ascending order and then would like to return the last element of the bid array(which is highest value i.e., 15000 in this case). I hope I am clear with what I am trying to get out of the program but just cant make it work. I am sorry. Please help.

many thanks
Dee

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.