From 95dd379d8169f5e1232e418f9c393cd28a460aec Mon Sep 17 00:00:00 2001 From: "zabiulla.ahmadi" <zabiullah.ahmadi@etu.hesge.ch> Date: Mon, 31 Oct 2022 08:07:29 +0100 Subject: [PATCH] POO project --- src/Config/Config.java | 31 +++++ src/Contacts/Contacts.java | 242 ++++++++++++++++++++++++++++++++ src/Helper/Helper.java | 276 +++++++++++++++++++++++++++++++++++++ src/Main.java | 9 ++ src/Person/Person.java | 80 +++++++++++ 5 files changed, 638 insertions(+) create mode 100644 src/Config/Config.java create mode 100644 src/Contacts/Contacts.java create mode 100644 src/Helper/Helper.java create mode 100644 src/Main.java create mode 100644 src/Person/Person.java diff --git a/src/Config/Config.java b/src/Config/Config.java new file mode 100644 index 0000000..6d4fff6 --- /dev/null +++ b/src/Config/Config.java @@ -0,0 +1,31 @@ +package Config; + +public class Config { + + // text color + public static final String RED = "\u001B[31m"; + public static final String BLACK = "\u001B[30m"; + public static final String GREEN = "\u001B[32m"; + public static final String BLUE = "\u001B[34m"; + public static final String RESET = "\u001B[0m"; + public static final String PURPLE = "\u001B[35m"; + public static final String CYAN = "\u001B[36m"; + public static final String YELLOW = "\u001B[33m"; + public static final String WHITE = "\u001B[37m"; + + public static final String YELLOW_BACKGROUND = "\u001B[43m"; + public static final String BLUE_BACKGROUND = "\u001B[44m"; + public static final String BLACK_BACKGROUND = "\u001B[40m"; + public static final String PURPLE_BACKGROUND = "\u001B[45m"; + public static final String CYAN_BACKGROUND = "\u001B[46m"; + public static final String GREEN_BACKGROUND = "\u001B[42m"; + public static final String WHITE_BACKGROUND = "\u001B[47m"; + + public static final int ASK_USER_INPUT = 110; + public static final int SHOW_CONTACTS = 1; + public static final int ADD_CONTACTS = 2; + public static final int UPDATE_CONTACTS = 3; + public static final int DELETE_CONTACT = 4; + public static final int EXIT_CONSOLE = 5; + +} \ No newline at end of file diff --git a/src/Contacts/Contacts.java b/src/Contacts/Contacts.java new file mode 100644 index 0000000..d988b7f --- /dev/null +++ b/src/Contacts/Contacts.java @@ -0,0 +1,242 @@ +package Contacts; + +import Config.Config; +import Helper.Helper; +import Person.Person; + +import java.util.Scanner; + +public class Contacts { + + static Person[] contact; + + public Contacts() { + } + + public Contacts(Person[] persons) { + contact = persons; + } + + public static Contacts of(Person... persons) { + return new Contacts(persons); + } + + public Person[] getContacts() { + return contact; + } + + public void setContacts(Person[] person) { + contact = person; + } + + private int getCharNumericValue(String value) { + return (int) (value.charAt(0)); + } + + private void shift(int index) { + Person tempContact = contact[index]; + contact[index] = contact[index - 1]; + contact[index - 1] = tempContact; + } + + private void bubbleSort() { + + for (int i = 0; i < contact.length; i++) { + + for (int j = contact.length - 1; j >= 1; j--) { + + int firstPersonNameValue = getCharNumericValue(contact[j].getName().toUpperCase()); + int firstPersonLastNameValue = getCharNumericValue(contact[j].getLastname()[0].toUpperCase()); + + int secondPersonNameValue = getCharNumericValue(contact[j - 1].getName().toUpperCase()); + int secondPersonLastNameValue = getCharNumericValue(contact[j - 1].getLastname()[0].toUpperCase()); + + // sort by last name if first names are equals + if (firstPersonNameValue == secondPersonNameValue) { + + if (firstPersonLastNameValue < secondPersonLastNameValue) { + shift(j); + } + + // sort by first name + } else if (firstPersonNameValue < secondPersonNameValue) { + shift(j); + } + + } + } + } + + public void add(Person newContact) { + Person[] temp = new Person[contact.length + 1]; + + for (int i = 0; i < contact.length; i++) { + temp[i] = contact[i]; + } + temp[contact.length] = newContact; + + contact = temp; + // bubble sort + bubbleSort(); + + } + + public Person createOrupdate(String flag) { + + Helper.clearConsoleScreen(); + Helper.PrintInColor(" ==================================================================================\n", + Config.GREEN); + Helper.PrintInColor(String.format("%s\n", flag), Config.BLUE); + Helper.PrintInColor("==================================================================================\n", + Config.GREEN); + + Person person = new Person(); + Scanner sc = new Scanner(System.in); + + System.out.print(" Enter a name [] : "); + String name = sc.nextLine(); + while (name.length() < 3) { + System.out + .println( + String.format("\n %sName should contain at least 3 character %s%s", + Config.RED, + Config.RED, + Config.RESET)); + System.out.println(); + System.out.print(" Enter a name [] : "); + name = sc.nextLine(); + } + person.setName(name); + + System.out.print(" Enter one or more last name separated by , [] : "); + String lastName = sc.nextLine(); + + while (lastName.length() < 3) { + System.out + .println( + String.format("\n %slast name should contain at least 3 character %s%s", + Config.RED, + Config.RED, + Config.RESET)); + System.out.println(); + System.out.print(" Enter one or more last name separated by , [] : "); + name = sc.nextLine(); + } + person.setLastname(lastName.split(",")); + + System.out.print(" Enter Address [] : "); + String address = sc.nextLine(); + while (address.length() < 15) { + System.out + .println( + String.format("\n %sAddress should at least contain 15 character length%s%s", + Config.RED, + Config.RED, + Config.RESET)); + System.out.println(); + System.out.print(" Enter Address [] : "); + address = sc.nextLine(); + } + person.setAddress(address); + + System.out.print(" Enter one or more email separated by , [] : "); + String email = sc.nextLine(); + + while (email.length() < 10 || !email.contains("@") || !email.contains(".")) { + System.out + .println( + String.format("\n %sPlease Insert a correct Email !%s%s", Config.RED, + Config.RED, + Config.RESET)); + System.out.println(); + System.out.print(" Enter one or more email separated by , [] : "); + + email = sc.nextLine(); + } + person.setEmailAddresses(email.split(",")); + + System.out.print(" Enter one or more telephone number separated by , [] : "); + String telephonNumber = sc.nextLine(); + + while (telephonNumber.length() < 12 || !telephonNumber.contains("+") + || telephonNumber.replaceAll(" ", telephonNumber).length() < 12) { // +4178 223 22 44 + System.out.println( + String.format("\n %sPLEASE INSERT A CORRECT TELEPHONE NUMBER !%s%s", + Config.RED, + Config.RED, + Config.RESET)); + System.out.println(); + System.out.print(" Enter one or more telephone number separated by , [] : "); + email = sc.nextLine(); + + } + person.setTelelphoneNumbers(telephonNumber.split(",")); + + System.out.print(" Enter one or more social acount URL separated by , [] :"); + String socialAcount = sc.nextLine(); + + while (!socialAcount.contains("https://")) { + System.out + .println( + String.format("\n %sPLEASE INSERT A CORRECT URL PREFIXED BY HTTPS:// !%s%s", + Config.RED, + Config.RED, + Config.RESET)); + System.out.println(); + System.out.print(" Enter one or more social acount URL separated by , [] :"); + socialAcount = sc.nextLine(); + } + person.setSocialAcounts(socialAcount.split(",")); + + System.out.print(" Enter Your Contact Profession [] : "); + String profession = sc.nextLine(); + + while (profession.length() < 5) { + System.out + .println( + String.format("\n %sPLEASE INSERT A CORRECT PROFESSION !%s%s", + Config.RED, + Config.RED, + Config.RESET)); + System.out.println(); + System.out.print(" Enter Your Contact Profession [] : "); + profession = sc.nextLine(); + } + person.setProfession(profession); + return person; + } + + public boolean delete(int id) { + + int elementId = id - 1; + if (elementId >= 0 && elementId < contact.length) { + // delete + Person[] temp = new Person[contact.length - 1]; + + int j = 0; + for (int i = 0; i < contact.length; i++) { + if (i != elementId) { + temp[j] = contact[i]; + j++; + } + } + contact = temp; + return true; + + } else { + return false; + } + } + + public boolean update(int id) { + + int elementId = id - 1; + if (elementId >= 0 && elementId < contact.length) { + + contact[elementId] = this.createOrupdate("UPDATE A CONTACT"); + return true; + } else { + return false; + } + } +} diff --git a/src/Helper/Helper.java b/src/Helper/Helper.java new file mode 100644 index 0000000..8b88238 --- /dev/null +++ b/src/Helper/Helper.java @@ -0,0 +1,276 @@ +package Helper; + +import Config.Config; +import Contacts.Contacts; +import Person.Person; +import java.util.Scanner; + +public class Helper extends Config { + + // clear console screen + public static void clearConsoleScreen() { + System.out.print("\033[H\033[2J"); + System.out.flush(); + } + + // print in color a given text or change background color + public static void PrintInColor(String text, String color) { + + System.out.format("%s %s %s", color, text, color); + System.out.format("%s", RESET); + } + + // Print header of our console application + public static void PrintHeader() { + + clearConsoleScreen(); + PrintInColor(" ==================================================================================\n", + YELLOW); + PrintInColor("WELLCOME TO CONTACT MANAGEMENT SYSTEM \n", GREEN); + PrintInColor("==================================================================================\n", + YELLOW); + + PrintInColor("[1]: \tshow contacts\t\t\t\t\n", CYAN); + PrintInColor("[2]: \tadd contact\t\t\t\t\n", CYAN); + PrintInColor("[3]: \tupdate a contact\t\t\t\t\n", CYAN); + PrintInColor("[4]: \tdelete a contact\t\t\t\t\n", CYAN); + PrintInColor("[5]: \texit\t\t\t\t\n", CYAN); + PrintInColor("==================================================================================\n", + YELLOW); + } + + // print OPTION ERROR + public static void PrintErrorAndReturn() { + + PrintInColor(" ----------------------------------------------------------------------------------\n", + YELLOW); + PrintInColor("ERROR: WRONG OPTION ID \n", RED); + PrintInColor("----------------------------------------------------------------------------------\n", + YELLOW); + + PrintInColor("[1]: \treturn to main menu\t\t\t\t\n", CYAN); + PrintInColor("[2]: \texit\t\t\t\t\n", CYAN); + PrintInColor("----------------------------------------------------------------------------------\n", + YELLOW); + + } + + public static void printFooter() { + + PrintInColor(" ----------------------------------------------------------------------------------\n", + YELLOW); + PrintInColor("RETURN TO HOME \n", CYAN); + PrintInColor("----------------------------------------------------------------------------------\n", + YELLOW); + + PrintInColor("[1]: \treturn to main menu\t\t\t\t\n", CYAN); + PrintInColor("[2]: \texit\t\t\t\t\n", CYAN); + PrintInColor("----------------------------------------------------------------------------------\n", + YELLOW); + } + + public static void deleteOption() { + PrintInColor(" ==================================================================================\n", + YELLOW); + PrintInColor("DELETE A CONCTACT\n", CYAN); + PrintInColor("==================================================================================\n", + YELLOW); + } + + public static void showContacts(Person[] person) { + + clearConsoleScreen(); + PrintInColor(" ==================================================================================\n", + YELLOW); + PrintInColor(String.format("SHOW CONTACTS \t\t\t\t\t\t\tTOTAL [%s %d %s]\n", WHITE, + (person != null) ? person.length : 0, + CYAN), CYAN); + PrintInColor("==================================================================================\n\n", + YELLOW); + + if (person == null) { + System.out.println(" No contacts yet !\n"); + + } + if (person != null) { + + for (int i = 0; i < person.length && person[i] != null; i++) { + + System.out.println( + "----------------------------------------------------------------------------------"); + System.out.println(); + + System.out.println(String.format(" %sId : %s", BLUE, BLUE) + + String.format("\t\t\t\t%s" + "[ " + (i + 1) + " ]", RESET)); + System.out.println(); + + System.out.println(String.format(" %sName : %s", BLUE, BLUE) + + String.format("\t\t\t%s" + person[i].getName(), RESET)); + + System.out.println(); + String lastnames = String.join(",", person[i].getLastname()); + System.out.println(String.format(" %sLast Name : %s", BLUE, + BLUE) + + String.format("\t\t\t%s" + lastnames, RESET)); + + System.out.println(); + System.out.println(String.format(" %sAddress : %s", BLUE, BLUE) + + String.format("\t\t\t%s" + person[i].getAddress(), RESET)); + + System.out.println(); + String emails = String.join(", ", person[i].getEmailAddresses()); + System.out.println(String.format(" %sEmail : %s", BLUE, BLUE) + + String.format("\t\t\t%s" + emails, RESET)); + + System.out.println(); + String telephone_numbers = String.join(", ", + person[i].getTelelphoneNumbers()); + System.out.println(String.format(" %sTelephone Number : %s", BLUE, + BLUE) + + String.format("\t\t%s" + telephone_numbers, RESET)); + + System.out.println(); + String social_acounts = String.join(", ", person[i].getSocialAcounts()); + System.out.println(String.format(" %sSocial Acount : %s", BLUE, + BLUE) + + String.format("\t\t%s" + social_acounts, RESET)); + + System.out.println(); + System.out.println(String.format(" %sProfession : %s", BLUE, + BLUE) + + String.format("\t\t\t%s" + person[i].getProfession(), RESET)); + + System.out.println(); + System.out.println( + "----------------------------------------------------------------------------------\n"); + } + } + + } + + public static int populateErrorOrReturnHome(int option) { + if (option == 1) { + return ASK_USER_INPUT; + } + if (option == 2) { + System.exit(0); + } + return 0; + } + + public static void runApplication() { + + int option = ASK_USER_INPUT; + + Contacts contacts = new Contacts(); + while (true) { + + Scanner scanner = new Scanner(System.in); + if (option == ASK_USER_INPUT) { + + Helper.PrintHeader(); + Helper.PrintInColor("Enter your option : ", WHITE); + + option = scanner.nextInt(); + + if (option == EXIT_CONSOLE) { + System.exit(0); + } + + if (option == ADD_CONTACTS) { + + Person[] newContact = { contacts.createOrupdate("Add A NEW CONTACT") }; + if (contacts.getContacts() == null) { + contacts = new Contacts(newContact); + } else { + contacts.add(newContact[0]); + } + option = ASK_USER_INPUT; + continue; + } + + if (option == SHOW_CONTACTS) { + + Helper.showContacts(contacts.getContacts()); + Helper.printFooter(); + + Helper.PrintInColor("Enter your option : ", WHITE); + option = scanner.nextInt(); + option = populateErrorOrReturnHome(option); + continue; + + } + + // update contact + if (option == UPDATE_CONTACTS) { + Helper.showContacts(contacts.getContacts()); + + if (contacts.getContacts() != null) { + + System.out.print("Enter a contact id to update : "); + int id = scanner.nextInt(); + + boolean updated = contacts.update(id); + + if (updated) { + option = ASK_USER_INPUT; + } else { + System.out.println(String.format( + " Contact with id %d doesn't exists", id)); + Helper.PrintErrorAndReturn(); + option = scanner.nextInt(); + option = populateErrorOrReturnHome(option); + } + + } else { + Helper.showContacts(contacts.getContacts()); + Helper.printFooter(); + option = scanner.nextInt(); + option = populateErrorOrReturnHome(option); + } + continue; + } + // delete contact + if (option == DELETE_CONTACT) { + Helper.showContacts(contacts.getContacts()); + + if (contacts.getContacts() != null) { + Helper.deleteOption(); + System.out.print("Enter contact id to delete: "); + int id = scanner.nextInt(); + + boolean deleted = contacts.delete(id); + + if (deleted) { + option = ASK_USER_INPUT; + } else { + System.out.println(String.format( + " Contact with id %d doesn't exists", id)); + Helper.PrintErrorAndReturn(); + option = scanner.nextInt(); + option = populateErrorOrReturnHome(option); + } + + } else { + Helper.printFooter(); + option = scanner.nextInt(); + option = populateErrorOrReturnHome(option); + } + continue; + } + + } + + if (option < 1 || option > 4) { + + Helper.clearConsoleScreen(); + Helper.PrintErrorAndReturn(); + option = scanner.nextInt(); + option = populateErrorOrReturnHome(option); + + } + + } + + } +} diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..f02a5f0 --- /dev/null +++ b/src/Main.java @@ -0,0 +1,9 @@ +import Helper.Helper; + +public class Main extends Helper { + + public static void main(String[] args) { + + Helper.runApplication(); + } +} \ No newline at end of file diff --git a/src/Person/Person.java b/src/Person/Person.java new file mode 100644 index 0000000..9dcf923 --- /dev/null +++ b/src/Person/Person.java @@ -0,0 +1,80 @@ +package Person; + +public class Person { + private String name; + private String[] lastname; + private String address; + private String[] telelphoneNumbers; + private String[] emailAddresses; + private String[] socialAcounts; + private String profession; + + + public Person(){} + + public Person(String name, String[] lastname, String address, String[] telelphoneNumbers, String[] emailAddresses, String[] socialAcounts, String profession) { + this.name = name; + this.lastname = lastname; + this.address = address; + this.telelphoneNumbers = telelphoneNumbers; + this.emailAddresses = emailAddresses; + this.socialAcounts = socialAcounts; + this.profession = profession; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String[] getLastname() { + return lastname; + } + + public void setLastname(String[] lastname) { + this.lastname = lastname; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + public String[] getTelelphoneNumbers() { + return telelphoneNumbers; + } + + public void setTelelphoneNumbers(String[] telelphoneNumbers) { + this.telelphoneNumbers = telelphoneNumbers; + } + + public String[] getEmailAddresses() { + return emailAddresses; + } + + public void setEmailAddresses(String[] emailAddresses) { + this.emailAddresses = emailAddresses; + } + + public String[] getSocialAcounts() { + return socialAcounts; + } + + public void setSocialAcounts(String[] socialAcounts) { + this.socialAcounts = socialAcounts; + } + + public String getProfession() { + return profession; + } + + public void setProfession(String profession) { + this.profession = profession; + } +} \ No newline at end of file -- GitLab