• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using a List<> as an attribute

 
Ranch Hand
Posts: 150
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to implement a Client class in a practice project that I am trying to create. The Client (a company that a recruiting company has as a client) and the Client can have more than one role to fill. I am trying to figure out how to implement the role field. I am hoping somone can tell me if the following code example is a viable/best practice way of doing it.

Thanks in advance.

 
Saloon Keeper
Posts: 10719
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you have is perfectly acceptable.
 
Marshal
Posts: 4509
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may want to consider using a Set rather than a List so that you can't end-up having duplicate roles in your collection.
 
Marshal
Posts: 79234
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could also consider combining all those address attributes into an Address object. If you need to do verification of the data, it will save duplication of effort if you have it all in one place.
 
Ray Gilbert
Ranch Hand
Posts: 150
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Campbell Ritchie

Yes,  that is something I plan to do. I am also thinking that should create a "BaseEntity" class and extend all of the model classes from it  to handle IDs rather than repeating.
 
Ray Gilbert
Ranch Hand
Posts: 150
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right now however, I am still trying to figure out how to display the List<Role> along with the Client. I'm getting null for now. Imma get back to hacking at it.
 
Carey Brown
Saloon Keeper
Posts: 10719
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Initialize it where it is declared in the class body.
 
Ray Gilbert
Ranch Hand
Posts: 150
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I'm going to have to wait until I learn JDBC, mySQL, and DAOs or maybe Spring and Spring Boot or save up a fortune to hire someone who already has 100 years of studying code to be able to display the data the way I want.
 
Campbell Ritchie
Marshal
Posts: 79234
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you shou;d try and display the data now. If they don't appear as you like it first time, you can easily change their format.
 
Ray Gilbert
Ranch Hand
Posts: 150
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Campbell Ritche

I was able to print out both lists with the one display method but I don't know how to get roles to print with clients. My goal is to display the client followed by all the roles from that client, then the next client followed be their roles.
I don't know how to do that with two different lists.
 
Marshal
Posts: 28221
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First define a Client which contains only one Role. Write your code to print a Client to your satisfaction.

Next modify Client to contain a List of Roles. Go through your code and find the place where you print the Role. Modify that little bit of code to print the List of Roles.
 
Ray Gilbert
Ranch Hand
Posts: 150
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tried chatGPT and I feel a lot better. Apparently, it is as stupid as I am, It can't solve it either. Lol
 
Sheriff
Posts: 17644
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

Ray Gilbert wrote:Tried chatGPT and I feel a lot better. Apparently, it is as stupid as I am, It can't solve it either. Lol


This won't make you feel better but ChatGPT is just a program. It only gives as good as it gets, so it's kind of like talking to a mirror.

Corey seems to have found a way to prompt it to give back some pretty decent code. He even showed that ChatGPT can even "understand" how to refactor code. https://coderanch.com/t/769972/java/ChatGPT-Java
 
Greenhorn
Posts: 5
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.
Not sure i've understood your problem.
Let me rephrase.

You have 3 classes :







You want to display all clients and their roles... let's say in text mode in a main().

If so, you can naively do this that way :



To go further, you could define toString() methods in all your business objects.
To go even further, maybe you could programm defensively. Starting by returning a deap copy of the roles Sets returned by getRoles()...

Hope this helps.
Regards.
 
Carey Brown
Saloon Keeper
Posts: 10719
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your Client class needs a toString() method that knows how to display itself, and as part of that method it  should have a loop that goes through all of the Roles and calls the toString() method for the Role class.
 
Carey Brown
Saloon Keeper
Posts: 10719
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Hector Beaujean
Greenhorn
Posts: 5
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think @Carey Brown gave a great reply.

To go further, maybe if you want an API on client to expose that client roles, you should write something like this :
Set<Role> getRoles() {}
on the client class.

To program defencively, this m├ęthod would return a copy of the real Set<>, or even better, maybe a deep copy.
 
reply
    Bookmark Topic Watch Topic
  • New Topic