Skip to content

finished lab #75

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 LABJavaExceptions/.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 LABJavaExceptions/.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 LABJavaExceptions/.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 LABJavaExceptions/.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 LABJavaExceptions/.idea/vcs.xml

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

26 changes: 26 additions & 0 deletions LABJavaExceptions/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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>LABJavaExceptions</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>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.12.0</version>
<scope>test</scope>
</dependency>
</dependencies>

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

import java.util.Objects;

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 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) {
this.age = age;
}

public String getOccupation() {
return occupation;
}

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

public void setMethodAge(int age) {
if (age <= 0) {
throw new IllegalArgumentException("Age must be greater than 0");
}
this.age = age;

}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null|| getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age && Objects.equals(name, person.name) && Objects.equals(occupation, person.occupation);
}
@Override
public int hashCode() {
return Objects.hash(name, age, occupation);
}

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

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

public class PersonsList {
private final List<Person> persons = new ArrayList<>();

public PersonsList() {
persons.add(new Person(1, "Robert Crumb", 79, "Comic Artist"));
persons.add(new Person(2, "Art Spiegelman", 73, "Cartoonist"));
persons.add(new Person(3, "Gilbert Shelton", 77, "Comic Artist"));
persons.add(new Person(4, "Trina Robbins", 75, "Cartoonist"));
persons.add(new Person(5, "Mary Fleener", 65, "Comic Artist"));
persons.add(new Person(6, "Kim Deitch", 75, "Cartoonist"));
}

public List<Person> getPersons() {
return persons;
}

public Person findByName(String name) {
if (!name.matches("^\\S+ \\S+$")) {
throw new IllegalArgumentException("The input is not properly formatted. " +
"Should be formatted as 'firstName lastName'");
}
for (Person person : persons) {
if (person.getName().equalsIgnoreCase(name)) {
return person;
}
}
return null;
}

public Person clone(Person person, int newId) {
if (person == null) {
throw new IllegalArgumentException("The person cannot be null");
}
return new Person(newId, person.getName(), person.getAge(), person.getOccupation());

}

public void writePersonToFile(Person person, String filename) {
try (FileWriter writer = new FileWriter(filename, true)) {
writer.write(person.toString() + System.lineSeparator());
} catch (IOException e) {
System.err.println("Error writing person to file: " + e.getMessage());
}
}

@Override
public String toString() {
return "PersonsList{" +
"persons=" + persons +
'}';
}
}
16 changes: 16 additions & 0 deletions LABJavaExceptions/src/test/java/com/ironhack/PersonTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.ironhack;

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

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

class PersonTest {

@Test
@DisplayName("Should throw an Exception if the age is 0 or negative")
void setMethodAge() {
Person p = new Person(1, "Test User", 25, "Tester");
assertThrows(IllegalArgumentException.class, () -> p.setMethodAge(-1));
}
}
37 changes: 37 additions & 0 deletions LABJavaExceptions/src/test/java/com/ironhack/PersonsListTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.ironhack;

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

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

class PersonsListTest {

@Test
@DisplayName("Should return a valid object Person")
void findByName() {
PersonsList list = new PersonsList();
Person person = list.findByName("Mary Fleener");
assertNotNull(person);
assertEquals("Mary Fleener", person.getName());
}
@Test
@DisplayName("Should return a exception if the formatted is incorrect")
void findByNameIncorrect() {
PersonsList list = new PersonsList();
assertThrows(IllegalArgumentException.class, () -> list.findByName("Mary"));
assertThrows(IllegalArgumentException.class, () -> list.findByName("Mary Fleener Plus"));
}

@Test
void testClone() {
PersonsList list = new PersonsList();
Person original = list.findByName("Mary Fleener");
Person clone = list.clone(original, 99);
assertNotNull(clone);
assertEquals(original.getName(), clone.getName());
assertEquals(original.getAge(), clone.getAge());
assertEquals(original.getOccupation(), clone.getOccupation());
assertNotEquals(original.getId(), clone.getId());
}
}