Win a copy of Python Continuous Integration and Delivery this week in the Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

reference one class in another and make an arraylist of objects in it  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am making a library style programme where different types of users can log in with different privileges such as admins and members. admin users process the loans and members can take the loans. they can borrow different media such as books and dvds etc. i have an arraylist of "users" in a model class and an arraylist of type "loan" in the users class. i know i need to reference the "media" class in the "loan" class as thats what the users will be able to borrow. ive been reading about coupling and object referencing for about a week and am just getting lost. where do i create the "loan" objects if i want to pre-populate the arraylist with a few loans, and have it so i can call it and create some loans when needed etc.  if anyone can point me in the right direction or give any help it would be greatly appreciated.

member class.

Media class.


Loan class.



 
Sheriff
Posts: 13155
219
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Your question is really about design in general and specifically about assigning certain responsibilities to the appropriate object. Unfortunately, this is a very broad topic and there are many things to consider that are probably beyond your current level of experience based on the code you have shown.

Having a list of Loan objects as an attribute of your FullMember class is probably not the best design choice. A Loan is a temporal relationship, meaning its existence is limited to a specific period in time. As such, it would probably be best modeled in your program as a separate entity. The first change I would make is to add a Member instance variable in the Loan class. Then, I would change the type of loanDate from String to a more appropriate type like LocalDate. Depending on your requirements, it might also be appropriate to add a field to record when the loan ended (when item was returned) and when it is due to be returned.

This is just a start; there are other issues in your code that you should address as well that requires further elaboration and discussion.
 
Junilu Lacar
Sheriff
Posts: 13155
219
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As to the question about where you would populate a list of initial Loan objects, I would expect something like that to be done by some kind of initialization process that is kicked off when the application is run. Where that is exactly we can't really say right now because you haven't shared any details about what kind of application this is and how you're running it.

If it is a command-line program, then it would be a method that gets called from main() or somewhere close to that.
 
Ryan Hodgson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:As to the question about where you would populate a list of initial Loan objects, I would expect something like that to be done by some kind of initialization process that is kicked off when the application is run. Where that is exactly we can't really say right now because you haven't shared any details about what kind of application this is and how you're running it.

If it is a command-line program, then it would be a method that gets called from main() or somewhere close to that.



thanks for the reply, it is a swing application i am building so have alot of JFrame classes for doing task such as adding new members and media etc. at start up they are all pre populated in Model class and sends messages to the controller with the member and media details. my main method creates an instance of controller "new Controller();" and then runs from there. i am only using the date as a string the now as a starting point so if i can get it working i have something to display to test.

i have the loan attribute in my fullmember class as that was how i was told how to do it so that the member would hold an arraylist of loans inside it. so if i try and create the loans in the model it cant find arraylist and doesnt create loans.

i will try implement it by adding an instance of member to the loan class and see if i can get it working.

thanks for the reply its given a better insight of what i should be trying to do.
 
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This applications screams for a db.
you're using swing? I did about the same project but in javaFx,
If you wanna have a look, you can find it on git.
Keep in mind that it's written a long time ago and I never fully finished it, but it works (allthough most likely still has a couple of bugs) and should give you a pretty good idea
 
Ryan Hodgson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:This applications screams for a db.
you're using swing? I did about the same project but in javaFx,
If you wanna have a look, you can find it on git.
Keep in mind that it's written a long time ago and I never fully finished it, but it works (allthough most likely still has a couple of bugs) and should give you a pretty good idea



ok thanks i will have a look, i just need to see how add loans to members tbh rest of programme done. a friend told me to just have the arraylist in members and bring in stock id with methods addLoan(stockID) and removeLoan(stockID) so am going to try implement it but will have a look at yours as well to hopfully get a better
understanding,

Thanks a lot, its much appreciated!!
 
Junilu Lacar
Sheriff
Posts: 13155
219
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ryan johnston wrote:i have the loan attribute in my fullmember class as that was how i was told how to do it so that the member would hold an arraylist of loans inside it.


I assume this is some kind of school work then and that it was your instructor who told you to do it that way. That's not really comforting to know. That design quickly falls apart and becomes unwieldy when you need to do something else like print a report of all media, in alphabetical order, along with who has loaned it if it is out on loan or who loaned it last if it is not out on loan. Or if you want to print out a list of items that are overdue.

so if i try and create the loans in the model it cant find arraylist and doesnt create loans.


This sounds like a very procedural-minded approach that will violate all kinds of object-oriented design principles, encapsulation and data hiding being at the top of the list.
 
Daniel Demesmaecker
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're set on this aproach I would use a separate CheckedOutClass containing the person and the media, along with some aditional variables like checkOutDate, returnDate and a boolean returned
 
Ryan Hodgson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Ryan johnston wrote:i have the loan attribute in my fullmember class as that was how i was told how to do it so that the member would hold an arraylist of loans inside it.


I assume this is some kind of school work then and that it was your instructor who told you to do it that way. That's not really comforting to know. That design quickly falls apart and becomes unwieldy when you need to do something else like print a report of all media, in alphabetical order, along with who has loaned it if it is out on loan or who loaned it last if it is not out on loan. Or if you want to print out a list of items that are overdue.

so if i try and create the loans in the model it cant find arraylist and doesnt create loans.


This sounds like a very procedural-minded approach that will violate all kinds of object-oriented design principles, encapsulation and data hiding being at the top of the list.



he said that was a way to do it not we have to do it that way i must of wrote that bit bad. i was looking at it the wrong way a friend gaveme few ideas and im understanding it better. i was just been bit stupid
 
Ryan Hodgson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so im back at this project and am trying to make the loan and bring in an instance of member and media because my thought is link the media to the member by the members username and medias title. so ive made a method to create loans but dont think its right because when i sign in as member and try list loan it returns 0 null null. any thoughts if i should do a better way? i thought about your idea of CheckedOut class but cant i just make that my loan one? thanks



controller where methos is to check member against members list


admin screen where loans are created.
 
Daniel Demesmaecker
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a new question, olkease post in a new thread. For now it dosn't mather, one of the mods is most likely gonna move it anyway.
My checkOutClass is your LoanClass, just the name is different
This would be the LoanClass:

So basicly the same as you have

admin:


controller:

Why are you creating a loan twice?
I just cleaned up your code a bit, didn't really change anything, if you wan't I could try working out an example
 
Ryan Hodgson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:If you have a new question, olkease post in a new thread. For now it dosn't mather, one of the mods is most likely gonna move it anyway.
My checkOutClass is your LoanClass, just the name is different
This would be the LoanClass:

So basicly the same as you have

admin:


controller:

Why are you creating a loan twice?
I just cleaned up your code a bit, didn't really change anything, if you wan't I could try working out an example



oh sorry i never knew about making new post, next time i know to make new post. i dunno why im making two loans wasnt what i was trying to do. yeah that would be great if you could do that. would be greatly appreciated. thanks
 
Daniel Demesmaecker
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll use javaFx though, I'm just more familiar with it, but the logic will be the same, dosn't really mather what frontend you use
 
Ryan Hodgson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah what ever is best for you. we got a choice but was told swing would be easier so went with it.
 
Daniel Demesmaecker
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry it took a while, but had some personal stuff to do first. Here is your example. I write the objects to a file instead of an list cause otherwise the objects are gone everytime you reun your program. I added some comments to clarify the code, but if you have any more questions, feel free to ask:
JavaObjects:




Controllers:




UtilityMethods:




 
Pay attention! Tiny ad!
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!