Zmienna 'choice' nie pozwala na podanie jej wartości z klawiatury gdy wychodzę z "2. Add movie / series to the database\r".
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; // Signals that an I/O exception of some sort has occurred import java.io.ObjectOutputStream; import java.nio.file.Files; // This class consists exclusively of static methods that operate on files, directories, or other types of files import java.nio.file.Paths; // An object that may be used to locate a file in a file system. It will typically represent a system dependent file path import java.util.HashMap; import java.util.Map; import java.util.Scanner; // A class for reading data from the input /** The main class of the program */ public class Database { private static Scanner input = new Scanner(System.in); static String choice; /** Main method */ static public void main(String[] args) { createTheRequiredFiles(); while(true) { System.out.println("1. Browse data" ); System.out.println("2. Add movie / series to the database\r" ); System.out.print("\n Your choice: " ); choice = input.nextLine(); switch(choice) { case "1": { while(true) { System.out.println("\n1. Movie database" ); System.out.println("2. Serials database\r" ); System.out.print("\n Your choice: " ); choice = input.next(); switch(choice) { case "1": { ////////////////// break; } case "2": { ////////////////// break; } default: { System.err.println("There is no such option in the menu!"); } } } } case "2": { while(true) { System.out.println("\n1. Add movie..." ); System.out.println("2. Add series..." ); System.out.println("9. Back\r" ); System.out.print("\n Your choice: " ); choice = input.nextLine(); if(choice.equals("9")) { break; } switch(choice) { case "1": { addMovieToTheDatabase(); break; } case "2": { addSeriesToTheDatabase(); break; } default: { System.out.println("There is no such option in the menu2!"); } } } } default: { System.err.println("There is no such option in the menu1!"); } } } } /** This method produces the required files */ static final void createTheRequiredFiles() { try { Files.createFile(Paths.get("movies.ser")); Files.createFile(Paths.get("series.ser")); } catch(IOException e) {} } /** This method adds the movie to the database */ static void addMovieToTheDatabase() { String title, description, director, releaseDate; int budget; int indexGenre, indexCountry; int rate; Map<Integer, String> genres = new HashMap<Integer, String>(7); genres.put(1, "Comedy");genres.put(2, "Adventure");genres.put(3, "Fantasy"); genres.put(4, "Animation");genres.put(5, "Horror");genres.put(6, "Thriller"); genres.put(7, "Action"); Map<Integer, String> country = new HashMap<Integer, String>(3); country.put(1, "USA");country.put(2, "UK");country.put(3, "Poland"); System.out.print("\nTitle: "); title = input.nextLine(); System.out.print("Description: "); description = input.nextLine(); System.out.print("Director: "); director = input.next(); System.out.print("Release date DD/MM/YYYY: "); releaseDate = input.next(); System.out.print("Genre: "); for(Integer i : genres.keySet()) { System.out.print(i + ". " + genres.get(i) + ""); } System.out.print("\nChoice: "); indexGenre = input.nextInt(); System.out.print("Country: "); for(Integer i : country.keySet()) { System.out.print(i + ". " + country.get(i) + ""); } System.out.print("\nChoice: "); indexCountry = input.nextInt(); System.out.println("Budget: "); budget = input.nextInt(); System.out.println("Rate 1-5: "); rate = input.nextInt(); Movie movie = new Movie(title, description, director, releaseDate, genres.get(indexGenre), country.get(indexCountry), budget, rate); if(serialization(movie)) { System.out.println("The film has been saved successfully!"); } else { System.out.println("The film was not saved!"); } } /** This method serializes the object movie. */ static boolean serialization(Movie movie) { try { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("movies.ser", true)); oos.writeObject(movie); oos.close(); return true; } catch (FileNotFoundException e) { System.out.println("Can not find the file. Please try again later..."); return false; } catch (IOException e) { System.out.println("Unable to save file. Please try again later..."); return false; } } /** This method adds the series to the database */ static void addSeriesToTheDatabase() { String title, description, director, releaseDate; int indexGenre, indexCountry; int rate; Map<Integer, String> genres = new HashMap<Integer, String>(7); genres.put(1, "Comedy");genres.put(2, "Adventure");genres.put(3, "Fantasy"); genres.put(4, "Animation");genres.put(5, "Horror");genres.put(6, "Thriller"); genres.put(7, "Action"); Map<Integer, String> country = new HashMap<Integer, String>(3); country.put(1, "USA");country.put(2, "UK");country.put(3, "Poland"); System.out.print("\nTitle: "); title = input.next(); System.out.print("Description: "); description = input.nextLine(); input.nextLine(); System.out.print("Director: "); director = input.nextLine(); System.out.print("Release date DD/MM/YYYY: "); releaseDate = input.nextLine(); System.out.print("Genre: "); for(Integer i : genres.keySet()) { System.out.print(i + ". " + genres.get(i) + ""); } System.out.print("\nChoice: "); indexGenre = input.nextInt(); System.out.print("Country: "); for(Integer i : country.keySet()) { System.out.print(i + ". " + country.get(i) + ""); } System.out.print("\nChoice: "); indexCountry = input.nextInt(); System.out.println("Rate 1-5: "); rate = input.nextInt(); Series series = new Series(title, description, director, releaseDate, genres.get(indexGenre), country.get(indexCountry), rate); if(serialization(series)) { System.out.println("The film has been saved successfully!"); } else { System.out.println("The film was not saved!"); } } /** This method serializes the object series */ static boolean serialization(Series series) { try { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("series.ser", true)); oos.writeObject(series); oos.close(); return true; } catch (FileNotFoundException e) { System.out.println("Can not find the file. Please try again later..."); return false; } catch (IOException e) { System.out.println("Unable to save file. Please try again later..."); return false; } } }
Tak wynik
1. Browse data 2. Add movie / series to the database Your choice: 2 1. Add movie... 2. Add series... 9. Back Your choice: 9 1. Browse data 2. Add movie / series to the database Your choice: There is no such option in the menu1!
Gdy wracam do głównego menu to wykonuje się automatycznie opcja 'default: '.