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

done #56

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

done #56

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
7 changes: 7 additions & 0 deletions src/main/java/com.ironhack/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main.java.com.ironhack;

public class Main {
public static void main(String[] args) {
PersonTest.main(args);
}
}
56 changes: 56 additions & 0 deletions src/main/java/com.ironhack/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main.java.com.ironhack;

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 void setAge(int age) {
if (age < 0) {
throw new IllegalArgumentException("Age cannot be less than 0");
}
this.age = age;
}

public boolean equals(Person other) {
if (other == null) return false;
return this.name.equals(other.name) &&
this.age == other.age &&
this.occupation.equals(other.occupation);
}

public Person clone(int newId) {
return new Person(newId, this.name, this.age, this.occupation);
}

public void writeToFile(String filename) {
try (java.io.FileWriter writer = new java.io.FileWriter(filename)) {
writer.write(this.toString());
} catch (java.io.IOException e) {
System.out.println("An error occurred while writing to file: " + e.getMessage());
}
}

@Override
public String toString() {
return "ID: " + this.id + ", Name: " + this.name + ", Age: " + this.age + ", Occupation: " + this.occupation;
}

public static boolean isValidName(String name) {
return name != null && name.matches("^[A-Za-z]+\\s[A-Za-z]+$");
}

public int getId() { return id; }
public String getName() { return name; }
public int getAge() { return age; }
public String getOccupation() { return occupation; }
}

42 changes: 42 additions & 0 deletions src/main/java/com.ironhack/PersonTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main.java.com.ironhack;

public class PersonTest {
public static void main(String[] args) {
try {
Person p1 = new Person(1, "John Doe", 30, "Engineer");
p1.setAge(-1);
} catch (IllegalArgumentException e) {
System.out.println("Test Passed: " + e.getMessage());
}

Person p1 = new Person(1, "John Doe", 30, "Engineer");
Person p2 = new Person(2, "Jane Doe", 28, "Doctor");
Person p3 = new Person(3, "John Doe", 30, "Engineer");
System.out.println("p1 equals p3: " + p1.equals(p3));
System.out.println("p1 equals p2: " + p1.equals(p2));

Person p4 = p1.clone(4);
System.out.println("Cloned Person: " + p4);

p1.writeToFile("person.txt");

PersonsList pl = new PersonsList();
pl.addPerson(new Person(1, "John Doe", 30, "Engineer"));
pl.addPerson(new Person(2, "Jane Doe", 28, "Doctor"));

try {
Person foundPerson = pl.findByName("John Doe");
System.out.println("Found Person: " + foundPerson);

pl.findByName("JohnDoe");
} catch (IllegalArgumentException e) {
System.out.println("Test Passed: " + e.getMessage());
}

Person clonedPerson = pl.clonePerson(p1, 5);
System.out.println("Cloned Person with new ID: " + clonedPerson);

pl.writePersonToFile(p1, "person_test.txt");
}
}

40 changes: 40 additions & 0 deletions src/main/java/com.ironhack/PersonsList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main.java.com.ironhack;

import java.util.ArrayList;
import java.util.List;

public class PersonsList {
private List<Person> persons;

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

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

public Person findByName(String name) {
if (!Person.isValidName(name)) {
throw new IllegalArgumentException("Name must be in the format 'firstName lastName'");
}
for (Person person : persons) {
if (person.getName().equals(name)) {
return person;
}
}
return null;
}

public Person clonePerson(Person person, int newId) {
return person.clone(newId);
}

public void writePersonToFile(Person person, String filename) {
person.writeToFile(filename);
}

public List<Person> getPersons() {
return persons;
}
}