• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assigning an object to a field in another object

 
Jack Fletcher
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I am making a mock system for a library where each User of the library is allowed to take out up to 3 books on loan.
The system should somehow assign the name of the User loaning the book out, in the object of the Book which is loaned.
So far I have a LinkedList which stores the Book objects and another to store the Users.
How do I go about assigning a Book to a User? I need to enable the user of the system to choose a User and then add their name to the String userBorrowing field in the Book object which is to be loaned.

Thanks.
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you want to assign a User to a Book, or a Book to a User.

Since Users are (should be) unique, why not have a simple Set within the Book object containing Users?

A LinkedList is a little overkill for such a trivial relationship IMHO.

WP
 
K. Tsang
Bartender
Posts: 3348
12
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This post is exactly the same one as this thread

Please don't double post and would the admins please close of these. Thanks.
 
Winston Gutkowski
Bartender
Pie
Posts: 10103
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William P O'Sullivan wrote:Do you want to assign a User to a Book, or a Book to a User.

Seems to me that either is perfectly reasonable; but in this case Jack's been quite clear: a "userBorrowing field in the Book object which is to be loaned."

Since Users are (should be) unique, why not have a simple Set within the Book object containing Users?

@Jack: I agree with William here. A Set (and I'd suggest a HashSet) would be better than a List (actually for both Lists), because it ensures
(a) that you can't accidentally store duplicates.
(b) that finding a Book or User is really quick.

A LinkedList is a little overkill for such a trivial relationship IMHO.

Again, a List (or, as you say, a Set) of Books or Users seems perfectly reasonable to me. I wonder if you're thinking Jack means a List per Book? - that wasn't my interpretation.

Jack Fletcher wrote:So far I have a LinkedList which stores the Book objects and another to store the Users.
How do I go about assigning a Book to a User? I need to enable the user of the system to choose a User and then add their name to the String userBorrowing field in the Book object which is to be loaned.

Right, well first: DON'T make the field a String; make it a User (I presume you have such a class).

Second: I don't quite understand your problem. What is it you don't understand about assigning a Book to a User?
If you're unclear about the procedure, write it out; and write it in English, not in Java.

Winston
 
fred rosenberger
lowercase baba
Bartender
Posts: 12085
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:This post is exactly the same one as this thread

Please don't double post and would the admins please close of these. Thanks.

Done. closed the other one.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic