Public paste
Undefined
By: Guest | Date: Apr 8 2009 21:25 | Format: None | Expires: never | Size: 3.49 KB | Hits: 1097

  1. //##########################################
  2. //# Assignment 7                           #
  3. //#                             #
  4. //# Finally done...                        #
  5. //##########################################
  6. import java.util.Date;
  7. import java.util.Calendar;
  8. import java.util.Scanner;
  9. /**
  10.  *
  11.  * This class will make and call books that are stored in a bookshelf.
  12.  *
  13.  */
  14. public class Bookmain
  15. {
  16.         public static void main (String[] args)
  17.        
  18.                 // making a new bookshelf object called shelf
  19.                 BookShelf shelf = new BookShelf();
  20.  
  21.                 Scanner scan = new Scanner(System.in);
  22.                 // getting ready for the while loop by setting up some variables that will control it. 
  23.                 boolean istrue = true;
  24.                 System.out.println("Lets add some books to our shelf");
  25.                 // while loop to add a book.
  26.                 while(istrue)
  27.                 {
  28.                         System.out.println("What is the books title? ");
  29.                         String title = scan.next();
  30.                         System.out.println("Who is the author of the book? ");
  31.                         String author = scan.next();
  32.                         System.out.println("What is the value of the book in dollars? ");
  33.                         Double price = scan.nextDouble();
  34.                         System.out.println("What is the Publisher? ");
  35.                         String publisher = scan.next();
  36.                         System.out.println("What is the copyright year? ");
  37.                         int year = scan.nextInt();
  38.                         Calendar greg = Calendar.getInstance();
  39.                         // set the calendar basically just for year.
  40.                         greg.set (year, 1, 1);
  41.                         Date copyright = greg.getTime();
  42.                        
  43.                                
  44.                         System.out.println("would you like to make another book? Y or N");
  45.                         String answer = scan.next();
  46.                         // if statement inside of the while loop that allows you to either make a new book or stop making books.
  47.                         // in either case it adds the book to shelf.
  48.                         if (answer.equals ("Y"))
  49.                         {
  50.                        
  51.                                 istrue = true;
  52.                                 Book abook = new Book(title, author, price, publisher, copyright);
  53.                                 shelf.add(abook);
  54.  
  55.                         }
  56.                         else
  57.                         {
  58.                                 istrue = false;
  59.                                 Book abook = new Book(title, author, price, publisher, copyright);
  60.                                 shelf.add(abook);
  61.                         }      
  62.                
  63.                        
  64.                 }
  65.                 // this is the code to find the book
  66.                 System.out.println("Type the title of the book you want to find: ");
  67.                 String titlesearch = scan.next();
  68.                 Book result = shelf.find(titlesearch);
  69.                 // if statement that says if the result is NOT null then print out what was found, IE found a book with that title.
  70.                 if(result != null)
  71.                 {      
  72.                         System.out.println(result);
  73.                 }
  74.                 // if it is null then the book doesnt exist.   
  75.                 else
  76.                 {
  77.                         System.out.println("That book doesnt exist");
  78.                 }
  79.                 // this variable sets up the next while loop
  80.                 boolean istrue2 = true;
  81.                 // while loop for removing a book.
  82.                 while(istrue2)
  83.                 {
  84.                                
  85.                         System.out.println("Would you like to remove a book? Y or N");
  86.                         String removeanswer = scan.next();
  87.                         // this if statement matches what the user enters.
  88.                        
  89.                         if (removeanswer.equals ("Y"))
  90.                         {
  91.                                 // in this case they want to remove a book, so enter the title and then it searches for the book.
  92.                                 System.out.println("enter title to remove: ");
  93.                                 String removesearch = scan.next();
  94.                                 Book removeresult = shelf.find(removesearch);
  95.                        
  96.                                 // this if statement says that if removeresult does NOT equal null then remove the book, meaning it exists and is a match.                              if(removeresult != null)
  97.                                         {      
  98.                                                 System.out.println(removeresult);
  99.                                                 shelf.remove(removeresult);
  100.                                                
  101.                                         }                              
  102.                                 }// end of answer is Yes
  103.                                 // if you dont want to remove a book then END the loop.
  104.                                 if(removeanswer.equals("N"))
  105.                                 {              
  106.                                         istrue2 = false;
  107.                                 }
  108.                                                
  109.                         }// end while          
  110.                 }
  111.         }