Skip to content

Instantly share code, notes, and snippets.

@YanchevskayaAnna
Created August 17, 2016 18:55
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/7219d3bef520a5fb68a48f675dd5a9ce to your computer and use it in GitHub Desktop.
Save YanchevskayaAnna/7219d3bef520a5fb68a48f675dd5a9ce to your computer and use it in GitHub Desktop.
package newcontactlist;
import java.util.*;
/**
* ContactList class
*/
//public class ContactList extends Contact {
public class ContactList{
public static final String MTS = "095"; //todo выношу в константы
public static final String KievStar = "097";
// ArrayList<Contact> contactList = new ArrayList<Contact>();
List<Contact> contactList = new ArrayList<Contact>(); //todo добавляем полиморфизм
/* public ArrayList<Contact> addToTheContactList(ArrayList<Contact> contactList, Contact contact) {
contactList.add(contact);
return contactList;
}*/
public boolean addToTheContactList(Contact contact) { //todo этот метод не static, его можно вызвать только через точку от какого-то конкретного контакт листа, поэтому contactList передавать как параметр не нужно
return contactList.add(contact);
}
/**
* Show all list
*/
/* public void contactListShow(ArrayList<Contact> contactList) {
for (int i = 0; i < contactList.size(); i++) {
contactList.get(i).contactShow();
}
}*/
public void contactListShow() {
for (int i = 0; i < contactList.size(); i++) {
contactList.get(i).contactShow();
}
}
/**
* Show first five
*/
/* public void contactListShowFirstFive(ArrayList<Contact> contactList) {
for (int i = 0; i < 5; i++) {
contactList.get(i).contactShow();
}
}*/
public void contactListShowFirst(int count) {
if (count <= 0 || count < contactList.size()) {
System.out.println("incorrect count");
return;
};
for (int i = 0; i < count; i++) {
contactList.get(i).contactShow();
}
}
/**
* Show last five
*/
/*public void contactListShowLastFive(ArrayList<Contact> contactList) {
for (int i = contactList.size() - 5; i < contactList.size(); i++) {
contactList.get(i).contactShow();
}
}*/
public void contactListShowLastFive() {
for (int i = contactList.size() - 5; i < contactList.size(); i++) {
contactList.get(i).contactShow();
}
}
/**
* Show all MTC. If not found show special message
*/
public String contactListShowMTC(ArrayList<Contact> contactList) {
int resultCounter = 0;
for (int i = 0; i < contactList.size(); i++) {
if (contactList.get(i).getPhoneNumber().substring(0, 3).contains(MTS)) {
contactList.get(i).contactShow();
} else resultCounter++;
}
if (resultCounter == contactList.size()) {
System.out.println("MTS contacts not found.");
}
return null;
}
public void contactListShowOperator(String operator) { //todo можно объединить в один метод
if (!((operator != null) && (operator.equals("MTC") || operator.equals("Kievstar")))){
System.out.println("unknown operator");;
}
String code = "";
if (operator.equals("MTC")) code = MTS; else code = KievStar;
int resultCounter = 0;
for (int i = 0; i < contactList.size(); i++) {
if (contactList.get(i).getPhoneNumber().substring(0, 3).contains(code)) {
contactList.get(i).contactShow();
} else resultCounter++;
}
if (resultCounter == contactList.size()) {
System.out.println("operator's contacts not found.");
}
}
/**
* Show all Kievstar. If not found show special message
*/
public String contactListShowKievstar(ArrayList<Contact> contactList) {
int resultCounter = 0;
for (int i = 0; i < contactList.size(); i++) {
if (contactList.get(i).getPhoneNumber().substring(0, 3).contains(KievStar)) {
contactList.get(i).contactShow();
} else resultCounter++;
}
if (resultCounter == contactList.size()) {
System.out.println("Kievstar contacts not found.");
}
return null;
}
/**
* Remove last contact from list
*/
/* public ArrayList<Contact> removeLastContact(ArrayList<Contact> contactList) {
contactList.remove(contactList.size() - 1); //todo Это не последний контакт
return contactList;
}*/
public boolean removeLastContact() {
return contactList.remove(contactList.size() - 1) != null;
}
/**
* Find by multiparameters. If not found show special message
*/
public List<Contact> findByFindParam(String findParam) { //todo List<Contact> вместо ArrayList<Contact>
int resultCounter = 0;
List<Contact> find = new ArrayList<Contact>();
for (int i = 0; i < contactList.size(); i++) {
if (contactList.get(i).getName().contains(findParam) || contactList.get(i).getPhoneNumber().contains(findParam)) {
//System.out.println("По критерию " + findParam + " найден контакт:");
//contactList.get(i).contactShow();
find.add(contactList.get(i));
} else resultCounter++;
}
if (resultCounter == contactList.size()) {
System.out.println("По параметру " + findParam + " контакты не найдены.");
}
return find;
}
/**
* Remove by delParam. If not found show special message
*/
public boolean removeByDelParam(String delParam) {
int resultCounter = 0;
for (int i = 0; i < contactList.size(); i++) {
if (contactList.get(i).getName().contains(delParam) || contactList.get(i).getPhoneNumber().contains(delParam)) {
contactList.remove(i); //todo сразу удалять нельзя! надо делать временный массив и потом вызывать метод contactList.removeAll
} else resultCounter++;
}
if (resultCounter == contactList.size()) {
System.out.println("По параметру " + delParam + " контакты не найдены.");
return false;
}
return true;
}
/**
* Update data by updateParam. If not found show special message
*/
public boolean updateByParam( String updateParam) { //todo
Scanner sc = new Scanner(System.in);
int resultCounter = 0;
for (int i = 0; i < contactList.size(); i++) {
if (contactList.get(i).getName().contains(updateParam) || contactList.get(i).getPhoneNumber().contains(updateParam)) {
System.out.println("Введите новое имя для " + contactList.get(i).name + ":");
contactList.get(i).nameInput(sc);
contactList.get(i).validatorName(contactList.get(i).name); //todo можно объединить эти два метода, один без другого не имеет смысла?
System.out.println("ВВедите новый номер вместо " + contactList.get(i).getPhoneNumber() + ":");
contactList.get(i).inputPhoneNamber(sc);
contactList.get(i).validatorPhone(contactList.get(i).getPhoneNumber());
} else resultCounter++;
}
if (resultCounter == contactList.size()) {
System.out.println("По параметру " + updateParam + " контакты не найдены.");
return false;
}
return true;
}
/**
* Метод для ввода имени контакта с консоли
*/
/* public Contact contactAddFromConsole(){ //todo метод не относится к контакт листу, поэтому убираю его и переношу в тест
Scanner sc = new Scanner(System.in);
System.out.println("\nДля записи имени используйте символы a-z, A-Z, 0-9, _\n" +
"Длина имени от 3 до 15 символов с учетом пробелов. Пример: Vasa Pyatochkin");
nameInput(sc);
validatorName(getName());
System.out.println("\nДля записи номера используйте цифры 0-9.\n" +
"Длина номера от 7 до 14 символов с учетом пробелов. Допускаются проблелы. Пример: 097 777 77 77");
inputPhoneNamber(sc);
validatorPhone(getPhoneNumber());
return new Contact(getName(), getPhoneNumber());
}*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment