Results 1 to 2 of 2
  1. #1
    Chopaan is offline Member
    Join Date
    Jan 2011
    Posts
    1
    Rep Power
    0

    Default Problem with my assignment - in need of help!

    My assignment for class is about creating a program where a person can own several things. Either jewelry, stocks or gadgets..

    I have therefore created 5 classes: Person, Thing, and then Jewelry, Stock and Gadget as subclasses of Thing.

    In the program the user can create a person, which will be saved in an ArrayList called allaPersoner. Another thing the user can do is create a thing, which will be owned by a certain person, decided when creating a 'thing'.
    To me it seems that every person would need an ArrayList for its things.

    Problem is, I don't know how to make it create an ArrayList for every object of Person that is created, and how would I access a specific ArrayList based on a name of a Person later on?


    It's kind of hard to explain when you're new and not familiar with all the terms and the language at all but any help would be much appreciated!

    My main program(not all of it):
    Java Code:
    public class HuvudProgram {
    		ArrayList<Person> allaPersoner = new ArrayList<Person>();
    		Scanner sc=new Scanner(System.in);
             Person getPerson(String namn){
    		for (Person p : allaPersoner)
    		if (p.getNamn().equalsIgnoreCase(namn))
    		return p;
    		return null;
    }
    	
    	void skapaPerson(){
    		String namn = readString("Nya personens namn: ");
    		Person p = getPerson(namn);
    		if (p!=null)
    		System.out.println("Den personen finns redan");
    		else{
    		Person ny = new Person(namn);
    		allaPersoner.add(ny);
    		}
    	}
           void skapaPryl(){
    		System.out.println("Skapa pryl."); //this is where a 'thing' should be created
    	}
    My Person class (As you see I've tried to put an ArrayList in it but I'm just not good enough to succeed):
    Java Code:
    public class Person {
    	Scanner sc=new Scanner(System.in);
    	private ArrayList<Pryl> prylar = new ArrayList<Pryl>(); //Pryl means thing.
    	private String namn;
    	
    	public Person(String namn){
    		this.namn=namn;
    		ArrayList<Pryl> prylar = new ArrayList<Pryl>();
    	}
    	
    
    	public String getNamn(){
    		return namn;
    	}
    	public ArrayList<Pryl> getPryl(){ 
    		return prylar; 
    	}
    }

    ANY help with what methods I would need or whatever is well needed, thank you!

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    You have made a good start. Just one problem, you have declared an ArrayList prylar as an instance variable as well as local to the constructor.

    Now, what methods do you need? As many as it takes for you to complete your assignment. Do you need to add Things to a Person's list? Hmmm maybe a method will do that. Do you need to find a Thing in a Person's list? Hmmm maybe a method can do that too. What else?

Similar Threads

  1. assignment problem with List<T>
    By Newbie666 in forum New To Java
    Replies: 11
    Last Post: 01-21-2010, 12:12 PM
  2. The Assignment Problem
    By bumblyb33 in forum New To Java
    Replies: 5
    Last Post: 03-04-2009, 04:21 AM
  3. Problem with an assignment: Backgammon game
    By Poddy in forum New To Java
    Replies: 6
    Last Post: 02-05-2009, 05:32 AM
  4. assignment problem help needed
    By tiggz1980 in forum New To Java
    Replies: 2
    Last Post: 02-06-2008, 11:14 PM
  5. Cannot solve the coding problem of my assignment
    By elimmom in forum New To Java
    Replies: 3
    Last Post: 08-13-2007, 11:33 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
  •