Beispiel #1
0
  public void run() {
    DatabaseContainer db = new DatabaseContainer();
    MyScanner sc = new MyScanner();

    System.out.println(":::::::::::::::::::::::::::::::");
    System.out.println("Welcome to the database system!");

    int choice = 0;
    while (choice != 4) {
      printMainMenu();
      choice = sc.nextInt("\nEnter your choice: ", 1, 4);

      if (choice == 1) {

        String fileName =
            sc.nextLine("Enter a name for your new database file (eg. something.db)", 3, 50);
        boolean valid = db.create(fileName);
        while (!valid) {
          fileName = sc.nextLine("That file already exists, please enter another", 3, 50);
          valid = db.create(fileName);
        }
        System.out.println("Database created!");
      } else if (choice == 2) {
        if (!db.isEmpty()) {
          System.out.println("Here is a list of existing files: ");
          db.printFileNames();
          String fileName = sc.nextLine("\nEnter the name of a valid database to open: ", 3, 50);
          boolean valid = db.open(fileName);
          while (!valid) {
            fileName = sc.nextLine("That filename does not exist, please try another.", 3, 50);
            valid = db.create(fileName);
          }

          useOpenMenu(db, sc);
        } else {
          System.out.println("There is currently no existing database files.");
        }
      } else if (choice == 3) {
        if (!db.isEmpty()) {
          System.out.println("Here is a list of existing files: ");
          db.printFileNames();
          String fileName = sc.nextLine("\nEnter the name of a valid database to delete: ", 3, 50);
          boolean valid = db.delete(fileName);
          while (!valid) {
            fileName = sc.nextLine("That filename does not exist, please try another.", 3, 50);
            valid = db.delete(fileName);
          }
        } else {
          System.out.println("There is currently no existing database files.");
        }
        System.out.println("Database deleted!");
      }
    }
  }