Skip to content

lab finished #76

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
3 changes: 3 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.

32 changes: 32 additions & 0 deletions lab204/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>com.ironhack</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>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
7 changes: 7 additions & 0 deletions lab204/src/main/java/com/ironhack/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.ironhack;

public class Main {
public static void main(String[] args) {

}
}
67 changes: 67 additions & 0 deletions lab204/src/main/java/com/ironhack/Person.java
Original file line number Diff line number Diff line change
@@ -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
// }
}
48 changes: 48 additions & 0 deletions lab204/src/main/java/com/ironhack/PersonsList.java
Original file line number Diff line number Diff line change
@@ -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<Person> 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());
}
}

}
44 changes: 44 additions & 0 deletions lab204/src/test/java/com/ironhack/PersonTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
90 changes: 90 additions & 0 deletions lab204/src/test/java/com/ironhack/PersonsListTest.java
Original file line number Diff line number Diff line change
@@ -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));
}


}