Skip to content
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

Master #53

Open
wants to merge 3 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
38 changes: 38 additions & 0 deletions .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
8 changes: 8 additions & 0 deletions .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 .idea/encodings.xml

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

15 changes: 15 additions & 0 deletions .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 .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 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>lab-java-exceptions</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>22</maven.compiler.source>
<maven.compiler.target>22</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.11.0-M1</version>
<scope>test</scope>
</dependency>
</dependencies>

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

public class Main {

public static void main(String[] args) {
}

}
71 changes: 71 additions & 0 deletions src/main/java/com/ironhack/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.ironhack;

import java.util.Objects;

public class Person {

// properties
private int id;
private String name;
private int age;
private String occupation;

// methods
public Person(int id, String name, int age, String occupation) {
this.id = id;
this.age = age;
this.occupation = occupation;

String[] parts = name.split(" ");
if (parts.length < 2)
throw new IllegalArgumentException("name should follow firstName lastName format");
else
this.name = name;
}

public void setAge(int age) {
if (age < 0)
throw new IllegalArgumentException("age cannot be negative");
else
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);
}

public int getId() {
return id;
}

public String getName() {
return name;
}

public int getAge() {
return age;
}

public String getOccupation() {
return occupation;
}

public Person() {
}

@Override
public String toString() {
final StringBuffer sb = new StringBuffer("Person{");
sb.append("id=").append(id);
sb.append(", name='").append(name).append('\'');
sb.append(", age=").append(age);
sb.append(", occupation='").append(occupation).append('\'');
sb.append('}');
return sb.toString();
}

}
57 changes: 57 additions & 0 deletions src/main/java/com/ironhack/PersonList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.ironhack;

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

public class PersonList {

private List<Person> personList;

public PersonList() {
this.personList = new ArrayList<>();
}

public void addPerson(Person person) {
personList.add(person);
}

public void removePerson(Person person) {
personList.remove(person);
}

public Person getPerson(int index) {
return personList.get(index);
}

public Person findByName(String name) {
String[] parts = name.split(" ");
if (parts.length < 2)
throw new IllegalArgumentException("name should follow firstName lastName format");
for (Person person : personList) {
if (person.getName().equals(name))
return person;
}
return null;
}


public Person clone(Person person, int id) {
if (person.getId() == id)
throw new IllegalArgumentException("Please use a different ID");
else
return new Person(id, person.getName(), person.getAge(), person.getOccupation());
}

public void writePersonToFile(Person person) {
try {
FileWriter writer = new FileWriter("person.txt");
writer.write(person.toString());
writer.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

}
45 changes: 45 additions & 0 deletions src/test/java/com/ironhack/PersonListTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.ironhack;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

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

class PersonListTest {

@Test
void findByName_ReturnsCorrectObject() {
// Arrange
Person samplePerson = new Person(1, "firstname lastname", 99, "occupation");
PersonList samplePersonList = new PersonList();
samplePersonList.addPerson(samplePerson);
// Assert
assertEquals(samplePerson, samplePersonList.findByName("firstname lastname"));
}

@Test
void findByName_ThrowsException() {
// Arrange
PersonList samplePersonList = new PersonList();
// Assert
assertThrows(IllegalArgumentException.class, () -> samplePersonList.findByName("firstname"));
}

@Test
void Clone() {
// Arrange
Person samplePerson = new Person(1, "firstname lastname", 99, "occupation");
PersonList samplePersonList = new PersonList();
samplePersonList.addPerson(samplePerson);
// Act
Person clonedPerson = samplePersonList.clone(samplePerson, 2);
// Assert
assertEquals(samplePerson, clonedPerson);
assertNotEquals(samplePerson.getId(), clonedPerson.getId());
}

}
17 changes: 17 additions & 0 deletions src/test/java/com/ironhack/PersonTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.ironhack;

import org.junit.jupiter.api.Test;

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

class PersonTest {

@Test
void setAge_ThrowsErrorWhenAgeIsLessThanZero() {
// Arrange
Person person = new Person();
// Assert
assertThrows(IllegalArgumentException.class, () -> person.setAge(-1));
}

}