Esempio n. 1
0
 @Override
 public List<Person> findAll() throws ServiceException {
   List<Person> people = new ArrayList<Person>();
   for (Person each : database.getPeople()) {
     if (each.isActive()) people.add(each);
   }
   return people;
 }
Esempio n. 2
0
  @Override
  public void create(Person model) throws ServiceException {
    if (model == null)
      throw new IllegalArgumentException("This method requires an argument of type Person!");
    if (find(model) != null) throw new ServiceException("This person already exists!");

    List<Person> people = database.getPeople();
    model.setActive(true);
    model.setId(people.size() + 1);
    people.add(model);

    database.setPeople(people);
  }
Esempio n. 3
0
  @Override
  public void update(Person model) throws ServiceException {
    if (model == null)
      throw new IllegalArgumentException("This method requires an argument of type Person!");
    if (find(model) == null) throw new ServiceException("This person does not exist!");

    List<Person> people = database.getPeople();
    people.set((int) model.getId() - 1, model);
    database.setPeople(people);
  }
Esempio n. 4
0
  @Override
  public Person find(Person model) throws ServiceException {
    List<Person> people = database.getPeople();

    // search by id
    try {
      return people.get((int) model.getId() - 1);
    } catch (IndexOutOfBoundsException e) {
      // do nothing
    }

    // search by name
    for (Person each : findAll()) {
      if (each.getFirstName().equalsIgnoreCase(model.getFirstName())
          && each.getLastName().equalsIgnoreCase(model.getLastName())) return each;
    }

    return null;
  }
Esempio n. 5
0
 @Override
 public void delete(Person model) throws ServiceException {
   model.setActive(false);
   update(model);
 }