diff --git a/1ano/2semestre/poo/src/aula06/Contact.java b/1ano/2semestre/poo/src/aula06/Contact.java new file mode 100644 index 0000000..73a711c --- /dev/null +++ b/1ano/2semestre/poo/src/aula06/Contact.java @@ -0,0 +1,55 @@ +package aula06; + +public class Contact { + private final int id; + private Person person; + private String email; + private String phone; + + private static int currentId = 1; + + public Contact(Person person, String email, String phone) { + if (email == null || email.isEmpty() || phone == null || phone.isEmpty()) + throw new IllegalArgumentException("Either email or phone must be provided"); + this.id = Contact.currentId++; + this.setPerson(person); + this.setEmail(email); + this.setPhone(phone); + } + + public int getId() { + return id; + } + + public Person getPerson() { + return person; + } + public void setPerson(Person person) { + if (person == null) + throw new IllegalArgumentException("Person must be provided"); + this.person = person; + } + + public String getEmail() { + return email; + } + public void setEmail(String email) { + if (!email.matches("^[a-zA-Z_0-9.]+@[a-zA-Z_0-9.]+\\.[a-zA-Z_0-9]+$")) + throw new IllegalArgumentException("Invalid email"); + this.email = email; + } + + public String getPhone() { + return phone; + } + public void setPhone(String phone) { + if (!phone.matches("^9[0-9]{8}$")) + throw new IllegalArgumentException("Invalid phone"); + this.phone = phone; + } + + @Override + public String toString() { + return String.format("ID: %d%nPerson: %s%nEmail: %s%nPhone: %s", this.id, this.person, this.email, this.phone); + } +} diff --git a/1ano/2semestre/poo/src/aula06/ContactList.java b/1ano/2semestre/poo/src/aula06/ContactList.java new file mode 100644 index 0000000..13f5ebb --- /dev/null +++ b/1ano/2semestre/poo/src/aula06/ContactList.java @@ -0,0 +1,177 @@ +package aula06; + +import utils.DateYMD; + +import java.util.Scanner; + +public class ContactList { + private static final Scanner sin = new Scanner(System.in); + private static Contact[] contacts; + + public static void main(String[] args) { + + while (true) { + System.out.println("Selecione uma opção:"); + System.out.println("1. Inserir contacto"); + System.out.println("2. Alterar contacto"); + System.out.println("3. Apagar contacto"); + System.out.println("4. Procurar contacto"); + System.out.println("5. Listar contactos"); + System.out.println("0. Sair"); + System.out.print("> "); + String option = sin.nextLine(); + switch (option) { + case "0" -> { + sin.close(); + System.exit(0); + } + case "1" -> { + System.out.print("Insira o nome: "); + String name = sin.nextLine(); + System.out.print("Insira o cc: "); + String ccStr = sin.nextLine(); + int cc = Integer.parseInt(ccStr); + System.out.print("Insira a data de nascimento (formado: dd-mm-yyyy): "); + String date = sin.nextLine(); + String[] dateParts = date.split("-"); + DateYMD birthDate = new DateYMD(Integer.parseInt(dateParts[0]), Integer.parseInt(dateParts[1]), Integer.parseInt(dateParts[2])); + System.out.print("Insira o email: "); + String email = sin.nextLine(); + System.out.print("Insira o telefone: "); + String phone = sin.nextLine(); + Person person = new Person(name, cc, birthDate); + + Contact contact = new Contact(person, email, phone); + + if (contacts == null) { + contacts = new Contact[1]; + contacts[0] = contact; + } else { + if (checkIfContactExists(cc)) break; + Contact[] newContacts = new Contact[contacts.length + 1]; + System.arraycopy(contacts, 0, newContacts, 0, contacts.length); + newContacts[contacts.length] = contact; + contacts = newContacts; + } + } + case "2" -> { + System.out.print("Insira o nome, email ou telefone do contacto que pretende alterar: "); + String query = sin.nextLine(); + int[] indexes = searchContactsIndex(query); + System.out.printf("Contactos encontrados: %d%n", indexes.length); + + if (indexes.length == 0) { + System.out.println("Não foram encontrados contactos com esse nome, email ou telefone"); + } else if (indexes.length == 1) { + System.out.printf("Alterando contacto: %s%n", contacts[indexes[0]]); + System.out.print("Insira o novo telefone: "); + String phone = sin.nextLine(); + System.out.print("Insira o novo email: "); + String email = sin.nextLine(); + contacts[indexes[0]].setPhone(phone); + contacts[indexes[0]].setEmail(email); + } else { + Contact contact = selectContact(indexes); + System.out.printf("Alterando contacto: %s%n", contact); + System.out.print("Insira o novo telefone: "); + String phone = sin.nextLine(); + System.out.print("Insira o novo email: "); + String email = sin.nextLine(); + contact.setPhone(phone); + contact.setEmail(email); + } + } + case "3" -> { + System.out.print("Insira o nome, email ou telefone do contacto que pretende alterar: "); + String query = sin.nextLine(); + int[] indexes = searchContactsIndex(query); + System.out.printf("Contactos encontrados: %d%n", indexes.length); + + if (indexes.length == 0) { + System.out.println("Não foram encontrados contactos com esse nome, email ou telefone"); + } else if (indexes.length == 1) { + System.out.printf("Apagando contacto: %s%n", contacts[indexes[0]]); + Contact[] newContacts = new Contact[contacts.length - 1]; + System.arraycopy(contacts, 0, newContacts, 0, indexes[0]); + System.arraycopy(contacts, indexes[0] + 1, newContacts, indexes[0], contacts.length - indexes[0] - 1); + contacts = newContacts; + } else { + Contact contact = selectContact(indexes); + System.out.printf("Apagando contacto: %s%n", contact); + Contact[] newContacts = new Contact[contacts.length - 1]; + System.arraycopy(contacts, 0, newContacts, 0, contact.getId()); + System.arraycopy(contacts, contact.getId() + 1, newContacts, contact.getId(), contacts.length - contact.getId() - 1); + contacts = newContacts; + } + } + case "4" -> { + System.out.print("Insira o nome, email ou telefone do contacto que pretende alterar: "); + String query = sin.nextLine(); + int[] indexes = searchContactsIndex(query); + System.out.printf("Contactos encontrados: %d%n", indexes.length); + + if (indexes.length == 0) { + System.out.println("Não foram encontrados contactos com esse nome, email ou telefone"); + } else if (indexes.length == 1) { + System.out.println(contacts[indexes[0]]); + } else { + for (int index : indexes) + System.out.printf("%s%n", contacts[index]); + } + System.out.println(); + } + case "5" -> { + for (Contact contact : contacts) + System.out.printf("%s%n", contact); + System.out.println(); + } + default -> System.out.println("Opção inválida!"); + } + } + } + + private static boolean checkIfContactExists(int cc) { + for (Contact c : contacts) { + if (c.getPerson().getCc() == cc) { + System.out.print("Já existe um contacto para essa pessoa, pretende criar um novo? (s/n): "); + String answer = sin.nextLine(); + return answer.equals("s"); + } + } + return true; + } + + private static int[] searchContactsIndex(String query) { + int[] indexes = new int[contacts.length]; + int index = 0; + for (int i = 0; i < contacts.length; i++) { + if (contacts[i].getPerson().getName().contains(query) || contacts[i].getEmail().contains(query) || contacts[i].getPhone().contains(query)) { + indexes[index] = i; + index++; + } + } + return indexes; + } + + private static Contact selectContact(int[] indexes) { + System.out.println("Foram encontrados vários contactos com esse nome, email ou telefone, selecione um:"); + for (int index : indexes) + System.out.printf("%s%n", contacts[index]); + + Contact contact; + do { + System.out.print("Insira o ID do contacto que pretende alterar: "); + int index = sin.nextInt(); + contact = searchContactById(index); + } while (contact == null); + + return contact; + } + + private static Contact searchContactById(int id) { + for (Contact contact : contacts) { + if (contact.getId() == id) return contact; + } + return null; + } +} diff --git a/1ano/2semestre/poo/src/aula06/Person.java b/1ano/2semestre/poo/src/aula06/Person.java index 09e10a0..a259876 100644 --- a/1ano/2semestre/poo/src/aula06/Person.java +++ b/1ano/2semestre/poo/src/aula06/Person.java @@ -44,6 +44,6 @@ public class Person { @Override public String toString() { - return String.format("%s; CC: %d; Date de nascimento: %s", this.name, this.cc, this.birthDate); + return String.format("%s; CC: %d; Data de nascimento: %s", this.name, this.cc, this.birthDate); } }