Skip to content

ODS_ANGELA RUIZ #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
32 changes: 32 additions & 0 deletions lab-exceptions/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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>lab-exceptions</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<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>


</project>
68 changes: 68 additions & 0 deletions lab-exceptions/src/main/java/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import java.util.Objects;

public class Person {
private int id;
private String name;
private int age;
private String occupation;

public Person(int id, String name, int age, String occupation) {
setId(id);
setName(name);
setAge(age);
setOccupation(occupation);
}

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 int getAge() {
return age;
}

public void setAge(int age) throws IllegalArgumentException {
if (age < 0) {
throw new IllegalArgumentException("Age must be a positive integer");
}
this.age = age;
}

public String getOccupation() {
return occupation;
}

public void setOccupation(String occupation) {
this.occupation = occupation;
}

@Override

public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

Person person = (Person) o;
return id == person.getId() &&
Objects.equals(age, person.getAge()) &&
Objects.equals(name, person.getName()) &&
Objects.equals(occupation, person.getOccupation())
;
}
}
50 changes: 50 additions & 0 deletions lab-exceptions/src/main/java/PersonList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class PersonList {
private List<Person> personList;

public PersonList() {
this.personList = new ArrayList<>();
}

public List<Person> getPersonList() {
return personList;
}

public void addPerson(Person person) {
this.personList.add(person);
}

public Person findByName(String name) {
if (name == null || !name.contains(" ")) {
throw new IllegalArgumentException("Name must be in the format 'FirstName LastName'");
}
for (Person person : personList) {

if (person.getName().equals(name)) {
return person;
}
}
return null;
}


public Person clone(Person person) {
return new Person(person.getId() + 1, person.getName(), person.getAge(), person.getOccupation());
}

public void writeToFile(Object o) {

try (FileWriter fw = new FileWriter("personList.txt", true)) {
fw.write(o.toString());
} catch (IOException e) {
System.out.println("Couldn´t write to file" + e.getMessage());
throw new RuntimeException(e);
}

}
}
20 changes: 20 additions & 0 deletions lab-exceptions/src/test/java/PersonListTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class PersonListTest {
@Test
@DisplayName("Gives an error if cloned person doesn`t have same properties")
void testCloneCreatesNewPerson() {
PersonList personList = new PersonList();
Person person = new Person(1, "Carlos Gomez", 40, "Abogado");
personList.addPerson(person);

Person clonedPerson = personList.clone(person);

assertNotEquals(person.getId(), clonedPerson.getId());
assertEquals(person.getName(), clonedPerson.getName());
assertEquals(person.getAge(), clonedPerson.getAge());
assertEquals(person.getOccupation(), clonedPerson.getOccupation());
}
}
35 changes: 35 additions & 0 deletions lab-exceptions/src/test/java/PersonTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class PersonTest {

@Test
@DisplayName("It should gives an error if age is negative")
void negativeAgeException() {
Person person = new Person(1, "Juan Perez", 30, "Developer");

assertThrows(IllegalArgumentException.class, () -> person.setAge(-5));
}

@Test
@DisplayName("Gives an error if it doesn't return correct person ")
void testFindByNameReturnsCorrectPerson() {
PersonList personList = new PersonList();
Person person = new Person(1, "María Ruiz", 25, "Actriz");
personList.addPerson(person);

Person foundPerson = personList.findByName("María Ruiz");
assertNotNull(foundPerson);
assertEquals("María Ruiz", foundPerson.getName());
}

@Test
@DisplayName("Gives an error if format is not correct and exception is not thrown ")
void testCorrectFormatException() {
PersonList personList = new PersonList();
personList.addPerson(new Person(1, "María Ruiz", 25, "Actriz"));

assertThrows(IllegalArgumentException.class, () -> personList.findByName("MaríaRuiz"));
}
}
Binary file added lab-exceptions/target/classes/Person.class
Binary file not shown.
Binary file added lab-exceptions/target/classes/PersonList.class
Binary file not shown.
Binary file not shown.
Binary file not shown.