aspose file tools*
The moose likes Beginning Java and the fly likes Assigning an object to a field in another object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Assigning an object to a field in another object" Watch "Assigning an object to a field in another object" New topic
Author

Assigning an object to a field in another object

Jack Fletcher
Greenhorn

Joined: Oct 09, 2012
Posts: 13
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

Joined: Mar 28, 2012
Posts: 859

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

Joined: Sep 13, 2007
Posts: 2615
    
    9

This post is exactly the same one as this thread

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


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8419
    
  23

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11498
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assigning an object to a field in another object