/
anton.buldygin
/
Contacts_Java
Обзор
Документация
Войти
/
anton.buldygin
/
Contacts_Java
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
Contacts (Java)/Searching/task-info.yaml
759 строк
33 KB
AntonBuldygin
Initial commit
19 дек 2023, 15:38
19 дек 2023, 15:38
bed9366
Код
Авторство
О чём код?
type: edu files: - name: src/contacts/Main.java visible: true text: | package contacts; public class Main { public static void main(String[] args) { System.out.println("Hello World!"); } } learner_created: false - name: test/ContactsTest.java visible: false text: "import org.hyperskill.hstest.stage.StageTest;\nimport org.hyperskill.hstest.testcase.CheckResult;\n\ import org.hyperskill.hstest.testcase.TestCase;\n\nimport java.util.List;\nimport\ \ java.util.function.Function;\n\n\nclass TestClue {\n\n Function<String, CheckResult>\ \ callback;\n\n TestClue(Function<String, CheckResult> callback) {\n \ \ this.callback = callback;\n }\n}\n\n\npublic class ContactsTest extends\ \ StageTest<TestClue> {\n\n private CheckResult splitActionsFeedback(int actualSize,\ \ int needSize) {\n if (actualSize < needSize) {\n return CheckResult.wrong(String.format(\"\ This test should contain at least %d actions, but you have only %d. \" +\n \ \ \"You should separate your actions with an empty line.\"\ ,\n needSize,\n actualSize));\n }\ \ else {\n return null;\n }\n }\n \n @Override\n \ \ public List<TestCase<TestClue>> generate() {\n return List.of (\n \ \ new TestCase<TestClue>()\n .setInput(\"exit\")\n \ \ .setAttach(new TestClue(output -> {\n output\ \ = output.strip().toLowerCase();\n if (!output.contains(\"\ enter action\")) {\n return new CheckResult(false,\n \ \ \"No \\\"Enter action\\\" substring found in the output\"\ );\n }\n return CheckResult.correct();\n\ \ })),\n\n new TestCase<TestClue>()\n \ \ .setInput(\n \"count\\n\" +\n \"exit\"\ )\n .setAttach(new TestClue(output -> {\n output\ \ = output.strip().toLowerCase();\n if (!output.contains(\"\ 0 records\")) {\n return new CheckResult(false,\n \ \ \"No \\\"0 records\\\" substring found in the output.\ \ \" +\n \"If you already have the database,\ \ try to delete it.\");\n }\n return CheckResult.correct();\n\ \ })),\n\n new TestCase<TestClue>()\n \ \ .setInput(\n \"add\\n\" +\n \"person\\\ n\" +\n \"John\\n\" +\n \"Smith\\\ n\" +\n \"\\n\" +\n \"\\n\" +\n\ \ \"123 456 789\\n\" +\n \"count\\n\"\ \ +\n \"exit\")\n .setAttach(new TestClue(output\ \ -> {\n output = output.strip().toLowerCase();\n \ \ if (output.contains(\"0 records\")) {\n return\ \ new CheckResult(false,\n \"Can't add the person\"\ );\n }\n return CheckResult.correct();\n\ \ })),\n\n new TestCase<TestClue>()\n \ \ .setInput(\n \"add\\n\" +\n \"person\\\ n\" +\n \"John\\n\" +\n \"Smith\\\ n\" +\n \"\\n\" +\n \"\\n\" +\n\ \ \"123 456 789\\n\" +\n \"list\\n\"\ \ +\n \"1\\n\" +\n \"menu\\n\" +\n \ \ \"exit\")\n .setAttach(new TestClue(output -> {\n\ \ String[] blocks = output.strip().split(\"(\\\\s*\\\\n\\\\\ s*){2,}\");\n var feedback = splitActionsFeedback(blocks.length,\ \ 3);\n if (feedback != null) return feedback;\n\n \ \ for (String infoBlock : blocks) {\n if (infoBlock.contains(\"\ Name: John\")\n && infoBlock.contains(\"Surname:\ \ Smith\")\n && infoBlock.contains(\"Birth date:\ \ [no data]\")\n && infoBlock.contains(\"Gender:\ \ [no data]\")\n && infoBlock.contains(\"Number:\ \ 123 456 789\")\n && infoBlock.contains(\"Time\ \ created:\")\n && infoBlock.contains(\"Time last\ \ edit:\")) {\n return CheckResult.correct();\n \ \ }\n }\n return new\ \ CheckResult(false,\n \"Something wrong with printing\ \ user info\");\n })),\n\n new TestCase<TestClue>()\n\ \ .setInput(\n \"add\\n\" +\n \ \ \"organization\\n\" +\n \"Pizza Shop\\n\" +\n\ \ \"Wall St. 1\\n\" +\n \"+0 (123)\ \ 456-789-9999\\n\" +\n \"list\\n\" +\n \ \ \"1\\n\" +\n \"menu\\n\" +\n \"exit\"\ )\n .setAttach(new TestClue(output -> {\n String[]\ \ blocks = output.strip().split(\"(\\\\s*\\\\n\\\\s*){2,}\");\n \ \ var feedback = splitActionsFeedback(blocks.length, 3);\n \ \ if (feedback != null) return feedback;\n\n for (String\ \ infoBlock : blocks) {\n if (infoBlock.contains(\"Organization\ \ name: Pizza Shop\")\n && infoBlock.contains(\"\ Address: Wall St. 1\")\n && infoBlock.contains(\"\ Number: +0 (123) 456-789-9999\")\n && infoBlock.contains(\"\ Time created:\")\n && infoBlock.contains(\"Time\ \ last edit:\")) {\n return CheckResult.correct();\n\ \ }\n }\n return\ \ new CheckResult(false,\n \"Something wrong with printing\ \ organization info\");\n })),\n\n new TestCase<TestClue>()\n\ \ .setInput(\n \"add\\n\" +\n \ \ \"person\\n\" +\n \"John\\n\" +\n \ \ \"Smith\\n\" +\n \"\\n\" +\n \ \ \"\\n\" +\n \"123 456 789\\n\" +\n \ \ \"list\\n\" +\n \"1\\n\" +\n \ \ \"edit\\n\" +\n \"gender\\n\" +\n \ \ \"M\\n\" +\n \"menu\\n\" +\n \"\ list\\n\" +\n \"1\\n\" +\n \"menu\\n\" +\n\ \ \"exit\")\n .setAttach(new TestClue(output\ \ -> {\n String[] blocks = output.strip().split(\"(\\\\s*\\\ \\n\\\\s*){2,}\");\n var feedback = splitActionsFeedback(blocks.length,\ \ 6);\n if (feedback != null) return feedback;\n\n \ \ for (String infoBlock : blocks) {\n if (infoBlock.contains(\"\ Name: John\")\n && infoBlock.contains(\"Surname:\ \ Smith\")\n && infoBlock.contains(\"Birth date:\ \ [no data]\")\n && infoBlock.contains(\"Gender:\ \ M\")\n && infoBlock.contains(\"Number: 123 456\ \ 789\")\n && infoBlock.contains(\"Time created:\"\ )\n && infoBlock.contains(\"Time last edit:\"))\ \ {\n return CheckResult.correct();\n \ \ }\n }\n return new CheckResult(false,\n\ \ \"Editing person is not working\");\n \ \ })),\n\n new TestCase<TestClue>()\n .setInput(\n\ \ \"add\\n\" +\n \"organization\\n\"\ \ +\n \"Pizza Shop\\n\" +\n \"Wall\ \ St. 1\\n\" +\n \"+0 (123) 456-789-9999\\n\" +\n \ \ \"list\\n\" +\n \"1\\n\" +\n \ \ \"edit\\n\" +\n \"address\\n\" +\n \ \ \"Wall St 2\\n\" +\n \"menu\\n\" +\n \ \ \"list\\n\" +\n \"1\\n\" +\n \ \ \"menu\\n\" +\n \"exit\")\n .setAttach(new\ \ TestClue(output -> {\n String[] blocks = output.strip().split(\"\ (\\\\s*\\\\n\\\\s*){2,}\");\n var feedback = splitActionsFeedback(blocks.length,\ \ 6);\n if (feedback != null) return feedback;\n\n \ \ for (String infoBlock : blocks) {\n if (infoBlock.contains(\"\ Organization name: Pizza Shop\")\n && infoBlock.contains(\"\ Address: Wall St 2\")\n && infoBlock.contains(\"\ Number: +0 (123) 456-789-9999\")\n && infoBlock.contains(\"\ Time created:\")\n && infoBlock.contains(\"Time\ \ last edit:\")) {\n return CheckResult.correct();\n\ \ }\n }\n return\ \ new CheckResult(false,\n \"Editing organization is\ \ not working\");\n })),\n\n new TestCase<TestClue>()\n\ \ .setInput(\n \"add\\n\" +\n \ \ \"organization\\n\" +\n \"Pizza Shop\\n\" +\n\ \ \"Wall St. 1\\n\" +\n \"+0 (123)\ \ 456-789-9999\\n\" +\n \"add\\n\" +\n \ \ \"person\\n\" +\n \"John\\n\" +\n \ \ \"Smith\\n\" +\n \"\\n\" +\n \ \ \"\\n\" +\n \"123 456 789\\n\" +\n \ \ \"add\\n\" +\n \"organization\\n\" +\n \ \ \"PizzaNuts\\n\" +\n \"Wall St. 6\\n\"\ \ +\n \"+0 (123) 456-789-9999\\n\" +\n \ \ \"search\\n\" +\n \"pizz\\n\" +\n \ \ \"1\\n\" +\n \"menu\\n\" +\n \"exit\"\ )\n .setAttach(new TestClue(output -> {\n String[]\ \ blocks = output.strip().split(\"(\\\\s*\\\\n\\\\s*){2,}\");\n \ \ var feedback = splitActionsFeedback(blocks.length, 4);\n \ \ if (feedback != null) return feedback;\n \n \n \ \ for (String infoBlock : blocks) {\n \ \ if (infoBlock.contains(\"Pizza Shop\")\n && infoBlock.contains(\"\ PizzaNuts\")\n && !infoBlock.contains(\"John\"\ )) {\n return CheckResult.correct();\n \ \ }\n }\n return new CheckResult(false,\n\ \ \"Search is not working\");\n })),\n\ \n new TestCase<TestClue>()\n .setInput(\n \ \ \"add\\n\" +\n \"organization\\n\" +\n \ \ \"Pizza Shop\\n\" +\n \"Wall St. 1\\\ n\" +\n \"+0 (123) 456-789-9999\\n\" +\n \ \ \"add\\n\" +\n \"person\\n\" +\n \ \ \"John\\n\" +\n \"Smith\\n\" +\n \ \ \"\\n\" +\n \"\\n\" +\n \ \ \"123 456 789\\n\" +\n \"add\\n\" +\n \ \ \"organization\\n\" +\n \"PizzaNuts\\n\" +\n\ \ \"Wall St. 6\\n\" +\n \"+0 (123)\ \ 456-789-9999\\n\" +\n \"search\\n\" +\n \ \ \"s\\n\" +\n \"1\\n\" +\n \"menu\\\ n\" +\n \"exit\")\n .setAttach(new TestClue(output\ \ -> {\n String[] blocks = output.strip().split(\"(\\\\s*\\\ \\n\\\\s*){2,}\");\n var feedback = splitActionsFeedback(blocks.length,\ \ 4);\n if (feedback != null) return feedback;\n\n \ \ for (String infoBlock : blocks) {\n if (infoBlock.contains(\"\ Pizza Shop\")\n && infoBlock.contains(\"John Smith\"\ )\n && infoBlock.contains(\"PizzaNuts\")) {\n \ \ return CheckResult.correct();\n \ \ }\n }\n return new CheckResult(false,\n\ \ \"Search is not working\");\n })),\n\ \n new TestCase<TestClue>()\n .setInput(\n \"\ add\\n\" +\n \"organization\\n\" +\n \"\ Pizza Shop\\n\" +\n \"Wall St. 1\\n\" +\n \ \ \"+0 (123) 456-789-9999\\n\" +\n \"add\\n\" +\n \ \ \"person\\n\" +\n \"John\\n\" +\n \ \ \"Smith\\n\" +\n \"\\n\" +\n \"\\\ n\" +\n \"123 456 789\\n\" +\n \"add\\n\" +\n\ \ \"organization\\n\" +\n \"PizzaNuts\\\ n\" +\n \"Wall St. 6\\n\" +\n \"+0 (123)\ \ 456-781-9999\\n\" +\n \"search\\n\" +\n \"\ 789\\n\" +\n \"1\\n\" +\n \"menu\\n\" +\n \ \ \"exit\")\n .setAttach(new TestClue(output -> {\n \ \ String[] blocks = output.strip().split(\"(\\\\s*\\\\n\\\\s*){2,}\"\ );\n var feedback = splitActionsFeedback(blocks.length, 4);\n \ \ if (feedback != null) return feedback;\n\n for\ \ (String infoBlock : blocks) {\n if (infoBlock.contains(\"\ Pizza Shop\")\n && infoBlock.contains(\"John Smith\"\ )\n && !infoBlock.contains(\"PizzaNuts\")) {\n \ \ return CheckResult.correct();\n\n } else\ \ if (infoBlock.contains(\"+0 (123) 456-789-9999\")\n \ \ && infoBlock.contains(\"123 456 789\")\n && !infoBlock.contains(\"\ +0 (123) 456-781-9999\")) {\n return CheckResult.correct();\n\ \ }\n }\n return new CheckResult(false,\n\ \ \"Search by phone number is not working\");\n \ \ }))\n );\n }\n\n @Override\n public CheckResult check(String\ \ reply, TestClue clue) {\n try {\n return clue.callback.apply(reply);\n\ \ }\n catch (Exception ex) {\n return new CheckResult(false,\ \ \"Can't check the answer\");\n }\n }\n}\n" learner_created: false - name: src/contacts/Contact.java visible: true text: | package contacts; import java.time.LocalDate; import java.time.LocalDateTime; import java.util.Date; public class Contact extends GeneralContact { public int getId() { return id; } public void setId(int id) { this.id = id; } private String contactName; private int id; private String name; private String surname; private String phoneNumber; public String getContactName() { return contactName; } public void setContactName(String contactName) { this.contactName = contactName; } public Contact(int id, String name, String surname, String phoneNumber, String birthDay, String gender, LocalDateTime created, LocalDateTime edited) { super(); this.id = id; this.name = name; this.surname = surname; this.phoneNumber = phoneNumber; this.birthDay = birthDay; this.gender = gender; this.created = created; this.edited = edited; } public String getBirthDay() { return birthDay; } public void setBirthDay(String birthDay) { this.birthDay = birthDay; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public LocalDateTime getCreated() { return created; } public void setCreated(LocalDateTime created) { this.created = created; } public LocalDateTime getEdited() { return edited; } public void setEdited(LocalDateTime edited) { this.edited = edited; } private String birthDay; private String gender; private LocalDateTime created; private LocalDateTime edited; public Contact() { } public Contact(String name, String surname, String phoneNumber) { this.name = name; this.surname = surname; this.phoneNumber = phoneNumber; } public Contact(int id, String name, String surname, String phoneNumber) { this.id = id; this.name = name; this.surname = surname; this.phoneNumber = phoneNumber; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSurname() { return surname; } public void setSurname(String surname) { this.surname = surname; } public String getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } @Override public String toString() { return "Name: " + name + "\n" + "Surname: " + surname + "\n" + "Birth date: " + birthDay + "\n" + "Gender: " + gender + "\n" + "Number: " + phoneNumber + "\n" + "Time created: " + created + "\n" + "Time last edit: " + edited + "\n"; } public String contactName() { return this.id + ". " + this.name + " " + this.surname; } } learner_created: true - name: src/contacts/GeneralContact.java visible: true text: | package contacts; import java.time.LocalDateTime; public class GeneralContact { private int id; private String name; private String phoneNumber; private LocalDateTime created; private LocalDateTime edited; public String getContactName() { return contactName; } public void setContactName(String contactName) { this.contactName = contactName; } private String contactName= contactName(); public String contactName() { return id + ". " + name; } public GeneralContact(int id, String name, String phoneNumber, LocalDateTime created, LocalDateTime edited) { this.id = id; this.name = name; this.phoneNumber = phoneNumber; this.created = created; this.edited = edited; } public GeneralContact() { } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } public LocalDateTime getCreated() { return created; } public void setCreated(LocalDateTime created) { this.created = created; } public LocalDateTime getEdited() { return edited; } public void setEdited(LocalDateTime edited) { this.edited = edited; } } learner_created: true - name: src/contacts/Organization.java visible: true text: | package contacts; import java.time.LocalDateTime; public class Organization extends GeneralContact { private int id; private String name; private String address; private String phoneNumber; private LocalDateTime created; private LocalDateTime edited; public String getContactName() { return contactName; } public void setContactName(String contactName) { this.contactName = contactName; } private String contactName= contactName(); public Organization(int id, String name, String phoneNumber, LocalDateTime created, LocalDateTime edited) { super(id, name, phoneNumber, created, edited); } public Organization() { } @Override public int getId() { return id; } @Override public void setId(int id) { this.id = id; } @Override public String getName() { return name; } @Override public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String getPhoneNumber() { return phoneNumber; } @Override public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } @Override public LocalDateTime getCreated() { return created; } @Override public void setCreated(LocalDateTime created) { this.created = created; } @Override public LocalDateTime getEdited() { return edited; } @Override public void setEdited(LocalDateTime edited) { this.edited = edited; } @Override public String toString() { return "Organization name: " + name + "\n" + "Address: " + address + "\n" + "Number: " + phoneNumber + "\n" + "Time created: " + created + "\n" + "Time last edit: " + edited + "\n"; } public String contactName() { return id + ". " + name ; } } learner_created: true - name: src/contacts/Contacts.java visible: true text: | package contacts; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Contacts { List<GeneralContact> contacts; public Contacts() { this.contacts = new ArrayList<>(); } public String add(Scanner scanner) { System.out.print("Enter the type (person, organization): "); String type = scanner.nextLine(); if (type.equals("person")) { Contact contact = new Contact(); contact.setCreated(LocalDateTime.now()); contact.setEdited(LocalDateTime.now()); System.out.print("Enter the name: "); contact.setName(scanner.nextLine()); System.out.print("Enter the surname: "); contact.setSurname(scanner.nextLine()); System.out.print("Enter the birth date: "); String birthDateinput = scanner.nextLine(); if (numberBirthdate(birthDateinput)) { // DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); contact.setBirthDay(birthDateinput); } else contact.setBirthDay("[no data]"); System.out.print("Enter the gender (M, F): "); String gender = scanner.nextLine(); if (gender.equals("M") || gender.equals("F")) { contact.setGender(gender); } else { contact.setGender("[no data]"); } System.out.print("Enter the number: "); String numberInput = scanner.nextLine(); if (numberCheck(numberInput)) { contact.setPhoneNumber(numberInput); } else { System.out.println("Wrong number format!"); contact.setPhoneNumber("[no number]"); } System.out.println("The record added."); contact.setId(contacts.size() + 1); contacts.add(contact); } if (type.equals("organization")) { Organization contact = new Organization(); contact.setCreated(LocalDateTime.now()); contact.setEdited(LocalDateTime.now()); System.out.print("Enter the organization name: "); contact.setName(scanner.nextLine()); System.out.print("Enter the address: "); contact.setAddress(scanner.nextLine()); System.out.print("Enter the number: "); String numberInput = scanner.nextLine(); if (numberCheck(numberInput)) { contact.setPhoneNumber(numberInput); } else { System.out.println("Wrong number format!"); contact.setPhoneNumber("[no number]"); } System.out.println("The record added."); contact.setId(contacts.size() + 1); contacts.add(contact); } return "The record added.\n\n"; } public String count() { return String.format("The Phone Book has %d records.", contacts.size()); } public List<GeneralContact> list() { return contacts; } public String remove(int i) { contacts.remove(i - 1); return "The record removed!"; } public String editContact(int record, String field) { Contact contact = (Contact) contacts.get(record - 1); Scanner scanner1 = new Scanner(System.in); if (field.equals("name")) { System.out.print("Enter the name:"); contact.setName(scanner1.nextLine()); } if (field.equals("surname")) { System.out.print("Enter the surname:"); // contact.setSurname(scanner1.nextLine()); } if (field.equals("number")) { System.out.print("Enter the number:"); String s = scanner1.nextLine(); if (numberCheck(s)) { contact.setPhoneNumber(s); } else { System.out.println("Wrong number format!"); contact.setPhoneNumber("[no number]"); } } if (field.equals("birth")) { System.out.print("Enter the birth date: "); String birthDateinput = scanner1.nextLine(); if (numberBirthdate(birthDateinput)) { // DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); contact.setBirthDay(birthDateinput); } else contact.setBirthDay("[no data]"); } if (field.equals("gender")) { System.out.print("Enter the gender (M, F): "); String gender = scanner1.nextLine(); if (gender.equals("M") || gender.equals("F")) { contact.setGender(gender); } else { contact.setGender("[no data]"); } } scanner1.close(); contacts.set(record - 1, contact); return "The record updated!\n"; } public String editOrganization(int record, String field) { Organization contact = (Organization) contacts.get(record - 1); Scanner scanner1 = new Scanner(System.in); if (field.equals("number")) { System.out.print("Enter the number:"); String s = scanner1.nextLine(); if (numberCheck(s)) { contact.setPhoneNumber(s); } else { System.out.println("Wrong number format!"); contact.setPhoneNumber("[no number]"); } } if (field.equals("address")) { System.out.print("Enter the address: "); contact.setAddress(scanner1.nextLine()); } scanner1.close(); contacts.set(record - 1, contact); return "The record updated!\n"; } public boolean numberCheck(String number) { Pattern compile = Pattern.compile("[+]?(([(]?[\\w]{1,}[)]?)|([\\w]+)([ -][(][\\w]{2,}[)])*)([ -][0-9a-zA-Z]{2,})*"); Matcher matcher = compile.matcher(number); boolean b = matcher.matches(); // System.out.println(b); // System.out.println(matcher.matches()); return b; } public boolean numberBirthdate(String number) { Pattern compile = Pattern.compile("[\\d]{4}-[\\d]{2}-[\\d]{2}"); Matcher matcher = compile.matcher(number); boolean b = matcher.matches(); // System.out.println(b); // System.out.println(matcher.matches()); return b; } } learner_created: true feedback_link: https://hyperskill.org/learn/step/5327#comment status: Solved feedback: message: Well done! You've finished the project. Select a new project on <a href="https://hyperskill.org/projects">Hyperskill</a> to continue learning. time: "Tue, 19 Dec 2023 09:55:09 UTC" record: 4