Skip to content

Lab4 #54

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

Lab4 #54

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
29 changes: 29 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
15 changes: 15 additions & 0 deletions Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
public static void main(String[] args) {
//TIP Press <shortcut actionId="ShowIntentionActions"/> with your caret at the highlighted text
// to see how IntelliJ IDEA suggests fixing it.
System.out.printf("Hello and welcome!");

for (int i = 1; i <= 5; i++) {
//TIP Press <shortcut actionId="Debug"/> to start debugging your code. We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/> breakpoint
// for you, but you can always add more by pressing <shortcut actionId="ToggleLineBreakpoint"/>.
System.out.println("i = " + i);
}
}
}
60 changes: 60 additions & 0 deletions Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import java.util.Objects;

public class Person {
private int id;
private String name;
private int age;
private 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 int getId() {
return id;
}

public String getName() {
return name;
}

public int getAge() {
return age;
}

public String getOccupation() {
return occupation;
}

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

@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 int hashCode() {
return Objects.hash(name, age, occupation);
}

@Override
public String toString() {
return String.format("ID: %d, Name: %s, Age: %d, Occupation: %s", id, name, age, occupation);
}
}


55 changes: 55 additions & 0 deletions PersonTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class PersonTest {

@Test
public void testSetAgeThrowsException() {
Person person = new Person(1, "John Doe", 25, "Engineer");
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
person.setAge(-1);
});

String expectedMessage = "Age cannot be less than 0";
String actualMessage = exception.getMessage();

assertTrue(actualMessage.contains(expectedMessage));
}

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

assertEquals(person, personsList.findByName("John Doe"));
}

@Test
public void testFindByNameThrowsException() {
PersonsList personsList = new PersonsList();

Exception exception = assertThrows(IllegalArgumentException.class, () -> {
personsList.findByName("JohnDoe");
});

String expectedMessage = "Name must be formatted as 'firstName lastName'";
String actualMessage = exception.getMessage();

assertTrue(actualMessage.contains(expectedMessage));
}

@Test
public void testClonePerson() {
Person original = new Person(1, "John Doe", 25, "Engineer");
PersonsList personsList = new PersonsList();
Person clone = personsList.clone(original, 2);

assertEquals(original.getName(), clone.getName());
assertEquals(original.getAge(), clone.getAge());
assertEquals(original.getOccupation(), clone.getOccupation());
assertNotEquals(original.getId(), clone.getId());
}
}


39 changes: 39 additions & 0 deletions PersonsList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

public class PersonsList {
private List<Person> persons = new ArrayList<>();

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

public Person findByName(String name) {
if (!Pattern.matches("^[A-Za-z]+ [A-Za-z]+$", name)) {
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, int newId) {
return new Person(newId, person.getName(), person.getAge(), person.getOccupation());
}

public void writePersonToFile(Person person, String fileName) {
try (FileWriter writer = new FileWriter(fileName)) {
writer.write(person.toString());
} catch (IOException e) {
System.err.println("Error writing to file: " + e.getMessage());
}
}
}


15 changes: 15 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
public static void main(String[] args) {
//TIP Press <shortcut actionId="ShowIntentionActions"/> with your caret at the highlighted text
// to see how IntelliJ IDEA suggests fixing it.
System.out.printf("Hello and welcome!");

for (int i = 1; i <= 5; i++) {
//TIP Press <shortcut actionId="Debug"/> to start debugging your code. We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/> breakpoint
// for you, but you can always add more by pressing <shortcut actionId="ToggleLineBreakpoint"/>.
System.out.println("i = " + i);
}
}
}
60 changes: 60 additions & 0 deletions src/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import java.util.Objects;

public class Person {
private int id;
private String name;
private int age;
private 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 int getId() {
return id;
}

public String getName() {
return name;
}

public int getAge() {
return age;
}

public String getOccupation() {
return occupation;
}

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

@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 int hashCode() {
return Objects.hash(name, age, occupation);
}

@Override
public String toString() {
return String.format("ID: %d, Name: %s, Age: %d, Occupation: %s", id, name, age, occupation);
}
}


55 changes: 55 additions & 0 deletions src/PersonTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class PersonTest {

@Test
public void testSetAgeThrowsException() {
Person person = new Person(1, "John Doe", 25, "Engineer");
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
person.setAge(-1);
});

String expectedMessage = "Age cannot be less than 0";
String actualMessage = exception.getMessage();

assertTrue(actualMessage.contains(expectedMessage));
}

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

assertEquals(person, personsList.findByName("John Doe"));
}

@Test
public void testFindByNameThrowsException() {
PersonsList personsList = new PersonsList();

Exception exception = assertThrows(IllegalArgumentException.class, () -> {
personsList.findByName("JohnDoe");
});

String expectedMessage = "Name must be formatted as 'firstName lastName'";
String actualMessage = exception.getMessage();

assertTrue(actualMessage.contains(expectedMessage));
}

@Test
public void testClonePerson() {
Person original = new Person(1, "John Doe", 25, "Engineer");
PersonsList personsList = new PersonsList();
Person clone = personsList.clone(original, 2);

assertEquals(original.getName(), clone.getName());
assertEquals(original.getAge(), clone.getAge());
assertEquals(original.getOccupation(), clone.getOccupation());
assertNotEquals(original.getId(), clone.getId());
}
}


39 changes: 39 additions & 0 deletions src/PersonsList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

public class PersonsList {
private List<Person> persons = new ArrayList<>();

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

public Person findByName(String name) {
if (!Pattern.matches("^[A-Za-z]+ [A-Za-z]+$", name)) {
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, int newId) {
return new Person(newId, person.getName(), person.getAge(), person.getOccupation());
}

public void writePersonToFile(Person person, String fileName) {
try (FileWriter writer = new FileWriter(fileName)) {
writer.write(person.toString());
} catch (IOException e) {
System.err.println("Error writing to file: " + e.getMessage());
}
}
}