private void createNewElement(Manager manager) { try { scanner = new Scanner(file); String partString = ""; String newString = ""; String readString = ""; String writeString = ""; int i = 0; while (scanner.hasNextLine()) { if (i++ == 0) { partString = scanner.nextLine(); readString += partString + "\n"; } else { readString += scanner.nextLine() + "\n"; } } int id = Integer.parseInt(partString.split(":")[1]) + 1; newString += "counter:" + id; String str = readString.replaceAll(partString, newString); writeString += str + "id:" + id + '\n'; writeString += "Name:" + manager.getName() + '\n'; writeString += "Surname:" + manager.getSurname() + '\n' + "Age:" + manager.getAge() + '\n'; writeString += "IDdepartment:" + manager.getDepartment().getIdDepartment(); fileOutputStream = new FileOutputStream(file); printWriter = new PrintWriter(fileOutputStream); printWriter.println(writeString); printWriter.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } }
private void createNewFile(Manager manager) { try { fileOutputStream = new FileOutputStream(file, true); printWriter = new PrintWriter(file); printWriter.println("counter:1"); printWriter.println("id:1"); printWriter.println("Name:" + manager.getName()); printWriter.println("Surname:" + manager.getSurname()); printWriter.println("Age:" + manager.getAge()); printWriter.println("IDdepartment:" + manager.getDepartment().getIdDepartment()); printWriter.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } }
public Manager getManager(int id) { try { scanner = new Scanner(file); while (scanner.hasNextLine()) { String str = scanner.nextLine(); String[] array = str.split(":"); if (array[0].equals("id") && Integer.parseInt(array[1]) == id) { Manager manager = new Manager(); manager.setIdManager(id); manager.setName(scanner.nextLine().split(":")[1]); manager.setSurname(scanner.nextLine().split(":")[1]); manager.setAge(Integer.parseInt(scanner.nextLine().split(":")[1])); manager.setDepartment( iDepartmentDao.getDepartment(Integer.parseInt(scanner.nextLine().split(":")[1]))); return manager; } } } catch (FileNotFoundException e) { e.printStackTrace(); } return null; }
/** A supposed existent user tries to login to our system. */ private void logIn() { boolean check; boolean exists = false; int tries = 0; String user, pass; Person pers = null; /** Trying to select the person by its username. Maximum number of tries: 3. */ while (!exists && tries < 3) { int i; Consola.escriu("Insert username: "******"Invalid username, please try again."); tries += 1; } } /** * If we reached the end, we wither have a person or we have more than 3 tries. More than 3 * tries means that exists = false. Let's ask for the password in the case that exists is true. */ if (exists) { exists = false; tries = 0; // You can try 3 times for your password. while (!exists && tries < 3) { Consola.escriu("Insert password: "******"Correct password"); } else { Consola.escriu("Incorrect password."); tries += 1; } } } /** * We can have two situations: either we have a correct person (with correct user and password) * or we have exceeded the maximum tries permited. If we have a correct person, we show the * pertinent menu. If we reach here with more than 3 tries, then you can't log in. */ if (pers instanceof Client && tries < 3) { // Given the client, we have to check if he can logIn // id est, if he has less than 3 admonishes. if (((Client) pers).canLogIn()) { // Succeed, the client can log in and is now in use of our application. current = pers; selectOptionMenuClient(); } else { // Our client can log in =( Inform him/her. Consola.escriu("You have 3 admonishes, you can't log in, sorry."); } } else if (pers instanceof Manager && tries < 3) { current = pers; selectOptionMenuManager(); } else if (pers instanceof Admin && tries < 3) { current = pers; selectOptionMenuAdmin(); } else { Consola.escriu("You've exceeded the maximum permited tries"); } }