• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

How to skip creating an object if already exists?

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First I had addEmployee() to create employees. The output was:

Type: ENGINEER, id: 1, monthly salary: 1000.0, commission: 0.0, bonus: 0.0, manager: 4.
Type: ENGINEER, id: 2, monthly salary: 1000.0, commission: 0.0, bonus: 0.0, manager: 4.
Type: SALESMAN, id: 3, monthly salary: 1000.0, commission: 100.0, bonus: 0.0, manager: 4.
Type: MANAGER, id: 4, monthly salary: 2000.0, commission: 0.0, bonus: 1000.0.

Because of inheritance I splitted up to addEngineer(), addSalesman() and addManager(). Now the output is:

Type: ENGINEER, id: 1, monthly salary: 1000.0, manager: 3.
Type: ENGINEER, id: 2, monthly salary: 1000.0, manager: 3.
Type: ENGINEER, id: 1, monthly salary: 1000.0, manager: 3. <-- must actually be skipped
Type: SALESMAN, id: 3, monthly salary: 1000.0, commission: 100.0.
Type: MANAGER, id: 4, monthly salary: 2000.0, bonus: 1000.0.

But the duplicate engineer must actually be skipped if instance already exists. I already wrote an if..else statement.
How can I skip creating an object when an object already exists?

 
Bartender
Posts: 7488
171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What type is "employee"? It looks like Set<Employee> - maybe it should be Map<Integer, Employee> instead. Then you could use the ID for a check of existence.
 
Alexander Visser
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Within this class (EmployeesAdmin) is ArrayList<Employee>.
 
Tim Moores
Bartender
Posts: 7488
171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, that amounts to the same thing here. Consider a Map instead.
 
Sheriff
Posts: 17405
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you implemented equals() for any of these classes? The contains() method will use equals() to determine whether an object is already in the collection or not.

As far as your design is concerned, it's not a very good one. What happens when you want to add additional types? You code will have to be changed to accommodate them, right? That violates a basic design principle: Open Closed Principle It also creates a lot of duplication in your code.
 
Junilu Lacar
Sheriff
Posts: 17405
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the id attribute establishes the identity of these objects, I would argue that using a Map<Integer, EmployeeType> breaks the encapsulation of these classes. I would prefer using the semantics of equals() instead.
 
Alexander Visser
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I implemented equals() and contains(). It is solved by fixing some errors in the subclasses. Indeed, also the duplicate code is fixed now.
 
Marshal
Posts: 77157
370
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How did you override equals()?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic