diff --git a/Extension/.gitignore b/Extension/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/Extension/.gitignore @@ -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 \ No newline at end of file diff --git a/Extension/.idea/.gitignore b/Extension/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/Extension/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/Extension/.idea/encodings.xml b/Extension/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/Extension/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/Extension/.idea/misc.xml b/Extension/.idea/misc.xml new file mode 100644 index 0000000..df00c07 --- /dev/null +++ b/Extension/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/Extension/.idea/vcs.xml b/Extension/.idea/vcs.xml new file mode 100644 index 0000000..68844c5 --- /dev/null +++ b/Extension/.idea/vcs.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/Extension/lab-java-exceptions b/Extension/lab-java-exceptions new file mode 160000 index 0000000..c30179c --- /dev/null +++ b/Extension/lab-java-exceptions @@ -0,0 +1 @@ +Subproject commit c30179c81c9259420be8c44a86c82d9b7f7e32c9 diff --git a/Extension/pom.xml b/Extension/pom.xml new file mode 100644 index 0000000..6659144 --- /dev/null +++ b/Extension/pom.xml @@ -0,0 +1,31 @@ + + + 4.0.0 + + org.example + Extension + 1.0-SNAPSHOT + + + 17 + 17 + UTF-8 + + + + + org.junit.jupiter + junit-jupiter-api + 5.7.1 + test + + + org.junit.jupiter + junit-jupiter-engine + 5.7.1 + test + + + \ No newline at end of file diff --git a/Extension/src/main/java/org/example/Main.java b/Extension/src/main/java/org/example/Main.java new file mode 100644 index 0000000..b39b37f --- /dev/null +++ b/Extension/src/main/java/org/example/Main.java @@ -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()); + } + + } +} diff --git a/Extension/src/main/java/org/example/Person.java b/Extension/src/main/java/org/example/Person.java new file mode 100644 index 0000000..1503bfb --- /dev/null +++ b/Extension/src/main/java/org/example/Person.java @@ -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 + '\'' + + '}'; + } + + +} \ No newline at end of file diff --git a/Extension/src/main/java/org/example/PersonsList.java b/Extension/src/main/java/org/example/PersonsList.java new file mode 100644 index 0000000..0d37f6a --- /dev/null +++ b/Extension/src/main/java/org/example/PersonsList.java @@ -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 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 getList(){ + return list; + } +} \ No newline at end of file diff --git a/Extension/src/test/java/PersonTest.java b/Extension/src/test/java/PersonTest.java new file mode 100644 index 0000000..8e14671 --- /dev/null +++ b/Extension/src/test/java/PersonTest.java @@ -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()); + + + } +} \ No newline at end of file