diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..723ef36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/LabMiguel-Exceptions/pom.xml b/LabMiguel-Exceptions/pom.xml new file mode 100644 index 0000000..45ea0d6 --- /dev/null +++ b/LabMiguel-Exceptions/pom.xml @@ -0,0 +1,42 @@ + + + 4.0.0 + + org.example + LabMiguel-Exceptions + 1.0-SNAPSHOT + + + 21 + 21 + UTF-8 + + + + org.junit.jupiter + junit-jupiter-engine + 5.9.2 + + + org.junit.jupiter + junit-jupiter-api + 5.9.2 + + + org.mockito + mockito-core + 4.11.0 + + + org.jetbrains + annotations + RELEASE + compile + + + + + + \ No newline at end of file diff --git a/LabMiguel-Exceptions/src/main/java/Person.java b/LabMiguel-Exceptions/src/main/java/Person.java new file mode 100644 index 0000000..11f9f31 --- /dev/null +++ b/LabMiguel-Exceptions/src/main/java/Person.java @@ -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 + '\'' + + '}'; + } +} diff --git a/LabMiguel-Exceptions/src/main/java/PersonsList.java b/LabMiguel-Exceptions/src/main/java/PersonsList.java new file mode 100644 index 0000000..ddafd45 --- /dev/null +++ b/LabMiguel-Exceptions/src/main/java/PersonsList.java @@ -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 + } + + +} + diff --git a/LabMiguel-Exceptions/src/test/java/PersonTest.java b/LabMiguel-Exceptions/src/test/java/PersonTest.java new file mode 100644 index 0000000..9ce4134 --- /dev/null +++ b/LabMiguel-Exceptions/src/test/java/PersonTest.java @@ -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; + } + +} diff --git a/LabMiguel-Exceptions/src/test/java/PersonsListTest.java b/LabMiguel-Exceptions/src/test/java/PersonsListTest.java new file mode 100644 index 0000000..67c9888 --- /dev/null +++ b/LabMiguel-Exceptions/src/test/java/PersonsListTest.java @@ -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()); + } + +} \ No newline at end of file diff --git a/LabMiguel-Exceptions/target/classes/Person.class b/LabMiguel-Exceptions/target/classes/Person.class new file mode 100644 index 0000000..e562413 Binary files /dev/null and b/LabMiguel-Exceptions/target/classes/Person.class differ diff --git a/LabMiguel-Exceptions/target/classes/PersonsList.class b/LabMiguel-Exceptions/target/classes/PersonsList.class new file mode 100644 index 0000000..be3ab77 Binary files /dev/null and b/LabMiguel-Exceptions/target/classes/PersonsList.class differ diff --git a/LabMiguel-Exceptions/target/test-classes/PersonTest.class b/LabMiguel-Exceptions/target/test-classes/PersonTest.class new file mode 100644 index 0000000..17ac288 Binary files /dev/null and b/LabMiguel-Exceptions/target/test-classes/PersonTest.class differ diff --git a/LabMiguel-Exceptions/target/test-classes/PersonsListTest.class b/LabMiguel-Exceptions/target/test-classes/PersonsListTest.class new file mode 100644 index 0000000..ede4737 Binary files /dev/null and b/LabMiguel-Exceptions/target/test-classes/PersonsListTest.class differ diff --git a/person.txt b/person.txt new file mode 100644 index 0000000..0c83d2d --- /dev/null +++ b/person.txt @@ -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'}