Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Address {
- private String city, street;
- public Address(String street, String city)
- {
- this.city = city;
- this.street = street;
- }
- public String city() { return city; }
- public String street() { return street; }
- public boolean hasKeyword(String keyword)
- {
- return city.contains(keyword) || street.contains(keyword);
- }
- @Override
- public String toString()
- {
- return street() + " " + city();
- }
- @Override
- public boolean equals(Object obj)
- {
- if(this == obj) return true;
- if(obj == null || this.getClass() != obj.getClass()) return false;
- Address other = (Address)obj;
- return this.city().equals(other.city()) && this.street().equals(other.street());
- }
- @Override
- public int hashCode()
- {
- int hash = 17;
- hash *= 53 + city.hashCode();
- hash *= 53 + street.hashCode();
- return hash;
- }
- }
- public class PhoneNumber {
- private String number;
- public PhoneNumber(String number)
- {
- this.number = number;
- }
- public String number() { return number; }
- public boolean hasKeyword(String keyword)
- {
- return number.contains(keyword);
- }
- @Override
- public String toString()
- {
- return number();
- }
- @Override
- public boolean equals(Object obj)
- {
- if(this == obj) return true;
- if(obj == null || this.getClass() != obj.getClass()) return false;
- PhoneNumber other = (PhoneNumber) obj;
- return this.number().equals(other.number());
- }
- @Override
- public int hashCode()
- {
- return number.hashCode();
- }
- }
- import java.util.ArrayList;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Set;
- public class Person implements Comparable<Person> {
- private String name;
- private Set<PhoneNumber> phoneNumbers;
- private Set<Address> addresses;
- public Person(String name)
- {
- this.name = name;
- this.phoneNumbers = new HashSet<PhoneNumber>();
- this.addresses = new HashSet<Address>();
- }
- public String name() { return name; }
- public void addPhoneNumber(String phoneNumber)
- {
- phoneNumbers.add(new PhoneNumber(phoneNumber));
- }
- public void addAddress(String street, String city)
- {
- addresses.add(new Address(street, city));
- }
- public boolean hasPhoneNumber(String phoneNumber)
- {
- PhoneNumber pn = new PhoneNumber(phoneNumber);
- return phoneNumbers.contains(pn);
- }
- public boolean hasAddress(String street, String city)
- {
- Address address = new Address(street, city);
- return addresses.contains(address);
- }
- public List<String> getPhoneNumbers()
- {
- ArrayList<String> numbers = new ArrayList<String>();
- for(PhoneNumber pn : phoneNumbers)
- numbers.add(pn.toString());
- return numbers;
- }
- public List<String> getAddresses()
- {
- ArrayList<String> addrs = new ArrayList<String>();
- for(Address a : addresses)
- addrs.add(a.toString());
- return addrs;
- }
- public boolean hasKeyword(String keyword)
- {
- if(name.contains(keyword))
- return true;
- for(PhoneNumber pn : phoneNumbers)
- if(pn.hasKeyword(keyword))
- return true;
- for(Address a : addresses)
- if(a.hasKeyword(keyword))
- return true;
- return false;
- }
- public String getPersonalInformation()
- {
- String pn = "phone numbers:\n";
- String addrs = "address:\n";
- for(PhoneNumber p : phoneNumbers)
- pn += p.toString() + "\n";
- for(Address a : addresses)
- addrs += a.toString() + "\n";
- if(phoneNumbers.isEmpty())
- pn = "phone numbers unknown";
- if(addresses.isEmpty())
- addrs = "address unknown";
- return addrs + "\n" + pn;
- }
- @Override
- public String toString()
- {
- return name() + "\n" + getPersonalInformation();
- }
- @Override
- public boolean equals(Object obj)
- {
- if(this == obj) return true;
- if(obj == null || this.getClass() != obj.getClass()) return false;
- Person other = (Person) obj;
- return this.name().equals(other.name());
- }
- @Override
- public int hashCode()
- {
- return name().hashCode();
- }
- @Override
- public int compareTo(Person other) {
- return this.name().compareTo(other.name());
- }
- }
- import java.util.TreeMap;
- import java.util.List;
- import java.util.ArrayList;
- public class Directory {
- private TreeMap<String, Person> people;
- public Directory() {
- this.people = new TreeMap<String, Person>();
- }
- public void addPhoneNumberToPerson(String name, String number) {
- if (!people.containsKey(name)) {
- Person person = new Person(name);
- person.addPhoneNumber(number);
- people.put(name, person);
- } else {
- Person person = people.get(name);
- person.addPhoneNumber(number);
- }
- }
- public void addAddressToPerson(String name, String street, String city) {
- if (!people.containsKey(name)) {
- Person person = new Person(name);
- person.addAddress(street, city);
- people.put(name, person);
- } else {
- Person person = people.get(name);
- person.addAddress(street, city);
- }
- }
- public List<String> searchForPhoneNumbersByPerson(String name) {
- List<String> phoneNumbers = new ArrayList<String>();
- if (!people.containsKey(name)) {
- phoneNumbers.add("not found");
- } else {
- Person person = people.get(name);
- phoneNumbers = person.getPhoneNumbers();
- if (phoneNumbers.isEmpty())
- phoneNumbers.add("phone number not found");
- }
- return phoneNumbers;
- }
- public List<String> searchForAddressByPerson(String name) {
- List<String> addresses = new ArrayList<String>();
- if (!people.containsKey(name)) {
- addresses.add("not found");
- } else {
- Person person = people.get(name);
- addresses = person.getAddresses();
- if (addresses.isEmpty())
- addresses.add("address not found");
- }
- return addresses;
- }
- public List<String> searchForPersonByPhoneNumber(String phoneNumber) {
- ArrayList<String> names = new ArrayList<String>();
- for (Person person : people.values())
- if (person.hasPhoneNumber(phoneNumber))
- names.add(person.name());
- if (names.isEmpty())
- names.add("not found");
- return names;
- }
- public List<String> searchForPersonByAddress(String street, String city) {
- ArrayList<String> names = new ArrayList<String>();
- for (Person person : people.values())
- if (person.hasAddress(street, city))
- names.add(person.name());
- if (names.isEmpty())
- names.add("not found");
- return names;
- }
- public String searchForPersonalInformation(String name) {
- String personalInformation = "";
- if (!people.containsKey(name))
- personalInformation = "not found";
- else {
- Person person = people.get(name);
- personalInformation = person.getPersonalInformation();
- }
- return personalInformation;
- }
- public void deletePersonalInformation(String name) {
- people.remove(name);
- }
- public List<String> filterListingByKeyword(String keyword) {
- ArrayList<String> listings = new ArrayList<String>();
- for (Person p : people.values())
- if (keyword.isEmpty() || p.hasKeyword(keyword))
- listings.add(p.toString());
- if(listings.isEmpty())
- listings.add("keyword not found");
- return listings;
- }
- }
- import java.util.List;
- import java.util.Scanner;
- public class Driver {
- public static void main(String[] args)
- {
- Scanner in = new Scanner(System.in);
- Directory directory = new Directory();
- boolean done = false;
- System.out.println("available operations:");
- System.out.println("1 add a number");
- System.out.println("2 search for a number");
- System.out.println("3 search for a person by phone number");
- System.out.println("4 add an address");
- System.out.println("5 search for personal information");
- System.out.println("6 delete personal information");
- System.out.println("7 filtered listing");
- System.out.println("x quit");
- while(!done)
- {
- System.out.print("command: ");
- String input = in.nextLine();
- switch(input.toLowerCase())
- {
- case "x" : done = true; break;
- case "1" : addNumber(directory, in); break;
- case "2" : searchNumber(directory, in); break;
- case "3" : searchPersonByNumber(directory, in); break;
- case "4" : addAddress(directory, in); break;
- case "5" : searchPersonalInformation(directory, in); break;
- case "6" : deletePersonalInformation(directory, in); break;
- case "7" : filterListing(directory, in); break;
- }
- }
- }
- private static void filterListing(Directory directory, Scanner in)
- {
- System.out.print("keyword (if empty, all listed): ");
- String keyword = in.nextLine();
- List<String> result = directory.filterListingByKeyword(keyword);
- for(String s : result)
- System.out.println(s);
- }
- private static void deletePersonalInformation(Directory directory, Scanner in)
- {
- System.out.print("whose information: ");
- String name = in.nextLine();
- directory.deletePersonalInformation(name);
- }
- private static void searchPersonalInformation(Directory directory, Scanner in)
- {
- System.out.print("whose information: ");
- String name = in.nextLine();
- String result = directory.searchForPersonalInformation(name);
- System.out.println(result);
- }
- private static void searchPersonByNumber(Directory directory, Scanner in)
- {
- System.out.print("number: ");
- String number = in.nextLine();
- List<String> result = directory.searchForPersonByPhoneNumber(number);
- for(String s : result)
- System.out.println(s);
- }
- private static void searchNumber(Directory directory, Scanner in)
- {
- System.out.print("whose number: ");
- String name = in.nextLine();
- List<String> result = directory.searchForPhoneNumbersByPerson(name);
- for(String s : result)
- System.out.println(s);
- }
- private static void searchAddress(Directory directory, Scanner in)
- {
- System.out.print("whose address: ");
- String name = in.nextLine();
- List<String> result = directory.searchForAddressByPerson(name);
- for(String s : result)
- System.out.println(s);
- }
- private static void addNumber(Directory directory, Scanner in)
- {
- System.out.print("whose number:" );
- String name = in.nextLine();
- System.out.print("number: ");
- String number = in.nextLine();
- directory.addPhoneNumberToPerson(name, number);
- }
- private static void addAddress(Directory directory, Scanner in)
- {
- System.out.print("whose address:" );
- String name = in.nextLine();
- System.out.print("street: ");
- String street = in.nextLine();
- System.out.print("city: ");
- String city = in.nextLine();
- directory.addAddressToPerson(name, street, city);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement