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

Week2Lab2 #52

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

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.

31 changes: 31 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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>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 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!");
}
}
60 changes: 60 additions & 0 deletions src/main/java/org/example/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.example;

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;
this.setAge(age);
this.occupation = occupation;
}

// Setters and Getters
public int getId() {
return id;
}

public String getName() {
return name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
if (age < 0) {
throw new IllegalArgumentException("Age cannot be less than 0");
}
this.age = age;
}

public String getOccupation() {
return occupation;
}

// Equals Method
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
Person person = (Person) obj;
return age == person.age &&
name.equals(person.name) &&
occupation.equals(person.occupation);
}

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

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

public class PersonList {

public static class PersonsList {
private List<Person> persons;

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

// Method to add a person to the list
public void addPerson(Person person) {
persons.add(person);
}

// Method to find a person by name
public Person findByName(String name) {
if (!name.contains(" ")) {
throw new IllegalArgumentException("Name must be formatted as 'firstName lastName'");
}
for (Person person : persons) {
if (person.getName().equals(name)) {
return person;
}
}
return null;
}

// Method to clone a person
public Person clone(Person person) {
return new Person(persons.size() + 1, person.getName(), person.getAge(), person.getOccupation());
}

// Method to write person to file
public void writePersonToFile(Person person, String filename) {
try (FileWriter writer = new FileWriter(filename)) {
writer.write(person.toString());
} catch (IOException e) {
e.printStackTrace();
}


}
}

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

import org.junit.jupiter.api.Test;

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

class PersonTest {

@Test
public void testSetAge() {
Person person = new Person(1, "John Doe", 25, "Engineer");
person.setAge(30);
assertEquals(30, person.getAge());

// Test throwing exception
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
person.setAge(-1);
});
assertEquals("Age cannot be less than 0", exception.getMessage());
}

@Test
public void testFindByName() {
PersonList.PersonsList personsList = new PersonList.PersonsList();
Person john = new Person(1, "John Doe", 25, "Engineer");
personsList.addPerson(john);

// Test finding person by name
Person foundPerson = personsList.findByName("John Doe");
assertEquals(john, foundPerson);

// Test throwing exception for improperly formatted name
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
personsList.findByName("John");
});
assertEquals("Name must be formatted as 'firstName lastName'", exception.getMessage());
}

@Test
public void testClonePerson() {
PersonList.PersonsList personsList = new PersonList.PersonsList();
Person john = new Person(1, "John Doe", 25, "Engineer");
personsList.addPerson(john);

// Test cloning
Person clonedJohn = personsList.clone(john);
assertEquals(john, clonedJohn);
}
}