Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.86 KB | None | 0 0
  1.     public static String save(BookStoreItem[] inventory) throws IOException {
  2.         final int BOOK = 0;
  3.         final int CD = 1;
  4.         final int DVD = 2;
  5.         String saveMessage;
  6.         boolean isSaved = false;
  7.         File catalogFile = new File("catalog.txt");
  8.  
  9.         PrintWriter out = new PrintWriter (catalogFile);
  10.        
  11.  
  12.             try {
  13.                 int index = 0;
  14.                 if (catalogFile.exists()!= true) {
  15.                     throw new FileNotFoundException(); 
  16.                 }  
  17.    
  18.                
  19.                 while (index < inventory.length) {
  20.                     if (inventory[index] != null) {
  21.                         if (inventory[index].getClass() == Book.class) {
  22.                             out.println(BOOK);
  23.                             out.println(inventory[index].getTitle());  
  24.                             out.println(inventory[index].getAuthor());
  25.                             out.println(inventory[index].getPrice());  
  26.                             out.println(((Book)inventory[index]).getPages());  
  27.    
  28.                         }
  29.                         else if (inventory[index].getClass() == CD.class) {
  30.                             out.println(CD);
  31.                             out.println(inventory[index].getTitle());  
  32.                             out.println(inventory[index].getAuthor());
  33.                             out.println(inventory[index].getPrice());  
  34.                             out.println(((CD)inventory[index]).getMinutes());
  35.                             out.println(((CD)inventory[index]).getSeconds());
  36.                                
  37.                         }
  38.                         else {
  39.                             out.println(DVD);
  40.                             out.println(inventory[index].getTitle());  
  41.                             out.println(inventory[index].getAuthor());
  42.                             out.println(inventory[index].getPrice());  
  43.                             out.println(((DVD)inventory[index]).getPlayingTime());
  44.                            
  45.                         }//end of else
  46.                        
  47.                     }//end of if statement.
  48.                     index++;
  49.                    
  50.                 }//end of while loop
  51.                 isSaved = true;
  52.                
  53.                
  54.                 }//end of try
  55.             catch(IOException ioe){
  56.                 throw ioe;
  57.             }
  58.             finally {
  59.                 if (isSaved = true) {
  60.                 saveMessage = "Saved to file";
  61.        
  62.                 }//end of if
  63.                 else {
  64.                     saveMessage = "Couldn't save to the file";
  65.        
  66.                 }
  67.                 out.close();
  68.             }
  69.        
  70.         return saveMessage;
  71.     }//end of save()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement