Results 1 to 9 of 9
  1. #1
    braddy is offline Member
    Join Date
    Sep 2010
    Posts
    11
    Rep Power
    0

    Default Help with arraylist

    Hi all

    I'm new to java programming, and I really would appreciate some of your help.
    Let say I have two classes, one is called Person, in this class I set things like name, adress, age and so on...My other class is Bike, this class is used for setting size of the bike, color, kind of bike and so on...

    My issue is that I want to collect all of this in an arraylist, but really need your help on how to do that.

    For example I can write Person person = new Person(name, age, ...) and also write Bike bike = new Bike(color, size, ...), one person can own one or many bikes. Let say I create two persons, (A & B) and three bikes (1 & 2 & 3) and I want to collect this in one (if necassary more than one) arraylist. I need to relate Mr. A to bike 1 & 2 and Mr. B to bike 3. I want to be able to add person and bikes and also to make a list of all persons and their bikes, so that I can see which bike and person are attached to each other. If I choose Mr. A it shows me A and then bike 1 & 2 as an example...

    I'm doing this training in console environment.

    Could someone please give me a sample code of how to do this, I really want to learn. It would be great if you made some smaller comments, so that I unnderstand what is happening. I have searched this on google for days without any luck. Thank you in advance.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,680
    Blog Entries
    7
    Rep Power
    21

    Default

    There is a 'has a' relationship between a Person and Bikes: a Person has a Bike. So add a collection of Bikes to a Person class and implement set/get methods for it in the Person class. Possibly the collection can be a Set<Bike> because a Person only owns (has) a bike only once ...

    kind regards,

    Jos

  3. #3
    braddy is offline Member
    Join Date
    Sep 2010
    Posts
    11
    Rep Power
    0

    Default

    Hi thanks for your reply, but please consider that I'm quite new to this so I would really appreciate a code example to make me understand what's going on. I'm not asking you for writing a book just a couple of lines of code to get me started. For example in my bike class should I have "public list myList()" to collect and pass on to person class? Thank you.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    We usually find that folks learn much better if they try on their own first, and post their code attempts, mistakes and all, if there are problems. Besides, you have nothing to lose!

    Best of luck!

  5. #5
    braddy is offline Member
    Join Date
    Sep 2010
    Posts
    11
    Rep Power
    0

    Default

    Hi again
    I found an example and I think I'm on right track now, could you please have a look and comment my code. I can not seem to get the printing correct, it just print the last record and not both if I choose to enter two bikes or more, why? Also any suggestion to how I'm gonna be able to select a person to add another bike later on?

    Here is my code:

    For my person class...
    Java Code:
    import java.util.List;
    
    	public class Person 
    {
    	private List<Bike> bikes;
    	private String name;
    	private int age;
    
    	public String getName() 
    		{
    			return name;
    		}
    
    	public void setName(String name) 
    		{
    			this.name = name;
    		}
    
    	public List<Bike> getBikes() 
    		{
    			return bikes;
    		}
    
    	public void setBikes(List<Bike> bikes) 
    		{
    			this.bikes = bikes;
    		}
    
    	public int getAge() 
    		{
    			return age;
    		}
    
    	public void setAge(int age) 
    		{
    			this.age = age;
    		}
    	public String toString()
        	{
        		return name;
       		}
    }
    And for my bike class....
    Java Code:
    	public class Bike 
    {
        private String type;
        private String color;
        private int year;
    
        public String getType() 
        {
            return type;
        }
    
        public void setType(String type) 
        {
            this.type = type;
        }
    
        public String getColor() 
        {
            return color;
        }
    
        public void setColor(String color) 
        {
            this.color = color;
        }
        
        public String toString()
        {
        	return type;
        }
        
        public void setYear(int year)
        {
        	this.year = year;
        }
        
        public int getYear()
        {
        	return year;
        }
    }
    And to run the program...
    Java Code:
    import java.util.*;
    
    public class Handler 
    {
        public static void main(String args[]) 
        {
        	Scanner scan = new Scanner(System.in);
        	System.out.print("How many reg: ");
        	int quantity = scan.nextInt();
        	
        	int i = 0;
        	while(i < quantity)
        	{
    		Driver drive = new Driver();
    		drive.Drive();
    		i++;
        	}
        }
    }
    class Driver
    {		 
    		private String bikeType, ownerName, bikeColor;
    		int year, age;
    			
    	    public void Drive()
        {
        	Scanner scan = new Scanner(System.in);
    
    		System.out.print("Enter type of bike: ");
    		bikeType = scan.nextLine();
    		System.out.print("Enter color of bike: ");
    		bikeColor = scan.nextLine();
    		System.out.print("Enter year of manufacturing: ");
    		year = scan.nextInt();
    		scan.nextLine();
    
    		System.out.print("Enter name of owner: ");
    		ownerName = scan.nextLine();
    		System.out.print("Enter age of owner: ");
    		age = scan.nextInt();
    		scan.nextLine();
    		
    		Bike bikes = new Bike();
            bikes.setType(bikeType);
            bikes.setColor(bikeColor);
            bikes.setYear(year);
    	
    		List<Bike> list = new ArrayList<Bike>();
            list.add(bikes);
    
    		Person person = new Person();
            person.setName(ownerName);
            person.setAge(age);
            person.setBikes(list);
    
    			for(int i = 0; i < list.size(); i++)
    			{
    				System.out.println(list.get(i));
    			}		
        }
    }
    Last edited by braddy; 09-22-2010 at 12:02 AM. Reason: Didn't have code blocks...

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

  7. #7
    braddy is offline Member
    Join Date
    Sep 2010
    Posts
    11
    Rep Power
    0

    Default

    Thanks Norm

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I think you're better off if Person did not have a setBikes method. Rather, create the ArrayList<Bike> once in the Person constructor, and give Person an addBike(Bike bike) method that adds a Bike to the ArrayList. Also, in your main you should create your one Person before the while loop, and then add Bikes to the person in the while loop.

  9. #9
    braddy is offline Member
    Join Date
    Sep 2010
    Posts
    11
    Rep Power
    0

Similar Threads

  1. Creating an ArrayList from an ArrayList
    By Klahking in forum New To Java
    Replies: 17
    Last Post: 09-09-2010, 04:34 PM
  2. Help with arraylist
    By alexisasoxfan in forum New To Java
    Replies: 12
    Last Post: 05-07-2010, 07:07 AM
  3. Replies: 2
    Last Post: 04-21-2008, 12:43 PM
  4. ArrayList
    By kizilbas1 in forum New To Java
    Replies: 11
    Last Post: 12-05-2007, 08:30 PM
  5. New to arraylist
    By kleave in forum New To Java
    Replies: 2
    Last Post: 11-19-2007, 07:45 PM

Tags for this Thread

Posting Permissions

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