//begin videostore ********************************************************************************************* //package videostore1; import java.io.*; import java.util.*; /** * This class implements a DOS application, building a * list of rentables (videos and video games) owned by the store * and available for checkout. *
* It offers the following menu: *
* NOTE: the "rentableDat.txt" file must be in the PARENT * directory of this package: videostore1. The text file * contains sample rentable information to be stored in * a rentable list. *
* * @author Kathlea Quebbeman */ public class VideoStore { /** * Reads input from the keyboard. */ BufferedReader keyboard = new BufferedReader( new InputStreamReader(System.in)); /** * List of videos and video games owned by the video store. */ RentableList rentableList = new RentableList(); /** * Default constructor.
* Sets up a data base of videos and video games owned by the store, * reading the list from the text file, "rentableDat.txt".
* Displays a menu of options from which the store * clerk may choose. */ public VideoStore() { int choice; String title="",type,platform; char ch; int num=0; boolean valid = false; Rentable rentable=new Video(); try { createRentableList("rentableDat.txt"); do { displayMenu(); try { choice = Integer.parseInt(keyboard.readLine()); } catch(NumberFormatException nfe) { choice = 0; } System.out.println(); switch(choice) { case 1: System.out.print("Enter V for video, G for game: "); type = keyboard.readLine(); ch = type.charAt(0); valid = false; switch(ch) { case 'v': case 'V': System.out.print("Enter the title: "); title = keyboard.readLine(); rentable = new Video(title,"","","","","",0); valid = true; break; case 'g': case 'G': System.out.print("Enter the title: "); title = keyboard.readLine(); System.out.print("Enter the game platform: "); platform = keyboard.readLine(); rentable = new Game(title,platform,"",0); valid = true; break; default: System.out.println("Not a valid rentable."); } if(valid) { System.out.println(); if(rentableList.contains(rentable)) System.out.println("The store does " + "stock " + rentable.getTitle()); else System.out.println("The store does not " + "stock " + rentable.getTitle()); } break; case 2: System.out.print("Enter V for video, G for game: "); type = keyboard.readLine(); ch = type.charAt(0); valid = false; switch(ch) { case 'v': case 'V': System.out.print("Enter the title: "); title = keyboard.readLine(); System.out.println(); rentable = new Video(title,"","","","","",0); num = 1; valid = true; break; case 'g': case 'G': System.out.print("Enter the title: "); title = keyboard.readLine(); System.out.print("Enter the game platform: "); platform = keyboard.readLine(); System.out.print("Enter the number of copies to rent: "); num = Integer.parseInt(keyboard.readLine()); rentable = new Game(title,platform,"",0); valid = true; break; default: System.out.println("Not a valid rentable."); } if(valid) { try { rentableList.rentableCheckOut(rentable, num); System.out.println("Enjoy " + rentable.getTitle()); } catch(OutOfBoundsException oobe) { System.out.println(oobe.getMessage()); } catch(NoSuchElementException nosee) { System.out.println(nosee.getMessage()); } } break; case 3: System.out.print("Enter V for video, G for game: "); type = keyboard.readLine(); ch = type.charAt(0); valid = false; switch(ch) { case 'v': case 'V': System.out.print("Enter the title: "); title = keyboard.readLine(); System.out.println(); rentable = new Video(title,"","","","","",0); num = 1; valid = true; break; case 'g': case 'G': System.out.print("Enter the title: "); title = keyboard.readLine(); System.out.print("Enter the game platform: "); platform = keyboard.readLine(); System.out.print("Enter the number of copies returned: "); num = Integer.parseInt(keyboard.readLine()); rentable = new Game(title,platform,"",0); valid = true; break; default: System.out.println("Not a valid rentable."); } if(valid) { try { rentableList.rentableCheckIn(rentable, num); System.out.println("Thank you for returning " + rentable.getTitle()); } catch(OutOfBoundsException oobe) { System.out.println(oobe.getMessage()); } catch(NoSuchElementException nosee) { System.out.println(nosee.getMessage()); } } break; case 4: System.out.print("Enter V for video, G for game: "); type = keyboard.readLine(); ch = type.charAt(0); valid = false; switch(ch) { case 'v': case 'V': System.out.print("Enter the title: "); title = keyboard.readLine(); System.out.println(); rentable = new Video(title,"","","","","",0); valid = true; break; case 'g': case 'G': System.out.print("Enter the title: "); title = keyboard.readLine(); System.out.print("Enter the game platform: "); platform = keyboard.readLine(); rentable = new Game(title,platform,"",0); valid = true; break; default: System.out.println("Not a valid rentable."); } if(valid) { try { if(rentableList.isRentableAvailable(rentable)) System.out.println(title + " is available for checkout"); else System.out.println(title + " is not available for checkout."); } catch(NoSuchElementException nosee) { System.out.println(nosee.getMessage()); } } break; case 5: System.out.println("This store owns the " +"following rentable titles:"); //System.out.println(rentableList.titleCopiesToString());//******************************* break; case 6: System.out.println("This store owns the " +"following rentable videos/games:"); System.out.println(rentableList.toString()); break; case 9: break; default: System.out.println("Invalid selection"); }//end switch }while(choice != 9); }//end try catch(InputMismatchException ime) { System.out.println(ime.toString()); } catch(NumberFormatException nfe) { System.out.println(nfe.toString()); } catch(FileNotFoundException fnfe) { System.out.println(fnfe.toString()); } catch(IOException ioe) { System.out.println(ioe.toString()); } catch(OutOfBoundsException oobe) { System.out.println(oobe.toString()); } } /** * Sets up data base of rentables owned by the store, * reading the rentable records from the text file * who's pathname is specified as a String. * * @param rentableFile String with pathname of text * file containing records of the rentables owned by * the store.
* each record: * @throws IOException if file not found or cannot * be read. */ public void createRentableList(String rentableFile) throws IOException { BufferedReader inFile = new BufferedReader(new FileReader(rentableFile)); String rentType,title,s1,s2,s3,s4,productionCo; int inStock; Rentable newVideo = new Video(); Rentable newGame = new Game(); rentType = inFile.readLine(); while(rentType != null) { title = inFile.readLine(); s1 = inFile.readLine(); s2 = inFile.readLine(); s3 = inFile.readLine(); s4 = inFile.readLine(); productionCo = inFile.readLine(); inStock = Integer.parseInt(inFile.readLine()); try { if(rentType.charAt(0) == 'V') { newVideo = new Video(title,s1,s2,s3,s4,productionCo,inStock); rentableList.add(newVideo); } else if(rentType.charAt(0) == 'G') { newGame = new Game(title,s1,productionCo,inStock); rentableList.add(newGame); } } catch(OutOfBoundsException oobe) { System.err.println(oobe.getMessage()); } rentType = inFile.readLine(); }//end while loop inFile.close(); }//end createVideoList /** * Displays a menu of options from which the store * clerk may choose.
* @menu
*
* @author Kathlea Quebbeman */ public class Video implements Cloneable, Rentable { /*------------Instance Variables-----------------*/ /** * Stores the title of the video */ private String videoTitle; /** * Stores the first star */ private String movieStar1; /** * Stores the second star */ private String movieStar2; /** * Stores the producer */ private String movieProducer; /** * Stores the director */ private String movieDirector; /** * Stores the production company */ private String movieProductionCo; /** * Stores the # of copies available for rent */ private int copiesInStock; /** * Stores the # of copies owned by the store */ private int copiesOwned; /*------------constructors-----------------------*/ /** * Default constructor.
* Instance variables are initialized to default * values: *
* Instance variables are set according to the * parameters * @param title New videoTitle * @param star1 New movieStar1 * @param star2 New movieStar2 * @param producer New movieProducer * @param director New movieDirector * @param productionCo New movieProductionCo * @param setOwned New # copies owned by the store and available * for rent. * @throws OutOfBoundsException if setOwned is negative */ public Video(String title,String star1, String star2,String producer,String director, String productionCo,int setOwned) throws OutOfBoundsException { setVideoInfo(title, star1, star2, producer, director, productionCo, setOwned); } /*------------update instance variables----------*/ /** * Set all instance variables: * * @param title New videoTitle * @param star1 New movieStar1 * @param star2 New movieStar2 * @param producer New movieProducer * @param director New movieDirector * @param productionCo New movieProductionCo * @param setOwned New # copies owned by the store and available * for rent. * @throws OutOfBoundsException if setOwned is negative */ public void setVideoInfo(String title,String star1, String star2,String producer,String director, String productionCo,int setOwned) throws OutOfBoundsException { videoTitle = title; movieStar1 = star1; movieStar2 = star2; movieProducer = producer; movieDirector = director; movieProductionCo = productionCo; if(setOwned < 0) throw new OutOfBoundsException( "\n Number of copies cannot be negative."); copiesInStock = setOwned; copiesOwned = setOwned; } /** * Set all instance variables: * * @param title New rental title * @param s1 New detail string1 * @param s2 New detail string2 * @param s3 New detail string3 * @param s4 New detail string4 * @param productionCo New production company * @param setInStock New copies owned by store and available * for rent. * @throws OutOfBoundsException if setOwned is negative */ public void setInfo(String title,String s1, String s2,String s3,String s4, String productionCo,int setOwned) throws OutOfBoundsException { setVideoInfo(title, s1, s2, s3, s4, productionCo, setOwned); } /** * Set number of copies owned by the store and available * for rent. * * @param num Number of copies of this video owned and * available for rent. * @throws OutOfBoundsException if num is negative */ public void setCopiesOwned(int num) throws OutOfBoundsException { if(num < 0) throw new OutOfBoundsException( "\n Number of copies cannot be negative."); copiesInStock = num; copiesOwned = num; } /** * Returns # of copies of this video available for * rent. * * @return copiesInStock */ public int getCopiesInStock() { return copiesInStock; } /** * Returns # of copies of this video owned by the store. * * @return copiesOwned */ public int getCopiesOwned() { return copiesOwned; } /** * Increments # of copies of this video available for rent. * * @param num Number of copies being returned must == 1. * @throws OutOfBoundsException if num is != 1 or if copiesInStock + 1 * exceeds number owned by the store. */ public void checkIn(int num) throws OutOfBoundsException { if(copiesInStock + 1 > copiesOwned) throw new OutOfBoundsException("This video not owned by this store."); if(num == 1) copiesInStock++; else throw new OutOfBoundsException("Must return exactly one copy."); } /** * Decrement # of copies of this video available * for rental. * * @param num number of videos to be checked out must == 1 * @throws OutOfBoundsException if num != 1 or if this video is out of stock */ public void checkOut(int num) throws OutOfBoundsException { if(copiesInStock > 0) { if(num != 1) throw new OutOfBoundsException("Can only check out one copy of " + "a video"); copiesInStock--; } else throw new OutOfBoundsException(videoTitle + " is out of stock"); } /** * Returns title String. * * @return Title of movie */ public String getTitle() { return videoTitle; } /** * Returns a String describing this video. * * @return String with all information about this * video. */ public String toString() { String str = "\n "+videoTitle; str += "\n Starring " + movieStar1 + " and " + movieStar2; str += "\n Producer: " + movieProducer; str += "\n Director: " + movieDirector; str += "\n Production Company: " + movieProductionCo; str += "\n Copies in stock: " + copiesInStock; str += "\n Copies in owned by the store: " + copiesOwned + "\n"; return str; } /** * Adds given number to # of copies of this video owned by the store * and currently available for rental. Allows negative # to remove * copies from the store's inventory. * * @param num Number of copies to add to copiesInStock and copiesOwned. * If num + copiesInStock is negative, copiesInStock is set to 0. * @throws OutOfBoundsException if num + copiesOwned is negative */ public void updateOwned(int num) throws OutOfBoundsException { if(num + copiesOwned < 0) throw new OutOfBoundsException( "\n Number of copies cannot be negative."); else { copiesOwned += num; if(num + copiesInStock < 0) copiesInStock = 0; else copiesInStock += num; } } /** * Tells if title of this movie is same as other's * title disregarding case. * * @return True of same title, false if not. */ public boolean equals(Object other) { if((other != null) && (other instanceof Video)) { Video temp = (Video) other; return videoTitle.compareToIgnoreCase( temp.videoTitle) == 0; } else return false; } /** * Returns a clone of this video, a reference to a * different object, a deep copy. * * @return Clone of this video, a deep copy. * @throws CloneNotSupportedException */ public Object clone() throws CloneNotSupportedException { return super.clone(); } } //end video************************************************************************