コード例 #1
0
 private static void createGuestPerson(String firstName, String lastName, int age) {
   AccessController.loginTempUser();
   Person person = new Person(firstName, lastName, age, false, Permission.GUEST);
   AccessController.setCurrentPerson(person);
   AccessController.logoutTempUser();
 }
コード例 #2
0
  private static void createPerson() throws IOException {
    boolean continueCreation = true;
    if (!AccessController.isLoggedInAsAdmin() && AccessController.getCurrentPerson() != null) {
      System.out.println(
          "Can have only 1 person at a time.\nDelete current person?" + getConfirmPhrase());
      String delete = br.readLine();
      if (delete.equals(confirmPhrase)) {
        // TODO only remove if admin requests this specific removal
        // PersonDatabase.removePerson(AccessController.getCurrentPerson()
        // .getId(), AccessController.getAdmin().getPerson());
        AccessController.setCurrentPerson(null);
      } else {
        System.out.println("Creation cancelled.");
        continueCreation = false;
      }
    }
    if (continueCreation) {
      String firstName = "user", lastName = "" + (++counter);
      int age = 0;
      boolean valid = false;
      while (!valid) {
        System.out.print("Enter first name:");
        firstName = br.readLine();
        if (!firstName.equals("")) {
          valid = true;
        }
      }
      valid = false;
      while (!valid) {
        System.out.print("Enter last name:");
        lastName = br.readLine();
        if (!lastName.equals("")) {
          valid = true;
        }
      }
      valid = false;
      while (!valid) {
        try {
          System.out.print("Enter age:");
          age = Integer.parseInt(br.readLine());
          valid = true;
        } catch (NumberFormatException nfe) {
          System.out.println("Invalid Format!");
        }
      }

      boolean editable = false;
      Permission permission = Permission.GUEST;
      if (AccessController.isLoggedInAsUser() || AccessController.isLoggedInAsAdmin()) {
        permission = Permission.USER;
        if (AccessController.isLoggedInAsAdmin()) {
          System.out.print("Editable person?" + getConfirmPhrase());
          String editString = br.readLine();
          if (editString.equals(confirmPhrase)) {
            editable = true;
          }
          System.out.print("Admin rights?" + getConfirmPhrase());
          String permString = br.readLine();
          if (permString.equals(confirmPhrase)) {
            permission = Permission.ADMIN;
          }
        }
      }
      if (AccessController.getCurrentUser() == null) {
        createGuestPerson(firstName, lastName, age);
      } else {
        Person person = new Person(firstName, lastName, age, editable, permission);
        AccessController.setCurrentPerson(person);
      }
      System.out.println(
          "Person created: "
              + AccessController.getCurrentPerson().getFirstName()
              + " "
              + AccessController.getCurrentPerson().getLastName()
              + " ["
              + AccessController.getCurrentPerson().getPermission()
              + "]");
    }
  }