@Log private Person createPerson(PersonDTO personDTO) throws DuplicateException, NullArgumentException { Person user = null; if (personDTO != null) { if (personDTO.getEmail() != null && personDTO.getPassword() != null) { try { user = em.createNamedQuery("Person.findByEmail", Person.class) .setParameter("email", personDTO.getEmail()) .getSingleResult(); } catch (NoResultException e) { } if (user == null) { Person persontemp = new Person(personDTO.getEmail()); persontemp.setName(personDTO.getFirstname()); persontemp.setSurname(personDTO.getSurname()); persontemp.setSsn(personDTO.getSsn()); persontemp.setPassword(getEncryptedPassword(personDTO.getPassword())); em.persist(persontemp); return persontemp; } else { throw new DuplicateException("Person already exists"); } } else { throw new NullArgumentException("Null argument for email or password"); } } return user; }
// read data from the starting XML file (people.xml) // and put them into a PeopleStore instance public static void initializeDB() { HealthProfileReader HP = new HealthProfileReader(); List<String> peopleInfo = null; try { peopleInfo = HP.readallinfo(); } catch (XPathExpressionException e) { e.printStackTrace(); } // "saves" all the retrieved information about people for (int i = 0; i < peopleInfo.size(); i += 8) { Person pallino = new Person( peopleInfo.get(i + 0), // id peopleInfo.get(i + 1), // firstname peopleInfo.get(i + 2), // lastname peopleInfo.get(i + 3)); // birthdate pallino.setHProfile( new HealthProfile( peopleInfo.get(i + 4), // last update Double.parseDouble(peopleInfo.get(i + 5)), // weight Double.parseDouble(peopleInfo.get(i + 6)))); // height // bmi will be calculated in HealthProfile people.getData().add(pallino); } }
@Override public boolean createOrUpdatePerson( String electronicShopID, String clientID, String name, String family, String addressCity, String addressLine, String phoneNumber, String mail, String IBANNumber, String SWIFTCode) { // check if we know that shop if (!ConfigurationProperties.getElectronicShopID().equals(electronicShopID)) { logger.warning("electronicShopID \"" + electronicShopID + "\" not known!"); return false; } List<Client> cl = Client.queryGetByForeignID(clientID, 0, 1); List<Person> pl = null; Client c; if (cl.size() == 0) { c = new Client(); } else { c = cl.get(0); pl = Person.queryGetAll(0, 1, c.getID()); } Person p; if ((pl == null) || (pl.size() == 0)) { p = new Person(); } else { p = pl.get(0); } try { c.setForeignID(clientID); c.setAddressCity(addressCity); c.setAddressLine(addressLine); c.setMail(mail); c.setPhoneNumber(phoneNumber); c.setIBANNumber(IBANNumber); c.setSWIFTCode(SWIFTCode); p.setName(name); p.setFamily(family); c.setPerson(p); c.writeToDB(); } catch (RuntimeException e) { logger.log(Level.WARNING, "Can't write to DB!", e); return false; } return true; }
@Override public void map(String key, Person value, Context<String, Integer> context) { if (state != null) { if (!state.equals(value.getState())) { return; } } String name = abbreviationMapping.get(value.getState()); context.emit(name, ONE); }
// Used for adding competences to the competence profile of an user @Log private void addCompetences(Person person, ApplicationDTO appDTO) { Competence tempcomp = null; Competence_profile competenceProfile; if (appDTO.getCompetences() != null) { for (CompetenceProfileDTO comp : appDTO.getCompetences()) { try { tempcomp = em.createNamedQuery( "Competence.findByName", Competence.class) // Check if competence exists .setParameter("name", comp.getName()) .getSingleResult(); } catch (NoResultException e) { } competenceProfile = new Competence_profile(tempcomp, person); if (!person .getCompetenceProfileCollection() .contains(competenceProfile)) { // Check if competence profile already exists competenceProfile.setYearsOfExperience(comp.getYearsOfExperience()); em.persist(competenceProfile); } } } }
private void updateFields(Person person) { this.person = person; tfFirstName.setText(person.getFirstName()); tfLastName.setText(person.getLastName()); tfAddress.setText(person.getAddress()); tfPhoneNumber.setText(person.getPhoneNumber()); DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH); try { Date result = df.parse(person.getDOB()); dateModel.setValue(result); dateModel.setSelected(true); } catch (ParseException ex) { Logger.getLogger(PersonForm.class.getName()).log(Level.SEVERE, null, ex); } }
@Override public void actionPerformed(ActionEvent e) { if (e.getSource() == btnSave) { if (tfFirstName.getText().isEmpty()) { JOptionPane.showMessageDialog(null, "Enter first name!"); return; } else if (tfLastName.getText().isEmpty()) { JOptionPane.showMessageDialog(null, "Enter last name!"); return; } else if (tfAddress.getText().isEmpty()) { JOptionPane.showMessageDialog(null, "Enter Address!"); return; } else if (tfPhoneNumber.getText().isEmpty()) { JOptionPane.showMessageDialog(null, "Enter phone number!"); return; } else if (dateModel.getValue().toString().isEmpty()) { JOptionPane.showMessageDialog(null, "Enter date of birth!"); return; } DBConnection dbconnection = new DBConnection(); if (person == null) { person = new Person(); } person.setFirstName(tfFirstName.getText()); person.setLastName(tfLastName.getText()); person.setAddress(tfAddress.getText()); person.setPhoneNumber(tfPhoneNumber.getText()); person.setDOB(dateModel.getYear() + "-" + dateModel.getMonth() + "-" + dateModel.getDay()); int result = -1; if (person.getId() == 0) { result = dbconnection.savePerson(person); } else { result = dbconnection.updatePerson(person); } if (result != -1) { JOptionPane.showMessageDialog(null, "Saved Successfully!"); } else { JOptionPane.showMessageDialog(null, "Save Unsuccessful!"); } } else if (e.getSource() == btnReset) { tfFirstName.setText(""); tfLastName.setText(""); tfAddress.setText(""); tfPhoneNumber.setText(""); tfDOB.setText(""); dateModel.setValue(new Date()); } else if (e.getSource() == btnDelete) { DBConnection dBConnection = new DBConnection(); int result = dBConnection.deletePerson(person.getId()); if (result != -1) { JOptionPane.showMessageDialog(null, "Deleted Successfully!"); } else { JOptionPane.showMessageDialog(null, "Deletion Unsuccessful!"); } } }
public static void main(String[] args) throws Exception { int argCount = args.length; String filein = argCount == 1 ? args[0] : "marshalpeople.xml"; // Checks if there's a file as argument, otherwise uses the // default /* Sets up the unmarshaller and extrapolates the data */ JAXBContext jc = JAXBContext.newInstance(Storage.class); Unmarshaller um = jc.createUnmarshaller(); Storage people = (Storage) um.unmarshal(new FileReader(filein)); List<Person> list = people.getData(); /* Prints the data out in a proper way */ for (Person person : list) { HealthProfile hp = person.gethProfile(); System.out.println("Person " + person.getPersonId() + ":"); System.out.println("\tName: " + person.getFirstname()); System.out.println("\tSurname: " + person.getLastname()); System.out.println("\tBirthdate: " + person.getBirthdate()); System.out.println("\tHealth Profile (updated on " + hp.getLastupdate() + "):"); System.out.println("\t\tHeight: " + hp.getHeight()); System.out.println("\t\tWeight: " + hp.getWeight()); System.out.println("\t\tBMI: " + hp.getBMI()); System.out.println(); } }
public void display() { // First, let's display the generic Person info. super.display(); // Then, display Professor-specific info. System.out.println("Professor-Specific Information:"); System.out.println("\tTitle: " + getTitle()); System.out.println("\tTeaches for Dept.: " + getDepartment()); displayTeachingAssignments(); // Finish with a blank line. System.out.println(); }
public void deletePerson(Person p) throws SQLException { conn = DBTool.getInstance(); System.out.println("Deleting person person..."); if (conn.isValid(10000)) { Statement s = conn.createStatement(); int idperson = p.getId(); s.execute("DELETE FROM guest WHERE idperson = " + idperson + ";"); s.execute("DELETE FROM person WHERE idperson = " + idperson + ";"); System.out.println("Deletion complete!"); s.close(); conn.close(); } else { System.out.println("Connection not valid"); conn.close(); } }
public int saveGuest(Person p, Guest g) throws SQLException { conn = DBTool.getInstance(); System.out.println("Saving guest..."); if (conn.isValid(10000)) { Statement s = conn.createStatement(); int idPerson = p.getId(); String guestFirstname = g.getFirstname(); String guestMiddlename = g.getMiddlename(); String guestLastname = g.getLastname(); String guestBirthday = ADate.formatADate(g.getBirthday(), ""); String guestCreationdate = ADate.formatADate(g.getCreationDate(), ""); s.execute( "INSERT INTO guest (firstname, middlename, lastname, creationdate, birthday, idperson) VALUES('" + guestFirstname + "', '" + guestMiddlename + "', '" + guestLastname + "', '" + guestCreationdate + "', '" + guestBirthday + "', '" + idPerson + "');"); ResultSet rs = s.executeQuery("SELECT LAST_INSERT_ID();"); rs.next(); int returnId = rs.getInt(1); System.out.println("Save complete!"); rs.close(); s.close(); conn.close(); return returnId; } else { System.out.println("Connection not valid"); conn.close(); return -1; } }
@Test public void testMinDistance() { MinDistance md = new MinDistance(); Person ThiagoCassioKrug = null; Person thiago = new Person(); thiago.setName("Thiago Cassio Krug"); thiago.setId(4); int i = persons.indexOf(thiago); if (i >= 0) { ThiagoCassioKrug = persons.get(i); md.computeScore(ThiagoCassioKrug, true); } else { fail("Não encontrou a pessoa da pesquisa!"); } Collections.sort(ThiagoCassioKrug.getRelationships()); List<Relationship> relationships = ThiagoCassioKrug.getRelationships(); int j = 0; assertEquals(relationships.get(j).getPerson2().getName(), "Marcelo Maia Lopes"); assertEquals(relationships.get(j++).getScore(), 0.99998153499579921154432062633294d, delta); assertEquals(relationships.get(j).getPerson2().getName(), "Bruno Vicelli"); assertEquals(relationships.get(j++).getScore(), 0.99998153499579921154432062633294d, delta); assertEquals(relationships.get(j).getPerson2().getName(), "Rafael Tavares Amorim"); assertEquals(relationships.get(j++).getScore(), 0.99991690748109645194944281849824d, delta); assertEquals(relationships.get(j).getPerson2().getName(), "Helison Reus Teixeira"); assertEquals(relationships.get(j++).getScore(), 0.99990767497899605772160313166471d, delta); assertEquals(relationships.get(j).getPerson2().getName(), "Thiago Aquino de Lima"); assertEquals(relationships.get(j++).getScore(), 0.99989844247689566349376344483118d, delta); assertEquals(relationships.get(j).getPerson2().getName(), "Mateus Henrique Dal Forno"); assertEquals(relationships.get(j++).getScore(), 0.9998061174558917212153665764959d, delta); assertEquals(relationships.get(j).getPerson2().getName(), "Rafhael Rodrigues Cunha"); assertEquals(relationships.get(j++).getScore(), 0.99959376990758265397505377932473d, delta); assertEquals(relationships.get(j).getPerson2().getName(), "Renan Marcel Uchôa"); assertEquals(relationships.get(j++).getScore(), 0.9993260273466712213677028611524d, delta); assertEquals(relationships.get(j).getPerson2().getName(), "Juliano Rodovalho Macedo"); assertEquals(relationships.get(j++).getScore(), 0.99711945934467700091401770793903d, delta); }
@Test public void testRelationshipOccurrenceNumber() { RelationshipOccurrenceNumber ron = new RelationshipOccurrenceNumber(); Person ThiagoCassioKrug = null; Person thiago = new Person(); thiago.setName("Thiago Cassio Krug"); thiago.setId(4); int i = persons.indexOf(thiago); if (i >= 0) { ThiagoCassioKrug = persons.get(i); ron.computeScore(ThiagoCassioKrug, true); } else { fail("Não encontrou a pessoa da pesquisa!"); } Collections.sort(ThiagoCassioKrug.getRelationships()); List<Relationship> relationships = ThiagoCassioKrug.getRelationships(); int j = 0; assertEquals(relationships.get(j).getPerson2().getName(), "Rafhael Rodrigues Cunha"); assertEquals(relationships.get(j++).getScore(), 1.0d, delta); assertEquals(relationships.get(j).getPerson2().getName(), "Helison Reus Teixeira"); assertEquals(relationships.get(j++).getScore(), 0.9333333333333333d, delta); assertEquals(relationships.get(j).getPerson2().getName(), "Rafael Tavares Amorim"); assertEquals(relationships.get(j++).getScore(), 0.9333333333333333d, delta); assertEquals(relationships.get(j).getPerson2().getName(), "Bruno Vicelli"); assertEquals(relationships.get(j++).getScore(), 0.8d, delta); assertEquals(relationships.get(j).getPerson2().getName(), "Mateus Henrique Dal Forno"); assertEquals(relationships.get(j++).getScore(), 0.8d, delta); assertEquals(relationships.get(j).getPerson2().getName(), "Renan Marcel Uchôa"); assertEquals(relationships.get(j++).getScore(), 0.7333333333333333d, delta); assertEquals(relationships.get(j).getPerson2().getName(), "Marcelo Maia Lopes"); assertEquals(relationships.get(j++).getScore(), 0.6666666666666666d, delta); assertEquals(relationships.get(j).getPerson2().getName(), "Thiago Aquino de Lima"); assertEquals(relationships.get(j++).getScore(), 0.6666666666666666d, delta); assertEquals(relationships.get(j).getPerson2().getName(), "Juliano Rodovalho Macedo"); assertEquals(relationships.get(j++).getScore(), 0.3333333333333333d, delta); }
@Test public void testMaxDistance() { MaxDistance md = new MaxDistance(); Person ThiagoCassioKrug = null; Person thiago = new Person(); thiago.setName("Thiago Cassio Krug"); thiago.setId(4); int i = persons.indexOf(thiago); if (i >= 0) { ThiagoCassioKrug = persons.get(i); md.computeScore(ThiagoCassioKrug, true); } else { fail("Não encontrou a pessoa da pesquisa!"); } Collections.sort(ThiagoCassioKrug.getRelationships()); List<Relationship> relationships = ThiagoCassioKrug.getRelationships(); int j = 0; assertEquals(relationships.get(j).getPerson2().getName(), "Marcelo Maia Lopes"); assertEquals(relationships.get(j++).getScore(), 0.98147960078660917895358821194132d, delta); assertEquals(relationships.get(j).getPerson2().getName(), "Bruno Vicelli"); assertEquals(relationships.get(j++).getScore(), 0.98142420577400681358655009094015d, delta); assertEquals(relationships.get(j).getPerson2().getName(), "Helison Reus Teixeira"); assertEquals(relationships.get(j++).getScore(), 0.97165621855178972053216142106672d, delta); assertEquals(relationships.get(j).getPerson2().getName(), "Renan Marcel Uchôa"); assertEquals(relationships.get(j++).getScore(), 0.87781706720338278876958444508046d, delta); assertEquals(relationships.get(j).getPerson2().getName(), "Juliano Rodovalho Macedo"); assertEquals(relationships.get(j++).getScore(), 0.80018095704116772686565786193716d, delta); assertEquals(relationships.get(j).getPerson2().getName(), "Mateus Henrique Dal Forno"); assertEquals(relationships.get(j++).getScore(), 0.78367324328566284748829780358775d, delta); assertEquals(relationships.get(j).getPerson2().getName(), "Rafael Tavares Amorim"); assertEquals(relationships.get(j++).getScore(), 0.62412637448875019619066963337734d, delta); assertEquals(relationships.get(j).getPerson2().getName(), "Thiago Aquino de Lima"); assertEquals(relationships.get(j++).getScore(), 0.24201157755763389436171096728925d, delta); assertEquals(relationships.get(j).getPerson2().getName(), "Rafhael Rodrigues Cunha"); assertEquals(relationships.get(j++).getScore(), 0.0d, delta); }
@Test public void testAverageDistance() { AverageDistance ad = new AverageDistance(); Person ThiagoCassioKrug = null; Person thiago = new Person(); thiago.setName("Thiago Cassio Krug"); thiago.setId(4); int i = persons.indexOf(thiago); if (i >= 0) { ThiagoCassioKrug = persons.get(i); ad.computeScore(ThiagoCassioKrug, true); } else { fail("Não encontrou a pessoa da pesquisa!"); } Collections.sort(ThiagoCassioKrug.getRelationships()); List<Relationship> relationships = ThiagoCassioKrug.getRelationships(); int j = 0; assertEquals(relationships.get(j).getPerson2().getName(), "Marcelo Maia Lopes"); assertEquals(relationships.get(j++).getScore(), 0.99972302493698817316480939499414d, delta); assertEquals(relationships.get(j).getPerson2().getName(), "Bruno Vicelli"); assertEquals(relationships.get(j++).getScore(), 0.99969224992998685907201043888237d, delta); assertEquals(relationships.get(j).getPerson2().getName(), "Helison Reus Teixeira"); assertEquals(relationships.get(j++).getScore(), 0.99966455242368567638849137838179d, delta); assertEquals(relationships.get(j).getPerson2().getName(), "Mateus Henrique Dal Forno"); assertEquals(relationships.get(j++).getScore(), 0.99941835236767516364609972948769d, delta); assertEquals(relationships.get(j).getPerson2().getName(), "Renan Marcel Uchôa"); assertEquals(relationships.get(j++).getScore(), 0.99847355965273482099717177685658d, delta); assertEquals(relationships.get(j).getPerson2().getName(), "Rafhael Rodrigues Cunha"); assertEquals(relationships.get(j++).getScore(), 0.99832583961912851335173678752012d, delta); assertEquals(relationships.get(j).getPerson2().getName(), "Rafael Tavares Amorim"); assertEquals(relationships.get(j++).getScore(), 0.99739643440768882774920831294489d, delta); assertEquals(relationships.get(j).getPerson2().getName(), "Thiago Aquino de Lima"); assertEquals(relationships.get(j++).getScore(), 0.98303066113947540923065559997415d, delta); assertEquals(relationships.get(j).getPerson2().getName(), "Juliano Rodovalho Macedo"); assertEquals(relationships.get(j++).getScore(), 0.97067295707809773526723477329591d, delta); }
public void savePerson(Person p) throws SQLException { conn = DBTool.getInstance(); System.out.println("Saving person..."); if (conn.isValid(10000)) { Statement s = conn.createStatement(); int idperson = p.getId(); String firstname = p.getFirstname(); String middlename = p.getMiddlename(); String lastname = p.getLastname(); String address = p.getAddress(); String birthday = ADate.formatADate(p.getBirthdayDate(), ""); String expirationDate = ADate.formatADate(p.getExpirationDate(), ""); File picturePathF = p.getPicturePath(); String picturePath = FileTool.getDefaultFolder() + "/" + picturePathF.getName(); int quarantine = 0; String quarantineExpirationDate = ""; if (p.getQuarantineExpirationDate() != null) { quarantineExpirationDate = p.getQuarantineExpirationDate().toString(); } int oneOne = 0; if (p.isQuarantine()) { quarantine = 1; } if (p.isOneOne()) { oneOne = 1; } int enrolled = 0; if (p.isEnrolled()) { enrolled = 1; } int hone = 0; if (p.isHone()) { hone = 1; } s.executeUpdate( "UPDATE person SET firstname = '" + firstname + "', middlename = '" + middlename + "', lastname = '" + lastname + "', address = '" + address + "', birthday = '" + birthday + "', expirationdate = '" + expirationDate + "', picturepath = '" + picturePath + "', oneone = " + oneOne + ", quarantine = " + quarantine + ", enrolled = " + enrolled + ", hone= " + hone + " WHERE idperson = " + idperson + ";"); System.out.println("Save complete!"); s.close(); conn.close(); } else { System.out.println("Connection not valid"); conn.close(); } }
public int insertPerson(Person p) throws SQLException { conn = DBTool.getInstance(); System.out.println("Inserting person..."); if (conn.isValid(10000)) { Statement s = conn.createStatement(); String firstname = p.getFirstname(); String middlename = p.getMiddlename(); String lastname = p.getLastname(); String address = p.getAddress(); String birthday = ADate.formatADate(p.getBirthdayDate(), ""); String expirationDate = ADate.formatADate(p.getExpirationDate(), ""); File picturePathF = p.getPicturePath(); String picturePath = FileTool.getDefaultFolder() + "/" + picturePathF.getName(); int quarantine = 0; String quarantineExpirationDate = ""; if (p.getQuarantineExpirationDate() != null) { quarantineExpirationDate = p.getQuarantineExpirationDate().toString(); } int oneOne = 0; String creationDate = ADate.formatADate(p.getCreationDate(), ""); if (p.isQuarantine()) { quarantine = 1; } if (p.isOneOne()) { oneOne = 1; } int enrolled = 0; if (p.isEnrolled()) { enrolled = 1; } int hone = 0; if (p.isHone()) { hone = 1; } s.executeUpdate( "INSERT INTO person (firstname, middlename, lastname, address, birthday, expirationdate, picturepath, quarantine, quarantineexpirationdate, oneone, creationdate, enrolled, hone) " + "VALUES('" + firstname + "', '" + middlename + "', '" + lastname + "', '" + address + "', '" + birthday + "', '" + expirationDate + "', '" + picturePath + "', '" + quarantine + "', '" + quarantineExpirationDate + "', '" + oneOne + "', '" + creationDate + "', '" + enrolled + "', '" + hone + "');"); ResultSet rs = s.executeQuery("SELECT LAST_INSERT_ID();"); rs.next(); int idback = rs.getInt(1); System.out.println("Insertion complete!"); rs.close(); s.close(); conn.close(); return idback; } else { System.out.println("Connection not valid"); conn.close(); return -1; } }
public ArrayList<Person> getAllPersons() throws SQLException { conn = DBTool.getInstance(); System.out.println("Getting all persons..."); if (conn.isValid(10000)) { Statement s = conn.createStatement(); ArrayList<Person> persons = new ArrayList<>(); ResultSet rs = s.executeQuery("SELECT * FROM person"); while (rs.next()) { int idperson = rs.getInt("idperson"); String firstname = rs.getString("firstname"); String middlename = rs.getString("middlename"); String lastname = rs.getString("lastname"); String address = rs.getString("address"); ADate birthday = new ADate(rs.getString("birthday")); ADate expirationDate = new ADate(rs.getString("expirationdate")); File picturePath = new File(rs.getString("picturepath")); ADate quarantineExpirationDate = null; if (!rs.getString("quarantineexpirationdate").isEmpty()) { quarantineExpirationDate = new ADate(rs.getString("quarantineexpirationdate")); } ADate creationDate = new ADate(rs.getString("creationdate")); boolean oneOne = false; if (rs.getInt("oneone") == 1) { oneOne = true; } boolean quarantine = false; if (rs.getInt("quarantine") == 1) { quarantine = true; } boolean enrolled = false; if (rs.getInt("enrolled") == 1) { enrolled = true; } boolean hone = false; if (rs.getInt("hone") == 1) { hone = true; } Statement s2 = conn.createStatement(); ArrayList<Guest> guests = new ArrayList<>(); ResultSet rsGuest = s2.executeQuery("SELECT * FROM guest WHERE idperson = " + idperson + ";"); while (rsGuest.next()) { Guest g = new Guest( rsGuest.getString("firstname"), rsGuest.getString("middlename"), rsGuest.getString("lastname"), new ADate(rsGuest.getString("birthday")), new ADate(rsGuest.getString("creationdate"))); g.setId(rsGuest.getInt("idguest")); guests.add(g); } Person p = new Person( firstname, middlename, lastname, address, birthday, expirationDate, picturePath, creationDate, oneOne); p.setId(idperson); p.setQuarantine(quarantine); p.setQuarantineExpirationDate(quarantineExpirationDate); p.setEnrolled(enrolled); p.setHone(hone); p.setGuests(guests); persons.add(p); rsGuest.close(); s2.close(); } System.out.println("Get complete!"); rs.close(); s.close(); conn.close(); return persons; } else { System.out.println("Connection invalid"); conn.close(); return null; } }
public void updatePerson(Person person) { Person personDao = em.find(Person.class, person.getId()); em.getTransaction().begin(); System.out.println("--->" + person); personDao.setFirstname(person.getFirstname()); personDao.setLastname(person.getLastname()); personDao.setBirthdate(person.getBirthdate()); personDao.setEmail(person.getEmail()); personDao.setWebsite(person.getWebsite()); if (person.getGroup() != null) { Group group = new Group(); group.setGroupID(person.getGroup().getGroupID()); group.setName(person.getGroup().getName()); personDao.setGroup(group); } em.getTransaction().commit(); }
/** 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"); } }