Skip to content
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
39 changes: 39 additions & 0 deletions lab03/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
.idea/

### 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
32 changes: 32 additions & 0 deletions lab03/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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>lab03</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 lab03/src/main/java/org/example/CorrectName.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.example;

public class CorrectName extends RuntimeException {
public CorrectName (String message) {
super(message);
}
}
12 changes: 12 additions & 0 deletions lab03/src/main/java/org/example/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.example;

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

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

}

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

import java.util.Objects;

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

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

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) throws RangeValues{
if(age<0){
throw new RangeValues("This value is less than 0.");
}
this.age = age;
}

public String getOccupation() {
return occupation;
}

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

@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return Objects.equals(name, person.name) && Objects.equals(age, person.age) && Objects.equals(occupation, person.occupation);
}

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

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

public class PersonsList {
private List<Person> myList;

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

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

public List<Person> getPersons() {
return this.myList;
}

public Person findByName(String name){
Person myPerson = null;
if(!name.contains(" ")){
throw new CorrectName("The name format is incorrect.");
}else{
for (int i=0; i<this.myList.size(); i++){
if(name.equals(this.myList.get(i).getName())){
myPerson = myList.get(i);
}
}
}

return myPerson;
}

public Person clone(Person person){
Person myPerson;
int newId;

newId = person.getId() +1;
myPerson = new Person(newId, person.getName(), person.getAge(), person.getOccupation());

return myPerson;
}

public void writeToPerson(Person person) throws IOException {
String filepath = "src/main/java/org/example/file.txt";

File file = new File(filepath);
FileWriter fileToString = new FileWriter(filepath, true);
fileToString.write(person.toString());
fileToString.close();


}

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

public class RangeValues extends RuntimeException {
public RangeValues(String message) {
super(message);
}
}

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

import org.junit.jupiter.api.Test;

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

class PersonTest {

@Test
void setAge_LessZero_Error() {
assertThrows(RangeValues.class, () -> {
Person myPerson1 = new Person (1234,"John Hood", -1, "Policeman");
myPerson1.setAge(-1);
});
}
}
39 changes: 39 additions & 0 deletions lab03/src/test/java/org/example/PersonsListTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.example;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;
class PersonsListTest {
private PersonsList myList = new PersonsList();

@BeforeEach
public void setUp() {
Person myPerson1 = new Person(1234, "John Hood", 34, "Policeman");
Person myPerson2 = new Person(4536, "Anais Nin", 66, "writer");
Person myPerson3 = new Person(7489, "Marry Kain", 54, "waiter");

myList.addPerson(myPerson1);
myList.addPerson(myPerson2);
myList.addPerson(myPerson3);
}

@Test
void findByName_CorrectNameFormat_Correct() {
assertEquals(myList.getPersons().get(1), myList.findByName("Anais Nin"));
}

@Test
void findByName_IncorrectNameFormat_Error() {
assertThrows(CorrectName.class, () -> {
myList.findByName("Anais");
});
}

@Test
void testClone_InstancePerson_Correct() {
assertTrue(myList.getPersons().get(2).equals(new Person (7489,"Marry Kain", 54,"waiter")) &&
myList.getPersons().get(2).getId()!=myList.clone(myList.getPersons().get(2)).getId());

}
}