Skip to content

Lab Finished #79

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
38 changes: 38 additions & 0 deletions Lab204/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
5 changes: 5 additions & 0 deletions Lab204/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Lab204/.idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions Lab204/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Lab204/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions Lab204/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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>Lab204</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.1</version>
<scope>compile</scope>
</dependency>
</dependencies>

</project>
64 changes: 64 additions & 0 deletions Lab204/src/main/java/org/example/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.example;

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

public Person(int id, String name, int age, String occupation) {
this.id = id;
this.name = name;
this.age = age;
this.occupation = 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) {
if (age < 0) {
throw new IllegalArgumentException("Esta persona aún no ha nacido");
}
this.age = age;
}

public String getOccupation() {
return occupation;
}

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

public boolean compare(Person other) {
return this.name.equals(other.name) && this.age == other.age && this.occupation.equals(other.occupation);
}

@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", occupation='" + occupation + '\'' +
'}';
}
}
47 changes: 47 additions & 0 deletions Lab204/src/main/java/org/example/PersonList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.example;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class PersonList {
private ArrayList<Person> people;

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

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

public Person findByName(String name) throws Exception {
String[] nameCheck = name.split(" ");
if (nameCheck.length != 2) {
throw new IllegalArgumentException("El nombre debe estar en formato firstName, lastName");
}
for (Person person : people) {
if (person.getName().equals(name)) {
return person;
}
}
throw new Exception("No se encontraron personas con ese nombre");
}

public Person clone(Person original, int newId) {
return new Person(newId, original.name, original.age, original.occupation);
}

public void writeToFile(Person person) {
try {
File file = new File("person.txt");
FileWriter writer = new FileWriter(file);
writer.write(person.toString());
writer.close();
;
} catch (IOException e) {
System.err.println("Error al escribir el archivo" + e);
}
}
}
65 changes: 65 additions & 0 deletions Lab204/src/main/java/org/example/Tests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.example;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class Tests {
private Person person1;
private Person person2;
private Person person3;
private PersonList personList;

@BeforeEach
public void setUp() {
person1 = new Person(1, "Pepito Pérez", 31, "Albañil");
person2 = new Person(2, "Mengano López", 45, "Taxista");
person3 = new Person(3, "Fulano Gómez", 70, "Jubilado");

personList = new PersonList();
personList.addPerson(person1);
personList.addPerson(person2);
personList.addPerson(person3);
}


@Test
public void NegativeAge_ThrowsException() {
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
person1.setAge(-5);
});

assertEquals("Esta persona aún no ha nacido", exception.getMessage());
}

@Test
void findByName_ReturnsCorrectPerson() {
try {
Person found = personList.findByName("Pepito Pérez");
assertEquals(person1, found);
} catch (Exception e) {
e.printStackTrace();
}
}

@Test
public void findByName_InvalidFormat() {
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
personList.findByName("PepitoPérez"); // improper format
});

assertEquals("El nombre debe estar en formato firstName, lastName", exception.getMessage());
}

@Test
void Cloned_SamePropertiesExceptId() {
Person clonedPerson = personList.clone(person1, 100);

assertNotEquals(clonedPerson.getId(), person1.getId());
assertEquals(clonedPerson.getName(), person1.getName());
assertEquals(clonedPerson.getAge(), person1.getAge());
assertEquals(clonedPerson.getOccupation(), person1.getOccupation());
}

}