diff --git a/LAB Java-Exceptions/pom.xml b/LAB Java-Exceptions/pom.xml new file mode 100644 index 0000000..a1ccc8b --- /dev/null +++ b/LAB Java-Exceptions/pom.xml @@ -0,0 +1,39 @@ + + + 4.0.0 + + org.example + LABJava-Exceptions + 1.0-SNAPSHOT + + + junit + junit + RELEASE + test + + + org.junit.jupiter + junit-jupiter + RELEASE + test + + + + + 17 + 17 + + + + org.junit.jupiter + junit-jupiter-api + 5.9.1 + test + + + + + \ No newline at end of file diff --git a/LAB Java-Exceptions/src/main/java/org/example/Person.java b/LAB Java-Exceptions/src/main/java/org/example/Person.java new file mode 100644 index 0000000..e1fee20 --- /dev/null +++ b/LAB Java-Exceptions/src/main/java/org/example/Person.java @@ -0,0 +1,59 @@ +package org.example; + +public class Person { + private int id; + private String firstName; + private String lastName; + private int age; + private String occupation; + + public Person(int id, String firstName, String lastName, int age, String occupation) throws IllegalArgumentException { + this.id = id; + this.firstName = firstName; + this.lastName = lastName; + setAge(age); + this.occupation = occupation; + } + + public void setAge(int age) throws IllegalArgumentException { + if (age < 0) { + throw new IllegalArgumentException("Age cannot be less than 0."); + } + this.age = age; + } + + public boolean equals(Person other) { + if (other == null) return false; + return this.getFullName().equals(other.getFullName()) && + this.age == other.age && + this.occupation.equals(other.occupation); + } + + public String toString() { + return "ID: " + id + ", Name: " + getFullName() + ", Age: " + age + ", Occupation: " + occupation; + } + + public String getFullName() { + return firstName + " " + lastName; + } + + public int getId() { + return id; + } + + public String getFirstName() { + return firstName; + } + + public String getLastName() { + return lastName; + } + + public int getAge() { + return age; + } + + public String getOccupation() { + return occupation; + } +} \ No newline at end of file diff --git a/LAB Java-Exceptions/src/main/java/org/example/PersonsList.java b/LAB Java-Exceptions/src/main/java/org/example/PersonsList.java new file mode 100644 index 0000000..df93d1b --- /dev/null +++ b/LAB Java-Exceptions/src/main/java/org/example/PersonsList.java @@ -0,0 +1,46 @@ +package org.example; + + +import java.util.*; + +public class PersonsList { + private List persons; + private int nextId = 1000; + + public PersonsList() { + persons = new ArrayList<>(); + } + + public void addPerson(Person person) { + persons.add(person); + } + + public Person findByName(String name) throws IllegalArgumentException { + try { + if (name == null || !name.matches("^[A-Za-z]+\\s[A-Za-z]+$")) { + throw new IllegalArgumentException("Name must be in the format 'FirstName LastName'."); + } + + return persons.stream() + .filter(p -> p.getFullName().equalsIgnoreCase(name)) + .findFirst() + .orElse(null); + + } catch (IllegalArgumentException e) { + + throw e; + } catch (Exception e) { + System.err.println("Unexpected error in findByName: " + e.getMessage()); + return null; + } + } + + public Object clone(Person person1) { + return null; + } + + public void writePersonToFile(Person person1, String s) { + } +} + + diff --git a/LAB Java-Exceptions/src/test/java/org/example/PersonTest.java b/LAB Java-Exceptions/src/test/java/org/example/PersonTest.java new file mode 100644 index 0000000..dcacb25 --- /dev/null +++ b/LAB Java-Exceptions/src/test/java/org/example/PersonTest.java @@ -0,0 +1,27 @@ +package org.example; + +import org.junit.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class PersonTest { + @Test + public void testSetValidAge() throws IllegalArgumentException { + Person person = new Person(1, "Lara", "Brown", 25, "Senior Developer"); + person.setAge(30); + assertEquals(30, person.getAge()); + } + + @Test + public void testSetAgeInvalid() throws IllegalArgumentException { + Person person = new Person(2, "Bob", "Green", 40, "Teacher"); + try { + person.setAge(-1); + fail("Expected IllegalArgumentException"); + } catch (IllegalArgumentException e) { + assertEquals("Age cannot be less than 0.", e.getMessage()); + } + } + + +} \ No newline at end of file diff --git a/LAB Java-Exceptions/src/test/java/org/example/PersonsListTest.java b/LAB Java-Exceptions/src/test/java/org/example/PersonsListTest.java new file mode 100644 index 0000000..bdfb53a --- /dev/null +++ b/LAB Java-Exceptions/src/test/java/org/example/PersonsListTest.java @@ -0,0 +1,52 @@ +package org.example; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class PersonsListTest { + + private PersonsList list; + private Person person1; + private Person person2; + + @BeforeEach + void setUp() throws IllegalArgumentException { + list = new PersonsList(); + person1 = new Person(1, "Alice", "Brown", 28, "Designer"); + person2 = new Person(2, "Charlie", "Black", 33, "Manager"); + list.addPerson(person1); + list.addPerson(person2); + } + + @Test + void testFindByNameValid() { + try { + Person found = list.findByName("Charlie Black"); + assertNotNull(found); + assertEquals("Charlie Black", found.getFullName()); + } catch (IllegalArgumentException e) { + fail("Exception should not be thrown for valid name format."); + } + } + + @Test + void testFindByNameInvalidFormat() { + try { + list.findByName("Charlie"); + fail("Expected IllegalArgumentException"); + } catch (IllegalArgumentException e) { + assertEquals("Name must be in the format 'FirstName LastName'.", e.getMessage()); + } + } + + @Test + void testWritePersonToFile() { + try { + list.writePersonToFile(person1, "test_output.txt"); + } catch (Exception e) { + fail("Should not throw any exception"); + } + } +} diff --git a/LAB Java-Exceptions/target/classes/org/example/Person.class b/LAB Java-Exceptions/target/classes/org/example/Person.class new file mode 100644 index 0000000..04df8b5 Binary files /dev/null and b/LAB Java-Exceptions/target/classes/org/example/Person.class differ diff --git a/LAB Java-Exceptions/target/classes/org/example/PersonsList.class b/LAB Java-Exceptions/target/classes/org/example/PersonsList.class new file mode 100644 index 0000000..c9d2ed7 Binary files /dev/null and b/LAB Java-Exceptions/target/classes/org/example/PersonsList.class differ diff --git a/LAB Java-Exceptions/target/test-classes/org/example/PersonTest.class b/LAB Java-Exceptions/target/test-classes/org/example/PersonTest.class new file mode 100644 index 0000000..fb30061 Binary files /dev/null and b/LAB Java-Exceptions/target/test-classes/org/example/PersonTest.class differ diff --git a/LAB Java-Exceptions/target/test-classes/org/example/PersonsListTest.class b/LAB Java-Exceptions/target/test-classes/org/example/PersonsListTest.class new file mode 100644 index 0000000..7780758 Binary files /dev/null and b/LAB Java-Exceptions/target/test-classes/org/example/PersonsListTest.class differ