Beispiel #1
0
  public void deleteRecord(DatabaseContainer db, MyScanner sc) {
    int id =
        sc.nextInt(
            "Please enter the id of the sudent you wish to delete: ", 1000000000, 1999999999);

    if (db.getFile().deleteStudent(db.getFile().getStudent(id)))
      System.out.println("Delete successful!");
    else System.out.println("That student appears to be gone already!");
  }
Beispiel #2
0
 public void showCount(DatabaseContainer db) {
   try {
     System.out.println(
         "There are " + db.getFile().getRecordCount() + " total records in the database.");
   } catch (IOException ex) {
     System.out.println(
         "IOException in MainApp::showCount -> StudentRandomFile::getRecordCount()");
   }
 }
Beispiel #3
0
  public void showAll(DatabaseContainer db) {
    ArrayList<Student> results = db.getFile().getStudents();

    if (!results.isEmpty()) {
      System.err.println("All the entries: ");
      for (Student s : results) {
        System.out.println("**********");
        System.out.println(s);
      }
    } else System.out.println("No entries found!");
  }
Beispiel #4
0
  public void searchByRange(DatabaseContainer db, MyScanner sc) {
    String typeStr =
        sc.validate("Please enter which field you want to search (age / gpa): ", "age|gpa");
    boolean type;
    if (typeStr.equals("age")) type = db.getFile().AGE_TYPE;
    else type = db.getFile().GPA_TYPE;

    int lo = sc.nextInt("What is the lower range of your search?");
    int hi = sc.nextInt("What is the higher ranger of your search?");

    ArrayList<Student> results = db.getFile().searchByRange(hi, lo, type);

    if (!results.isEmpty()) {
      System.out.println("Results found: ");
      for (Student s : results) {
        System.out.println("**********");
        System.out.println(s);
      }
    } else System.out.println("No results found!");
  }
Beispiel #5
0
  public void searchByRegex(DatabaseContainer db, MyScanner sc) {
    String regex = sc.nextLine("Please enter the regex you want to search with: ", 1, 100);
    ArrayList<Student> results = db.getFile().searchByRegex(regex);

    if (!results.isEmpty()) {
      System.out.println("Results found: ");
      for (Student s : results) {
        System.out.println("**********");
        System.out.println(s);
      }
    } else System.out.println("No results found!");
  }
Beispiel #6
0
  public void addRecord(DatabaseContainer db, MyScanner sc) {
    int id =
        sc.nextInt("Please enter student ID (1000000000 - 1999999999): ", 1000000000, 1999999999);
    String fName = sc.nextLine("Please enter student First Name: ", 1, 50);
    String lName = sc.nextLine("Please enter student Last Name: ", 1, 50);
    int age = sc.nextInt("Please enter student age: ");
    boolean sex;
    String sexStr = sc.validate("Please enter the student sex (male / female)", "male|female");
    if (sexStr.equals("male")) sex = true;
    else sex = false;
    double GPA = sc.nextDouble("Please enter the student's current GPA (0-100): ", 0.0, 100.0);

    Student newStudent = new Student(id, fName, lName, age, sex, GPA);
    db.getFile().addStudent(newStudent);
    System.out.println("Student added!");
  }
Beispiel #7
0
  public void editRecord(DatabaseContainer db, MyScanner sc) {
    int id =
        sc.nextInt("Please enter the id of the student you wish to edit: ", 1000000000, 1999999999);
    Student s = db.getFile().getStudent(id);
    int choice =
        sc.nextInt(
            "Please enter the number of the field you wish to edit:"
                + "\n1. ID"
                + "\n2. First Name"
                + "\n3. Last Name"
                + "\n4. Age"
                + "\n5. Sex"
                + "\n6. Current GPA",
            1,
            6);

    if (choice == 1) {
      s.setID(
          sc.nextInt(
              "Please enter student ID (1000000000 - 1999999999): ", 1000000000, 1999999999));
    } else if (choice == 2) {
      s.setFirstName(sc.nextLine("Please enter student First Name: ", 1, 50));
    } else if (choice == 3) {
      s.setLastName(sc.nextLine("Please enter student Last Name: ", 1, 50));
    } else if (choice == 4) {
      s.setAge(sc.nextInt("Please enter student age: "));
    } else if (choice == 5) {
      boolean sex;
      String sexStr = sc.validate("Please enter the student sex (male / female)", "male|female");
      if (sexStr.equals("male")) sex = true;
      else sex = false;
      s.setSex(sex);
    } else if (choice == 6) {
      s.setCurrentGPA(
          sc.nextDouble("Please enter the student's current GPA (0-100): ", 0.0, 100.0));
    }
    System.out.println("Record Edited!");
  }
Beispiel #8
0
 public void deleteAll(DatabaseContainer db) {
   if (db.getFile().deleteAll()) System.out.println("Delete successful!");
   else System.out.println("Something seems to have gone wrong."); // IOException
 }
Beispiel #9
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!");
      }
    }
  }