Skip to content

lab-java-exceptions-MiguelPacheca #67

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

<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.2</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.11.0</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>

</dependencies>


</project>
57 changes: 57 additions & 0 deletions LabMiguel-Exceptions/src/main/java/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
public class Person {
private int id;
private String name;
private int age;
private String ocupation;

public Person(int id, String name, int age, String occupation) {
this.id = id;
this.name = name;
setAge(age);
this.ocupation = 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("Introduce un valor mayor que 0");
} // genera un error si age es menor que 0.
this.age = age;
}

public String getOcupation() {
return ocupation;
}

public void setOcupation(String ocupation) {
this.ocupation = ocupation;
}

@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", ocupation='" + ocupation + '\'' +
'}';
}
}
70 changes: 70 additions & 0 deletions LabMiguel-Exceptions/src/main/java/PersonsList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import java.io.FileWriter;
import java.io.IOException;

public class PersonsList extends Person {
private String lastName;

public PersonsList(int id, String name, String lastName, int age, String occupation) {
super(id, name, age, occupation);
this.lastName = lastName;
}

public static String concat(String name, String lastName) throws IllegalArgumentException {
if (name == null || lastName == null || name.isEmpty() || lastName.isEmpty() || name.contains(" ") || lastName.contains(" ")) {
throw new IllegalArgumentException("First and last name cannot be null");
}
return name + " " + lastName;
} // Comprueba el formato


public Person clonePerson(Person origin){
int newId = generateNewId();
return new Person(newId, origin.getName(), origin.getAge(), origin.getOcupation());
} // Metodo para clonar

public int generateNewId() {
return (int) (Math.random() * 10); // Genera un nuevo id para el clonado
}

public static void main(String[] args) throws IOException {
Person person1 = new Person(1, "John Smith", 30, "Teacher");
System.out.println("Original: " + person1);

//Clona a la persona 1
PersonsList clonerP1 = new PersonsList(0, "", "", 1, "");
Person cloned = clonerP1.clonePerson(person1);
System.out.println("Clonado: " + cloned);

//Comprobación de formato
String user1 = concat("John", "Smith");
try {
System.out.println(user1);
} catch (IllegalArgumentException e) {
System.out.println("Error " + e);
}

// Escribe en el archivo
try {
FileWriter writer = new FileWriter("person.txt", true);
writer.write("Se ha clonado la persona: " + cloned + "\n");
writer.close();
System.out.println("Se ha almacendo a la persona: " + cloned);
} catch (IOException e) {
System.err.println("Error al escribir en el archivo: " + e.getMessage());
}

// Comprueba el ID de person y clone
if(person1.getId() == cloned.getId()) {
throw new IllegalArgumentException("ID NULL");
} else {
System.out.println(person1.getId());
System.out.println(cloned.getId());
}

// La edad se comprueba desde PersonTest
// El restante en PersonListTest
}


}

11 changes: 11 additions & 0 deletions LabMiguel-Exceptions/src/test/java/PersonTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import org.junit.jupiter.api.Test;

public class PersonTest {

@Test
public void ageMore(){
var personTest = new Person(1, "John Smith", 30, "Teacher");
assert personTest.getAge() > 0;
}

}
31 changes: 31 additions & 0 deletions LabMiguel-Exceptions/src/test/java/PersonsListTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

class PersonsListTest {

@Test
void testConcat() {
PersonsList stringUtils = new PersonsList(1, "John", "Smith", 30,"Teacher");
String name = "John";
String firstName = "Smith";
String concatResult = "John Smith";

String concatResultPrueba = stringUtils.concat(name, firstName);
assertEquals(concatResult, concatResultPrueba);
}

@Test
void clonePerson() {
PersonsList clonedP1Test = new PersonsList(0, "", " ", 30 , "Teacher");
Person originalPerson = new Person(1, "Alice", 25, "Engineer");

Person clonedPerson = clonedP1Test.clonePerson(originalPerson);
assertNotEquals(originalPerson.getId(), clonedPerson.getId());
assertEquals(originalPerson.getName(), clonedPerson.getName());
assertEquals(originalPerson.getAge(), clonedPerson.getAge());
assertEquals(originalPerson.getOcupation(), clonedPerson.getOcupation());
}

}
Binary file added LabMiguel-Exceptions/target/classes/Person.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
20 changes: 20 additions & 0 deletions person.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Se ha clonado la persona: Person{name='John Smith', age=30, ocupation='Teacher'}Se ha clonado la persona: Person{name='John Smith', age=30, ocupation='Teacher'}Se ha clonado la persona: Person{name='John Smith', age=30, ocupation='Teacher'}
Se ha clonado la persona: Person{name='John Smith', age=30, ocupation='Teacher'}
Se ha clonado la persona: Person{name='John Smith', age=30, ocupation='Teacher'}
Se ha clonado la persona: Person{name='John Smith', age=30, ocupation='Teacher'}
Se ha clonado la persona: Person{name='John Smith', age=30, ocupation='Teacher'}
Se ha clonado la persona: Person{name='John Smith', age=30, ocupation='Teacher'}
Se ha clonado la persona: Person{name='John Smith', age=30, ocupation='Teacher'}
Se ha clonado la persona: Person{name='John Smith', age=30, ocupation='Teacher'}
Se ha clonado la persona: Person{name='John Smith', age=0, ocupation='Teacher'}
Se ha clonado la persona: Person{name='John Smith', age=0, ocupation='Teacher'}
Se ha clonado la persona: Person{name='John Smith', age=30, ocupation='Teacher'}
Se ha clonado la persona: Person{name='John Smith', age=30, ocupation='Teacher'}
Se ha clonado la persona: Person{name='John', age=30, ocupation='Teacher'}
Se ha clonado la persona: Person{name='John', age=30, ocupation='Teacher'}
Se ha clonado la persona: Person{name='John', age=30, ocupation='Teacher'}
Se ha clonado la persona: Person{name='John Smith', age=30, ocupation='Teacher'}
Se ha clonado la persona: Person{name='John Smith', age=30, ocupation='Teacher'}
Se ha clonado la persona: Person{name='John Smith', age=30, ocupation='Teacher'}
Se ha clonado la persona: Person{name='John Smith', age=30, ocupation='Teacher'}
Se ha clonado la persona: Person{name='John Smith', age=30, ocupation='Teacher'}