Results 1 to 19 of 19
  1. #1
    Bob80 is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default Simple bid collector

    Hi all,

    I am new to Java and tried following code to experiment with it. It is an array of images which you can bid on. The thing is, I don't really know how to show all bids which are placed... can somebody assist me? Thank you!

    Java Code:
    //////Images.java
    
    import java.util.ArrayList;
    import java.util.Iterator;
    
    public class Images {
    	
    	private ArrayList images;
    	
    	public Images() {
    		
    		images = new ArrayList();
    		
    	}
    	public void addImage(String image, String price){
    		
    		images.add(new Item(image, price));
    	}
    	public void showImages() {
    		
    		Iterator it = images.iterator();
    		while(it.hasNext()){
    			
    			Item item = (Item) it.next();
    			System.out.println(item.showBoth());
    		}
    	}
    	public void showSize(){
    		System.out.println(images.size());
    	}
    	public void bidFor(int number, String bidder, int bid){
    		Item selectedItem = getItem(number);
    		selectedItem.bidFor(bidder, bid);
    		System.out.println("The bid of " + bid + " dollar by " + bidder + " on Item " + number + " was accepted.");
    	}
    	public Item getItem(int number){
    		number = number -1;
    		Item selectedItem = (Item) images.get(number);
    		return selectedItem;
    		
    	}
    	public void showAllBids(){
    		//don't know how to begin at this.
    	}
    	
    }
    Java Code:
    /////Item.java
    
    
    public class Item {
    	
    	private String name;
    	private String cost;
    	private String bidder;
    	private int price;
    	
    	public Item(String image, String price) {
    		name = image;
    		cost = price;
    	}
    
    	public String showBoth() {
    		return(name + " costs " + cost + " dollar.");
    		
    	}
    	public void bidFor(String bidder, int price){
    		this.bidder = bidder;
    		this.price = price;
    		
    	}
    
    }
    Last edited by Bob80; 08-31-2012 at 02:21 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,757
    Rep Power
    19

    Default Re: Simple bid collector

    Loop over the array list and print out each Item?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Bob80 is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default Re: Simple bid collector

    Quote Originally Posted by Tolls View Post
    Loop over the array list and print out each Item?
    You mean the image array with the items? Because the bid on an item itself is never stored in an array, and I only want to show the bids?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,274
    Rep Power
    25

    Default Re: Simple bid collector

    Where and when do you want to show the bids?
    What does the program print out now? Can you post its output and add some comments to show what you want its output to look like.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Bob80 is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default Re: Simple bid collector

    Quote Originally Posted by Norm View Post
    Where and when do you want to show the bids?
    What does the program print out now? Can you post its output and add some comments to show what you want its output to look like.
    Where and when doesn't really matter. Let's say when I call the showAllBids() method in the main Java.

    Let's say I have following bids:

    Java Code:
    Images images = new Images();
    images.addImage("image1", "50");
    images.addImage("image2", "40");
    images.addImage("image3", "30");
    	
    images.bidFor(1, "An", 20);
    images.bidFor(1, "Jay", 40);
    images.bidFor(3, "Mike", 10);
    Then I would like to output it something like this:

    Bid Overview
    __________
    Item 1
    An placed a bid of 20 dollars.
    Jay placed a bid of 40 dollars.

    Item 2
    No bids placed.

    Item 3
    Mike placed a bid of 10 dollars.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,757
    Rep Power
    19

    Default Re: Simple bid collector

    Quote Originally Posted by Bob80 View Post
    You mean the image array with the items? Because the bid on an item itself is never stored in an array, and I only want to show the bids?
    The bid for an Item is held in the Item, which is held in the ArrayList (and you need to read up on generics...that should be an ArrayList<Item>).
    So loop over that and print out the relevant bits. You'll need some methods to get at the data in Item, but I'm sure you can handle getters.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,757
    Rep Power
    19

    Default Re: Simple bid collector

    Our posts crossed.
    If you want all the bids that have ever been placed on an Item then you need to store them with the Item.
    Currently you are only storing the latest bid.
    I would suggest a Bid class, and the Item will hold an array of these as historicalBids, as well as a latestBid object.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,274
    Rep Power
    25

    Default Re: Simple bid collector

    Where and when doesn't really matter.
    Then print them as soon as they are received,say in the bidFor() method.
    Don't wait to print them later when a method is called.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Bob80 is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default Re: Simple bid collector

    Ok, I realize now that is does matter. It is possible that bids come in chaotically (bid for item 1, bid for item 2, again bid for item 1, etc...) The method I want to write should categorize the bids per item. (maybe even if possible from low bids to high bids)
    If I print them immediately it will just list up all bids as they come in.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,274
    Rep Power
    25

    Default Re: Simple bid collector

    Then look at what Tolls suggested: Create a class and save instances of it in a collection in the Item object.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    Bob80 is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default Re: Simple bid collector

    Quote Originally Posted by Norm View Post
    Then look at what Tolls suggested: Create a class and save instances of it in a collection in the Item object.
    Thanks Norm and Tolls, I'll try that!

  12. #12
    Bob80 is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default Re: Simple bid collector

    Mmm.. I don't seem to be able to figure it out.. my code right now is the following. What am I doing wrong? :s

    Java Code:
    //Images.java
    import java.util.ArrayList;
    import java.util.Iterator;
    
    public class Images {
    	
    	private ArrayList images;
    	
    	public Images() {
    		
    		images = new ArrayList<Item>();
    		
    	}
    	public void addImage(String image, String price){
    		
    		images.add(new Item(image, price));
    	}
    	public void showImages() {
    		
    		Iterator it = images.iterator();
    		while(it.hasNext()){
    			
    			Item item = (Item) it.next();
    			System.out.println(item.showBoth());
    		}
    	}
    	public void showSize(){
    		System.out.println(images.size());
    	}
    	public void bidFor(int number, String bidder, int bid){
    		Item selectedItem = getItem(number);
    		selectedItem.bidFor(bidder, bid);
    		System.out.println("The bid of " + bid + " dollar by " + bidder + " on Item " + number + " was accepted.");
    	}
    	public Item getItem(int number){
    		number = number -1;
    		Item selectedItem = (Item) images.get(number);
    		return selectedItem;
    		
    	}
    	public void showAllBids(){
    		//iterator here.
    	}
    	
    }
    Java Code:
    import java.util.ArrayList;
    
    
    public class Item {
    	
    	private String name;
    	private String cost;
    	private String bidder;
    	private int price;	
    	private ArrayList bids;
    	
    	
    	public Item(String image, String price) {
    		name = image;
    		cost = price;
    	}
    
    	public String showBoth() {
    		return(name + " costs " + cost + " dollar.");
    		
    	}
    	public void bidFor(String bidder, int price){
    		
    		bids = new ArrayList();
    		
    		bids.add(new Bid(bidder, price));
    		
    		//this.bidder = bidder;
    		//this.price = price;
    		
    	}
    
    }
    Java Code:
    public class Bid {
    	String bidder;
    	int amount;
    
    	
    	public Bid(String bidder, int amount) {
    		
    		this.bidder = bidder;
    		this.amount = amount;
    		
    		
    	}
    
    }

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,274
    Rep Power
    25

    Default Re: Simple bid collector

    What happens when you compile and execute the code?
    Can you explain what the problem is?
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    Bob80 is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default Re: Simple bid collector

    Quote Originally Posted by Norm View Post
    What happens when you compile and execute the code?
    Can you explain what the problem is?
    I should explain better what the problem is indeed.
    It doesnt really give an error, but i think it is not well structured. Now I think that everytime when the method bidFor is called, it makes a new ArrayList bids and adds just that last bid. I guess it overwrites the old ArrayList bids each time?

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,274
    Rep Power
    25

    Default Re: Simple bid collector

    If you want the list to be associated with the Item, the create the list when the Item is created, not every time a bid is made.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    Bob80 is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default Re: Simple bid collector

    Quote Originally Posted by Norm View Post
    If you want the list to be associated with the Item, the create the list when the Item is created, not every time a bid is made.
    I got it, I'll share the solution. If you have tips considering the coding, please tell me :) Thanks!

    Java Code:
    import java.util.ArrayList;
    import java.util.Iterator;
    
    public class Images {
    	
    	private ArrayList images;
    	
    	public Images() {
    		
    		images = new ArrayList<Item>();
    		
    	}
    	public void addImage(String image, String price){
    		
    		images.add(new Item(image, price));
    	}
    	public void showImages() {
    		
    		Iterator it = images.iterator();
    		while(it.hasNext()){
    			
    			Item item = (Item) it.next();
    			System.out.println(item.showBoth());
    		}
    	}
    	public void showSize(){
    		System.out.println(images.size());
    	}
    	public void bidFor(int number, String bidder, int bid){
    		Item selectedItem = getItem(number);
    		selectedItem.bidFor(bidder, bid);
    		System.out.println("The bid of " + bid + " dollar by " + bidder + " on Item " + number + " was accepted.");
    	}
    	public Item getItem(int number){
    		number = number -1;
    		Item selectedItem = (Item) images.get(number);
    		return selectedItem;
    		
    	}
    	public void showAllBids(){
    				
    		Iterator it = images.iterator();
    		while(it.hasNext()){
    			Item item = (Item) it.next();
    			System.out.println(item.getName());
    			System.out.println("-------------");
    			item.showItemBids();
    			
    		}
    	}
    	
    }
    Java Code:
    import java.util.ArrayList;
    
    
    public class Item {
    	
    	private String name;
    	private String cost;
    	private String bidder;
    	private int price;	
    	private ArrayList bids;
    	
    	 
    	public Item(String image, String price) {
    		name = image;
    		cost = price;
    		bids = new ArrayList();
    	}
    
    	public String showBoth() {
    		return(name + " costs " + cost + " dollar.");
    		
    	}
    	public void bidFor(String bidder, int price){
    				
    		bids.add(new Bid(bidder, price));
    		
    		//this.bidder = bidder;
    		//this.price = price;
    		
    	}
    	public String getName(){
    		return name;
    	}
    	public void showItemBids(){
    		
    		if(bids.size() != 0){
    			
    			for(int i=0;i<bids.size();i++){
    			
    			System.out.println(((Bid) bids.get(i)).getBid());
    			
    			}
    		}
    		
    			else {
    				
    				System.out.println("Bummer, no bids placed.");
    			}
    			
    		}
    		
    	}
    Java Code:
    public class Bid {
    	String bidder;
    	int amount;
    
    	
    	public Bid(String bidder, int amount) {
    		
    		this.bidder = bidder;
    		this.amount = amount;
    		
    		
    	}
    	
    	public String getBid(){
    		
    		
    		return bidder + " offered " + amount;
    		
    	}
    
    }

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,274
    Rep Power
    25

    Default Re: Simple bid collector

    You could use generics to define the ArrayList to show it contains Bid objects and save having to cast the objects you get from the list.
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    Bob80 is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default Re: Simple bid collector

    You mean this?

    bids = new ArrayList<Bid>();

    What is the advantage of it? Just that the ArrayList knows what to expect? Speed in execution time?

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,274
    Rep Power
    25

    Default Re: Simple bid collector

    Allows the compiler to check that the add() is for the right class and to do a cast for gets.
    Helps keep the programmer from making a mistake.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Garbage Collector
    By giorgi in forum New To Java
    Replies: 27
    Last Post: 05-20-2011, 10:09 AM
  2. about garbage collector
    By sudharani in forum New To Java
    Replies: 1
    Last Post: 04-25-2011, 11:25 AM
  3. Q about Garbage Collector
    By m00nchile in forum New To Java
    Replies: 4
    Last Post: 02-05-2010, 05:57 AM
  4. Garbage Collector and finalize()
    By arefeh in forum New To Java
    Replies: 5
    Last Post: 01-09-2010, 09:04 PM
  5. How to use the garbaje collector
    By Eric in forum Advanced Java
    Replies: 2
    Last Post: 06-29-2007, 01:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •