Skip to content
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
29 changes: 29 additions & 0 deletions untitled/.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
3 changes: 3 additions & 0 deletions untitled/.idea/.gitignore

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

6 changes: 6 additions & 0 deletions untitled/.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 untitled/.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 untitled/.idea/vcs.xml

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

34 changes: 34 additions & 0 deletions untitled/src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
public class Main {
public static void main(String[] args) {
Person person1 = new Person(1, "Cheyenne Saiz ", 32, "Problem Solver");
Person person2 = new Person(2, "Elsa Lorente ", 32, "Profesora");

PersonsList personsList = new PersonsList();

// Prueba de setAge
try {
person1.setAge(-5);
} catch (IllegalArgumentException e) {
System.out.println("Error en setAge: " + e.getMessage());
}


try {
Person foundPerson = personsList.findByName("Cheyenne Saiz");
System.out.println("Persona encontrada: " + foundPerson.getName());
} catch (IllegalArgumentException e) {
System.out.println("Error en findByName: " + e.getMessage());
}


try {
personsList.findByName("Cheyenne");
} catch (IllegalArgumentException e) {
System.out.println("Error en findByName: " + e.getMessage());
}


Person clonedPerson = personsList.clone(person1);
System.out.println("Clon de la persona: " + clonedPerson.getName() + ", Nuevo ID: " + clonedPerson.getId());
}
}
42 changes: 42 additions & 0 deletions untitled/src/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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.setAge(age);
this.occupation = occupation;
}

public void setAge(int age) {
if (age < 0) {
throw new IllegalArgumentException("La edad no puede ser menor que 0");
}
this.age = age;
}

public boolean equals(Person otherPerson) {
return this.name.equals(otherPerson.name) &&
this.age == otherPerson.age &&
this.occupation.equals(otherPerson.occupation);
}

public int getId() {
return id;
}

public String getName() {
return name;
}

public int getAge() {
return age;
}

public String getOccupation() {
return occupation;
}
}
45 changes: 45 additions & 0 deletions untitled/src/PersonsList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import java.util.ArrayList;
import java.util.List;

public class PersonsList {
private List<Person> persons;

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

public Person findByName(String name) {
String[] parts = name.split(" ");
if (parts.length != 2) {
throw new IllegalArgumentException("El nombre debe tener el formato 'nombreApellido'");
}
String firstName = parts[0];
String lastName = parts[1];

for (Person person : persons) {
String[] personNameParts = person.getName().split(" ");
if (personNameParts[0].equals(firstName) && personNameParts[1].equals(lastName)) {
return person;
}
}

throw new IllegalArgumentException("No se encontró ninguna persona con el nombre proporcionado");
}

public Person clone(Person originalPerson) {
return new Person(
generateNewId(),
originalPerson.getName(),
originalPerson.getAge(),
originalPerson.getOccupation()
);
}

private int generateNewId() {
return (int) (Math.random() * 1000);
}

public void writeToFile(Person person) {

}
}
11 changes: 11 additions & 0 deletions untitled/untitled.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>