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

Finished Lab #41

Open
wants to merge 10 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 Extension/.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 Extension/.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 Extension/.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 Extension/.idea/misc.xml

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

7 changes: 7 additions & 0 deletions Extension/.idea/vcs.xml

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

1 change: 1 addition & 0 deletions Extension/lab-java-exceptions
Submodule lab-java-exceptions added at c30179
31 changes: 31 additions & 0 deletions Extension/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>Extension</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>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
28 changes: 28 additions & 0 deletions Extension/src/main/java/org/example/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.example;

public class Main {
public static void main(String[] args) {

PersonsList personList = new PersonsList();

try{
personList.getList().add(new Person(1, "John Doe", 30, "Software Developer"));
personList.getList().add(new Person(2, "Jane Doe", 25, "Software Developer"));
personList.getList().add(new Person(3, "John Smith", 35, "Wifi Hacker"));

for(Person p : personList.getList()){
Person p1 = personList.findByName(p.getName());
System.out.println("Found: " + p1);

Person clonedPerson = personList.clone(p);
System.out.println("Cloned: " + clonedPerson);

personList.writePersonToFile(p);
}
}
catch (Exception e){
System.out.println("Error: " + e.getMessage());
}

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

import java.util.Objects;

public class Person {

//PRIVATE PROPERTIES
private int id;
private String name;
private int age;
private String occupation;

//CONSTRUCTOR
public Person(int id, String name, int age, String occupation){
try {
setId(id);
setName(name);
setAge(age);
setOccupation(occupation);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

//GETTERS AND SETTERS
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) {
if(age < 0) {
throw new IllegalArgumentException("Age must be greater than 0");
}
this.age = age;
}

public String getOccupation() {
return occupation;
}

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

//METHODS
@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 String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", occupation='" + occupation + '\'' +
'}';
}


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

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

public class PersonList {
private List<Person> list = new ArrayList<>();


//METHODS
public Person findByName(String n) {
if (n.split(" ").length != 2)
throw new IllegalArgumentException("Invalid name format. It should be 'firstName lastName'.");

for (Person p : list) {
if (p.getName().equals(n)) {
return p;
}
}
return null;
}

public Person clone(Person p) {
return new Person(UUID.randomUUID().hashCode(), p.getName(), p.getAge(), p.getOccupation());

}

public void writePersonToFile (Person p) {
try{
FileWriter fileWriter = new FileWriter("person.txt");
PrintWriter printWriter = new PrintWriter(fileWriter);
printWriter.println(p.toString());
printWriter.close();
}catch (IOException e){
System.out.println("Error: " + e.getMessage());
}

}

//ARRAYLIST METHODS
public List<Person> getList(){
return list;
}
}
38 changes: 38 additions & 0 deletions Extension/src/test/java/PersonTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.example;

import org.junit.jupiter.api.Test;

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

class PersonListTest {

@Test
void findByName1() {
PersonList personList = new PersonList();
Person p = new Person(1, "Jenna Doe", 30, "Software Developer");
personList.getList().add(p);
Person foundPerson = personList.findByName("Jenna Doe");
assertEquals(p, foundPerson);
}

@Test
void findByName2() {
PersonList personList = new PersonList();
Person p = new Person(1, "Jenna Doe", 30, "Software Developer");
personList.getList().add(p);
assertThrows(IllegalArgumentException.class, () -> personList.findByName("Jenna_Doe"));
}

@Test
void testClone() {
PersonList personList = new PersonList();
Person p = new Person(1, "John Doe", 30, "Software Developer");
Person clonedPerson = personList.clone(p);
assertEquals(p.getName(), clonedPerson.getName());
assertEquals(p.getAge(), clonedPerson.getAge());
assertEquals(p.getOccupation(), clonedPerson.getOccupation());
assertNotEquals(p.getId(), clonedPerson.getId());


}
}