-3

I have to make a class housenumber. The housenumber consists of an int and a char. The housenumber will be assigned to another class House where Person can live.

What I want to do:

Whenever an object housenumber is created I want it to check if it's the same as another one or not. But only if another object has been created.

What I think could be useful:

So whenever an object is created we run the method control(); The method control is a boolean and should return true if they are the same. I know .equals(object) can be used but I don't know how to get an object in there. Lets say I create a. It should check if b is created but because there isn't hè returns a true value and the object can be created. If I create b. It will check and Find a exists. Then if b!=a it will return a false value and b can be created. If a==b it will return false and b will just be set to a (point to the same variables and stuff)

I hope it's not too vague

Beau Grantham
  • 3,435
  • 5
  • 33
  • 43

2 Answers2

2

What you want to do, is to create factory for your Housenumber objects. Such factory can should hold references (in eg. List)to every Housenumber object you will create on runtime. Implementation of of create method for such factory, should take parameters that would be normally passed to the constructor, and if there was already created objcect with the same input parameters (whitch would be equal to me) than return existing object or create new one and add it to collection otherwise.

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
0

You have to write your own implementation to check if the objects are equal. The thing is that .equals() only check if both pointers refer to the same object.

To create instances of the objects, you would have to do something like this:

HouseNumber hn1 = new HouseNumber();

And after this, if you want to compare them:

hn1.equals(hn2); //here you would have to override it with your own implementation

This implementation is not that difficult. You would have to check if the char and the int match.

Luis Lavieri
  • 4,064
  • 6
  • 39
  • 69