diff --git a/tp/src/main/java/org/example/Main.java b/tp/src/main/java/org/example/Main.java index 19195dffe705142e3d37223af82029bdb73cf077..c48e660f48aa6a8ae6d54412afed2d99a715e112 100644 --- a/tp/src/main/java/org/example/Main.java +++ b/tp/src/main/java/org/example/Main.java @@ -2,12 +2,13 @@ package org.example; import java.util.Scanner; public class Main { - static Person p1 = new Person("Doe", "John"); - static String[] phone = {"0"}; - static String[] social = {"S"}; - static String[] emails = {"a"}; - static Contact c1 = new Contact(p1, "jst", phone, emails, social, "j"); - static Contact[] contactArray = {c1}; +// static Person p1 = new Person("Doe", "John"); +// static String[] phone = {"0"}; +// static String[] social = {"S"}; +// static String[] emails = {"a"}; +// static Contact c1 = new Contact(p1, "jst", phone, emails, social, "j"); +// static Contact c2 = new Contact(p1,"u",phone, emails,social, "NONE"); + static Contact[] contactArray = {}; public static void main(String[] args) { while(true){ System.out.println(""); @@ -33,13 +34,14 @@ public class Main { Show(); break; default: - System.out.println("Please enter valid number"); + System.out.println("Please enter valid input"); break; } } } public static void Add(){ - int size = contactArray.length; + int size_array = contactArray.length; + Contact[] newContactArray = new Contact[size_array + 1]; System.out.println("Enter first names"); Scanner myObj = new Scanner(System.in); String firstName = myObj.nextLine(); @@ -63,80 +65,105 @@ public class Main { String[] socialList = phone.split(" "); Contact c = new Contact(p, address, phoneList, emailList, socialList, job); - contactArray[size] = c; + System.arraycopy(contactArray,0,newContactArray, 0,size_array); + newContactArray[size_array] = c; + contactArray = newContactArray; } public static void Edit(){ - System.out.println("Which contact?"); - Scanner myObj = new Scanner(System.in); - String contactNb = myObj.nextLine(); - if(TryNumber(contactNb)){ - System.out.println("[0] Edit First names"); - System.out.println("[1] Edit Last names"); - System.out.println("[2] Edit Address"); - System.out.println("[3] Edit Phones"); - System.out.println("[4] Edit Emails"); - System.out.println("[5] Edit Social"); - System.out.println("[6] Edit Job"); - String usrInput = myObj.nextLine(); + int size_array = contactArray.length; + if(size_array == 0){ + System.out.println("List is empty"); + } + else{ + System.out.println("Which contact?"); + Scanner myObj = new Scanner(System.in); + String contactNb = myObj.nextLine(); + if(TryNumber(contactNb)){ + System.out.println("[0] Edit First names"); + System.out.println("[1] Edit Last names"); + System.out.println("[2] Edit Address"); + System.out.println("[3] Edit Phones"); + System.out.println("[4] Edit Emails"); + System.out.println("[5] Edit Social"); + System.out.println("[6] Edit Job"); + String usrInput = myObj.nextLine(); - switch (usrInput){ - case "0": - System.out.println("[0] Enter new first names"); - String editFirst = myObj.nextLine(); - contactArray[Integer.parseInt(contactNb)].EditFirstName(editFirst); - break; - case "1": - System.out.println("[0] Enter new last names"); - String editLast = myObj.nextLine(); - contactArray[Integer.parseInt(contactNb)].EditLastName(editLast); - break; - case "2": - System.out.println("[0] Enter new first address"); - String editAddr = myObj.nextLine(); - contactArray[Integer.parseInt(contactNb)].EditFirstName(editAddr); - break; - case "3": - System.out.println("[0] Enter new phone numbers (use / as separator)"); - String editPhone = myObj.nextLine(); - String[] editPhoneList = editPhone.split("/"); - contactArray[Integer.parseInt(contactNb)].EditPhone(editPhoneList); - break; - case "4": - System.out.println("[0] Enter new Emails (use / as separator)"); - String editEmail = myObj.nextLine(); - String[] editEmailList = editEmail.split("/"); - contactArray[Integer.parseInt(contactNb)].EditEmail(editEmailList); - break; - case "5": - System.out.println("[0] Enter new Socials (use / as separator)"); - String editSocial = myObj.nextLine(); - String[] editSocialList = editSocial.split("/"); - contactArray[Integer.parseInt(contactNb)].EditEmail(editSocialList); - break; - case "6": - System.out.println("[0] Enter new Job"); - String editJob = myObj.nextLine(); - contactArray[Integer.parseInt(contactNb)].EditLastName(editJob); - break; - default: - System.out.println("Please enter correct value"); - break; + switch (usrInput){ + case "0": + System.out.println("Enter new first names"); + String editFirst = myObj.nextLine(); + contactArray[Integer.parseInt(contactNb)].EditFirstName(editFirst); + break; + case "1": + System.out.println("Enter new last names"); + String editLast = myObj.nextLine(); + contactArray[Integer.parseInt(contactNb)].EditLastName(editLast); + break; + case "2": + System.out.println("Enter new first address"); + String editAddr = myObj.nextLine(); + contactArray[Integer.parseInt(contactNb)].EditFirstName(editAddr); + break; + case "3": + System.out.println("Enter new phone numbers (use / as separator)"); + String editPhone = myObj.nextLine(); + String[] editPhoneList = editPhone.split("/"); + contactArray[Integer.parseInt(contactNb)].EditPhone(editPhoneList); + break; + case "4": + System.out.println("Enter new Emails (use / as separator)"); + String editEmail = myObj.nextLine(); + String[] editEmailList = editEmail.split("/"); + contactArray[Integer.parseInt(contactNb)].EditEmail(editEmailList); + break; + case "5": + System.out.println("Enter new Socials (use / as separator)"); + String editSocial = myObj.nextLine(); + String[] editSocialList = editSocial.split("/"); + contactArray[Integer.parseInt(contactNb)].EditEmail(editSocialList); + break; + case "6": + System.out.println("Enter new Job"); + String editJob = myObj.nextLine(); + contactArray[Integer.parseInt(contactNb)].EditLastName(editJob); + break; + default: + System.out.println("Please enter correct value"); + break; + } + } + else { + System.out.println("Please enter valid input"); + System.out.println("It must be a number between 0 and " + (size_array -1)); } - } - else { - System.out.println("Please enter valid number"); } } public static void Delete(){ - Scanner myObj = new Scanner(System.in); - String usrInput = myObj.nextLine(); - System.out.println("Which Contact?"); - if(TryNumber(usrInput)){ - + int size_array = contactArray.length; + if(size_array == 0){ + System.out.println("List is empty"); } else{ - System.out.println("Please enter valid number"); + Contact[] newContactArray = new Contact[size_array -1]; + System.out.println("Which Contact?"); + Scanner myObj = new Scanner(System.in); + String usrInput = myObj.nextLine(); + if(TryNumber(usrInput)){ + for(int i = 0; i < size_array -1; i++){ + if(i >= Integer.parseInt(usrInput)){ + newContactArray[i] = contactArray[i+1]; + } + else{ + newContactArray[i] = contactArray[i]; + } + } + contactArray = newContactArray; + } + else{ + System.out.println("Please enter valid input"); + System.out.println("It must be a number between 0 and " + (size_array -1)); + } } } public static void Show(){ @@ -145,7 +172,9 @@ public class Main { System.out.println(); } } + //Test if the value is a number or not and if it's withing the range of the array public static boolean TryNumber(String nb){ + int size_array = contactArray.length; int nbInt = 0; try{ nbInt = Integer.parseInt(nb); @@ -153,6 +182,6 @@ public class Main { catch (NumberFormatException e){ } - return (nbInt < contactArray.length); + return (nbInt < size_array); } } \ No newline at end of file diff --git a/tp/target/classes/org/example/Main.class b/tp/target/classes/org/example/Main.class index c85a5a2fadb8dd33a5b7275e60c107969aa4c652..f1a65bacc03d7eae4f883748d81361de8ec72936 100644 Binary files a/tp/target/classes/org/example/Main.class and b/tp/target/classes/org/example/Main.class differ