From c4b8bf907750d5321cb169c20c72b677d87d1679 Mon Sep 17 00:00:00 2001 From: mohammedqasim91 Date: Thu, 1 Aug 2024 09:46:00 +0200 Subject: [PATCH] done --- src/main/java/com.ironhack/Main.java | 7 +++ src/main/java/com.ironhack/Person.java | 56 +++++++++++++++++++++ src/main/java/com.ironhack/PersonTest.java | 42 ++++++++++++++++ src/main/java/com.ironhack/PersonsList.java | 40 +++++++++++++++ 4 files changed, 145 insertions(+) create mode 100644 src/main/java/com.ironhack/Main.java create mode 100644 src/main/java/com.ironhack/Person.java create mode 100644 src/main/java/com.ironhack/PersonTest.java create mode 100644 src/main/java/com.ironhack/PersonsList.java diff --git a/src/main/java/com.ironhack/Main.java b/src/main/java/com.ironhack/Main.java new file mode 100644 index 0000000..848dde8 --- /dev/null +++ b/src/main/java/com.ironhack/Main.java @@ -0,0 +1,7 @@ +package main.java.com.ironhack; + +public class Main { + public static void main(String[] args) { + PersonTest.main(args); + } +} diff --git a/src/main/java/com.ironhack/Person.java b/src/main/java/com.ironhack/Person.java new file mode 100644 index 0000000..a1de6c5 --- /dev/null +++ b/src/main/java/com.ironhack/Person.java @@ -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; } +} + diff --git a/src/main/java/com.ironhack/PersonTest.java b/src/main/java/com.ironhack/PersonTest.java new file mode 100644 index 0000000..940e4eb --- /dev/null +++ b/src/main/java/com.ironhack/PersonTest.java @@ -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"); + } +} + diff --git a/src/main/java/com.ironhack/PersonsList.java b/src/main/java/com.ironhack/PersonsList.java new file mode 100644 index 0000000..b874ea3 --- /dev/null +++ b/src/main/java/com.ironhack/PersonsList.java @@ -0,0 +1,40 @@ +package main.java.com.ironhack; + +import java.util.ArrayList; +import java.util.List; + +public class PersonsList { + private List 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 getPersons() { + return persons; + } +}