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

-m "java exceptions lab" #50

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
70 changes: 70 additions & 0 deletions Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
public class Person {
private int id;
private String name;
private int age;
private String occupation;

// Constructor
public Person(int id, String name, int age, String occupation) {
this.id = id;
this.name = name;
this.setAge(age); // Using the setAge method to enforce age validation
this.occupation = occupation;
}

// Getter for id
public int getId() {
return id;
}

// Getter and Setter for name
public String getName() {
return name;
}

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

// Getter and Setter for age
public int getAge() {
return age;
}

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

// Getter and Setter for occupation
public String getOccupation() {
return occupation;
}

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

// Equals method to compare Person objects excluding the id property
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);
}

// Method to display Person details (optional, for testing purposes)
public void displayPerson() {
System.out.println("ID: " + id);
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Occupation: " + occupation);
}
@Override
public String toString() {
return "Person{id=" + id + ", name='" + name + "', age=" + age + ", occupation='" + occupation + "'}";
}

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

public class PersonsList {
private List<Person> persons;

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

// Method to add a Person to the list
public void addPerson(Person person) {
this.persons.add(person);
}

// Method to find a Person by name
public Person findByName(String name) {
if (!name.matches("[a-zA-Z]+ [a-zA-Z]+")) {
throw new IllegalArgumentException("Name must be formatted as 'firstName lastName'");
}
for (Person person : persons) {
if (person.getName().equals(name)) {
return person;
}
}
return null; // Return null if no match is found
}

// Method to clone a Person with a new id
public Person clone(Person person, int newId) {
return new Person(newId, person.getName(), person.getAge(), person.getOccupation());
}

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

// Main method for testing
public static void main(String[] args) {
PersonsList personsList = new PersonsList();

// Adding persons to the list
Person person1 = new Person(1, "John Doe", -1, "Software Engineer");
Person person2 = new Person(2, "Jane Smith", 28, "Doctor");
personsList.addPerson(person1);
personsList.addPerson(person2);

// Finding a person by name
try {
Person foundPerson = personsList.findByName("John Doe");
Person foundPerson2 = personsList.findByName("JohnDoe");
System.out.println("Found person: " + foundPerson);
System.out.println("Found person: " + foundPerson2);
} catch (IllegalArgumentException e) {
System.err.println(e.getMessage());
}

// Cloning a person with a new id
Person clonedPerson = personsList.clone(person1, 3);
System.out.println("Cloned person: " + clonedPerson);

// Writing a person to a file
personsList.writePersonToFile(person1, "person1.txt");
}
}