Download presentation
Presentation is loading. Please wait.
Published byEarl Wilkerson Modified over 9 years ago
1
Library Books exercise cosc110 -001
2
The classes Library class +ArrayList -ArrayList + Library() +initializeDefaultCategories():void +displayAllCategories():void BookShelf class -ArrayList -String + BookShelf(String) +addKnownBook(String,String,String,String):void +addBookWithDetails():void +getShelf():ArrayList +getCategory():String Book class +String + Book(String,String,String,String) +getIsbn():String +getTitle():String +getAuthor():String +getGenre():String IndianaLibrary class +main():void
3
IndianaLibrary.java public class IndianaLibrary { public static void main(String[] args) { Library ifl = new Library(); }
4
Library.java import java.util.ArrayList; import java.util.Collections; public class Library { public static ArrayList categories = new ArrayList (); private ArrayList inventory = new ArrayList (); public Library() { initializeDefaultCategories(); inventory.add(new BookShelf("Biography")); inventory.add(new BookShelf("Fiction")); inventory.add(new BookShelf("Non Fiction")); inventory.add(new BookShelf("Science Fiction")); inventory.add(new BookShelf("Art")); inventory.add(new BookShelf("Self Help")); inventory.add(new BookShelf("Kids")); inventory.add(new BookShelf("Young Adults")); } public void initializeDefaultCategories() { categories.add("Biography"); categories.add("Fiction"); categories.add("Non Fiction"); categories.add("Science Fiction"); categories.add("Art"); categories.add("Self Help"); categories.add("Kids"); categories.add("Young Adults"); Collections.sort(categories); } public void displayAllCategories() { System.out.println("The current list of book genres:"); if (!categories.isEmpty()) { for (int x = 0; x < categories.size(); x++) { System.out.println(categories.get(x)); }
5
BookShelf.java import java.util.ArrayList; import java.util.Scanner; public class BookShelf { private ArrayList shelf = new ArrayList (); private String category = ""; public BookShelf(String c) { category = c; if (Library.categories.contains(c)) { System.out.println("A book shelf with category " + c + “already exists, you may add books to the " + c + " shelf."); } else { addBookWithDetails(); } public void addKnownBook(String i, String t, String a, String g) { shelf.add(new Book(i,t,a,g)); } public void addBookWithDetails() { Scanner in = new Scanner(System.in); System.out.println("Enter Book ISBN:"); String i = in.nextLine(); System.out.println("Enter Book Title:"); String t = in.nextLine(); System.out.println("Enter Book Author:"); String a = in.nextLine(); System.out.println("the genre is " + category); shelf.add(new Book(i,t,a,category)); in.close(); } public ArrayList getShelf() { return shelf; } public String getCategory() { return category; }
6
Book.java public class Book { private String isbn = ""; private String title = ""; private String author = ""; private String genre = ""; public Book (String i, String t, String a, String genre) { isbn = i; title = t; author = a; this.genre = genre; } public String getIsbn() { return isbn; } public String getTitle() { return title; } public String getAuthor() { return author; } public String getGenre() { return genre; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.