Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package bookassignment; //Used to create the Package BookAssignment
- import java.util.Scanner; //Used to Import the Scanner Util
- import java.util.LinkedList; //Used to import the Linked List Util
- public class Collection //Creates the class Collection to Handle Library Books
- {
- Scanner keyboard = new Scanner(System.in); //Used to activated the Keyboard Scanner for Input
- Scanner intKeyboard = new Scanner(System.in); //Create new int for Keyboard input
- LinkedList<Book> bookList = new LinkedList<Book>(); //Creates the list of Books
- public void menuSelection() //Creates the Menu for handling interactions with the Library Collection
- {
- System.out.println("What would you like to do? (Enter integer)"); //Prints out options to Screen
- System.out.println("1 - Add a book to the library."); //Prints out options to Screen
- System.out.println("2 - Check out a book."); //Prints out options to Screen
- System.out.println("3 - Return a book."); //Prints out options to Screen
- int selection = intKeyboard.nextInt(); //Stores the input the user gives from the keyboard
- if(selection == 1) //What happens when user inputs 1
- {
- addBook(); //Calls the addbook Method to add a new book to the library
- menuSelection(); //Calls the Menu Selection Method
- }
- else if(selection == 2) //What happens when user inputs 2
- {
- checkOutBook(); //Calls the Book Checkout Method
- menuSelection(); //Calls the Menu Selection Method
- }
- else if(selection == 3) //What happens when user inputs 3
- {
- returnBook(); //Call the Book return methoad
- menuSelection(); //Calls the Menu Selection Method
- }
- else //Used if the user doesnt input the correct option
- {
- System.out.println("You didn't select one of the options. Please try again"); //Prints to screen that the user input the wrong int.
- menuSelection(); //Calls the Menu Selection Method
- }
- }
- //adds a book to the library
- public void addBook() //Add Book Methoad
- {
- System.out.println("What is the name of the book?");
- System.out.print("Name: ");
- String bN = keyboard.nextLine();
- System.out.println("How many copies of this book are there?");
- int tC = intKeyboard.nextInt();
- //create a new book and add it to a linked list
- bookList.add(new Book(bN, tC));
- }
- //check out a book from the library
- public void checkOutBook()
- {
- System.out.println("What book are you looking for?");
- String bN = keyboard.nextLine();
- //cycles through each book in the library
- for(Book book : bookList)
- {
- if(book.getName().equals(bN))
- {
- if(book.checkAvailable() == true)
- {
- book.checkOut();
- System.out.println("You successfully checked out " + bN + ".");
- }
- else
- System.out.println("You could not check out " + bN + ".");
- }
- }
- }
- //return (aka check in) a book to the library
- public void returnBook()
- {
- System.out.println("What book are you returning?");
- String bN = keyboard.nextLine();
- for(Book book : bookList)
- {
- if(book.getName().equals(bN))
- book.checkIn();
- else
- System.out.println(bN + " is not in the library.");
- }
- }
- public static void main(String[] args)
- {
- Collection library = new Collection();
- library.menuSelection();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement