diff --git a/tp/src/main/java/org/example/Contact.java b/tp/src/main/java/org/example/Contact.java index 590b07c3f556f13bf006470bde5c0e0ca94908ee..41229ec4a1749647d243b75ce6e95b5ce6957004 100644 --- a/tp/src/main/java/org/example/Contact.java +++ b/tp/src/main/java/org/example/Contact.java @@ -1,6 +1,6 @@ package org.example; -public abstract class Contact { +public abstract class Contact implements Comparable<Contact>{ //Fields protected Person pers; protected String addr; @@ -110,4 +110,8 @@ public abstract class Contact { return pers.toString() + "\n" + this.addr + "\n" + w_phone + "\n" + w_email + "\n" + w_social + "\n" + this.job; } + @Override + public int compareTo(Contact c){ + return this.pers.getFirst_name().compareTo(c.getPers().getFirst_name()); + } } diff --git a/tp/src/main/java/org/example/Main.java b/tp/src/main/java/org/example/Main.java index 65e89b86a253954d1242d88e377e84e5d5ceba2c..b56cdca075561522bdc137fbdfd86be403306323 100644 --- a/tp/src/main/java/org/example/Main.java +++ b/tp/src/main/java/org/example/Main.java @@ -1,7 +1,5 @@ package org.example; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Scanner; +import java.util.*; public class Main { static ArrayList<Contact> contactArray = new ArrayList<Contact>(); @@ -39,6 +37,9 @@ public class Main { } } } + /* + * Pick which type contact the user wants to add + */ public static void Pick(){ int size_array = contactArray.size(); @@ -65,6 +66,9 @@ public class Main { break; } } + /* + * Adds a friend contact + */ public static void AddPal(){ System.out.println("Enter first names"); Scanner myObj = new Scanner(System.in); @@ -91,6 +95,9 @@ public class Main { Friend pal = new Friend(p, address, phoneList, emailList, socialList, job); Add(pal); } + /* + * Adds a family contact + */ public static void AddFam(){ System.out.println("Enter first names"); Scanner myObj = new Scanner(System.in); @@ -119,6 +126,9 @@ public class Main { Family fam = new Family(p, address, phoneList, emailList, socialList, job, relation); Add(fam); } + /* + * Adds a professional contact (obviously) + */ public static void AddPro(){ System.out.println("Enter first names"); Scanner myObj = new Scanner(System.in); @@ -149,7 +159,7 @@ public class Main { } public static void Add(Contact c){ contactArray.add(c); - OrderArray(); + Collections.sort(contactArray); } public static void Edit(){ int size_array = contactArray.size(); @@ -245,13 +255,16 @@ public class Main { } } public static void Search(){ + System.out.println("Enter Contact first name"); + Scanner myObj = new Scanner(System.in); + String ctName = myObj.nextLine(); for(Contact c: contactArray){ - if(c.getPers().getFirst_name() == ""){ - + if(c.getPers().getFirst_name().equals(ctName)){ + System.out.println(c.toString()); } } } - //Test if the value is a number or not and if it's withing the range of the array + //Test if the value is a number or not and if it's withing the range of the list public static boolean TryNumber(String nb){ int size_array = contactArray.size(); int nbInt = 0; @@ -263,26 +276,4 @@ public class Main { } return (nbInt < size_array); } - public static void OrderArray(){ - int size_array = contactArray.size(); - if(size_array > 1){ - ArrayList<Contact> newContactArray = new ArrayList<Contact>(); - for(int i = 0; i < size_array - 1; i++){ //-1 as we check +1 later (< as lst starts at 0) - String nameP1 = contactArray.get(i).getPers().getFirst_name(); - String nameP2 = contactArray.get(i+1).getPers().getFirst_name(); - int test = nameP1.compareTo(nameP2); //Return distance between 2 letter (f and a = -5 / a and b = 1) - if(test > 0){ - newContactArray.add(contactArray.get(i+1)); - newContactArray.add(contactArray.get(i)); - } - else{ - newContactArray.add(contactArray.get(i)); - } - if(i == size_array - 1){ //-2 to take last iteration only (as we do "i < size -1") - newContactArray.add(contactArray.get(i+1)); - contactArray = newContactArray; - } - } - } - } } \ No newline at end of file diff --git a/tp/target/classes/org/example/Contact.class b/tp/target/classes/org/example/Contact.class index 90a523062b11ba0b0f791d92073dc5d969add585..b50d2edd3543dfc535159a3b7a9f27b70b6beffe 100644 Binary files a/tp/target/classes/org/example/Contact.class and b/tp/target/classes/org/example/Contact.class differ diff --git a/tp/target/classes/org/example/Main.class b/tp/target/classes/org/example/Main.class index 26c69ecfe8a389705cae4a52d07bc900fb788bde..7edea38a5acabb6a15a10b2e9f9543d1a5a6435f 100644 Binary files a/tp/target/classes/org/example/Main.class and b/tp/target/classes/org/example/Main.class differ