// Inner class for building objects of class Person public static class Builder { private String firstName; private String lastName; private String phoneNumber; private Address address;
public Person build() { return new Person(firstName, lastName, phoneNumber, address); } } }
public class Address { private String street; private String city; private String country;
public Address(String street, String city, String country) { this.street = street; this.city = city; this.country = country; }
public String getFullAddress() { return street + ", " + city + ", " + country; } }
public class PhoneBook { private List people;
public PhoneBook() { people = new ArrayList<>(); }
public void addPerson(Person person) { people.add(person); }
public List searchPeopleByLastName(String lastName) { List matchingPeople = new ArrayList<>(); for (Person person : people) { if (person.getLastName().equals(lastName)) { matchingPeople.add(person); } } return matchingPeople; }
public List searchPeopleByPhoneNumber(String phoneNumber) { List matchingPeople = new ArrayList<>(); for (Person person : people) { if (person.getPhoneNumber().equals(phoneNumber)) { matchingPeople.add(person); } } return matchingPeople; } }
// Usage example public static void main(String[] args) { Person.Builder personBuilder = new Person.Builder(); personBuilder.setFirstName("John") .setLastName("Doe") .setPhoneNumber("1234567890") .setAddress(new Address("Street 1", "City", "Country")); Person person = personBuilder.build();
PhoneBook phoneBook = new PhoneBook(); phoneBook.addPerson(person);
List matchingPeople = phoneBook.searchPeopleByLastName("Doe"); for (Person matchingPerson : matchingPeople) { System.out.println(matchingPerson.getFullName()); }
List matchingPeople2 = phoneBook.searchPeopleByPhoneNumber("1234567890"); for (Person matchingPerson : matchingPeople2) { System.out.println(matchingPerson.getFullName()); }
public class Person { private String firstName; private String lastName; private String phoneNumber; private Address address; public Person(String firstName, String lastName, String phoneNumber, Address address) { this.firstName = firstName; this.lastName = lastName; this.phoneNumber = phoneNumber; this.address = address; }
public String getFullName() { return firstName + " " + lastName; }
// Inner class for building objects of class Person public static class Builder { private String firstName; private String lastName; private String phoneNumber; private Address address;
public Builder setFirstName(String firstName) { this.firstName = firstName; return this; }
public Builder setLastName(String lastName) { this.lastName = lastName; return this; }
public Builder setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; return this; }
public Builder setAddress(Address address) { this.address = address; return this; }
public Person build() { return new Person(firstName, lastName, phoneNumber, address); } } }
public class Address { private String street; private String city; private String country;
public Address(String street, String city, String country) { this.street = street; this.city = city; this.country = country; }
public String getFullAddress() { return street + ", " + city + ", " + country; } }
public class PhoneBook { private List people;
public PhoneBook() { people = new ArrayList<>(); }
public void addPerson(Person person) { people.add(person); }
public List searchPeopleByLastName(String lastName) { List matchingPeople = new ArrayList<>(); for (Person person : people) { if (person.getLastName().equals(lastName)) { matchingPeople.add(person); } } return matchingPeople; }
public List searchPeopleByPhoneNumber(String phoneNumber) { List matchingPeople = new ArrayList<>(); for (Person person : people) { if (person.getPhoneNumber().equals(phoneNumber)) { matchingPeople.add(person); } } return matchingPeople; } }
// Usage example public static void main(String[] args) { Person.Builder personBuilder = new Person.Builder(); personBuilder.setFirstName("John") .setLastName("Doe") .setPhoneNumber("1234567890") .setAddress(new Address("Street 1", "City", "Country")); Person person = personBuilder.build();
PhoneBook phoneBook = new PhoneBook(); phoneBook.addPerson(person);
List matchingPeople = phoneBook.searchPeopleByLastName("Doe"); for (Person matchingPerson : matchingPeople) { System.out.println(matchingPerson.getFullName()); }
List matchingPeople2 = phoneBook.searchPeopleByPhoneNumber("1234567890"); for (Person matchingPerson : matchingPeople2) { System.out.println(matchingPerson.getFullName()); }