Skip to content
Snippets Groups Projects
Commit 6b554411 authored by jonas.stirnema's avatar jonas.stirnema
Browse files

Added search

parent 76eb6598
No related branches found
No related tags found
No related merge requests found
......@@ -9,13 +9,13 @@ public class Contact {
// --------------------------------------------------
// -------------------- ATTRIBUTES ------------------
// --------------------------------------------------
private String fname;
private List<String> names;
private String address;
private List<String> phoneNumbers;
private List<String> emails;
private List<String> socials;
private String job;
protected String fname;
protected List<String> names;
protected String address;
protected List<String> phoneNumbers;
protected List<String> emails;
protected List<String> socials;
protected String job;
// --------------------------------------------------
// -------------------- CONSTRUCTORS ----------------
......@@ -225,4 +225,32 @@ public class Contact {
System.out.println("(6) Job - " + this.job);
}
public boolean attributesContain(String substr)
{
// Go through attributes by hand cause java is tr**h
List<String> lookinto = new ArrayList<String>(100);
lookinto.add(this.fname);
for (String string : this.names) {
lookinto.add(string);
}
lookinto.add(this.address);
for (String phoneNumber : this.phoneNumbers) {
lookinto.add(phoneNumber);
}
for (String email : this.emails) {
lookinto.add(email);
}
for (String social : this.socials) {
lookinto.add(social);
}
lookinto.add(this.job);
// Return true when matched and false when not
return lookinto.stream().anyMatch(s -> s.contains(substr));
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.swing.text.StyledEditorKit.ForegroundAction;
public class DynArray {
......@@ -181,4 +187,14 @@ public class DynArray {
}
}
public List<Contact> getContactsContaining(String substr)
{
List<Contact> conts = Arrays.stream(this.array).filter(Objects::nonNull).collect(Collectors.toList());
for (Contact contact : conts) {
System.out.println(contact);
}
return conts.stream().filter(x -> x.attributesContain(substr)).collect(Collectors.toList());
}
}
\ No newline at end of file
......@@ -94,7 +94,12 @@ public class Family extends Contact
public void showFull()
{
super.showFull();
System.out.println("(7) - " + this.relation);
System.out.println("Relation : " + this.relation);
}
@Override
public String toShortString() {
return this.fname + " " + this.names.get(0) + "(FAMILY)";
}
protected static Family createCompleteFamily(Scanner sc)
......
......@@ -96,6 +96,11 @@ public class Friend extends Contact
System.out.println("(7) - " + this.relation);
}
@Override
public String toShortString() {
return this.fname + " " + this.names.get(0) + "(FRIEND)";
}
protected static Friend createCompleteFriend(Scanner sc)
{
Contact temp = createCompleteContact(sc);
......
......@@ -19,13 +19,14 @@ public class Main {
// Menu state
Scanner sc = new Scanner(System.in);
Menu mainm = new Menu(sc, "Show", "Add", "Modify", "Delete", "Quit");
Menu mainm = new Menu(sc, "Show", "Add", "Modify", "Delete", "Search", "Quit");
final int MAIN = 0;
final int SHOW = 1;
final int ADD = 2;
final int MOD = 3;
final int RMV = 4;
final int SCH = 5;
int state = MAIN;
boolean run = true;
......@@ -52,6 +53,10 @@ public class Main {
run = mainm.removeUser(contacts);
state = MAIN;
break;
case SCH:
run = mainm.searchContact(contacts);
state = MAIN;
break;
default:
run = false;
break;
......
......@@ -4,6 +4,7 @@ import java.util.Scanner; // Import the Scanner class
import javax.lang.model.util.ElementScanner14;
import java.util.List;
import java.util.ListIterator;
public class Menu
{
......@@ -235,5 +236,56 @@ public class Menu
return true;
}
// --------------------------------------------------
// -------------------- SEARCH MENU -----------------
// --------------------------------------------------
public boolean searchContact(DynArray contacts)
{
// Get the fitlered contacts
String str = askForSearchString();
List<Contact> conts = contacts.getContactsContaining(str);
// GTFO if empty
if (conts.isEmpty())
{
System.out.println("Nothing left");
return false;
}
return askSearchShow(conts);
}
private String askForSearchString()
{
System.out.println("The pattern you'll enter will be searched into every field!");
System.out.print("Searching : ");
return this.sc.nextLine();
}
// Ask the user what user he wants to see
private boolean askSearchShow(List<Contact> conts)
{
// Show every remaining contacts
ListIterator<Contact> it = conts.listIterator();
while (it.hasNext()) {
Contact c = it.next();
System.out.println("(" + it.nextIndex() + ")" + c.getFname() + " " + c.getNames().get(0));
}
System.out.println("(" + conts.size() +") - Quit");
System.out.print("Look at : ");
int res = this.sc.nextInt();
if((res > -1) && (res < conts.size()))
{
conts.get(res).showFull();
return true;
}
return false;
}
}
......@@ -97,6 +97,11 @@ public class Professional extends Contact
System.out.println("(7) - " + this.relation);
}
@Override
public String toShortString() {
return this.fname + " " + this.names.get(0) + "(PROFESIONNAL)";
}
protected static Professional createCompleteProfessional(Scanner sc)
{
Contact temp = createCompleteContact(sc);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment