Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions LAB Java-Exceptions/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>LABJava-Exceptions</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.1</version>
<scope>test</scope>
</dependency>



</project>
59 changes: 59 additions & 0 deletions LAB Java-Exceptions/src/main/java/org/example/Person.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
46 changes: 46 additions & 0 deletions LAB Java-Exceptions/src/main/java/org/example/PersonsList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.example;


import java.util.*;

public class PersonsList {
private List<Person> 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) {
}
}


27 changes: 27 additions & 0 deletions LAB Java-Exceptions/src/test/java/org/example/PersonTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}


}
52 changes: 52 additions & 0 deletions LAB Java-Exceptions/src/test/java/org/example/PersonsListTest.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.