Skip to content

Instantly share code, notes, and snippets.

@YanchevskayaAnna
Created August 16, 2016 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YanchevskayaAnna/4bf5923cd11e23cbf531bd9773365185 to your computer and use it in GitHub Desktop.
Save YanchevskayaAnna/4bf5923cd11e23cbf531bd9773365185 to your computer and use it in GitHub Desktop.
package Level_2.week_1.ContactListApplication;
/**
* Created by pc on 08.08.2016.
*/
public class ContactList {
private Contact[] contactList;
private int busyPlaces;
public void createContactList(){
contactList = new Contact[100];
}
public boolean addContact(Contact contact){
if (!contactValidation(contact)) {
System.out.println("Wrong validation for contact with name : " + contact.getName());
return false;
}
if (!checkUniqueName(contact)) {
System.out.println(contact.getName() + " not unique");
return false;
}
expandContactList();
contactList[busyPlaces++] = contact;
return true;
}
public boolean removeContact(String contactName){
int contactPosition = findContactIndex(contactName);
if (contactPosition == -1) {
return false;
}
contactList[contactPosition] = null;
busyPlaces--;
if (contactPosition != contactList.length - 1) {
System.arraycopy(contactList, contactPosition + 1, contactList, contactPosition, contactList.length - (contactPosition + 1)); //todo busyPlaces - (contactPosition + 1)
}
return true;
}
public boolean updateContactInfo(String contactName, String newContactData){
if (!contactValidation(newContactData)) {
System.out.println("contact with name: " + contactName + " doesn`t exist");
return false;
}
if (!checkUniqueName(newContactData)) {
System.out.println(newContactData + " not unique");
return false;
}
int contactPosition = findContactIndex(contactName);
if (contactPosition != -1) {
contactList[contactPosition].setName(newContactData);
return true;
}
return false;
}
public boolean checkUniqueName(Contact contact){
return checkUniqueName(contact.getName());
}
public boolean checkUniqueName(String contactName){
return findContactIndex(contactName) == -1;
}
public int findContactIndex(String contactName) {
for (int i = 0; i < busyPlaces; i++) {
if (contactList[i].getName().equals(contactName)) {
return i;
}
}
return -1;
}
public Contact findContact(String contactName) {
for (int i = 0; i < busyPlaces; i++)
return contactList[i].getName().equals(contactName) ? contactList[i] : null;
return null;
}
public void expandContactList(){
if (busyPlaces >= contactList.length) {
System.out.println("need to expand contact list");
Contact[] newContactList = new Contact[contactList.length * 2];
System.arraycopy(contactList, 0, newContactList, 0, contactList.length);
contactList = newContactList;
}
}
public String showLast(int count){
StringBuilder resultContacts = new StringBuilder();
int counter = 0;
if (validateNumber(count)) {//todo change the condition, check for busyPlases
for (int i = busyPlaces - 1; i >= 0; i--) { //todo for (int i = busyPlaces - 1; i > busyPlaces - count; i--)
resultContacts.append(" " + i + "| " + contactList[i].toString() + "\n");
counter++; //todo waste variable
if (counter == count) {
return resultContacts.toString();
} else if (counter < count && i == 0) {
return String.valueOf(resultContacts.append("contactList contains less than" + count + " contacts: \n" + resultContacts)); //todo waste inspection, you already checked if (validateNumber(count)) {
}
}
}
return "number not valid";
}
public String showFirst(int count){
StringBuilder resultContacts = new StringBuilder();
if (validateNumber(count)) {
for (int i = 0; i < count; i++) {
if (contactList[i] == null) {
return String.valueOf(resultContacts.append(" contact not exist \n"));
} else {
resultContacts.append(" " + i + "| " + contactList[i].toString() + "\n");
}
}
}
return "number not valid";
}
public String showALLContactList(){
StringBuilder resultContacts = new StringBuilder();
for (int i = 0; i < contactList.length; i++) {
if (contactList[i] != null) {
resultContacts.append(" " + i + "| "+ contactList[i].toString() + "\n");
} else {
resultContacts.append(i + "| contact not exist \n");
}
}
return resultContacts.toString().trim();
}
public StringBuilder showContactsByOperator(String operator){
StringBuilder result = new StringBuilder("");
if (operator == "LIFE") {
for (int j = 0; j < busyPlaces; j++) {
if (contactList[j].getOperator() == "LIFE") {
result.append(contactList[j].toString() + " \n");
}
}
} else {
if (operator == "KIEVSTAR") {
for (int j = 0; j < busyPlaces; j++) {
if (contactList[j].getOperator() == "KIEVSTAR") {
result.append(contactList[j].toString() + " \n");
}
}
} else {
result.append("not exist");
}
}
return result;
}
public boolean contactValidation(Contact contact){
return (contact.getName() == null || contact.getSurname() == null) ? false :
(!contact.getName().matches("^\\D*$")) || (!contact.getSurname().matches("^\\D*$")) ? false : true;
}
public boolean contactValidation(String contactName){
return (contactName == null) ? false : !(contactName.matches("^\\D*$")) ? false : true;
}
public boolean validateNumber(int value){
if (0 < value && value <= contactList.length) {
return true;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment