I have a problem in java coding: Please could you have a look at it, the problem is with addcopyofbook to produce a stock number. For example if you get the list of books it produces a unique stock code for every book, even if they are teh same book, also when you call it up in teh objects bench its hould give a differnt stock number for each book, taht u had to teh list.
if you can help I would appreciate this. Please could you please look at this attachment enclosed, which will give you a better idea.

Also any ideas of how to do a hashmap within a hashmap.

Thanks

public class Library 
{ 
       private String name; 
       private String address; 
       private HashMap<String, Book> books; 
       private int stockNumber; 
       private ArrayList<Book> stock; 
       private Random randomGenerator = new Random(); 

       public Library() 
       { 
           name = "London Library"; 
           address = "74 brunel street, nn23j7b"; 
           books = new HashMap<String, Book>(); 
           stock = new ArrayList<Book>(); 
       } 
       public void addBookCatalogue (Book book) 
   { 
       String title = book.getTitle(); 
       books.put(title, book); 
   } 
       public void addcopyOfbook (Book book, int stock,int x) 
       { 
           String title = book.getTitle(); 
           books.put(title, book); 
           stockNumber = 0; 
           for (int i = 1; i <= stock; i++) 
           { 
               stockNumber++;x++; 
               System.out.println(stockNumber); 
           } 
       } 
       public void addBookStock (Book book,int sN) 
       { 
           for (int i = 1; i <=sN; i++) 
           { 
               Book bk = new Book (book.getTitle(), book.getAuthor(), book.getIsbn()); 
               stock.add (bk); 
               stockNumber = randomGenerator.nextInt(); 
           } 
       } 
       public void addBookCatalogue2 (Book book) 
       { 
           if (books.containsKey(book)) 
           { 
           System.out.println ("This book is already in the catalogue"); 
           } 
           else 
           { 
               String tit = book.getTitle(); 
               books.put(tit, book); 
           } 
       } 
       public void showCatalogue() 
       { 
           Collection<Book> theValues = books.values(); 
           for (Book bk : theValues) 
           { 
               System.out.println (bk.toString()); 
           } 
       } 
       public String toString() 
       { 
           return name + ": " + address + "\nCatalogue:\n" + books + "\n\nStock List: " + stock + stockNumber; 
       } 
   }

Edited 6 Years Ago by peter_budo: Adding code tags to old thread

And what's your question about that code?
What doesn't work, and why?

As to adding a Map to a Map, that's easy. But depending on what you mean by it (there are several possible interpretations of the phrase) the implementation would differ and so would the effect.

This article has been dead for over six months. Start a new discussion instead.