Created
August 19, 2016 10:37
-
-
Save YanchevskayaAnna/68983518e1b40dbf924799b06dfec907 to your computer and use it in GitHub Desktop.
ContactList_Jack
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 com.jss.newcontactlist; | |
import java.util.Scanner; | |
import java.util.regex.*; | |
/**Изменены некоторіе методі по рекомендациям Ани | |
* Не передаем в методы аргументом лист, а юзаем нестатик метод напрямую от инстанса объекта класса ContactList | |
*/ | |
/** | |
* @autor Jack: | |
* email: jackkobec@gmail.com | |
* Skype: skypejs77 | |
* @version 3.1 Anna Edition | |
*/ | |
/** | |
* Contact class | |
*/ | |
public class Contact { | |
private String name; | |
private String phoneNumber; | |
/** | |
* Constructors | |
*/ | |
public Contact() { | |
} | |
public Contact(String name, String phoneNumber) { | |
this.name = name; | |
this.phoneNumber = phoneNumber; | |
} | |
/** | |
* Getters \ setters | |
*/ | |
public String getName() { | |
return name; | |
} | |
public String getPhoneNumber() { | |
return phoneNumber; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public void setPhoneNumber(String phoneNumber) { | |
this.phoneNumber = phoneNumber; | |
} | |
/** | |
* Format contact info to template and show | |
*/ | |
public String contactShow() { //todo метод ничего не возвращает, тогда это void | |
System.out.println(String.format("\nname: %s\nphoneNamber: %s ", name, phoneNumber | |
+ "\n------------------------")); | |
return ""; | |
} | |
/** | |
* Input contact name from console | |
*/ | |
public void nameInput(Scanner sc) { //todo я бы вынесла методвы свзаннst c вводом с консоли в тесты, они к сути объекта не относятся | |
System.out.print("\n>>>Enter contact name: "); | |
setName(sc.nextLine()); | |
} | |
/** | |
* Input contact phone from console | |
*/ | |
public void inputPhoneNamber(Scanner sc) { | |
System.out.print("\n>>>Enter contact phone: "); | |
setPhoneNumber(sc.nextLine()); | |
} | |
/** | |
* Валидатор имени символы a-z, A-Z, 0-9, длина 3-15 | |
*/ | |
public void validatorName(String name) { //todo валидацию можно сделать без сканера и использовать в setName и в тестах | |
Scanner sc = new Scanner(System.in); | |
while (!checkName()) { | |
System.out.println("Не правильно введено имя. Для записи имени используйте символы a-z, A-Z, 0-9, _\n" + | |
"Длина имени от 3 до 15 символов с учетом пробелов. Пример: Vasa Pyatochkin"); | |
nameInput(sc); | |
} | |
} | |
/** | |
* Валидатор иномера телефона символы -9, длина 7-14 | |
*/ | |
public void validatorPhone(String phoneNamber) { | |
Scanner sc = new Scanner(System.in); | |
while (!checkPhoneNamber()) { | |
System.out.println("Не правильно введен номер телефона. Для записи номера используйте цифры 0-9.\n" + | |
"Длина номера от 7 до 14 символов с учетом пробелов. Допускаются проблелы. Пример: 093 772 17 49"); | |
inputPhoneNamber(sc); | |
} | |
} /** | |
* Патерн валидации имени | |
*/ | |
public boolean checkName() { | |
Pattern p = Pattern.compile("^[\\sA-Za-z0-9_]{3,15}$"); | |
Matcher m = p.matcher(name); | |
return m.matches(); | |
} | |
/** | |
* Патерн валидации номера телефона | |
*/ | |
public boolean checkPhoneNamber() { | |
Pattern p = Pattern.compile("^[\\s0-9\\s]{7,14}$"); | |
Matcher m = p.matcher(getPhoneNumber()); | |
return m.matches(); | |
} | |
public Contact contactAddFromConsole(){ | |
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()); | |
} | |
} | |
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 com.jss.newcontactlist; | |
import java.util.*; | |
/** | |
* ContactList class атикпате | |
*/ | |
public class ContactList { | |
public static final String MTS = "095"; | |
public static final String KievStar = "097"; | |
//ArrayList<Contact> contactList = new ArrayList<Contact>(); | |
List<Contact> contactList = new ArrayList<>(); // ромбовидная запись из JDK 7 | |
public boolean addToTheContactList(Contact contact) { | |
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(); | |
} | |
}*/ | |
/** | |
* Show contacts by count | |
*/ | |
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() { //todo сделать универсально как в contactListShowFirst и добавить проверку на count | |
for (int i = contactList.size() - 5; i < contactList.size(); i++) { //todo size может быть и меньше 5 и будет exception | |
contactList.get(i).contactShow(); | |
} | |
} | |
/** | |
* Joined method for show operators contact by incoming code | |
*/ | |
public void contactListShowOperator(String operator) { | |
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 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; | |
} | |
/** | |
* 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); | |
return contactList; | |
}*/ | |
public boolean removeLastContact() { | |
return contactList.remove(contactList.size() - 1) != null; //todo будет ошибка, если контакт лист пустой, добавить проверку | |
} | |
/** | |
* Find by multiparameters. If not found show special message | |
*/ | |
/* public ArrayList<Contact> findByFindParam(ArrayList<Contact> contactList, String findParam) { | |
int resultCounter = 0; | |
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(); | |
} else resultCounter++; | |
} | |
if (resultCounter == contactList.size()) { | |
System.out.println("По параметру " + findParam + " контакты не найдены."); | |
} | |
return contactList; | |
}*/ | |
public List<Contact> findByFindParam(String findParam) { | |
int resultCounter = 0; | |
List<Contact> find = new ArrayList<>(); | |
for (int i = 0; i < contactList.size(); i++) { | |
if (contactList.get(i).getName().contains(findParam) || contactList.get(i).getPhoneNumber().contains(findParam)) { | |
find.add(contactList.get(i)); | |
find.get(i).contactShow(); | |
} else resultCounter++; | |
} | |
if (resultCounter == contactList.size()) { | |
System.out.println("По параметру " + findParam + " контакты не найдены."); | |
} | |
return find; | |
} | |
/** | |
* Remove by delParam. If not found show special message.// | |
*/ | |
/* public ArrayList<Contact> removeByDelParam(ArrayList<Contact> contactList, String delParam) {//todo while instead if | |
int resultCounter = contactList.size(); | |
for (int i = 0; i < contactList.size(); i++) { | |
while (contactList.get(i).getName().contains(delParam) || contactList.get(i).getPhoneNumber().contains(delParam)) { | |
contactList.remove(i); | |
} | |
} | |
if (resultCounter == contactList.size()) { | |
System.out.println("По параметру " + delParam + " контакты не найдены."); | |
} | |
return contactList; | |
}*/ | |
public boolean removeByDelParam(String delParam) { | |
int resultCounter = contactList.size(); | |
for (int i = 0; i < contactList.size(); i++) { | |
while (contactList.get(i).getName().contains(delParam) || contactList.get(i).getPhoneNumber().contains(delParam)) { | |
contactList.remove(i); | |
} | |
} | |
if (resultCounter == contactList.size()) { | |
System.out.println("По параметру " + delParam + " контакты не найдены."); | |
return false; | |
} | |
return true; | |
} | |
/** | |
* Update data by updateParam. If not found show special message | |
*/ | |
/* public ArrayList<Contact> updateByParam(ArrayList<Contact> contactList, String updateParam) { | |
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).getName() + ":"); | |
contactList.get(i).nameInput(sc); | |
contactList.get(i).validatorName(contactList.get(i).getName()); | |
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 contactList; | |
}*/ | |
public boolean updateByParam( String updateParam) { | |
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).getName() + ":"); | |
contactList.get(i).nameInput(sc); | |
contactList.get(i).validatorName(contactList.get(i).getName()); | |
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() { | |
// Scanner sc = new Scanner(System.in); | |
// | |
// System.out.println("\nДля записи имени используйте символы a-z, A-Z, 0-9, _\n" + | |
// "Длина имени от 3 до 15 символов с учетом пробелов. Пример: Vasa Pyatochkin"); | |
// new Contact().nameInput(sc); | |
// new Contact().validatorName(new Contact().getName()); | |
// | |
// System.out.println("\nДля записи номера используйте цифры 0-9.\n" + | |
// "Длина номера от 7 до 14 символов с учетом пробелов. Допускаются проблелы. Пример: 097 777 77 77"); | |
// new Contact().inputPhoneNamber(sc); | |
// new Contact().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