Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Final Status #91

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
35 changes: 35 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +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";
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
Expand All @@ -22,6 +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 = "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.";

Expand Down Expand Up @@ -53,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);
}
Expand Down Expand Up @@ -113,6 +124,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;
Expand Down Expand Up @@ -147,4 +167,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(REPEATS_EXIST);
else
System.out.println(REPEATS_NOT_EXIST);

}
}
4 changes: 4 additions & 0 deletions src/contactBook/Contact.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
25 changes: 24 additions & 1 deletion src/contactBook/ContactBook.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package contactBook;

import contactBook.Contact;

public class ContactBook {
static final int DEFAULT_SIZE = 100;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
Expand All @@ -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<counter && !found)
if (phone == (contacts[i].getPhone()))
found = true;
else
i++;
if (found) result = i;
return result;
}

private void resize() {
Contact tmp[] = new Contact[2*contacts.length];
for (int i=0;i<counter; i++)
Expand All @@ -93,4 +111,9 @@ public Contact next() {
return contacts[currentContact++];
}

//Pre: index >= 0 && index < counter
public Contact getContact(int index) {
return contacts[index];
}

}