Exemplo n.º 1
0
  public static void main(String[] args) {
    MyScanner sc = new MyScanner();
    out = new PrintWriter(new BufferedOutputStream(System.out));

    int n = sc.nextInt();

    int[] inp = new int[n];

    for (int i = 0; i < n; i++) inp[i] = sc.nextInt();

    Arrays.sort(inp);

    long ans = inp[n - 1];
    int ok = inp[n - 1] - 1;
    for (int i = n - 2; ok > 0 && i >= 0; i--) {
      if (ok > inp[i]) {
        ans += inp[i];
        ok = inp[i] - 1;
      } else {
        ans += ok;
        ok--;
      }
    }

    out.println(ans);

    // Start writing your solution here.

    /*
           int n      = sc.nextInt();        // read input as integer
           long k     = sc.nextLong();       // read input as long
           double d   = sc.nextDouble();     // read input as double
           String str = sc.next();           // read input as String
           String s   = sc.nextLine();       // read whole line as String

    while(sc.hasNext()) {
    	...
    }

           int result = 3*n;
           out.println(result);                    // print via PrintWriter
           */

    // Stop writing your solution here.
    out.close();
  }
Exemplo n.º 2
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!");
  }
Exemplo n.º 3
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!");
      }
    }
  }
Exemplo n.º 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!");
  }
Exemplo n.º 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!");
  }
Exemplo n.º 6
0
  public void useOpenMenu(DatabaseContainer db, MyScanner sc) {

    int choice = 0;
    while (choice != 9) {
      printOpenMenu();
      choice = sc.nextInt("Enter your choice: ", 1, 9);

      if (choice == 1) addRecord(db, sc);
      else if (choice == 2) editRecord(db, sc);
      else if (choice == 3) searchByRegex(db, sc);
      else if (choice == 4) searchByRange(db, sc);
      else if (choice == 5) deleteRecord(db, sc);
      else if (choice == 6) deleteAll(db);
      else if (choice == 7) showAll(db);
      else if (choice == 8) showCount(db);
    }
  }
Exemplo n.º 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!");
  }
Exemplo n.º 8
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!");
  }
Exemplo n.º 9
0
 public static void main(String[] args) {
   MyScanner sc = new MyScanner();
   out = new PrintWriter(new BufferedOutputStream(System.out));
   while (sc.hasNext()) out.println(Math.abs(sc.nextLong() - sc.nextLong()));
   out.close();
 }