Created
August 9, 2016 19:12
-
-
Save YanchevskayaAnna/4f89e13609cf638f1879aa76b757ec07 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Level_2.week_1.ContactListApplication; | |
/** | |
* Created by pc on 08.08.2016. | |
*/ | |
public class ContactList { | |
/* Yanchevskaya A. основное замечание: | |
1.Насколько я поняла код: добавляем в первое свободное место, | |
Свободное место может быть, так как при удалении сейчас остаются пустые ячейки | |
Нужно: при удалении сдвигать оставшиеся справа элементы влево на 1 позицию. | |
Сейчас при удалении ты что-то подобное делаешь с элементами, но это не сдвиг на одну позицию. | |
Например, тест | |
contactList.removeContact("nameFirstA"); | |
System.out.println(contactList.showSomeContacts("ALL")); | |
показывает, что сейчас при удалении нет сдвига влево на 1 позицию | |
Сдвиг лучше всего делать с помощью System.ArrayCopy | |
Что это даст: | |
1.1. Добавлять можно будет всегда в конец и не искать позицию: просто contactList[ busyPlaces++] = contact | |
1.2. Можно будет реализовать метод removeLast, сейчас нужно хранить инддекс последней ячейки, в которую добавляли, иначе не получится | |
1.3 checkPresentContactInContactList - станет лишняя, так как по busyPlaces можно будет понять | |
2. Методы add, remove лучше сделать boolean | |
*/ | |
Contact[] contactList; //Yanchevskaya A. поле private? | |
private int contactPosition; //Yanchevskaya A. зачем это поле на уровне класса? | |
private int busyPlaces; | |
private String showFirstFiveContacts = "FIRST_FIVE"; //Yanchevskaya A. лучше сделать отдельными методами. | |
private String showLastFiveContacts = "LAST_FIVE"; | |
private String showALLContacts = "ALL"; | |
private String showLifeContacts = "LIFE"; | |
private String showKievstarContacts = "KIEVSTAR"; | |
public void createContactList(){ | |
contactList = new Contact[100]; | |
} | |
public Contact[] addContact(Contact contact){ //Yanchevskaya A. Зачем возвращать весь массив? public boolean addContact(Contact contact) - лучше вернуть boolean - добавили мили нет | |
expandContactList(); | |
if (contactValidation(contact) == true) { | |
if (checkUniqueName(contact) == true) { | |
for (int i = 0; i < contactList.length; i++) { | |
if (contactList[i] == null) { //Yanchevskaya A. см. п.1 вначале | |
contactList[i] = contact; | |
busyPlaces++; | |
break; | |
} | |
} | |
} else | |
System.out.println(contact.getName() + " not unique"); | |
} else { | |
System.out.println("Wrong validation for contact with name : " + contact.getName()); | |
} | |
return contactList; | |
} | |
public Contact[] removeContact(String contactName){//Yanchevskaya A. Зачем возвращать весь массив? public boolean removeContact(...) - лучше вернуть boolean - удалили или нет | |
if (checkPresentContactInContactList() == true) { //Yanchevskaya A. Можно упрощать код if (checkPresentContactInContactList()) { т.е. писать без "==true" | |
int contactPosition = findContactPositionInContactList(contactName); | |
contactList[contactPosition] = null; | |
if (contactPosition != contactList.length - 1) { | |
for (int j = contactPosition + 1; j < contactList.length; j++) { | |
if (contactList[j] != null) { //Yanchevskaya A. см. п.1 вначале | |
contactList[contactPosition] = contactList[j]; | |
} | |
} | |
} | |
busyPlaces--; | |
System.out.println("contact with name : " + contactName + " removed"); | |
} | |
return contactList; | |
} | |
// update name for contact | |
public Contact[] updateContactInfo(String contactName, String newContactData){ | |
if (contactValidation(newContactData) == true) { | |
int contactPosition = findContactPositionInContactList(contactName); | |
contactList[contactPosition].setName(newContactData); //Yanchevskaya A. А если не нашли позицию? | |
} | |
return contactList; | |
} | |
// update contact name to new contact name | |
// public Contact[] updateContactInfo(int contactPosition, String newContactName){ | |
// | |
// if (contactValidation(newContactName) == true) { | |
// for (int i = 0; i < contactList.length; i++) { | |
// Contact con = contactList[contactPosition]; | |
// con.getName() = newContactName; | |
// } | |
// } | |
// return contactList; | |
// } | |
public boolean checkUniqueName(Contact contact){ | |
boolean uniqueNameResult = false; | |
if (checkPresentContactInContactList() == true) { | |
for (int i = 0; i < busyPlaces; i++) { | |
if (contactList[i].getName() == contact.getName()) { | |
uniqueNameResult = false; | |
break; | |
} else { | |
uniqueNameResult = true; | |
} | |
} | |
} else { | |
uniqueNameResult = true; | |
} | |
return uniqueNameResult; | |
} | |
// find contact position in Contact list by contact name | |
public int findContactPositionInContactList(String contactName) { | |
int counter = 0; | |
if (checkPresentContactInContactList() == true) { | |
for (int i = 0; i < busyPlaces; i++) { //Yanchevskaya A. тут счетчик не по всему массиву, хотя элементы сейчас могут быть разбросаны по всему | |
String name = contactList[i].getName(); | |
if (name == contactName) { | |
contactPosition = i; | |
counter++; //Yanchevskaya A. надо сразу делать return i. Переменная counter лишняя | |
} | |
} | |
if (counter == 0) { | |
System.out.println("contact with name: " + contactName + " doesn`t exist"); | |
} | |
} | |
return contactPosition; | |
} | |
// find and show contact info by contact name | |
public String findContactInContactList(String contactName) { | |
if (checkPresentContactInContactList() == true) { | |
for (int i = 0; i < contactList.length; i++) | |
return contactList[i].getName() == contactName ? contactList[i].toString() : "contact doesn`t exist"; | |
} | |
return "contact doesn`t exist"; | |
} | |
public boolean checkPresentContactInContactList(){ | |
int counter = 0; | |
for (int i = 0; i < contactList.length; i++) { | |
if (contactList[i] != null) { | |
counter++; | |
break; | |
} | |
} | |
if (counter == 0) { | |
System.out.println("contact list empty"); | |
return false; | |
} else | |
return true; | |
} | |
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; | |
} | |
} | |
/* return result depends on String number: | |
can be: LAST_FIVE - return last five contacts | |
FIRST_FIVE - return string with first five contacts | |
ALL - return all Contacts list | |
*/ | |
public String showSomeContacts(String number){ //Yanchevskaya A нужно переделать после основных замечаний | |
String resultContacts = ""; | |
int counter = 0; | |
if (number == showLastFiveContacts) { | |
for (int i = 0; i < contactList.length; i++) { | |
int lastPosition = contactList.length - i - 1; | |
if (contactList[lastPosition] == null) { | |
i++; | |
} else { | |
resultContacts += contactList[lastPosition].toString() + "\n"; | |
counter++; | |
if (counter == 5) { | |
resultContacts += " " + contactList[i].toString() + "\n"; | |
} else { | |
if (counter < 5 && i == lastPosition) { | |
resultContacts = "contactList contains less than 5 contacts: \n" + resultContacts; | |
} | |
} | |
} | |
} | |
} else { | |
if (number == showFirstFiveContacts) { | |
for (int i = 0; i < 5; i++) { | |
if (contactList[i] == null) { | |
resultContacts += " contact not exist \n"; | |
} else { | |
resultContacts += " " + contactList[i].toString() + "\n"; | |
} | |
} | |
} else { | |
if (number == showALLContacts) { | |
for (int i = 0; i < busyPlaces; i++) { | |
if (contactList[i] == null) { | |
resultContacts += " contact not exist \n"; | |
} else { | |
resultContacts += " " + contactList[i].toString() + "\n"; | |
} | |
} | |
} | |
} | |
} | |
return resultContacts; | |
} | |
public String showContactsByOperator(String operator){ | |
String result = ""; //Yanchevskaya A. если уже учили, лучше StringBuilder | |
if (operator == showLifeContacts) { //Yanchevskaya A. Можно сначала перед циклом сделать проверку оператора, а потом просто цикл поиска | |
for (int j = 0; j < busyPlaces; j++) { | |
if (contactList[j].getOperator() == operator) { | |
result += contactList[j].toString() + " \n"; | |
} | |
} | |
} else { | |
if (operator == showKievstarContacts) { | |
for (int j = 0; j < busyPlaces; j++) { | |
if (contactList[j].getOperator() == operator) { | |
result += contactList[j].toString() + " \n"; | |
} | |
} | |
} | |
} | |
if (result == "") { | |
result = operator + " not exist"; | |
} | |
return result; | |
} | |
// check validation for contact name and contact surname | |
public boolean contactValidation(Contact contact){ | |
return (contact.getName() == null || contact.getSurname() == null) ? false : | |
(!contact.getName().matches("^\\D*$")) || (!contact.getSurname().matches("^\\D*$")) ? false : true; | |
} | |
// check all symbols 7sdf463dfg276 | |
public boolean contactValidation(String contactName){ | |
return (contactName == null) ? false : !(contactName.matches("^\\D*$")) ? false : true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment