• Post Reply Bookmark Topic Watch Topic
  • New Topic

Count number of books in Arraylist  RSS feed

 
Iwan van den Hoek
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am new to Java programming and studying it at school.
We have been given an assignment to create a small library, with the following classes Library, Book, Author and Copy. With a given class Biblio which has predefined code and adds the books to the class book in an arraylist in Class Copy.
The UML Domain is attached so you know the flow of the classes
Everything is working fine and the generated output is correct.

There is just one method in class Library that is not working, the int method has to count the number of Copy's based on the Class Book (String):
I have to go through the Arraylist in Class Copy and look for a specific book and return the number of copy's.
Sorry for the Dutch language in the code.
I have tried multiple steps using a for loop
Now I have found a similar post the uses hashset, I have tried below code but the return comes back with 0. (There are 3 copy's)



Any help is much appreciated. I have added the complete code
Class-Diagram.PNG
[Thumbnail for Class-Diagram.PNG]
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, posting the code with (Double) Dutch doesn't make it too user friendly. Boek --> Book I can understand, but what is Exemplaar?
Also posting the whole class wasn't necessary.
Presumably this is the method you were wanting help with:



>the int method has to count the number of Copy's based on the Class Book (String):
>I have to go through the Arraylist in Class Copy and look for a specific book and return the number of copy's.

My observations:
You don't appear to be using the b1 parameter for anything in this method. What is it meant for?
You are building up a set of book titles. What purpose does this serve?
Can you explain how this code is meant to work? i.e. In English rather than computerese , what are the steps you are trying to accomplish?

 
Iwan van den Hoek
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stefan,

Thank you for replying.
Yeah I should of changed the Dutch but here is a quick translation:
Boek = Book
Bibliotheek = Library
Exemplaar = Copy
Autheur = Author
Boekenlijst = ListofBooks

To answer your question:
Yes that is the code I need help with.
What I am trying to accomplish is to create a method that will return the number of copy's based on the String value Book b1

b1 is a parameter of a Book that is listed in the Arraylist boekenlijst.

The Hashset part of the code is what I have found on this site. The building up of a set of book titles van be removed, I used the code as I thought it was necessary for the code to work.


I tested the code above and the output from System.out.println(a + " " + count); is:
Einstein 1
Onder professoren 1
Het laatste bevel 1
Nooit meer slapen 3
De buitenvrouw 2

but that is not exactly what I need.


The Class Biblio requests the following line : System.out.println("There are "+bieb.telExemplaren(b1)+" copies of "+b1.getTitel());
and should print:
There are 3 copies of Nooit meer slapen


The return statement should return the number of copies. but it prints There are 0 copies of Nooit meer slapen
.

 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 22.

count = 0; ?
 
Mark King
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you only need one title, why are you checking all of the titles?
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iwan van den Hoek wrote:
The return statement should return the number of copies. but it prints There are 0 copies of Nooit meer slapen


At line 22 you reset count to 0.
At line 25 you return count.
 
Iwan van den Hoek
Ranch Hand
Posts: 40
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for taking the time, but I figured it out.
I was going about it all wrong and thanks to a tip I got I changed the code to:


This does exactly what it needs to do.

The code I used does work except not as a getmethode and Setting the count =0; outside the loop resets the count back to 0 when it is going through the Arraylist and so it returns the correct number instead of increasing the number it returns.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iwan van den Hoek wrote:Thank you all for taking the time, but I figured it out.
I was going about it all wrong and thanks to a tip I got I changed the code to:


We are more than happy that you figured it out yourself.

One more thing: use brackets in all for's and if's. There are some missing, which means you can loose marks in a coding-style part.

Best luck with your assignment.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!