diff --git a/src/phonebook/Contact.java b/src/phonebook/Contact.java new file mode 100644 index 0000000..c3ccc28 --- /dev/null +++ b/src/phonebook/Contact.java @@ -0,0 +1,20 @@ +package phonebook; + +public class Contact { + + private String number; + private String name; + + public Contact(String number, String name) { + this.number = number; + this.name = name; + } + + public String getName() { + return name; + } + + public String getNumber() { + return number; + } +} diff --git a/src/phonebook/Main.java b/src/phonebook/Main.java index 100546c..222cfcb 100644 --- a/src/phonebook/Main.java +++ b/src/phonebook/Main.java @@ -1,7 +1,87 @@ package phonebook; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; +import java.util.stream.Stream; + public class Main { - public static void main(String[] args) { - System.out.print("Hello world!"); + + private static final String CONTACT_DATA_DIRECTORY = "data/directory.txt"; + private static final String FIND_REQUESTS_DIRECTORY = "data/find.txt"; + + public static void main(String[] args) throws IOException { + List contacts = parseContacts(); + quickSort(contacts); + + List requests = Files.readAllLines(Paths.get(FIND_REQUESTS_DIRECTORY)); + + long start = System.currentTimeMillis(); + int cnt = 1; + for (String request : requests) { + Contact contact = search(contacts, request); + if ((Objects.nonNull(contact))) { + System.out.printf("%d. %s has number %s\n", cnt++, request, contact.getNumber()); + } + } + System.out.printf("To find all entries it taken %d ms\n", System.currentTimeMillis() - start); + } + + private static Contact search(List contacts, String name) { + int lo = 0, hi = contacts.size() - 1; + while (lo <= hi) { + int mid = lo + (hi - lo) / 2; + Contact contact = contacts.get(mid); + int comparing = Objects.compare(contact.getName(), name, Comparator.naturalOrder()); + if (comparing == 0) { + return contact; + } else if (comparing < 0) { + lo = mid + 1; + } else { + hi = mid - 1; + } } + return null; + } + + private static void quickSort(List contacts) { + quickSort(contacts, 0, contacts.size() - 1); + } + + private static void quickSort(List contacts, int lo, int hi) { + if (lo >= hi) { + return; + } + int p = pivot(contacts, lo, hi); + quickSort(contacts, lo, p - 1); + quickSort(contacts, p + 1, hi); + } + + private static int pivot(List contacts, int lo, int hi) { + Contact pivot = contacts.get(lo); + int j = lo + 1; + for (int i = lo + 1; i <= hi; i++) { + int comparing = Objects + .compare(pivot, contacts.get(i), Comparator.comparing(Contact::getName)); + if (comparing >= 0) { + Collections.swap(contacts, i, j); + j++; + } + } + Collections.swap(contacts, lo, j - 1); + return j - 1; + } + + private static List parseContacts() throws IOException { + Stream directoryLines = Files.lines(Paths.get(CONTACT_DATA_DIRECTORY)); + return directoryLines.map(line -> new Contact(line.substring(0, line.indexOf(" ")), + line.substring(line.indexOf(" ") + 1))) + .collect(Collectors.toCollection(ArrayList::new)); + } } \ No newline at end of file