From 0f507fa3c5c1e54cfc799a116070210ab2762c27 Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 16 Sep 2024 18:43:46 +0100 Subject: [PATCH 1/6] Update README.md --- README.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 17283ab6..06bb5a58 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,4 @@ # ContactBookGit -This is a starter kit for the first lab of the Software Engineering Course. -Fork this project to make your own version of it. - -You will notice some things need to be updated in this code. It was originally made with Java 8 and JUnit 4. The plan is for you to still use JUnit 4, but you can and should use a more recent version, like Java 21. You may also note a few other redundant things (e.g. imports). Fix whatever you need to. +Belmiro Antonio 58490 +Nasha Bagasse 60903 +Lourenço Cunha 62693 From 39d8c83aaf33245b370ecd0cc2261fecdc277b02 Mon Sep 17 00:00:00 2001 From: Nasha Date: Mon, 16 Sep 2024 21:24:58 +0100 Subject: [PATCH 2/6] GN function working, missing implementing message for user and method on switch (TODO) --- src/Main.java | 11 +++++++++++ src/contactBook/Contact.java | 4 ++++ src/contactBook/ContactBook.java | 20 +++++++++++++++++++- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index 2209d6fd..3eccbd50 100644 --- a/src/Main.java +++ b/src/Main.java @@ -10,6 +10,7 @@ public class Main { public static final String REMOVE_CONTACT = "RC"; public static final String GET_PHONE = "GP"; public static final String GET_EMAIL = "GE"; + public static final String GET_NAME = "GN"; //TODO put on switch getName public static final String SET_PHONE = "SP"; public static final String SET_EMAIL = "SE"; public static final String LIST_CONTACTS = "LC"; @@ -22,6 +23,7 @@ public class Main { public static final String CONTACT_REMOVED = "contactBook.Contact removed."; public static final String CONTACT_UPDATED = "contactBook.Contact updated."; public static final String BOOK_EMPTY = "contactBook.Contact book empty."; + public static final String PHONE_NOT_EXIST = ""; //TODO public static final String QUIT_MSG = "Goodbye!"; public static final String COMMAND_ERROR = "Unknown command."; @@ -113,6 +115,15 @@ private static void getEmail(Scanner in, ContactBook cBook) { else System.out.println(NAME_NOT_EXIST); } + private static void getName (Scanner in, ContactBook cBook){ + int number; + number = Integer.parseInt(in.nextLine()); + if(cBook.hasContactByPhone(number)){ + System.out.println(cBook.getName(number)); + } + else System.out.println(PHONE_NOT_EXIST); + } + private static void setPhone(Scanner in, ContactBook cBook) { String name; int phone; diff --git a/src/contactBook/Contact.java b/src/contactBook/Contact.java index 09e2acb7..84812052 100644 --- a/src/contactBook/Contact.java +++ b/src/contactBook/Contact.java @@ -35,4 +35,8 @@ public void setEmail(String email) { public boolean equals(Contact otherContact) { return name.equals(otherContact.getName()); } + + public boolean equalsPhone(Contact otherContact) { + return phone==otherContact.getPhone(); + } } diff --git a/src/contactBook/ContactBook.java b/src/contactBook/ContactBook.java index c00edcc4..62bdc7fa 100644 --- a/src/contactBook/ContactBook.java +++ b/src/contactBook/ContactBook.java @@ -1,6 +1,5 @@ package contactBook; -import contactBook.Contact; public class ContactBook { static final int DEFAULT_SIZE = 100; @@ -20,6 +19,10 @@ public boolean hasContact(String name) { return searchIndex(name) >= 0; } + public boolean hasContactByPhone(int phone) { + return searchIndexByPhone(phone) >= 0; + } + public int getNumberOfContacts() { return counter; } @@ -50,6 +53,8 @@ public String getEmail(String name) { return contacts[searchIndex(name)].getEmail(); } + public String getName(int phone){ return contacts[searchIndexByPhone(phone)].getName();} + //Pre: name != null && hasContact(name) public void setPhone(String name, int phone) { contacts[searchIndex(name)].setPhone(phone); @@ -73,6 +78,19 @@ private int searchIndex(String name) { return result; } + private int searchIndexByPhone(int phone) { + int i = 0; + int result = -1; + boolean found = false; + while (i Date: Tue, 17 Sep 2024 02:08:37 +0100 Subject: [PATCH 3/6] Update ContactBook.java --- src/contactBook/ContactBook.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/contactBook/ContactBook.java b/src/contactBook/ContactBook.java index c00edcc4..0b345fd1 100644 --- a/src/contactBook/ContactBook.java +++ b/src/contactBook/ContactBook.java @@ -93,4 +93,9 @@ public Contact next() { return contacts[currentContact++]; } + //Pre: index >= 0 && index < counter + public Contact getContact(int index) { + return contacts[index]; + } + } From 15e1c2d607a2310bdc635af7f5389e9919399539 Mon Sep 17 00:00:00 2001 From: Neil Date: Tue, 17 Sep 2024 02:09:27 +0100 Subject: [PATCH 4/6] Update Main.java --- src/Main.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/Main.java b/src/Main.java index 2209d6fd..da2e5fa9 100644 --- a/src/Main.java +++ b/src/Main.java @@ -147,4 +147,19 @@ private static void listAllContacts(ContactBook cBook) { } else System.out.println(BOOK_EMPTY); } + + private static void haveRepeatedContact(ContactBook cBook) { + boolean found = false; + for (int i = 0; i < cBook.getNumberOfContacts(); i++) { + for (int j = i + 1; j < cBook.getNumberOfContacts() && !found; j++) { + if (cBook.getContact(i).getPhone() == cBook.getContact(j).getPhone()) + found = true; + } + } + if (found) + System.out.println("There are contacts that share phone numbers."); + else + System.out.println("All contacts have different phone numbers."); + + } } From b833f23abaf51b85a13ea1570f4d805c31faa191 Mon Sep 17 00:00:00 2001 From: Lourenco Date: Tue, 17 Sep 2024 11:12:29 +0100 Subject: [PATCH 5/6] EP and GN commands added to the command reader. Added missing constants for both commands. --- src/Main.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/Main.java b/src/Main.java index 3eccbd50..ab00ba1a 100644 --- a/src/Main.java +++ b/src/Main.java @@ -10,10 +10,11 @@ public class Main { public static final String REMOVE_CONTACT = "RC"; public static final String GET_PHONE = "GP"; public static final String GET_EMAIL = "GE"; - public static final String GET_NAME = "GN"; //TODO put on switch getName + public static final String GET_NAME = "GN"; public static final String SET_PHONE = "SP"; public static final String SET_EMAIL = "SE"; public static final String LIST_CONTACTS = "LC"; + public static final String REPEATED_PHONES = "EP"; public static final String QUIT = "Q"; //Constantes que definem as mensagens para o utilizador @@ -23,7 +24,9 @@ public class Main { public static final String CONTACT_REMOVED = "contactBook.Contact removed."; public static final String CONTACT_UPDATED = "contactBook.Contact updated."; public static final String BOOK_EMPTY = "contactBook.Contact book empty."; - public static final String PHONE_NOT_EXIST = ""; //TODO + public static final String PHONE_NOT_EXIST = "Phone number does not exist."; + public static final String REPEATS_EXIST = "There are contacts that share phone numbers."; + public static final String REPEATS_NOT_EXIST = "All contacts have different phone numbers.";// public static final String QUIT_MSG = "Goodbye!"; public static final String COMMAND_ERROR = "Unknown command."; @@ -55,6 +58,12 @@ public static void main(String[] args) { case LIST_CONTACTS: listAllContacts(cBook); break; + case GET_NAME: + getName(in,cBook); + break; + case REPEATED_PHONES: + haveRepeatedContact(cBook); + break; default: System.out.println(COMMAND_ERROR); } @@ -115,7 +124,7 @@ private static void getEmail(Scanner in, ContactBook cBook) { else System.out.println(NAME_NOT_EXIST); } - private static void getName (Scanner in, ContactBook cBook){ + private static void getName(Scanner in, ContactBook cBook){ int number; number = Integer.parseInt(in.nextLine()); if(cBook.hasContactByPhone(number)){ From 66fc9cf34d44e33ead20dfa17b98365359a764f9 Mon Sep 17 00:00:00 2001 From: Lourenco Date: Tue, 17 Sep 2024 14:29:36 +0100 Subject: [PATCH 6/6] EP and GN are fully functional. Passes all tests. --- src/Main.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Main.java b/src/Main.java index 03ddaf6f..76bdaa8f 100644 --- a/src/Main.java +++ b/src/Main.java @@ -177,9 +177,9 @@ private static void haveRepeatedContact(ContactBook cBook) { } } if (found) - System.out.println("There are contacts that share phone numbers."); + System.out.println(REPEATS_EXIST); else - System.out.println("All contacts have different phone numbers."); + System.out.println(REPEATS_NOT_EXIST); } }