Skip to content

update #55

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
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
3 changes: 3 additions & 0 deletions .idea/.gitignore

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

37 changes: 37 additions & 0 deletions .idea/Exceptions - Week 2 Lab.iml

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

6 changes: 6 additions & 0 deletions .idea/misc.xml

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

8 changes: 8 additions & 0 deletions .idea/modules.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.

71 changes: 71 additions & 0 deletions Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
public class Person {
private int id;
private String name;
private int age;
private String occupation;

//constructor with parameters

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 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) {
this.age = age;
}

public String getOccupation() {
return occupation;
}

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

@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", occupation='" + occupation + '\'' +
'}';
}

@Override
public boolean equals(Object obj) {
return super.equals(obj);
}

@Override
public int hashCode() {
return super.hashCode();
}

public Person() {
super();
}
}
17 changes: 17 additions & 0 deletions PersonTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

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

@Test
public void testEquals() {
Person person1 = new Person(1, "John Doe", 25, "Engineer");
Person person2 = new Person(2, "John Doe", 25, "Engineer");
assertTrue(person1.equals(person2));
}
}
63 changes: 63 additions & 0 deletions PersonsList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import java.io.BufferedWriter;
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;

public PersonsList() {
this.persons = new ArrayList<>();
}
// Method to find Person by Name
public Person findByName(String name) throws IllegalArgumentException {
if (!isValidName(name)) {
throw new IllegalArgumentException("Name must be in 'firstName lastName' format.");
}

for (Person person : persons) {
if (person.getName().equals(name)) {
return person;
}
}
return null;
}

private boolean isValidName(String name) {
// Simple regex to validate 'firstName lastName' format
return Pattern.matches("[A-Za-z]+ [A-Za-z]+", name);
}

// Method to clone a Person with a new ID
public Person clone(Person person) {
// Increment ID by 1 for the new clone (simple approach)
return new Person(generateNewId(), person.getName(), person.getAge(), person.getOccupation());
}

private int generateNewId() {
// Generate a new ID based on existing IDs in the list (simple approach)
int newId = 1;
for (Person person : persons) {
if (person.getId() >= newId) {
newId = person.getId() + 1;
}
}
return newId;
}

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

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

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

public class PersonsListTest {
@Test
public void testFindByName() {
PersonsList list = new PersonsList();
Person person = new Person(1, "John Doe", 25, "Engineer");
list.addPerson(person);

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

@Test
public void testFindByNameInvalidFormat() {
PersonsList list = new PersonsList();
assertThrows(IllegalArgumentException.class, () -> list.findByName("JohnDoe"));
}

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

assertNotEquals(original.getId(), clone.getId());
assertEquals(original.getName(), clone.getName());
assertEquals(original.getAge(), clone.getAge());
assertEquals(original.getOccupation(), clone.getOccupation());
}
}
3 changes: 3 additions & 0 deletions out/production/Exceptions - Week 2 Lab/.idea/.gitignore

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

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

6 changes: 6 additions & 0 deletions out/production/Exceptions - Week 2 Lab/.idea/misc.xml

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

8 changes: 8 additions & 0 deletions out/production/Exceptions - Week 2 Lab/.idea/modules.xml

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

6 changes: 6 additions & 0 deletions out/production/Exceptions - Week 2 Lab/.idea/vcs.xml

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

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading