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

Laboratory 2.04 is finished. #44

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 Lab2.04.new/.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 Lab2.04.new/.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 Lab2.04.new/.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 Lab2.04.new/.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 Lab2.04.new/.idea/vcs.xml

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

28 changes: 28 additions & 0 deletions Lab2.04.new/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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>Ironhack.schl</groupId>
<artifactId>Lab2.04.new</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.10.2</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
60 changes: 60 additions & 0 deletions Lab2.04.new/src/main/java/Ironhack/schl/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package Ironhack.schl;

public class Person {
private int id;
private String name;
private int age;
private String occupation;

//Constructor

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

public void setAge(int age) throws IllegalArgumentException {
if (age < 0) {
throw new IllegalArgumentException("A non-negative age value is required.");
} else {
this.age = age;
}
}

public String getName() {
return name;
}

public int getAge() {
return age;
}

public String getOccupation() {
return occupation;
}

public int getId() {
return id;
}


public boolean equals(Person otherPerson) {
return this.name.equals(otherPerson.name) &&
this.age == otherPerson.age &&
this.occupation.equals(otherPerson.occupation);
}

@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", occupation='" + occupation + '\'' +
'}';
}
}


47 changes: 47 additions & 0 deletions Lab2.04.new/src/main/java/Ironhack/schl/PersonList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package Ironhack.schl;

import java.util.List;
import java.util.stream.Collectors;

public class PersonList {
private List<Person> personList;

public PersonList(List<Person> personList) {
this.personList = personList;
}

public Person findByName(String name) throws IllegalArgumentException {
String[] fullName = name.split(" ");
if (fullName.length != 2) {
throw new IllegalArgumentException("Please enter your first and last name separated by a space.");
}
String firstName = fullName[0];
String lastName = fullName[1];

return personList.stream()
.filter(person -> person.getName().equals(firstName + " " + lastName))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("The person was not found."));
}


public Person clone(Person person) {
int newId = personList.stream()
.mapToInt(Person::getId)
.max()
.orElse(0) + 1;

return new Person(newId, person.getName(), person.getAge(), person.getOccupation());
}

public void writeToFile(Person person, String filePath) {
try {
java.io.FileWriter fileWriter = new java.io.FileWriter(filePath);
fileWriter.write(person.toString());
fileWriter.close();
} catch (Exception e) {
System.out.println("An error occurred when writing to file: " + e.getMessage());

}
}
}
40 changes: 40 additions & 0 deletions Lab2.04.new/src/test/java/PersonListTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import Ironhack.schl.Person;
import Ironhack.schl.PersonList;
import org.junit.jupiter.api.Test;

import java.util.List;

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

public class PersonListTest {
@Test
public void testFindByName() throws IllegalArgumentException {

Person person1 = new Person(407809, "Rori Donovan", 35, "Engineer");
PersonList personList1 = new PersonList(List.of(person1));
// To verify that the method returns the correct person when the name is found.
assertEquals(person1, personList1.findByName("Rori Donovan"));
}
@Test
public void testFindByName_ImproperFormatting() throws IllegalArgumentException {

Person person2 = new Person(407809, "DeidreLlewellyn", 55, "Physicist");
PersonList personList2 = new PersonList(List.of(person2));
// To verify that the method throws an exception when the name is not found.
assertThrows(IllegalArgumentException.class, () -> personList2.findByName("Deidre Llewellyn"));
}
@Test
public void testClone() throws IllegalArgumentException {

Person person1 = new Person(407809, "Rori Donovan", 35, "Engineer");
PersonList personList1 = new PersonList(List.of(person1));
//To verify that the method clones the person's name, age, and occupation correctly.
// The id should differ from the original person.
Person person3 = personList1.clone(person1);
assertEquals(person1.getName(), person3.getName());
assertEquals(person1.getAge(), person3.getAge());
assertEquals(person1.getOccupation(), person3.getOccupation());
assertNotEquals(person1.getId(), person3.getId());
}

}
24 changes: 24 additions & 0 deletions Lab2.04.new/src/test/java/PersonTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Ironhack.schl.Person;
import Ironhack.schl.PersonList;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class PersonTest {

@Test
public void testSetAge() throws IllegalArgumentException {
// To verify that the method sets the age correctly.
Person person = new Person(407809, "Rori Donovan", 35, "Engineer");
assertEquals(35, person.getAge());
// To verify that the method throws an exception when the age is negative.
assertThrows(IllegalArgumentException.class, () -> person.setAge(-36));

// To verify that the method sets an updated age correctly.
person.setAge(36);
assertEquals(36, person.getAge());
}
}