Skip to content
Snippets Groups Projects
Commit 6d321cb8 authored by vincent.steinman's avatar vincent.steinman
Browse files

End section 2

parent 45c9feef
No related branches found
No related tags found
No related merge requests found
package org.example; package org.example;
public abstract class Contact { public abstract class Contact implements Comparable<Contact>{
//Fields //Fields
protected Person pers; protected Person pers;
protected String addr; protected String addr;
...@@ -110,4 +110,8 @@ public abstract class Contact { ...@@ -110,4 +110,8 @@ public abstract class Contact {
return pers.toString() + "\n" + this.addr + "\n" + w_phone + "\n" + w_email + "\n" return pers.toString() + "\n" + this.addr + "\n" + w_phone + "\n" + w_email + "\n"
+ w_social + "\n" + this.job; + w_social + "\n" + this.job;
} }
@Override
public int compareTo(Contact c){
return this.pers.getFirst_name().compareTo(c.getPers().getFirst_name());
}
} }
package org.example; package org.example;
import java.util.ArrayList; import java.util.*;
import java.util.Arrays;
import java.util.Scanner;
public class Main { public class Main {
static ArrayList<Contact> contactArray = new ArrayList<Contact>(); static ArrayList<Contact> contactArray = new ArrayList<Contact>();
...@@ -39,6 +37,9 @@ public class Main { ...@@ -39,6 +37,9 @@ public class Main {
} }
} }
} }
/*
* Pick which type contact the user wants to add
*/
public static void Pick(){ public static void Pick(){
int size_array = contactArray.size(); int size_array = contactArray.size();
...@@ -65,6 +66,9 @@ public class Main { ...@@ -65,6 +66,9 @@ public class Main {
break; break;
} }
} }
/*
* Adds a friend contact
*/
public static void AddPal(){ public static void AddPal(){
System.out.println("Enter first names"); System.out.println("Enter first names");
Scanner myObj = new Scanner(System.in); Scanner myObj = new Scanner(System.in);
...@@ -91,6 +95,9 @@ public class Main { ...@@ -91,6 +95,9 @@ public class Main {
Friend pal = new Friend(p, address, phoneList, emailList, socialList, job); Friend pal = new Friend(p, address, phoneList, emailList, socialList, job);
Add(pal); Add(pal);
} }
/*
* Adds a family contact
*/
public static void AddFam(){ public static void AddFam(){
System.out.println("Enter first names"); System.out.println("Enter first names");
Scanner myObj = new Scanner(System.in); Scanner myObj = new Scanner(System.in);
...@@ -119,6 +126,9 @@ public class Main { ...@@ -119,6 +126,9 @@ public class Main {
Family fam = new Family(p, address, phoneList, emailList, socialList, job, relation); Family fam = new Family(p, address, phoneList, emailList, socialList, job, relation);
Add(fam); Add(fam);
} }
/*
* Adds a professional contact (obviously)
*/
public static void AddPro(){ public static void AddPro(){
System.out.println("Enter first names"); System.out.println("Enter first names");
Scanner myObj = new Scanner(System.in); Scanner myObj = new Scanner(System.in);
...@@ -149,7 +159,7 @@ public class Main { ...@@ -149,7 +159,7 @@ public class Main {
} }
public static void Add(Contact c){ public static void Add(Contact c){
contactArray.add(c); contactArray.add(c);
OrderArray(); Collections.sort(contactArray);
} }
public static void Edit(){ public static void Edit(){
int size_array = contactArray.size(); int size_array = contactArray.size();
...@@ -245,13 +255,16 @@ public class Main { ...@@ -245,13 +255,16 @@ public class Main {
} }
} }
public static void Search(){ 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){ 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){ public static boolean TryNumber(String nb){
int size_array = contactArray.size(); int size_array = contactArray.size();
int nbInt = 0; int nbInt = 0;
...@@ -263,26 +276,4 @@ public class Main { ...@@ -263,26 +276,4 @@ public class Main {
} }
return (nbInt < size_array); 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
No preview for this file type
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment