import java.util.ArrayList; import java.util.HashMap; /** * Bookstore is the class for bookstores * In early version we have little info about the shop * The central in the bookstore, is the books. * * @author Hallgeir * @version 1, 2014 */ /** Hallgeir: Where shuld number of books registered be, in the Book class * or in the BookStore class * */ public class BookStore { private String storeName; private ArrayList produkter; private static final int maxBooks = 200; // static: one value; final: cannot be changed private HashMap bookList; // key value /** * Constructor for objects of class BookStore * @param storeName Name of Chain and bookstore */ public BookStore(String storeName) { this.storeName = storeName; produkter = new ArrayList(); bookList = new HashMap(); } /** * Add an existing book to the list * @param newBook (Book object) * This is the only method that adds books to arraylist and hashmap * PS: Every book object exist as one Book object. Then there is a reference * to each Book object both from the ArrayList and the HashMap */ public void addBook(Book newBook) { produkter.add(newBook); //adds the book to the ArrayList String title = newBook.getTitle(); bookList.put(title,newBook); //adds the book to the HashMap } /** * Add a new NOT existing book to the list * First, create the book object * Second: Add the new book to the list * @param author Name of the author * @param title Full title of the book * @param price Standard price. */ public void addNewBook(String author, String title, int price) { Book newBook; newBook = new Book(author, title, price); addBook(newBook); // NB: Avoid duplication of code; Use existing code for updating ArrayList and HashMap } /** * Print details of all the books. */ public void printBookDetails() { System.out.println("ALL BOOKS IN " + storeName); for(Produkt p : produkter) { if (p.getProduktType().equals("Book")) {} if (p instanceof Book) {/* gjør det som skal gjøres med boka */} p.printDetails(); System.out.println("======================================================="); // HN: Kunne denne vært i klassen Book? } System.out.println(); } /** * Print details of all the books, version 2 * What is your opinion to this version regarding Coupling? */ public void printBookDetailsv2() { System.out.println("ALL BOOKS IN " + storeName); for(Produkt p : produkter) { String title = p.getTitle(); if (p instanceof Book) { Book oneBook = (Book)p; System.out.println ("Title : " + title); System.out.println ("Author : " + oneBook.author); System.out.println("======================================================="); } System.out.println(); } } /** * Print details of the books with a higher or equal price than specified. * @param priceLimit Will list all books priced higher or equal than priceLimit */ public void printBookDetailsByCost(int priceLimit) { System.out.println("ALL BOOKS IN " + storeName + " MORE EXPENSIVE THAN " + priceLimit); for(Produkt oneBook : produkter) { if (oneBook.getPrice() >= priceLimit) { oneBook.printDetails(); System.out.println("======================================================="); } } System.out.println(); } /** * Print details of the FIRST book that contains a certain phrase in the title. * @param searchPhrase * * Hallgeir: What to do if we want ALL books containing the given phrase */ public void printOneBook(String searchPhrase) { System.out.println("THE FIRST BOOK IN " + storeName + " WITH " + searchPhrase + " IN TITLE"); boolean found = false; int loper = 0; while ( (found == false) && (loper < produkter.size()) ) { if (produkter.get(loper).getTitle().contains(searchPhrase)) { produkter.get(loper).printDetails(); found = true; } loper++; } System.out.println(); } /** * Print a book with a certain title.It is extremely important to do this * very quick, and also this happens very often. * That is why we use HashMap, where we do direct "oppslag" * @param SearchTitle * */ public void printBookTitle(String searchTitle) { Produkt oneBook = bookList.get(searchTitle); if (oneBook == null) System.out.println("The book with title " + searchTitle + " was not found"); else oneBook.printDetails(); } /** * THIS METHOD IS FOR TESTING PURPOSES * Add books in the arraylist * @param numBooks is the number of books that will be added to the */ public void addTestData(int numBooks) { String author; String title; int price; Book newBook; int runner = 0; while (runner < numBooks) { author = "Hemingway " + runner; title = "And sun passes by" + runner; price = 100 + runner; newBook = new Book(author, title, price); addBook(newBook); runner++; } } }