Public paste
main
By: microhaxo | Date: Apr 11 2009 20:57 | Format: None | Expires: never | Size: 3.92 KB | Hits: 856

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