From afd3610bf304f98ffdde5c9663711b37850464ca Mon Sep 17 00:00:00 2001 From: Vladislav Kostylev Date: Mon, 26 Nov 2018 09:20:31 +0300 Subject: [PATCH 1/5] Initial commit --- src/simpleSearchEngine/Main.java | 113 +++++++++++++++++++++++++++++-- 1 file changed, 109 insertions(+), 4 deletions(-) diff --git a/src/simpleSearchEngine/Main.java b/src/simpleSearchEngine/Main.java index b8ecb08..4db1beb 100644 --- a/src/simpleSearchEngine/Main.java +++ b/src/simpleSearchEngine/Main.java @@ -1,7 +1,112 @@ package simpleSearchEngine; -public class Main { - public static void main(String[] args) { - System.out.print("Hello world!"); +import java.util.*; + +class Main { + + public static void main(String args[]) { + List persons = new ArrayList<>(); + Scanner sc = new Scanner(System.in); + System.out.println("Enter persons data in format: first name | last name | email (optional). Each entry from a new line, delimit with spaces. To terminate enter /stop"); + while(true) { + String input = sc.nextLine(); + if (input.trim().equals("/stop")) break; + if (input != null && input.length() > 0) { + String words[] = input.trim().split(" "); + if (words.length >= 2) { + String lastName = words[1]; + String firstName = words[0]; + String email = (words.length == 3) ? words[2] : null; + Person person = new Person(firstName, lastName, email); + persons.add(person); + } else { + System.out.println("First name and last name are requied params"); + } + } } -} \ No newline at end of file + for (Person person : persons) { + Finder.addPerson(person); + } + + System.out.println("Enter search term. To terminate enter /stop"); + while(true) { + String input = sc.nextLine(); + if (input.trim().equals("/stop")) break; + if (input != null && input.length() > 0) { + if (Finder.find(input.trim()) != null) { + System.out.println("Found people: "); + for (Person person : Finder.find(input)) { + System.out.println(person.toString()); + } + } else { + System.out.println("Not found"); + } + } + } + } +} + + class Person { + + public Person(String firstName, String lastName, String email) { + + if (firstName != null) this.firstName = firstName.trim(); + if (lastName != null) this.lastName = lastName.trim(); + if (email != null) this.email = email.trim(); + + } + + public String toString() { + if (email != null) { + return firstName + " " + lastName + " " + email; + } else { + return firstName + " " + lastName; + } + } + + public List getFields() { + List result = new ArrayList<>(); + if (firstName != null) result.add(firstName); + if (lastName != null) result.add(lastName); + if (email != null) result.add(email); + return result; + } + + private String firstName; + private String lastName; + private String email; + } + + class Finder { + + private Finder(){}; + + public static void addPerson(Person person) { + List fields = person.getFields(); + for (String field : fields) { + if (mapping.containsKey(field.toLowerCase())) { + Set persons = mapping.get(field.toLowerCase()); + persons.add(person); + } else { + Set persons = new HashSet<>(); + persons.add(person); + mapping.put(field.toLowerCase(), persons); + } + } + } + + public static Set find(String term) { + if (mapping.containsKey(term.trim().toLowerCase())) { + return mapping.get(term.trim().toLowerCase()); + } else return null; + + } + + private static Map> mapping = new HashMap<>(); + +} + +/* +javac src/com/kostylevv/paksrms/*.java +java -cp src/ com.kostylevv.paksrms.Paks +*/ From 90e237730d87daec759aeaf987835ca7d7e73cb1 Mon Sep 17 00:00:00 2001 From: Vladislav Kostylev Date: Mon, 26 Nov 2018 21:31:34 +0300 Subject: [PATCH 2/5] Stage 3 --- db/pdata.txt | 6 +++ src/simpleSearchEngine/Main.java | 83 ++++++++++++++++++++++++-------- src/simpleSearchEngine/Util.java | 42 ++++++++++++++++ 3 files changed, 110 insertions(+), 21 deletions(-) create mode 100644 db/pdata.txt create mode 100644 src/simpleSearchEngine/Util.java diff --git a/db/pdata.txt b/db/pdata.txt new file mode 100644 index 0000000..4517bfb --- /dev/null +++ b/db/pdata.txt @@ -0,0 +1,6 @@ +Dwight Joseph djo@gmail.com +Rene Webb webb@gmail.com +Katie Jacobs +Erick Harrington harrington@gmail.com +Myrtle Medina +Erick Burgess diff --git a/src/simpleSearchEngine/Main.java b/src/simpleSearchEngine/Main.java index 4db1beb..4e4975e 100644 --- a/src/simpleSearchEngine/Main.java +++ b/src/simpleSearchEngine/Main.java @@ -3,12 +3,71 @@ import java.util.*; class Main { - public static void main(String args[]) { List persons = new ArrayList<>(); + + try { + persons = Util.getPersonsFromFile("pdata.txt"); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + + + for (Person person : persons) { + Finder.addPerson(person); + } + Scanner sc = new Scanner(System.in); - System.out.println("Enter persons data in format: first name | last name | email (optional). Each entry from a new line, delimit with spaces. To terminate enter /stop"); + boolean cont = true; + while(cont) { + System.out.println(" === Menu ==="); + System.out.println(" 1. Find a person "); + System.out.println(" 2. Print all people"); + System.out.println(" 0. Exit "); + int input = sc.nextInt(); + switch(input) { + case 1: + System.out.println("Enter search term. To terminate enter /stop"); + while(true) { + String term = sc.nextLine(); + if (term.trim().equals("/stop")) break; + + if (term != null && term.length() > 0) { + + if (Finder.find(term.trim()) != null) { + System.out.println("Found people: "); + for (Person person : Finder.find(term)) { + System.out.println(person.toString()); + } + } else { + System.out.println("Not found"); + } + } + } + break; + + case 2: + for(Person p : persons) { + System.out.println(p.toString()); + } + break; + + case 0: + cont = false; + System.out.println("Bye!"); + break; + + default: + System.out.println("Incorrect option! Try again."); + + } + } + } + + private static List addPersonsFromCLI(Scanner sc){ + List persons = new ArrayList<>(); while(true) { + System.out.println("Enter persons data in format: first name | last name | email (optional). Each entry from a new line, delimit with spaces. To terminate enter /stop"); String input = sc.nextLine(); if (input.trim().equals("/stop")) break; if (input != null && input.length() > 0) { @@ -24,25 +83,7 @@ public static void main(String args[]) { } } } - for (Person person : persons) { - Finder.addPerson(person); - } - - System.out.println("Enter search term. To terminate enter /stop"); - while(true) { - String input = sc.nextLine(); - if (input.trim().equals("/stop")) break; - if (input != null && input.length() > 0) { - if (Finder.find(input.trim()) != null) { - System.out.println("Found people: "); - for (Person person : Finder.find(input)) { - System.out.println(person.toString()); - } - } else { - System.out.println("Not found"); - } - } - } + return persons; } } diff --git a/src/simpleSearchEngine/Util.java b/src/simpleSearchEngine/Util.java new file mode 100644 index 0000000..ed7557e --- /dev/null +++ b/src/simpleSearchEngine/Util.java @@ -0,0 +1,42 @@ +package simpleSearchEngine; + +import java.util.*; +import static java.nio.file.StandardOpenOption.*; +import java.nio.file.*; +import java.io.*; + +public class Util { + + private static final String DB_PATH = "db\\"; + + public static List getPersonsFromFile(String filename) throws Exception { + List persons = new ArrayList<>(); + Path file = Paths.get(DB_PATH + filename); + try (InputStream in = Files.newInputStream(file); + BufferedReader reader = new BufferedReader(new InputStreamReader(in))) { + String line = null; + while ((line = reader.readLine()) != null) { + + String[] words = line.trim().split(" "); + + switch(words.length) { + case 2: + Person p = new Person(words[0], words[1], null); + persons.add(p); + break; + case 3: + Person z = new Person(words[0], words[1], words[2]); + persons.add(z); + break; + + default: + System.out.println("Person entry should contain at least first name and last name delimited with space"); + } + + } + } + + return persons; + + } +} From b76964ec6fe35f79f40ee83dd19129c8f348530e Mon Sep 17 00:00:00 2001 From: Vladislav Kostylev Date: Tue, 27 Nov 2018 19:00:27 +0300 Subject: [PATCH 3/5] Stage 5 --- db/pdata.txt | 52 +++++++++++++++++++++++++++++++- src/simpleSearchEngine/Main.java | 30 ++++++++++++------ 2 files changed, 71 insertions(+), 11 deletions(-) diff --git a/db/pdata.txt b/db/pdata.txt index 4517bfb..3244671 100644 --- a/db/pdata.txt +++ b/db/pdata.txt @@ -3,4 +3,54 @@ Rene Webb webb@gmail.com Katie Jacobs Erick Harrington harrington@gmail.com Myrtle Medina -Erick Burgess +Erick Burgess erb@mail.com +Romelia Trial +Carmella Thome tc9900@yahoo.com +Paige Fritsche paige@mymail.cn +Charlsie Mendes +Sam Rusk  ruskrusk@msft.com +Joie Lenhart +Tommye Rothfuss +Cassi Gifford  cassig@gmail.com +Ciera Barkley +Hiedi Slaubaugh f@mail.ru +Kenyetta Attaway +Irma Dunaway  Dunaway@Dunaway.com +Deidre Culotta +Mack Keogh  keogh@bmw.com +Jame Remigio +Zackary Rish   +Berna Leedy +Leatrice Mcmanus   +Sierra Stockwell +Retta Bosh +Louis Nay  +Lesia Leaks +Del Muench +Consuela Choi   +Jalisa Yim +Janise Neighbors +Dorla Bucklin dorlsb@gmail.com +Kylie Manross +Dortha Morrissey +Ester Golding +Dawn Garson   +Stephania Boykins +Hertha Jefferis +Keshia Michna +Geraldine Schrader +Tad Steve tadsteve@mymail.com +Raeann Frese +Laure Burbidge +Maryanna Barnhart MaryannaBarn@gmail.com +Corrinne Hazelip +Monica Harrell +Brenton Lujan +Lisabeth Gress +Mafalda Laurent +Mendy Alcantar +Jackqueline Esteban +Rusty Noakes +Reginia Beltrami +Hanh Houck +Genia Royse diff --git a/src/simpleSearchEngine/Main.java b/src/simpleSearchEngine/Main.java index 4e4975e..a62a4b0 100644 --- a/src/simpleSearchEngine/Main.java +++ b/src/simpleSearchEngine/Main.java @@ -24,7 +24,14 @@ public static void main(String args[]) { System.out.println(" 1. Find a person "); System.out.println(" 2. Print all people"); System.out.println(" 0. Exit "); - int input = sc.nextInt(); + String inputLine = sc.nextLine(); + int input = -1; + try { + input = Integer.parseInt(inputLine); + } catch (Exception e) { + System.out.println("Only numeric options allowed."); + } + switch(input) { case 1: System.out.println("Enter search term. To terminate enter /stop"); @@ -33,14 +40,15 @@ public static void main(String args[]) { if (term.trim().equals("/stop")) break; if (term != null && term.length() > 0) { - - if (Finder.find(term.trim()) != null) { - System.out.println("Found people: "); - for (Person person : Finder.find(term)) { + term = term.trim(); + if (Finder.find(term) != null) { + Set found = Finder.find(term); + System.out.printf("Found %d people: %n", found.size()); + for (Person person : found) { System.out.println(person.toString()); } } else { - System.out.println("Not found"); + System.out.println("No matching people found."); } } } @@ -58,12 +66,14 @@ public static void main(String args[]) { break; default: - System.out.println("Incorrect option! Try again."); + System.out.println("Incorrect option. Try again."); } } } + /** + **/ private static List addPersonsFromCLI(Scanner sc){ List persons = new ArrayList<>(); while(true) { @@ -91,9 +101,9 @@ class Person { public Person(String firstName, String lastName, String email) { - if (firstName != null) this.firstName = firstName.trim(); - if (lastName != null) this.lastName = lastName.trim(); - if (email != null) this.email = email.trim(); + if (firstName != null) this.firstName = firstName.replaceAll("\\p{C}", "?").trim(); + if (lastName != null) this.lastName = lastName.replaceAll("\\p{C}", "?").trim(); + if (email != null) this.email = email.replaceAll("\\p{C}", "?").trim(); } From ae20b9e8cb9a18f6c0a43680e4c4636b3f1e7f58 Mon Sep 17 00:00:00 2001 From: Vladislav Kostylev Date: Sat, 22 Dec 2018 16:03:41 +0300 Subject: [PATCH 4/5] Stage 6 --- db/pdata.txt | 4 + src/simpleSearchEngine/Main.java | 302 +++++++++++++++++++++++++------ 2 files changed, 248 insertions(+), 58 deletions(-) diff --git a/db/pdata.txt b/db/pdata.txt index 3244671..f93232c 100644 --- a/db/pdata.txt +++ b/db/pdata.txt @@ -6,17 +6,21 @@ Myrtle Medina Erick Burgess erb@mail.com Romelia Trial Carmella Thome tc9900@yahoo.com +Romelia Jopal Paige Fritsche paige@mymail.cn Charlsie Mendes Sam Rusk  ruskrusk@msft.com Joie Lenhart +Romelia Jordal Tommye Rothfuss Cassi Gifford  cassig@gmail.com Ciera Barkley +Attaway Haffordy Hiedi Slaubaugh f@mail.ru Kenyetta Attaway Irma Dunaway  Dunaway@Dunaway.com Deidre Culotta +Cisco Romelia Mack Keogh  keogh@bmw.com Jame Remigio Zackary Rish   diff --git a/src/simpleSearchEngine/Main.java b/src/simpleSearchEngine/Main.java index a62a4b0..7c12d17 100644 --- a/src/simpleSearchEngine/Main.java +++ b/src/simpleSearchEngine/Main.java @@ -5,18 +5,15 @@ class Main { public static void main(String args[]) { List persons = new ArrayList<>(); + Finder finder = new Finder(); try { persons = Util.getPersonsFromFile("pdata.txt"); + finder.addAll(persons); } catch (Exception e) { System.out.println(e.getMessage()); } - - for (Person person : persons) { - Finder.addPerson(person); - } - Scanner sc = new Scanner(System.in); boolean cont = true; while(cont) { @@ -34,30 +31,76 @@ public static void main(String args[]) { switch(input) { case 1: - System.out.println("Enter search term. To terminate enter /stop"); + System.out.println("Select a matching strategy: ALL, ANY, NONE. Enter /stop to terminate. Case sensitive."); while(true) { - String term = sc.nextLine(); - if (term.trim().equals("/stop")) break; - - if (term != null && term.length() > 0) { - term = term.trim(); - if (Finder.find(term) != null) { - Set found = Finder.find(term); - System.out.printf("Found %d people: %n", found.size()); - for (Person person : found) { - System.out.println(person.toString()); + String strategy = sc.nextLine(); + if (strategy.trim().equals("/stop")) break; + try { + switch(strategy) { + + case "ALL": + System.out.println("Enter seach terms, delimited with whitespace. Programm will return records which contains all terms. Enter /stop to terminate."); + String termAll = sc.nextLine(); + String[] termsAll = termAll.trim().split(" "); + List allPersons = finder.findAll(termsAll); + if (allPersons != null) { + System.out.printf("Found %d people: %n", allPersons.size()); + + for (Person person : allPersons) { + System.out.print(person.toString()); + } + System.out.println(""); + } else { + System.out.println("No matching people found."); } - } else { - System.out.println("No matching people found."); + break; + case "ANY": + System.out.println("Enter seach terms, delimited with whitespace. Programm will return records which contains any of terms. Enter /stop to terminate."); + String termAny = sc.nextLine(); + String[] termsAny = termAny.trim().split(" "); + List anyPersons = finder.findAny(termsAny); + if (anyPersons != null) { + System.out.printf("Found %d people: %n", anyPersons.size()); + for (Person person : anyPersons) { + System.out.print(person.toString()); + } + System.out.println(""); + } else { + System.out.println("No matching people found."); + } + break; + case "NONE": + System.out.println("Enter seach terms, delimited with whitespace. Programm will return records which does not contain any of terms. Enter /stop to terminate."); + String noneTerm = sc.nextLine(); + String[] noneTerms = noneTerm.trim().split(" "); + List nonePersons = finder.findNone(noneTerms); + if (nonePersons != null) { + System.out.printf("Found %d people: %n", nonePersons.size()); + for (Person person : nonePersons) { + System.out.print(person.toString()); + } + System.out.println(""); + } else { + System.out.println("No matching people found."); + } + break; + default: + System.out.println("Select a matching strategy: ALL, ANY, NONE. Enter /stop to terminate. Case sensitive."); + break; } + } catch (Exception e) { + System.out.println("An error occured: "); + e.printStackTrace(); } } break; case 2: + System.out.printf("Found %d people: %n", persons.size()); for(Person p : persons) { - System.out.println(p.toString()); + System.out.print(p.toString()); } + System.out.println(""); break; case 0: @@ -69,6 +112,8 @@ public static void main(String args[]) { System.out.println("Incorrect option. Try again."); } + + } } @@ -97,63 +142,204 @@ private static List addPersonsFromCLI(Scanner sc){ } } - class Person { +interface Searcheable { + /** + Returns values which should be indexed + **/ + List getIndex(); +} + +class Finder { - public Person(String firstName, String lastName, String email) { + private final boolean caseSensitive; - if (firstName != null) this.firstName = firstName.replaceAll("\\p{C}", "?").trim(); - if (lastName != null) this.lastName = lastName.replaceAll("\\p{C}", "?").trim(); - if (email != null) this.email = email.replaceAll("\\p{C}", "?").trim(); + public Finder() { + caseSensitive = false; + elements = new ArrayList<>(); + mapping = new HashMap<>(); + } - } + public Finder(boolean caseSensitive) { + this.caseSensitive = caseSensitive; + elements = new ArrayList<>(); + mapping = new HashMap<>(); + } - public String toString() { - if (email != null) { - return firstName + " " + lastName + " " + email; - } else { - return firstName + " " + lastName; - } - } + public List getAll() { + + return elements; + + } + + public void addAll(List list) throws Exception { + + for (T element : list) { - public List getFields() { - List result = new ArrayList<>(); - if (firstName != null) result.add(firstName); - if (lastName != null) result.add(lastName); - if (email != null) result.add(email); - return result; + add(element); + + } + + } + + public void add(T element) throws Exception{ + + boolean elementAdded = elements.add(element); + int position = elements.lastIndexOf(element); + + if (!elementAdded || position == -1) throw new Exception(); + + addToIndex(element, position); + + } + + private List findElements(Set indexes) throws Exception { + + if (indexes == null || indexes.size() == 0) return null; + List result = new ArrayList<>(); + + for (Integer i : indexes) { + result.add(elements.get(i)); } - private String firstName; - private String lastName; - private String email; + return result; + + } - class Finder { + private Set find(String term) { - private Finder(){}; + term = sanitizeIndexValue(term); - public static void addPerson(Person person) { - List fields = person.getFields(); - for (String field : fields) { - if (mapping.containsKey(field.toLowerCase())) { - Set persons = mapping.get(field.toLowerCase()); - persons.add(person); + if (mapping.containsKey(term)) { + + return mapping.get(term); + + } else return null; + } + + + public List findAll(String[] terms) throws Exception { + if (terms == null || terms.length == 0) return null; + + Set result = new HashSet<>(); + + for (String term : terms) { + if (result.size() == 0) { + Set init = find(term); + if (init != null) result.addAll(find(term)); + } else { + Set found = find(term); + if (found != null) { + result.retainAll(found); } else { - Set persons = new HashSet<>(); - persons.add(person); - mapping.put(field.toLowerCase(), persons); + return null; } + } + } + + return findElements(result); + } + + public List findAny(String[] terms) throws Exception { + if (terms == null || terms.length == 0) return null; + + Set result = new HashSet<>(); + + for (String term : terms) { + if (result.size() == 0) { + Set init = find(term); + if (init != null) result.addAll(find(term)); + } else { + Set found = find(term); + if (found != null) { + result.addAll(found); + } } - } + } + + return findElements(result); + } + + public List findNone(String[] terms) throws Exception { + if (terms == null || terms.length == 0) return null; + List anys = findAny(terms); + + if (anys != null) { + List noneElements = new ArrayList(elements); + noneElements.removeAll(anys); + return noneElements; + } else return elements; + } + + private void addToIndex(T element, int pos) { + + List index = element.getIndex(); - public static Set find(String term) { - if (mapping.containsKey(term.trim().toLowerCase())) { - return mapping.get(term.trim().toLowerCase()); - } else return null; + for (String string : index) { + string = sanitizeIndexValue(string); + if (string != null) { + + if (mapping.containsKey(string)) { + Set postions = mapping.get(string); + postions.add(pos); + } else { + Set postions = new HashSet<>(); + postions.add(pos); + mapping.put(string, postions); + } } + } + } + + + private String sanitizeIndexValue(String string) { + + if (string == null || string.trim().length() == 0) return null; + + if (caseSensitive) { + return string.trim(); + } else { + return string.trim().toLowerCase(); + } + } + + private List elements; + private Map> mapping; +} + + + +class Person implements Searcheable { + + Person(String firstName, String lastName, String email) { + this.firstName = firstName; + this.lastName = lastName; + this.email = email; + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + if (firstName != null) sb.append(firstName).append(" "); + if (lastName != null) sb.append(lastName).append(" "); + if (email != null) sb.append(email); + sb.append("; "); + return sb.toString(); + } + + public List getIndex() { + List result = new ArrayList<>(); + + if (firstName != null) result.add(firstName); + if (lastName != null) result.add(lastName); + if (email != null) result.add(email); + + return result; + } - private static Map> mapping = new HashMap<>(); + private String firstName; + private String lastName; + private String email; } From 5e6fc2a157856f1a9b86f1ddbe44a5d09f172a9d Mon Sep 17 00:00:00 2001 From: Vladislav Kostylev Date: Sun, 6 Jan 2019 15:34:09 +0300 Subject: [PATCH 5/5] Refactoring --- .idea/misc.xml | 2 +- src/simpleSearchEngine/Main.java | 548 ++++++++++++++----------------- src/simpleSearchEngine/Util.java | 19 +- 3 files changed, 255 insertions(+), 314 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index a165cb3..0c4841a 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/src/simpleSearchEngine/Main.java b/src/simpleSearchEngine/Main.java index 7c12d17..5dd450b 100644 --- a/src/simpleSearchEngine/Main.java +++ b/src/simpleSearchEngine/Main.java @@ -2,348 +2,288 @@ import java.util.*; +interface Searchable { + /** + * Returns values which should be indexed + **/ + List getIndex(); +} + class Main { - public static void main(String args[]) { - List persons = new ArrayList<>(); - Finder finder = new Finder(); - - try { - persons = Util.getPersonsFromFile("pdata.txt"); - finder.addAll(persons); - } catch (Exception e) { - System.out.println(e.getMessage()); - } + public static void main(String[] args) { + List persons = new ArrayList<>(); + Finder finder = new Finder<>(); + + try { + persons = Util.getPersonsFromFile("pdata.txt"); + finder.addAll(persons); + } catch (Exception e) { + System.out.println(e.getMessage()); + } - Scanner sc = new Scanner(System.in); - boolean cont = true; - while(cont) { - System.out.println(" === Menu ==="); - System.out.println(" 1. Find a person "); - System.out.println(" 2. Print all people"); - System.out.println(" 0. Exit "); - String inputLine = sc.nextLine(); - int input = -1; - try { - input = Integer.parseInt(inputLine); - } catch (Exception e) { - System.out.println("Only numeric options allowed."); - } - - switch(input) { - case 1: - System.out.println("Select a matching strategy: ALL, ANY, NONE. Enter /stop to terminate. Case sensitive."); - while(true) { - String strategy = sc.nextLine(); - if (strategy.trim().equals("/stop")) break; - try { - switch(strategy) { - - case "ALL": - System.out.println("Enter seach terms, delimited with whitespace. Programm will return records which contains all terms. Enter /stop to terminate."); - String termAll = sc.nextLine(); - String[] termsAll = termAll.trim().split(" "); - List allPersons = finder.findAll(termsAll); - if (allPersons != null) { - System.out.printf("Found %d people: %n", allPersons.size()); - - for (Person person : allPersons) { - System.out.print(person.toString()); - } - System.out.println(""); - } else { - System.out.println("No matching people found."); - } - break; - case "ANY": - System.out.println("Enter seach terms, delimited with whitespace. Programm will return records which contains any of terms. Enter /stop to terminate."); - String termAny = sc.nextLine(); - String[] termsAny = termAny.trim().split(" "); - List anyPersons = finder.findAny(termsAny); - if (anyPersons != null) { - System.out.printf("Found %d people: %n", anyPersons.size()); - for (Person person : anyPersons) { - System.out.print(person.toString()); - } - System.out.println(""); - } else { - System.out.println("No matching people found."); - } - break; - case "NONE": - System.out.println("Enter seach terms, delimited with whitespace. Programm will return records which does not contain any of terms. Enter /stop to terminate."); - String noneTerm = sc.nextLine(); - String[] noneTerms = noneTerm.trim().split(" "); - List nonePersons = finder.findNone(noneTerms); - if (nonePersons != null) { - System.out.printf("Found %d people: %n", nonePersons.size()); - for (Person person : nonePersons) { - System.out.print(person.toString()); - } - System.out.println(""); - } else { - System.out.println("No matching people found."); - } - break; - default: - System.out.println("Select a matching strategy: ALL, ANY, NONE. Enter /stop to terminate. Case sensitive."); - break; - } + Scanner sc = new Scanner(System.in); + boolean cont = true; + while (cont) { + System.out.println(" === Menu ==="); + System.out.println(" 1. Find a person "); + System.out.println(" 2. Print all people"); + System.out.println(" 0. Exit "); + String inputLine = sc.nextLine(); + int input = -1; + try { + input = Integer.parseInt(inputLine); } catch (Exception e) { - System.out.println("An error occured: "); - e.printStackTrace(); + System.out.println("Only numeric options allowed."); } - } - break; - case 2: - System.out.printf("Found %d people: %n", persons.size()); - for(Person p : persons) { - System.out.print(p.toString()); - } - System.out.println(""); - break; - - case 0: - cont = false; - System.out.println("Bye!"); - break; - - default: - System.out.println("Incorrect option. Try again."); + switch (input) { + case 1: + System.out.println("Select a matching strategy: ALL, ANY, NONE. Enter /stop to terminate. Case sensitive."); + while (true) { + String strategy = sc.nextLine(); + if (strategy.trim().equals("/stop")) break; + try { + switch (strategy) { + + case "ALL": + System.out.println("Enter seach terms, delimited with whitespace. Programm will return records which contains all terms. Enter /stop to terminate."); + String termAll = sc.nextLine(); + String[] termsAll = termAll.trim().split(" "); + List allPersons = finder.findAll(termsAll); + printPersons(allPersons); + break; + case "ANY": + System.out.println("Enter seach terms, delimited with whitespace. Programm will return records which contains any of terms. Enter /stop to terminate."); + String termAny = sc.nextLine(); + String[] termsAny = termAny.trim().split(" "); + List anyPersons = finder.findAny(termsAny); + printPersons(anyPersons); + break; + case "NONE": + System.out.println("Enter seach terms, delimited with whitespace. Programm will return records which does not contain any of terms. Enter /stop to terminate."); + String noneTerm = sc.nextLine(); + String[] noneTerms = noneTerm.trim().split(" "); + List nonePersons = finder.findNone(noneTerms); + printPersons(nonePersons); + break; + default: + System.out.println("Select a matching strategy: ALL, ANY, NONE. Enter /stop to terminate. Case sensitive."); + break; + } + } catch (Exception e) { + System.out.println("An error occured: "); + e.printStackTrace(); + } + } + break; - } + case 2: + System.out.printf("Found %d people: %n", persons.size()); + for (Person p : persons) { + System.out.print(p.toString()); + } + System.out.println(); + break; + case 0: + cont = false; + System.out.println("Bye!"); + break; + default: + System.out.println("Incorrect option. Try again."); + } } - } - - /** - **/ - private static List addPersonsFromCLI(Scanner sc){ - List persons = new ArrayList<>(); - while(true) { - System.out.println("Enter persons data in format: first name | last name | email (optional). Each entry from a new line, delimit with spaces. To terminate enter /stop"); - String input = sc.nextLine(); - if (input.trim().equals("/stop")) break; - if (input != null && input.length() > 0) { - String words[] = input.trim().split(" "); - if (words.length >= 2) { - String lastName = words[1]; - String firstName = words[0]; - String email = (words.length == 3) ? words[2] : null; - Person person = new Person(firstName, lastName, email); - persons.add(person); + } + + private static void printPersons(List persons) { + if (persons != null) { + System.out.printf("Found %d people: %n", persons.size()); + for (Person person : persons) { + System.out.print(person.toString()); + } + System.out.println(); } else { - System.out.println("First name and last name are requied params"); + System.out.println("No matching people found."); } - } } - return persons; - } -} -interface Searcheable { - /** - Returns values which should be indexed - **/ - List getIndex(); + private static List addPersonsFromCLI(Scanner sc) { + List persons = new ArrayList<>(); + while (true) { + System.out.println("Enter persons data in format: first name | last name | email (optional). Each entry from a new line, delimit with spaces. To terminate enter /stop"); + String input = sc.nextLine(); + if (input.trim().equals("/stop")) break; + if (input.length() > 0) { + String[] words = input.trim().split(" "); + if (words.length >= 2) { + String lastName = words[1]; + String firstName = words[0]; + String email = (words.length == 3) ? words[2] : null; + Person person = new Person(firstName, lastName, email); + persons.add(person); + } else { + System.out.println("First name and last name are requied params"); + } + } + } + return persons; + } } -class Finder { - - private final boolean caseSensitive; - - public Finder() { - caseSensitive = false; - elements = new ArrayList<>(); - mapping = new HashMap<>(); - } - - public Finder(boolean caseSensitive) { - this.caseSensitive = caseSensitive; - elements = new ArrayList<>(); - mapping = new HashMap<>(); - } - - public List getAll() { - - return elements; - - } - - public void addAll(List list) throws Exception { - - for (T element : list) { - - add(element); - - } +class Finder { - } + private final boolean caseSensitive; + private final List elements; + private final Map> mapping; - public void add(T element) throws Exception{ - - boolean elementAdded = elements.add(element); - int position = elements.lastIndexOf(element); - - if (!elementAdded || position == -1) throw new Exception(); - - addToIndex(element, position); - - } - - private List findElements(Set indexes) throws Exception { - - if (indexes == null || indexes.size() == 0) return null; - List result = new ArrayList<>(); - - for (Integer i : indexes) { - result.add(elements.get(i)); + public Finder() { + caseSensitive = false; + elements = new ArrayList<>(); + mapping = new HashMap<>(); } - return result; + public Finder(boolean caseSensitive) { + this.caseSensitive = caseSensitive; + elements = new ArrayList<>(); + mapping = new HashMap<>(); + } + public List getAll() { + return elements; + } - } + public void addAll(List list) throws Exception { + for (T element : list) { + add(element); + } + } - private Set find(String term) { + private void add(T element) throws Exception { + boolean elementAdded = elements.add(element); + int position = elements.lastIndexOf(element); + if (!elementAdded || position == -1) throw new Exception(); + addToIndex(element, position); + } - term = sanitizeIndexValue(term); + private List findElements(Set indexes) { + if (indexes == null || indexes.size() == 0) return null; + List result = new ArrayList<>(); + for (Integer i : indexes) { + result.add(elements.get(i)); + } + return result; + } - if (mapping.containsKey(term)) { + private Set find(String term) { + term = sanitizeIndexValue(term); + return mapping.getOrDefault(term, null); + } - return mapping.get(term); + public List findAll(String[] terms) { + if (terms == null || terms.length == 0) return null; + Set result = new HashSet<>(); + for (String term : terms) { + if (result.size() == 0) { + Set init = find(term); + if (init != null) result.addAll(find(term)); + } else { + Set found = find(term); + if (found != null) { + result.retainAll(found); + } else { + return null; + } + } + } + return findElements(result); + } - } else return null; - } + public List findAny(String[] terms) { + if (terms == null || terms.length == 0) return null; + Set result = new HashSet<>(); + for (String term : terms) { + if (result.size() == 0) { + Set init = find(term); + if (init != null) result.addAll(find(term)); + } else { + Set found = find(term); + if (found != null) { + result.addAll(found); + } + } + } + return findElements(result); + } + public List findNone(String[] terms) throws Exception { + if (terms == null || terms.length == 0) return null; + List anys = findAny(terms); - public List findAll(String[] terms) throws Exception { - if (terms == null || terms.length == 0) return null; + if (anys != null) { + List noneElements = new ArrayList<>(elements); + noneElements.removeAll(anys); + return noneElements; + } else return elements; + } - Set result = new HashSet<>(); + private void addToIndex(T element, int pos) { + List index = element.getIndex(); + for (String string : index) { + string = sanitizeIndexValue(string); + if (string != null) { + if (mapping.containsKey(string)) { + Set postions = mapping.get(string); + postions.add(pos); + } else { + Set postions = new HashSet<>(); + postions.add(pos); + mapping.put(string, postions); + } + } + } + } - for (String term : terms) { - if (result.size() == 0) { - Set init = find(term); - if (init != null) result.addAll(find(term)); - } else { - Set found = find(term); - if (found != null) { - result.retainAll(found); + private String sanitizeIndexValue(String string) { + if (string == null || string.trim().length() == 0) { + return null; + } + if (caseSensitive) { + return string.trim(); } else { - return null; + return string.trim().toLowerCase(); } - } - } - - return findElements(result); - } - - public List findAny(String[] terms) throws Exception { - if (terms == null || terms.length == 0) return null; - - Set result = new HashSet<>(); - - for (String term : terms) { - if (result.size() == 0) { - Set init = find(term); - if (init != null) result.addAll(find(term)); - } else { - Set found = find(term); - if (found != null) { - result.addAll(found); - } - } - } - - return findElements(result); - } - - public List findNone(String[] terms) throws Exception { - if (terms == null || terms.length == 0) return null; - List anys = findAny(terms); - - if (anys != null) { - List noneElements = new ArrayList(elements); - noneElements.removeAll(anys); - return noneElements; - } else return elements; - } - - private void addToIndex(T element, int pos) { - - List index = element.getIndex(); - - for (String string : index) { - - string = sanitizeIndexValue(string); - if (string != null) { - - if (mapping.containsKey(string)) { - Set postions = mapping.get(string); - postions.add(pos); - } else { - Set postions = new HashSet<>(); - postions.add(pos); - mapping.put(string, postions); - } } - } - } - - - private String sanitizeIndexValue(String string) { - - if (string == null || string.trim().length() == 0) return null; - - if (caseSensitive) { - return string.trim(); - } else { - return string.trim().toLowerCase(); - } - } - - private List elements; - private Map> mapping; } +class Person implements Searchable { + private final String firstName; + private final String lastName; + private final String email; -class Person implements Searcheable { - - Person(String firstName, String lastName, String email) { - this.firstName = firstName; - this.lastName = lastName; - this.email = email; - } - - public String toString() { - StringBuilder sb = new StringBuilder(); - if (firstName != null) sb.append(firstName).append(" "); - if (lastName != null) sb.append(lastName).append(" "); - if (email != null) sb.append(email); - sb.append("; "); - return sb.toString(); - } - - public List getIndex() { - List result = new ArrayList<>(); - - if (firstName != null) result.add(firstName); - if (lastName != null) result.add(lastName); - if (email != null) result.add(email); + Person(String firstName, String lastName, String email) { + this.firstName = firstName; + this.lastName = lastName; + this.email = email; + } - return result; - } + public String toString() { + StringBuilder sb = new StringBuilder(); + if (firstName != null) sb.append(firstName).append(" "); + if (lastName != null) sb.append(lastName).append(" "); + if (email != null) sb.append(email); + sb.append("; "); + return sb.toString(); + } - private String firstName; - private String lastName; - private String email; + public List getIndex() { + List result = new ArrayList<>(); -} + if (firstName != null) result.add(firstName); + if (lastName != null) result.add(lastName); + if (email != null) result.add(email); -/* -javac src/com/kostylevv/paksrms/*.java -java -cp src/ com.kostylevv.paksrms.Paks -*/ + return result; + } +} \ No newline at end of file diff --git a/src/simpleSearchEngine/Util.java b/src/simpleSearchEngine/Util.java index ed7557e..aac5bc2 100644 --- a/src/simpleSearchEngine/Util.java +++ b/src/simpleSearchEngine/Util.java @@ -1,11 +1,15 @@ package simpleSearchEngine; -import java.util.*; -import static java.nio.file.StandardOpenOption.*; -import java.nio.file.*; -import java.io.*; +import java.io.BufferedReader; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; -public class Util { +class Util { private static final String DB_PATH = "db\\"; @@ -14,7 +18,7 @@ public static List getPersonsFromFile(String filename) throws Exception Path file = Paths.get(DB_PATH + filename); try (InputStream in = Files.newInputStream(file); BufferedReader reader = new BufferedReader(new InputStreamReader(in))) { - String line = null; + String line; while ((line = reader.readLine()) != null) { String[] words = line.trim().split(" "); @@ -32,11 +36,8 @@ public static List getPersonsFromFile(String filename) throws Exception default: System.out.println("Person entry should contain at least first name and last name delimited with space"); } - } } - return persons; - } }