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!"); }
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!"); }
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!"); }
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!"); } } }