if an archive file exits {
construct a Scanner object on the archive file
read in the the first line as the last_collection_no
while there there is a next line {
read in the the class name
if class is Movie {
call a method to read in lines and create a Movie object
add it to library
} else {
call a method to read in lines and create a Lectures object
add it to library
}
}
private static void restore_library_state() throws FileNotFoundException{ File archive_file = new File(archive_filename); if (archive_file.exists()) { Scanner input = new Scanner(archive_file); input.nextLine(); // skip past blank first line while (input.hasNextLine()){ String class_name = input.nextLine(); if (class_name.equals("Movie")){ library.add(movie_from_file(input)); } else { library.add(lectures_from_file(input)); } } } }
public static void main (String[] args) throws FileNotFoundException, IOException{ restore_library_state();
private static Movie movie_from_file(Scanner input){ int collection_no = Integer.parseInt(input.nextLine()); String title = input.nextLine(); int length = Integer.parseInt(input.nextLine()); String format = input.nextLine(); String director = input.nextLine(); String studio = input.nextLine(); Movie movie = new Movie(collection_no, title, length, format, director, studio); while (input.hasNextLine()){ String line = input.nextLine(); if (line.equals(separator)){ break; } movie.add_actor(line); } return movie; }
private static Lectures lectures_from_file (Scanner input){ int collection_no = Integer.parseInt(input.nextLine()); int length = Integer.parseInt(input.nextLine()); String format = input.nextLine(); String course = input.nextLine(); int disc_no = Integer.parseInt(input.nextLine()); String instructor = input.nextLine(); Lectures lectures = new Lectures(collection_no, length, format, course, disc_no, instructor); while (input.hasNextLine()){ String line = input.nextLine(); if (line.equals(separator)){ break; } lectures.add_lecture(line); } return lectures; }
$ java VideoLibrary9 M(ovie), L(ecture), S(ee entries), D(one): m Filename: forbidden_planet_3.txt #1: Forbidden Planet, 98 minutes, DVD, Fred McLeod Wilcox, MGM Walter Pidgeon Anne Francis Leslie Nielson Warren Stevens M(ovie), L(ecture), S(ee entries), D(one): l Filename: ancient_civilizations_1_3.txt #2: Origins of Great Ancient Civilizations - Disc 1, 180 minutes, DVD, Kenneth W. Harl Cradles of Civilization First Cities of Sumer Mesopotamian Kings and Scribes Hammurabi’s Babylon Egypt in the Pyramid Age The Middle Kingdom M(ovie), L(ecture), S(ee entries), D(one): d Exiting
$ java VideoLibrary9 M(ovie), L(ecture), S(ee entries), D(one): s #1: Forbidden Planet, 98 minutes, DVD, Fred McLeod Wilcox, MGM Walter Pidgeon Anne Francis Leslie Nielson Warren Stevens #2: Origins of Great Ancient Civilizations - Disc 1, 180 minutes, DVD, Kenneth W. Harl Cradles of Civilization First Cities of Sumer Mesopotamian Kings and Scribes Hammurabi’s Babylon Egypt in the Pyramid Age The Middle Kingdom M(ovie), L(ecture), S(ee entries), D(one): d Exiting
private static Movie add_movie(Scanner console)throws FileNotFoundException{ System.out.print("Filename: "); String filename = console.next(); File file = new File(filename); Scanner input = new Scanner(file); int collection_no = get_collection_no(); String title = input.nextLine(); int length = Integer.parseInt(input.nextLine()); String format = input.nextLine(); String director = input.nextLine(); String studio = input.nextLine(); Movie movie = new Movie(collection_no, title, length, format, director, studio); while (input.hasNextLine()){ movie.add_actor(input.nextLine()); } System.out.println(movie); return movie; } private static Movie movie_from_file(Scanner input){ int collection_no = get_collection_no(); String title = input.nextLine(); int length = Integer.parseInt(input.nextLine()); String format = input.nextLine(); String director = input.nextLine(); String studio = input.nextLine(); Movie movie = new Movie(collection_no, title, length, format, director, studio); while (input.hasNextLine()){ String line = input.nextLine(); if (line.equals(separator)){ break; } movie.add_actor(line); } return movie; }
while
loop are similarprivate static Movie add_movie(Scanner console)throws FileNotFoundException{ System.out.print("Filename: "); String filename = console.next(); File file = new File(filename); Scanner input = new Scanner(file); Movie movie = movie_from_file(input); System.out.println(movie); return movie; }
private static Lectures add_lectures(Scanner console)throws FileNotFoundException{ System.out.print("Filename: "); String filename = console.next(); File file = new File(filename); Scanner input = new Scanner(file); int collection_no = get_collection_no(); int length = Integer.parseInt(input.nextLine()); String format = input.nextLine(); String course = input.nextLine(); int disc_no = Integer.parseInt(input.nextLine()); String instructor = input.nextLine(); Lectures lectures = new Lectures(collection_no, length, format, course, disc_no, instructor); while (input.hasNextLine()){ lectures.add_lecture(input.nextLine()); } System.out.println(lectures); return lectures; } private static Lectures lectures_from_file (Scanner input){ int collection_no = get_collection_no(); int length = Integer.parseInt(input.nextLine()); String format = input.nextLine(); String course = input.nextLine(); int disc_no = Integer.parseInt(input.nextLine()); String instructor = input.nextLine(); Lectures lectures = new Lectures(collection_no, length, format, course, disc_no, instructor); while (input.hasNextLine()){ String line = input.nextLine(); if (line.equals(separator)){ break; } lectures.add_lecture(line); } rreturn lectures; }
$ java VideoLibrary10 M(ovie), L(ecture), S(ee entries), D(one): s #1: Forbidden Planet, 98 minutes, DVD, Fred McLeod Wilcox, MGM Walter Pidgeon Anne Francis Leslie Nielson Warren Stevens #2: Origins of Great Ancient Civilizations - Disc 1, 180 minutes, DVD, Kenneth W. Harl Cradles of Civilization First Cities of Sumer Mesopotamian Kings and Scribes Hammurabi’s Babylon Egypt in the Pyramid Age The Middle Kingdom #3: The Day the Earth Stood Still, 92 minutes, DVD, Robert Wise, 20th Century Fox Michael Rennie Patricia Neal Hugh Marlowe M(ovie), L(ecture), S(ee entries), D(one): d Exiting
public String toString(){ return "#" + collection_no + ": " + title + ", " + length + " minutes, " + format; } public String short_string(){ return toString(); }
public String toString(){ String value = short_string(); for (int i = 0; i < actors.size(); i++){ value += "\n" + actors.get(i) ; } return value; } public String short_string(){ return super.toString() + ", " + director + ", " + studio; }
public String toString() { String value = short_string(); for (int i = 0; i < lectures.size(); i++){ value += "\n" + lectures.get(i) ; } return value; } public String short_string() { return super.toString() + instructor; }}
private static void list_videos(){
for (int i = 0; i < library.size(); i++){
Video entry = library.get(i);
System.out.println(entry.short_string());
}
}
$ java VideoLibrary11 M(ovie), L(ecture), S(ee entries), D(one): s #1: Forbidden Planet, 98 minutes, DVD, Fred McLeod Wilcox, MGM #2: Origins of Great Ancient Civilizations - Disc 1, 180 minutes, DVD, Kenneth W. Harl #3: The Day the Earth Stood Still, 92 minutes, DVD, Robert Wise, 20th Century Fox #4: Origins of Great Ancient Civilizations - Disc 2, 180 minutes, DVD, Kenneth W. Harl M(ovie), L(ecture), S(ee entries), D(one): d Exiting
private static final String prompt = "M(ovie), L(ecture), S(ee entries), F(ind), D(one): ";
while
loop of the
main method
} else if (action.equals("f")) { find_videos(console); }
private static final String find_prompt = "M(ovies), L(ectures): ";
private static void find_videos(Scanner console){ System.out.print(find_prompt); String action = console.next(); action = action.trim(); // remove any leading or trailing whitespace action = action.toLowerCase(); // only lowercase letters if (action.equals("m")) { videos_for_class("Movie"); } else if (action.equals("l")) { videos_for_class("Lectures"); } else { System.out.println(action + " is not a valid choice"); } } private static void videos_for_class(String class_name){ for (int i = 0; i < library.size(); i++){ Video entry = library.get(i); String entry_class_name = entry.getClass().getSimpleName(); if (entry_class_name.equals(class_name)){ System.out.println(entry); System.out.println(separator); } } }
$ jv VideoLibrary12.java M(ovie), L(ecture), S(ee entries), F(ind), D(one): f M(ovies), L(ectures): m #1: Forbidden Planet, 98 minutes, DVD, Fred McLeod Wilcox, MGM Walter Pidgeon Anne Francis Leslie Nielson Warren Stevens ---------------------------------------- #3: The Day the Earth Stood Still, 92 minutes, DVD, Robert Wise, 20th Century Fox Michael Rennie Patricia Neal Hugh Marlowe ---------------------------------------- M(ovie), L(ecture), S(ee entries), F(ind), D(one): f M(ovies), L(ectures): l #2: Origins of Great Ancient Civilizations - Disc 1, 180 minutes, DVD, Kenneth W. Harl Cradles of Civilization First Cities of Sumer Mesopotamian Kings and Scribes Hammurabi’s Babylon Egypt in the Pyramid Age The Middle Kingdom ---------------------------------------- #4: Origins of Great Ancient Civilizations - Disc 2, 180 minutes, DVD, Kenneth W. Harl Imperial Egypt New Peoples of the Bronze Age The Collapse of the Bronze Age From Hebrews to Jews Imperial Assyria The Persian Empire ---------------------------------------- M(ovie), L(ecture), S(ee entries), F(ind), D(one): d Exiting
private static final String find_prompt = "M(ovies), L(ectures, D(irectors), I(instructors): ";
private static void find_videos(Scanner console){
String search_value;
System.out.print(find_prompt);
String action = console.next();
action = action.trim(); // remove any leading or trailing whitespace
action = action.toLowerCase(); // only lowercase letters
if (action.equals("m")) {
videos_for_class("Movie");
} else if (action.equals("l")) {
videos_for_class("Lectures");
} else if (action.equals("d")) {
search_value = get_search_value(console, "Movie", "director");
videos_for_field("Movie", "director", search_value);
} else if (action.equals("i")) {
search_value = get_search_value(console, "Lectures", "instructor");
videos_for_field("Lectures", "instructor", search_value);
} else {
System.out.println(action + " is not a valid choice");
}
}
private static String get_search_value(Scanner console, String class_name, String field_name){ System.out.print(field_name + " value: "); String serach_value = console.next(); serach_value = serach_value.trim(); return serach_value.toLowerCase(); } private static void videos_for_field(String class_name, String field_name, String search_value){ String list = ""; String field_value; for (int i = 0; i < library.size(); i++){ Video entry = library.get(i); String entry_class_name = entry.getClass().getSimpleName(); if (entry_class_name.equals(class_name)){ if (class_name.equals("Movie")) { Movie movie = (Movie) entry; field_value = movie.director().toLowerCase(); if (field_value.contains(search_value)) { list += entry.toString() + "\n" + separator + "\n"; } } else if (class_name.equals("Lectures")) { Lectures lectures = (Lectures) entry; field_value = lectures.instructor().toLowerCase(); if (field_value.contains(search_value)) { list += entry.toString() + "\n" + separator + "\n"; } } } } if (list.length() > 0) { System.out.println(list); } else { System.out.println("Could not find " + field_name + " matching " + search_value); } }
$ java VideoLibrary13 M(ovie), L(ecture), S(ee entries), F(ind), D(one): f M(ovies), L(ectures, D(irectors), I(instructors): d director value: adfafd Could not find director matching adfafd M(ovie), L(ecture), S(ee entries), F(ind), D(one): f M(ovies), L(ectures, D(irectors), I(instructors): d director value: wilcox #1: Forbidden Planet, 98 minutes, DVD, Fred McLeod Wilcox, MGM Walter Pidgeon Anne Francis Leslie Nielson Warren Stevens ---------------------------------------- M(ovie), L(ecture), S(ee entries), F(ind), D(one): f M(ovies), L(ectures, D(irectors), I(instructors): i instructor value: harl #3: Origins of Great Ancient Civilizations - Disc 1, 180 minutes, DVD, Kenneth W. Harl Cradles of Civilization First Cities of Sumer Mesopotamian Kings and Scribes Hammurabi’s Babylon Egypt in the Pyramid Age The Middle Kingdom ---------------------------------------- #4: Origins of Great Ancient Civilizations - Disc 2, 180 minutes, DVD, Kenneth W. Harl Imperial Egypt New Peoples of the Bronze Age The Collapse of the Bronze Age From Hebrews to Jews Imperial Assyria The Persian Empire ---------------------------------------- M(ovie), L(ecture), S(ee entries), F(ind), D(one): d Exiting