[POO] refactoring and validation changes

This commit is contained in:
TiagoRG 2023-03-27 13:47:28 +01:00
parent efe3159d63
commit 92ee5f7397
Signed by untrusted user who does not match committer: TiagoRG
GPG Key ID: DFCD48E3F420DB42
2 changed files with 129 additions and 113 deletions

View File

@ -9,7 +9,7 @@ public class Contact {
private static int currentId = 1; private static int currentId = 1;
public Contact(Person person, String email, String phone) { public Contact(Person person, String email, String phone) {
if (email == null || email.isEmpty() || phone == null || phone.isEmpty()) if ((email == null || email.isEmpty()) && (phone == null || phone.isEmpty()))
throw new IllegalArgumentException("Either email or phone must be provided"); throw new IllegalArgumentException("Either email or phone must be provided");
this.id = Contact.currentId++; this.id = Contact.currentId++;
this.setPerson(person); this.setPerson(person);
@ -34,7 +34,8 @@ public class Contact {
return email; return email;
} }
public void setEmail(String email) { public void setEmail(String email) {
if (!email.matches("^[a-zA-Z_0-9.]+@[a-zA-Z_0-9.]+\\.[a-zA-Z_0-9]+$")) if (!(email == null || email.isEmpty()) &&
!email.matches("^[a-zA-Z_0-9.]+@[a-zA-Z_0-9.]+\\.[a-zA-Z_0-9]+$"))
throw new IllegalArgumentException("Invalid email"); throw new IllegalArgumentException("Invalid email");
this.email = email; this.email = email;
} }
@ -43,7 +44,8 @@ public class Contact {
return phone; return phone;
} }
public void setPhone(String phone) { public void setPhone(String phone) {
if (!phone.matches("^9[0-9]{8}$")) if (!(phone == null || phone.isEmpty()) &&
!phone.matches("^9[0-9]{8}$"))
throw new IllegalArgumentException("Invalid phone"); throw new IllegalArgumentException("Invalid phone");
this.phone = phone; this.phone = phone;
} }

View File

@ -11,6 +11,24 @@ public class ContactList {
public static void main(String[] args) { public static void main(String[] args) {
while (true) { while (true) {
String option = menu();
switch (option) {
case "0" -> {
sin.close();
System.exit(0);
}
case "1" -> addContact();
case "2" -> changeContact();
case "3" -> deleteContact();
case "4" -> searchContact();
case "5" -> listContacts();
default -> System.out.println("Opção inválida!");
}
}
}
// Menu methods
private static String menu() {
System.out.println("Selecione uma opção:"); System.out.println("Selecione uma opção:");
System.out.println("1. Inserir contacto"); System.out.println("1. Inserir contacto");
System.out.println("2. Alterar contacto"); System.out.println("2. Alterar contacto");
@ -19,19 +37,15 @@ public class ContactList {
System.out.println("5. Listar contactos"); System.out.println("5. Listar contactos");
System.out.println("0. Sair"); System.out.println("0. Sair");
System.out.print("> "); System.out.print("> ");
String option = sin.nextLine(); return sin.nextLine();
switch (option) {
case "0" -> {
sin.close();
System.exit(0);
} }
case "1" -> { private static void addContact() {
System.out.print("Insira o nome: "); System.out.print("Insira o nome: ");
String name = sin.nextLine(); String name = sin.nextLine();
System.out.print("Insira o cc: "); System.out.print("Insira o cc: ");
String ccStr = sin.nextLine(); String ccStr = sin.nextLine();
int cc = Integer.parseInt(ccStr); int cc = Integer.parseInt(ccStr);
System.out.print("Insira a data de nascimento (formado: dd-mm-yyyy): "); System.out.print("Insira a data de nascimento (formato: dd-mm-yyyy): ");
String date = sin.nextLine(); String date = sin.nextLine();
String[] dateParts = date.split("-"); String[] dateParts = date.split("-");
DateYMD birthDate = new DateYMD(Integer.parseInt(dateParts[0]), Integer.parseInt(dateParts[1]), Integer.parseInt(dateParts[2])); DateYMD birthDate = new DateYMD(Integer.parseInt(dateParts[0]), Integer.parseInt(dateParts[1]), Integer.parseInt(dateParts[2]));
@ -47,14 +61,14 @@ public class ContactList {
contacts = new Contact[1]; contacts = new Contact[1];
contacts[0] = contact; contacts[0] = contact;
} else { } else {
if (checkIfContactExists(cc)) break; if (checkIfContactExists(cc)) return;
Contact[] newContacts = new Contact[contacts.length + 1]; Contact[] newContacts = new Contact[contacts.length + 1];
System.arraycopy(contacts, 0, newContacts, 0, contacts.length); System.arraycopy(contacts, 0, newContacts, 0, contacts.length);
newContacts[contacts.length] = contact; newContacts[contacts.length] = contact;
contacts = newContacts; contacts = newContacts;
} }
} }
case "2" -> { private static void changeContact() {
System.out.print("Insira o nome, email ou telefone do contacto que pretende alterar: "); System.out.print("Insira o nome, email ou telefone do contacto que pretende alterar: ");
String query = sin.nextLine(); String query = sin.nextLine();
int[] indexes = searchContactsIndex(query); int[] indexes = searchContactsIndex(query);
@ -81,7 +95,8 @@ public class ContactList {
contact.setEmail(email); contact.setEmail(email);
} }
} }
case "3" -> {
private static void deleteContact() {
System.out.print("Insira o nome, email ou telefone do contacto que pretende alterar: "); System.out.print("Insira o nome, email ou telefone do contacto que pretende alterar: ");
String query = sin.nextLine(); String query = sin.nextLine();
int[] indexes = searchContactsIndex(query); int[] indexes = searchContactsIndex(query);
@ -104,7 +119,8 @@ public class ContactList {
contacts = newContacts; contacts = newContacts;
} }
} }
case "4" -> {
private static void searchContact() {
System.out.print("Insira o nome, email ou telefone do contacto que pretende alterar: "); System.out.print("Insira o nome, email ou telefone do contacto que pretende alterar: ");
String query = sin.nextLine(); String query = sin.nextLine();
int[] indexes = searchContactsIndex(query); int[] indexes = searchContactsIndex(query);
@ -120,22 +136,20 @@ public class ContactList {
} }
System.out.println(); System.out.println();
} }
case "5" -> {
for (Contact contact : contacts) private static void listContacts() {
System.out.printf("%s%n", contact); for (Contact c : contacts)
System.out.printf("%s%n", c);
System.out.println(); System.out.println();
} }
default -> System.out.println("Opção inválida!");
}
}
}
// Auxiliary methods
private static boolean checkIfContactExists(int cc) { private static boolean checkIfContactExists(int cc) {
for (Contact c : contacts) { for (Contact c : contacts) {
if (c.getPerson().getCc() == cc) { if (c.getPerson().getCc() == cc) {
System.out.print("Já existe um contacto para essa pessoa, pretende criar um novo? (s/n): "); System.out.print("Já existe um contacto para essa pessoa, pretende criar um novo? (s/n): ");
String answer = sin.nextLine(); String answer = sin.nextLine();
return answer.equals("s"); return !answer.equals("s");
} }
} }
return true; return true;