Is there a way to map multiple values to the same 'person' without creating objects for them each time.

I.E I have a class called Person and Ill need to continuously add to it information like the type of car this person owns (more than one car). I want to be able to add new persons and new cars associated to THAT person.

I need the whole system to be done via terminal so eliminating the need to do the following in the main class for each person.
Person John = new Person();

ALL WITHOUT USING DATABASES. How can I do this?

Recommended Answers

All 3 Replies

map multiple values to the same 'person' without creating objects for them each time.

The Map class would allow you to have many keys refer to the same object.
Or you could have an array (or any container) with many of its elements pointing to the same object.

need to continuously add to it information like the type of car this person owns (more than one car). I want to be able to add new persons and new cars associated to THAT person.

The Person class could contain references to other classes like ArrayList that can hold as much data as you want to add to it.

I'm sorry, maybe it's because I'm not that experienced as a user but I dont quite understand how I would implement what you said

Define variables of different container classes in the Person class to hold the values.
Add methods to the Person class to add specific data types to the appropriate container.

class Person {
  Acontainer theCars = new AContainer();

  public void addCar(Car aCar) {
     theCars.add(aCar);   // save the car in the container
  }
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.