-1

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?

2
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by NormR1
0

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.

0

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

0

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
  }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.