Advertisement
Guest User

Untitled

a guest
Feb 26th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.14 KB | None | 0 0
  1. public class Address {
  2.     private String city, street;
  3.  
  4.     public Address(String street, String city)
  5.     {
  6.         this.city = city;
  7.         this.street = street;
  8.     }
  9.  
  10.     public String city() { return city; }
  11.     public String street() { return street; }
  12.  
  13.     public boolean hasKeyword(String keyword)
  14.     {
  15.         return city.contains(keyword) || street.contains(keyword);
  16.     }
  17.  
  18.  
  19.     @Override
  20.     public String toString()
  21.     {
  22.         return street() + " " + city();
  23.     }
  24.  
  25.     @Override
  26.     public boolean equals(Object obj)
  27.     {
  28.         if(this == obj) return true;
  29.         if(obj == null || this.getClass() != obj.getClass()) return false;
  30.  
  31.         Address other = (Address)obj;
  32.  
  33.         return this.city().equals(other.city()) && this.street().equals(other.street());
  34.     }
  35.  
  36.     @Override
  37.     public int hashCode()
  38.     {
  39.         int hash = 17;
  40.         hash *= 53 + city.hashCode();
  41.         hash *= 53 + street.hashCode();
  42.  
  43.         return hash;
  44.     }
  45.  
  46. }
  47.  
  48. public class PhoneNumber {
  49.     private String number;
  50.  
  51.     public PhoneNumber(String number)
  52.     {
  53.         this.number = number;
  54.     }
  55.  
  56.     public String number() { return number; }
  57.  
  58.     public boolean hasKeyword(String keyword)
  59.     {
  60.         return number.contains(keyword);
  61.     }
  62.  
  63.     @Override
  64.     public String toString()
  65.     {
  66.         return number();
  67.     }
  68.  
  69.     @Override
  70.     public boolean equals(Object obj)
  71.     {
  72.         if(this == obj) return true;
  73.         if(obj == null || this.getClass() != obj.getClass()) return false;
  74.  
  75.         PhoneNumber other = (PhoneNumber) obj;
  76.  
  77.         return this.number().equals(other.number());
  78.     }
  79.  
  80.     @Override
  81.     public int hashCode()
  82.     {
  83.         return number.hashCode();
  84.     }
  85.  
  86. }
  87.  
  88. import java.util.ArrayList;
  89. import java.util.HashSet;
  90. import java.util.List;
  91. import java.util.Set;
  92.  
  93. public class Person implements Comparable<Person> {
  94.     private String name;
  95.     private Set<PhoneNumber> phoneNumbers;
  96.     private Set<Address> addresses;
  97.  
  98.     public Person(String name)
  99.     {
  100.         this.name = name;
  101.         this.phoneNumbers = new HashSet<PhoneNumber>();
  102.         this.addresses = new HashSet<Address>();
  103.     }
  104.  
  105.     public String name() { return name; }
  106.  
  107.     public void addPhoneNumber(String phoneNumber)
  108.     {
  109.         phoneNumbers.add(new PhoneNumber(phoneNumber));
  110.     }
  111.  
  112.     public void addAddress(String street, String city)
  113.     {
  114.         addresses.add(new Address(street, city));
  115.     }
  116.  
  117.     public boolean hasPhoneNumber(String phoneNumber)
  118.     {
  119.         PhoneNumber pn = new PhoneNumber(phoneNumber);
  120.  
  121.         return phoneNumbers.contains(pn);
  122.     }
  123.  
  124.     public boolean hasAddress(String street, String city)
  125.     {
  126.         Address address = new Address(street, city);
  127.  
  128.         return addresses.contains(address);
  129.     }
  130.  
  131.     public List<String> getPhoneNumbers()
  132.     {
  133.         ArrayList<String> numbers = new ArrayList<String>();
  134.         for(PhoneNumber pn : phoneNumbers)
  135.             numbers.add(pn.toString());
  136.         return numbers;
  137.     }
  138.  
  139.     public List<String> getAddresses()
  140.     {
  141.         ArrayList<String> addrs = new ArrayList<String>();
  142.         for(Address a : addresses)
  143.             addrs.add(a.toString());
  144.         return addrs;
  145.     }
  146.  
  147.     public boolean hasKeyword(String keyword)
  148.     {
  149.         if(name.contains(keyword))
  150.             return true;
  151.  
  152.         for(PhoneNumber pn : phoneNumbers)
  153.             if(pn.hasKeyword(keyword))
  154.                 return true;
  155.  
  156.         for(Address a : addresses)
  157.             if(a.hasKeyword(keyword))
  158.                 return true;
  159.  
  160.         return false;
  161.     }
  162.  
  163.     public String getPersonalInformation()
  164.     {
  165.         String pn = "phone numbers:\n";
  166.         String addrs = "address:\n";
  167.  
  168.         for(PhoneNumber p : phoneNumbers)
  169.             pn += p.toString() + "\n";
  170.         for(Address a : addresses)
  171.             addrs += a.toString() + "\n";
  172.  
  173.         if(phoneNumbers.isEmpty())
  174.             pn = "phone numbers unknown";
  175.         if(addresses.isEmpty())
  176.             addrs = "address unknown";
  177.  
  178.         return  addrs + "\n" + pn;
  179.     }
  180.  
  181.     @Override
  182.     public String toString()
  183.     {
  184.         return name() + "\n" + getPersonalInformation();
  185.     }
  186.  
  187.     @Override
  188.     public boolean equals(Object obj)
  189.     {
  190.         if(this == obj) return true;
  191.         if(obj == null || this.getClass() != obj.getClass()) return false;
  192.  
  193.         Person other = (Person) obj;
  194.  
  195.         return this.name().equals(other.name());
  196.     }
  197.  
  198.     @Override
  199.     public int hashCode()
  200.     {
  201.         return name().hashCode();
  202.     }
  203.  
  204.     @Override
  205.     public int compareTo(Person other) {
  206.         return this.name().compareTo(other.name());
  207.     }
  208. }
  209.  
  210. import java.util.TreeMap;
  211. import java.util.List;
  212. import java.util.ArrayList;
  213.  
  214. public class Directory {
  215.     private TreeMap<String, Person> people;
  216.  
  217.     public Directory() {
  218.         this.people = new TreeMap<String, Person>();
  219.     }
  220.  
  221.     public void addPhoneNumberToPerson(String name, String number) {
  222.         if (!people.containsKey(name)) {
  223.             Person person = new Person(name);
  224.             person.addPhoneNumber(number);
  225.             people.put(name, person);
  226.         } else {
  227.             Person person = people.get(name);
  228.             person.addPhoneNumber(number);
  229.         }
  230.     }
  231.  
  232.     public void addAddressToPerson(String name, String street, String city) {
  233.         if (!people.containsKey(name)) {
  234.             Person person = new Person(name);
  235.             person.addAddress(street, city);
  236.             people.put(name, person);
  237.         } else {
  238.             Person person = people.get(name);
  239.             person.addAddress(street, city);
  240.         }
  241.     }
  242.  
  243.     public List<String> searchForPhoneNumbersByPerson(String name) {
  244.         List<String> phoneNumbers = new ArrayList<String>();
  245.         if (!people.containsKey(name)) {
  246.             phoneNumbers.add("not found");
  247.  
  248.         } else {
  249.             Person person = people.get(name);
  250.             phoneNumbers = person.getPhoneNumbers();
  251.  
  252.             if (phoneNumbers.isEmpty())
  253.                 phoneNumbers.add("phone number not found");
  254.         }
  255.  
  256.         return phoneNumbers;
  257.     }
  258.  
  259.     public List<String> searchForAddressByPerson(String name) {
  260.         List<String> addresses = new ArrayList<String>();
  261.         if (!people.containsKey(name)) {
  262.             addresses.add("not found");
  263.  
  264.         } else {
  265.             Person person = people.get(name);
  266.             addresses = person.getAddresses();
  267.  
  268.             if (addresses.isEmpty())
  269.                 addresses.add("address not found");
  270.         }
  271.  
  272.         return addresses;
  273.     }
  274.  
  275.     public List<String> searchForPersonByPhoneNumber(String phoneNumber) {
  276.         ArrayList<String> names = new ArrayList<String>();
  277.         for (Person person : people.values())
  278.             if (person.hasPhoneNumber(phoneNumber))
  279.                 names.add(person.name());
  280.         if (names.isEmpty())
  281.             names.add("not found");
  282.         return names;
  283.     }
  284.  
  285.     public List<String> searchForPersonByAddress(String street, String city) {
  286.         ArrayList<String> names = new ArrayList<String>();
  287.         for (Person person : people.values())
  288.             if (person.hasAddress(street, city))
  289.                 names.add(person.name());
  290.         if (names.isEmpty())
  291.             names.add("not found");
  292.         return names;
  293.     }
  294.  
  295.     public String searchForPersonalInformation(String name) {
  296.         String personalInformation = "";
  297.  
  298.         if (!people.containsKey(name))
  299.             personalInformation = "not found";
  300.         else {
  301.             Person person = people.get(name);
  302.             personalInformation = person.getPersonalInformation();
  303.         }
  304.  
  305.         return personalInformation;
  306.     }
  307.  
  308.     public void deletePersonalInformation(String name) {
  309.         people.remove(name);
  310.     }
  311.  
  312.     public List<String> filterListingByKeyword(String keyword) {
  313.         ArrayList<String> listings = new ArrayList<String>();
  314.  
  315.         for (Person p : people.values())
  316.             if (keyword.isEmpty() || p.hasKeyword(keyword))
  317.                 listings.add(p.toString());
  318.  
  319.         if(listings.isEmpty())
  320.             listings.add("keyword not found");
  321.  
  322.         return listings;
  323.     }
  324. }
  325.  
  326. import java.util.List;
  327. import java.util.Scanner;
  328.  
  329. public class Driver {
  330.     public static void main(String[] args)
  331.     {
  332.         Scanner in = new Scanner(System.in);
  333.  
  334.         Directory directory = new Directory();
  335.  
  336.         boolean done = false;
  337.  
  338.         System.out.println("available operations:");
  339.         System.out.println("1 add a number");
  340.         System.out.println("2 search for a number");
  341.         System.out.println("3 search for a person by phone number");
  342.         System.out.println("4 add an address");
  343.         System.out.println("5 search for personal information");
  344.         System.out.println("6 delete personal information");
  345.         System.out.println("7 filtered listing");
  346.         System.out.println("x quit");
  347.  
  348.         while(!done)
  349.         {
  350.             System.out.print("command: ");
  351.             String input = in.nextLine();
  352.             switch(input.toLowerCase())
  353.             {
  354.                 case "x" : done = true; break;
  355.                 case "1" : addNumber(directory, in); break;
  356.                 case "2" : searchNumber(directory, in); break;
  357.                 case "3" : searchPersonByNumber(directory, in); break;
  358.                 case "4" : addAddress(directory, in); break;
  359.                 case "5" : searchPersonalInformation(directory, in); break;
  360.                 case "6" : deletePersonalInformation(directory, in); break;
  361.                 case "7" : filterListing(directory, in); break;
  362.             }
  363.         }
  364.  
  365.     }
  366.  
  367.     private static void filterListing(Directory directory, Scanner in)
  368.     {
  369.         System.out.print("keyword (if empty, all listed): ");
  370.         String keyword = in.nextLine();
  371.  
  372.         List<String> result = directory.filterListingByKeyword(keyword);
  373.  
  374.         for(String s : result)
  375.             System.out.println(s);
  376.     }
  377.  
  378.     private static void deletePersonalInformation(Directory directory, Scanner in)
  379.     {
  380.         System.out.print("whose information: ");
  381.         String name = in.nextLine();
  382.  
  383.         directory.deletePersonalInformation(name);
  384.     }
  385.  
  386.     private static void searchPersonalInformation(Directory directory, Scanner in)
  387.     {
  388.         System.out.print("whose information: ");
  389.         String name = in.nextLine();
  390.  
  391.         String result = directory.searchForPersonalInformation(name);
  392.  
  393.         System.out.println(result);
  394.     }
  395.  
  396.     private static void searchPersonByNumber(Directory directory, Scanner in)
  397.     {
  398.         System.out.print("number: ");
  399.         String number = in.nextLine();
  400.  
  401.         List<String> result = directory.searchForPersonByPhoneNumber(number);
  402.  
  403.         for(String s : result)
  404.             System.out.println(s);
  405.  
  406.     }
  407.  
  408.     private static void searchNumber(Directory directory, Scanner in)
  409.     {
  410.         System.out.print("whose number: ");
  411.         String name = in.nextLine();
  412.  
  413.         List<String> result = directory.searchForPhoneNumbersByPerson(name);
  414.  
  415.         for(String s : result)
  416.             System.out.println(s);
  417.     }
  418.  
  419.     private static void searchAddress(Directory directory, Scanner in)
  420.     {
  421.         System.out.print("whose address: ");
  422.         String name = in.nextLine();
  423.  
  424.         List<String> result = directory.searchForAddressByPerson(name);
  425.  
  426.         for(String s : result)
  427.             System.out.println(s);
  428.     }
  429.  
  430.     private static void addNumber(Directory directory, Scanner in)
  431.     {
  432.         System.out.print("whose number:" );
  433.         String name = in.nextLine();
  434.         System.out.print("number: ");
  435.         String number = in.nextLine();
  436.  
  437.         directory.addPhoneNumberToPerson(name, number);
  438.     }
  439.  
  440.     private static void addAddress(Directory directory, Scanner in)
  441.     {
  442.         System.out.print("whose address:" );
  443.         String name = in.nextLine();
  444.         System.out.print("street: ");
  445.         String street = in.nextLine();
  446.         System.out.print("city: ");
  447.         String city = in.nextLine();
  448.  
  449.         directory.addAddressToPerson(name, street, city);
  450.     }
  451. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement