Skip to content

Last commit #72

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 .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 .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.

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

18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,32 @@ Once you finish the assignment, submit a URL link to your repository or your pul

## Instructions

1. Create a `Person` class that has the following properties:
1. Create a `org.example.Person` class that has the following properties:

- `id`: an integer
- `name`: a string formatted as "firstName lastName"
- `age`: an integer
- `occupation`: a string

The `Person` class should have the following methods:
The `org.example.Person` class should have the following methods:

- A constructor that takes in an integer `id`, a string `name`, an integer `age` and a string `occupation` as arguments and sets their respective properties.
- A `setAge` method that takes in an integer `age` and sets the `age` property, but throws an error if `age` is less than 0.
- An `equals` method that takes in a `Person` object and returns `true` if their properties are the same, excluding the `id` property.
- An `equals` method that takes in a `org.example.Person` object and returns `true` if their properties are the same, excluding the `id` property.

2. Create a `PersonsList` class that holds a list of `Person` objects.
- Create a `findByName` method that takes in a string `name` and returns the `Person` object with a name that matches exactly. The `name` parameter should be formatted as "firstName lastName". This method should throw an exception if the `name` parameter is not properly formatted.
- Create a `clone` method that takes in a `Person` object and returns a new `Person` object with the same properties, except with a new `id`.
- Create a method that takes in a `Person` object as a parameter and uses the `toString` method to write the `Person` information to a file. This method should handle any errors as necessary.
2. Create a `PersonsList` class that holds a list of `org.example.Person` objects.
- Create a `findByName` method that takes in a string `name` and returns the `org.example.Person` object with a name that matches exactly. The `name` parameter should be formatted as "firstName lastName". This method should throw an exception if the `name` parameter is not properly formatted.
- Create a `clone` method that takes in a `org.example.Person` object and returns a new `org.example.Person` object with the same properties, except with a new `id`.
- Create a method that takes in a `org.example.Person` object as a parameter and uses the `toString` method to write the `org.example.Person` information to a file. This method should handle any errors as necessary.

<br>

## Test Cases

1. Test the `setAge` method to ensure that it throws an error if the age is less than 0.
2. Test the `findByName` method to ensure that it properly finds and returns the correct `Person` object when given a properly formatted name.
2. Test the `findByName` method to ensure that it properly finds and returns the correct `org.example.Person` object when given a properly formatted name.
3. Test the `findByName` method to ensure that it throws an exception if the `name` parameter is not properly formatted.
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`.
4. Test the `clone` method to ensure that it creates a new `org.example.Person` object with the same properties as the original, except with a new `id`.

<br>

Expand Down
25 changes: 25 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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>org.example</groupId>
<artifactId>lab-java-exceptions</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<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 src/main/java/org/example/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.example;

public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
58 changes: 58 additions & 0 deletions src/main/java/org/example/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.example;

public class Person {

public static int NEXT_ID = 0;

int id;
String name;
int age;
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 void setAge(int age) throws IllegalArgumentException {
if (age < 0) {
throw new IllegalArgumentException("Age cannot be negative");
}
this.age = age;
}

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 String getOccupation() {
return occupation;
}

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

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

}
47 changes: 47 additions & 0 deletions src/main/java/org/example/PersonsList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.example;

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

public class PersonsList {


private List<Person> persons;

public PersonsList() {
this.persons = new ArrayList<>();
}

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

public Person findByName(String name) throws IllegalArgumentException {
String[] nameParts = name.split(" ");
if (nameParts.length != 2) {
throw new IllegalArgumentException("Name must be formatted as 'firstName lastName'");
}
for (Person person : persons) {
if (person.getName().equals(name)) {
return person;
}
}
return null;
}

public Person clone(Person person) {
return new Person(Person.NEXT_ID++, person.getName(), person.getAge(), person.getOccupation());
}

public void writeToFile(Person person, String filePath) throws IOException {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath, true))) {
writer.write(person.toString());
writer.newLine();
} catch (IOException e) {
System.err.println("Error writing to file: " + e.getMessage());
}
}
}
59 changes: 59 additions & 0 deletions src/test/java/PersonListsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import org.example.Person;
import org.example.PersonsList;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import javax.annotation.processing.SupportedAnnotationTypes;

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

public class PersonListsTest {

private Person person;
private PersonsList personsList;

@BeforeEach
public void setUp() {
person = new Person(Person.NEXT_ID++, "John Doe", 30, "Engineer");
personsList = new PersonsList();
personsList.addPerson(new Person(1, "John Doe", 30, "Engineer"));
personsList.addPerson(new Person(2, "Jane Smith", 25, "Designer"));
}

@Test
public void testSetAge() {
assertThrows(IllegalArgumentException.class, () -> {
person.setAge(-1);
});
}

@Test
public void testFindByName() {
Person foundPerson = personsList.findByName("John Doe");
assertEquals("John Doe", foundPerson.getName());
}

@Test
public void testFindByNameInvalidFormat() throws IllegalArgumentException {
assertThrows(IllegalArgumentException.class, () -> {
personsList.findByName("John");
});
}

@Test
public void testClone() {
Person cloned = personsList.clone(person);
assertNotEquals(person.getId(), cloned.getId());
assertEquals(person.getName(), cloned.getName());
assertEquals(person.getAge(), cloned.getAge());
assertEquals(person.getOccupation(), cloned.getOccupation());
}







}