Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static String save(BookStoreItem[] inventory) throws IOException {
- final int BOOK = 0;
- final int CD = 1;
- final int DVD = 2;
- String saveMessage;
- boolean isSaved = false;
- File catalogFile = new File("catalog.txt");
- PrintWriter out = new PrintWriter (catalogFile);
- try {
- int index = 0;
- if (catalogFile.exists()!= true) {
- throw new FileNotFoundException();
- }
- while (index < inventory.length) {
- if (inventory[index] != null) {
- if (inventory[index].getClass() == Book.class) {
- out.println(BOOK);
- out.println(inventory[index].getTitle());
- out.println(inventory[index].getAuthor());
- out.println(inventory[index].getPrice());
- out.println(((Book)inventory[index]).getPages());
- }
- else if (inventory[index].getClass() == CD.class) {
- out.println(CD);
- out.println(inventory[index].getTitle());
- out.println(inventory[index].getAuthor());
- out.println(inventory[index].getPrice());
- out.println(((CD)inventory[index]).getMinutes());
- out.println(((CD)inventory[index]).getSeconds());
- }
- else {
- out.println(DVD);
- out.println(inventory[index].getTitle());
- out.println(inventory[index].getAuthor());
- out.println(inventory[index].getPrice());
- out.println(((DVD)inventory[index]).getPlayingTime());
- }//end of else
- }//end of if statement.
- index++;
- }//end of while loop
- isSaved = true;
- }//end of try
- catch(IOException ioe){
- throw ioe;
- }
- finally {
- if (isSaved = true) {
- saveMessage = "Saved to file";
- }//end of if
- else {
- saveMessage = "Couldn't save to the file";
- }
- out.close();
- }
- return saveMessage;
- }//end of save()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement