Advertisement
Guest User

BookUpdate

a guest
Sep 1st, 2014
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1.  
  2.  
  3. package bookassignment; //Used to create the Package BookAssignment
  4.  
  5. import java.util.Scanner; //Used to Import the Scanner Util
  6. import java.util.LinkedList; //Used to import the Linked List Util
  7.  
  8. public class Collection //Creates the class Collection to Handle Library Books
  9. {
  10. Scanner keyboard = new Scanner(System.in); //Used to activated the Keyboard Scanner for Input
  11. Scanner intKeyboard = new Scanner(System.in); //Create new int for Keyboard input
  12. LinkedList<Book> bookList = new LinkedList<Book>(); //Creates the list of Books
  13.  
  14. public void menuSelection() //Creates the Menu for handling interactions with the Library Collection
  15. {
  16. System.out.println("What would you like to do? (Enter integer)"); //Prints out options to Screen
  17. System.out.println("1 - Add a book to the library."); //Prints out options to Screen
  18. System.out.println("2 - Check out a book."); //Prints out options to Screen
  19. System.out.println("3 - Return a book."); //Prints out options to Screen
  20.  
  21. int selection = intKeyboard.nextInt(); //Stores the input the user gives from the keyboard
  22.  
  23. if(selection == 1) //What happens when user inputs 1
  24. {
  25. addBook(); //Calls the addbook Method to add a new book to the library
  26. menuSelection(); //Calls the Menu Selection Method
  27. }
  28. else if(selection == 2) //What happens when user inputs 2
  29. {
  30. checkOutBook(); //Calls the Book Checkout Method
  31. menuSelection(); //Calls the Menu Selection Method
  32. }
  33. else if(selection == 3) //What happens when user inputs 3
  34. {
  35. returnBook(); //Call the Book return methoad
  36. menuSelection(); //Calls the Menu Selection Method
  37. }
  38. else //Used if the user doesnt input the correct option
  39. {
  40. System.out.println("You didn't select one of the options. Please try again"); //Prints to screen that the user input the wrong int.
  41. menuSelection(); //Calls the Menu Selection Method
  42. }
  43. }
  44.  
  45. //adds a book to the library
  46.  
  47. public void addBook() //Add Book Methoad
  48. {
  49. System.out.println("What is the name of the book?");
  50. System.out.print("Name: ");
  51. String bN = keyboard.nextLine();
  52. System.out.println("How many copies of this book are there?");
  53. int tC = intKeyboard.nextInt();
  54.  
  55. //create a new book and add it to a linked list
  56. bookList.add(new Book(bN, tC));
  57. }
  58.  
  59. //check out a book from the library
  60. public void checkOutBook()
  61. {
  62. System.out.println("What book are you looking for?");
  63. String bN = keyboard.nextLine();
  64.  
  65. //cycles through each book in the library
  66. for(Book book : bookList)
  67. {
  68. if(book.getName().equals(bN))
  69. {
  70. if(book.checkAvailable() == true)
  71. {
  72. book.checkOut();
  73. System.out.println("You successfully checked out " + bN + ".");
  74. }
  75. else
  76. System.out.println("You could not check out " + bN + ".");
  77. }
  78. }
  79. }
  80.  
  81. //return (aka check in) a book to the library
  82. public void returnBook()
  83. {
  84. System.out.println("What book are you returning?");
  85. String bN = keyboard.nextLine();
  86.  
  87. for(Book book : bookList)
  88. {
  89. if(book.getName().equals(bN))
  90. book.checkIn();
  91.  
  92. else
  93. System.out.println(bN + " is not in the library.");
  94. }
  95. }
  96.  
  97.  
  98. public static void main(String[] args)
  99. {
  100. Collection library = new Collection();
  101. library.menuSelection();
  102.  
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement