import java.util.ArrayList; /** * 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 2, 2015 */ /** 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 books; private static final int maxBooks = 200; // static: one value = class variable; final: cannot be changed private static int antBoker; // one common value, even if several bookstores /** * Constructor for objects of class BookStore * @param storeName Name of Chain and bookstore */ public BookStore(String storeName) { this.storeName = storeName; books = new ArrayList(); } /** * Add an existing book to the list * @param newBook (Book object) * This is the only method that adds books to arraylist */ public void addBook(Book newBook) { books.add(newBook); //adds the book to the ArrayList } /** * 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 } /** * Add a new NOT existing CrimeBook 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. * @param genre The genre of this crime book */ public void addNewCrimeBook(String author, String title, int price,String genre) { CrimeBook newCrimeBook; newCrimeBook = new CrimeBook(author, title, price, genre); addBook(newCrimeBook); // NB: Avoid duplication of code; Use existing code for updating ArrayList } /** * Add a new NOT existing UniversityBook 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. * @param studyProgram The study program where this book is used */ public void addNewUniversityBook(String author, String title, int price,String studyProgram) { UniversityBook newUniversityBook; newUniversityBook = new UniversityBook(author, title, price, studyProgram); addBook(newUniversityBook); // NB: Avoid duplication of code; Use existing code for updating ArrayList } /** * Print details of all the books. */ public void printBookDetails() { System.out.println("ALL BOOKS IN " + storeName); for(Book oneBook : books) { oneBook.writeBook(); System.out.println("======================================================="); // HN: Kunne denne vært i klassen Book? } System.out.println(); } /** * Print details of all the Crime books. */ public void printCrimeBookDetails() { System.out.println("ALL CRIME BOOKS IN " + storeName); for(Book oneBook : books) { if (oneBook instanceof CrimeBook) { oneBook.writeBook(); System.out.println("======================================================="); } } System.out.println(); } /** * Print details of all the Crime books. */ public void printUniversityBookDetails() { System.out.println("ALL UNIVERSITY BOOKS IN " + storeName); for(Book oneBook : books) { if (oneBook instanceof UniversityBook) { oneBook.writeBook(); 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(Book oneBook : books) { if (oneBook.getPrice() >= priceLimit) { oneBook.writeBook(); 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 < books.size()) ) { if (books.get(loper).getTitle().contains(searchPhrase)) { books.get(loper).writeBook(); found = true; } loper++; } System.out.println(); } /** * 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; String genre; String studyProgram; Book newBook; CrimeBook newCrimeBook; UniversityBook newUniversityBook; int runner = 0; while (runner < numBooks) { author = "Hemingway " + runner; title = "And sun passes by" + runner; price = 100 + runner; genre = "Mord og mysterier " + runner; studyProgram = "Informasjonssystemer " + runner; newBook = new Book(author, title, price); addBook(newBook); newCrimeBook = new CrimeBook(author, title, price,genre); addBook(newCrimeBook); newUniversityBook = new UniversityBook(author, title, price,studyProgram); addBook(newUniversityBook); runner++; } } }