From db62fdd99901d2480434f5e73cef10711adce79b Mon Sep 17 00:00:00 2001 From: Ignacio Talento Date: Mon, 5 May 2025 17:00:48 +0200 Subject: [PATCH] lab finished --- lab204/.gitignore | 38 ++++++++ lab204/.idea/.gitignore | 3 + lab204/.idea/encodings.xml | 7 ++ lab204/.idea/misc.xml | 14 +++ lab204/.idea/vcs.xml | 6 ++ lab204/pom.xml | 32 +++++++ lab204/src/main/java/com/ironhack/Main.java | 7 ++ lab204/src/main/java/com/ironhack/Person.java | 67 ++++++++++++++ .../main/java/com/ironhack/PersonsList.java | 48 ++++++++++ .../test/java/com/ironhack/PersonTest.java | 44 +++++++++ .../java/com/ironhack/PersonsListTest.java | 90 +++++++++++++++++++ 11 files changed, 356 insertions(+) create mode 100644 lab204/.gitignore create mode 100644 lab204/.idea/.gitignore create mode 100644 lab204/.idea/encodings.xml create mode 100644 lab204/.idea/misc.xml create mode 100644 lab204/.idea/vcs.xml create mode 100644 lab204/pom.xml create mode 100644 lab204/src/main/java/com/ironhack/Main.java create mode 100644 lab204/src/main/java/com/ironhack/Person.java create mode 100644 lab204/src/main/java/com/ironhack/PersonsList.java create mode 100644 lab204/src/test/java/com/ironhack/PersonTest.java create mode 100644 lab204/src/test/java/com/ironhack/PersonsListTest.java diff --git a/lab204/.gitignore b/lab204/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/lab204/.gitignore @@ -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 \ No newline at end of file diff --git a/lab204/.idea/.gitignore b/lab204/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/lab204/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/lab204/.idea/encodings.xml b/lab204/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/lab204/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/lab204/.idea/misc.xml b/lab204/.idea/misc.xml new file mode 100644 index 0000000..df00c07 --- /dev/null +++ b/lab204/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/lab204/.idea/vcs.xml b/lab204/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/lab204/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/lab204/pom.xml b/lab204/pom.xml new file mode 100644 index 0000000..ac6885c --- /dev/null +++ b/lab204/pom.xml @@ -0,0 +1,32 @@ + + + 4.0.0 + + com.ironhack + lab204 + 1.0-SNAPSHOT + + + 17 + 17 + UTF-8 + + + + + junit + junit + RELEASE + test + + + org.junit.jupiter + junit-jupiter + RELEASE + test + + + + \ No newline at end of file diff --git a/lab204/src/main/java/com/ironhack/Main.java b/lab204/src/main/java/com/ironhack/Main.java new file mode 100644 index 0000000..7c9e44a --- /dev/null +++ b/lab204/src/main/java/com/ironhack/Main.java @@ -0,0 +1,7 @@ +package com.ironhack; + +public class Main { + public static void main(String[] args) { + + } +} \ No newline at end of file diff --git a/lab204/src/main/java/com/ironhack/Person.java b/lab204/src/main/java/com/ironhack/Person.java new file mode 100644 index 0000000..6c46365 --- /dev/null +++ b/lab204/src/main/java/com/ironhack/Person.java @@ -0,0 +1,67 @@ +package com.ironhack; + +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) { + this.id = id; + this.name = name; + this.age = age; + this.occupation = occupation; + } + + public int getId() { + return id; + } + + public String getName() { + return name; + } + + public int getAge() { + return age; + } + + public String getOccupation() { + return occupation; + } + + public void setAge(int age) { + if (age < 0) { + throw new IllegalArgumentException("Age cannot be negative"); + } + this.age = age; + } + + @Override + public String toString() { + return "Person{" + + "id=" + id + + ", name='" + name + '\'' + + ", age=" + age + + ", occupation='" + occupation + '\'' + + '}'; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null || getClass() != obj.getClass()) return false; + + Person person = (Person) obj; + + if (age != person.age) return false; + if (!name.equals(person.name)) return false; + return occupation.equals(person.occupation); + } + + // Example usage: + // public static void main(String[] args) { + // Person person1 = new Person(1, "John Doe", 30, "Developer"); + // Person person2 = new Person(2, "John Doe", 30, "Developer"); + // System.out.println(person1.equals(person2)); // true + // } +} \ No newline at end of file diff --git a/lab204/src/main/java/com/ironhack/PersonsList.java b/lab204/src/main/java/com/ironhack/PersonsList.java new file mode 100644 index 0000000..3f7437a --- /dev/null +++ b/lab204/src/main/java/com/ironhack/PersonsList.java @@ -0,0 +1,48 @@ +package com.ironhack; + +import java.io.BufferedWriter; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Pattern; + +public class PersonsList { + private List persons; + + public PersonsList() { + persons = new ArrayList<>(); + } + + public void addPerson(Person person) { + persons.add(person); + } + + public Person findByName(String name) { + // Validate name format (firstName lastName) + if (!Pattern.matches("^[A-Za-z]+ [A-Za-z]+$", name)) { + throw new IllegalArgumentException("Name must be formatted as 'firstName lastName'"); + } + + for (Person person : persons) { + if (person.getName().equals(name)) { + return person; + } + } + + throw new RuntimeException("No person found with name: " + name); + } + + public Person clone(Person person, int newId) { + return new Person(newId, person.getName(), person.getAge(), person.getOccupation()); + } + + public void writePersonToFile(Person person, String fileName) { + try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) { + writer.write(person.toString()); + } catch (IOException e) { + System.err.println("Error writing to file: " + e.getMessage()); + } + } + +} diff --git a/lab204/src/test/java/com/ironhack/PersonTest.java b/lab204/src/test/java/com/ironhack/PersonTest.java new file mode 100644 index 0000000..06364c2 --- /dev/null +++ b/lab204/src/test/java/com/ironhack/PersonTest.java @@ -0,0 +1,44 @@ +package com.ironhack; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +public class PersonTest { + + private Person person; + private PersonsList personsList; + + @BeforeEach + public void setUp() { + // Create a sample person and a persons list + person = new Person(1, "John Doe", 30, "Developer"); + personsList = new PersonsList(); + personsList.addPerson(person); + personsList.addPerson(new Person(2, "Jane Smith", 25, "Designer")); + } + + // Test case 1: Test the setAge method to ensure that it throws an error if the age is less than 0 + @Test + public void testSetAgeWithNegativeValue() { + // Attempt to set a negative age + Exception exception = assertThrows(IllegalArgumentException.class, () -> { + person.setAge(-5); + }); + + // Verify the exception message + String expectedMessage = "Age cannot be negative"; + String actualMessage = exception.getMessage(); + assertTrue(actualMessage.contains(expectedMessage)); + } + + // Test case 1 extension: Verify that valid age updates work correctly + @Test + public void testSetAgeWithValidValue() { + // Set a valid age + person.setAge(35); + + // Verify the age was updated + assertEquals(35, person.getAge()); + } +} \ No newline at end of file diff --git a/lab204/src/test/java/com/ironhack/PersonsListTest.java b/lab204/src/test/java/com/ironhack/PersonsListTest.java new file mode 100644 index 0000000..ee652cf --- /dev/null +++ b/lab204/src/test/java/com/ironhack/PersonsListTest.java @@ -0,0 +1,90 @@ +package com.ironhack; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class PersonsListTest { + + private Person person; + private PersonsList personsList; + + @BeforeEach + public void setUp() { + // Create a sample person and a persons list for testing + person = new Person(1, "John Doe", 30, "Developer"); + personsList = new PersonsList(); + personsList.addPerson(person); + personsList.addPerson(new Person(2, "Jane Smith", 25, "Designer")); + } + + // Test case 2: Test the findByName method to ensure that it properly finds and returns the correct Person object when given a properly formatted name + @Test + public void testFindByNameWithValidName() { + // Find a person by name + Person foundPerson = personsList.findByName("John Doe"); + + // Verify the found person has the correct properties + assertEquals(1, foundPerson.getId()); + assertEquals("John Doe", foundPerson.getName()); + assertEquals(30, foundPerson.getAge()); + assertEquals("Developer", foundPerson.getOccupation()); + } + + // Test case 3: Test the findByName method to ensure that it throws an exception if the name parameter is not properly formatted + @Test + public void testFindByNameWithInvalidFormat() { + // Test with various improperly formatted names + String[] invalidNames = { + "JohnDoe", // No space + "John", // Only first name + "John Doe Jr", // More than firstName lastName + "123 456", // Numbers, not letters + " John Doe", // Leading space + "John Doe " // Trailing space + }; + + for (String invalidName : invalidNames) { + Exception exception = assertThrows(IllegalArgumentException.class, () -> { + personsList.findByName(invalidName); + }); + + String expectedMessage = "Name must be formatted as 'firstName lastName'"; + assertTrue(exception.getMessage().contains(expectedMessage)); + } + } + + // Test case 3 extension: Test that findByName throws an exception when no matching person exists + @Test + public void testFindByNameWithNonExistentName() { + Exception exception = assertThrows(RuntimeException.class, () -> { + personsList.findByName("Alice Johnson"); + }); + + String expectedMessage = "No person found with name: Alice Johnson"; + assertEquals(expectedMessage, exception.getMessage()); + } + + // Test case 4: Test the clone method to ensure that it creates a new Person object with the same properties as the original, except with a new id + @Test + public void testClone() { + // Clone the person with a new ID + int newId = 99; + Person clonedPerson = personsList.clone(person, newId); + + // Verify the cloned person has the same properties except ID + assertEquals(newId, clonedPerson.getId()); + assertEquals(person.getName(), clonedPerson.getName()); + assertEquals(person.getAge(), clonedPerson.getAge()); + assertEquals(person.getOccupation(), clonedPerson.getOccupation()); + + // Verify it's a different object instance + assertNotSame(person, clonedPerson); + + // Verify equals method works correctly (should return true since it ignores ID) + assertTrue(person.equals(clonedPerson)); + } + + +} \ No newline at end of file