Skip to content

commit whit run test #64

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.

13 changes: 13 additions & 0 deletions .idea/compiler.xml

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

7 changes: 7 additions & 0 deletions .idea/encodings.xml

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

20 changes: 20 additions & 0 deletions .idea/jarRepositories.xml

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

9 changes: 9 additions & 0 deletions .idea/lab-java-exceptions.iml

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

14 changes: 14 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.

29 changes: 29 additions & 0 deletions labExceptions/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.calculator</groupId>
<artifactId>labExceptions</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.2</version>
</dependency>
</dependencies>

</project>
82 changes: 82 additions & 0 deletions labExceptions/src/main/java/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
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) throws IllegalArgumentException {
if(id<=0){
throw new IllegalArgumentException("The Id cannot be less than " +
"zero");
}
if (name.isEmpty()){
throw new IllegalArgumentException("The name cannot be empty");
}
if (age<0){
throw new IllegalArgumentException("The age cannot be less than " +
"zero");
}
if (occupation.isEmpty()){
throw new IllegalArgumentException("the occupation cannot be " +
"empty");
}
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) throws IllegalArgumentException{
if (age<0){
throw new IllegalArgumentException("the age cannot be less than " +
"zero ");
}
this.age = age;
}

public String getOccupation() {
return occupation;
}

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

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


@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", occupation='" + occupation + '\'' +
'}';
}
}
90 changes: 90 additions & 0 deletions labExceptions/src/main/java/PersonList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class PersonList {
private static List<Person> person = new ArrayList<>();

public static Person findByName(String name) throws IllegalArgumentException {
if (name == null || name.isEmpty()) {
throw new IllegalArgumentException("The name cannot be null or " +
"empty");
}
// trim name
String nameTrim = name.trim();
if (!nameTrim.contains(" ")) {
throw new IllegalArgumentException("you must have a valid name " +
"with Name and FirstName");
}
// split name
String[] nameSplit = name.split(" ");
String nameForFind = nameSplit[0];
for (Person people : person) {
if (people.getName().contains(nameForFind)) {
return people;
}

}

throw new IllegalArgumentException("Person no found with name: "+ name);
}


public static Person clone(Person people) throws IllegalArgumentException{
Random random=new Random();
int randomId=random.nextInt(1000)+1;
return new Person(randomId,people.getName(), people.getAge(),
people.getOccupation());
}

public static void writePerson(Person people) throws IllegalArgumentException{

String path="labExceptions/src/main/resources/Persons.txt";
try{
FileWriter fileWriter=new FileWriter(path,true);
fileWriter.write(people.toString());
fileWriter.close();
}catch (IOException e){
throw new IllegalArgumentException("mesage: "+e.getMessage()+" " +
"cause: "+e.getCause());
}



}


public static void main(String[] args) {
Person p1=new Person(1,"Ana Ortiz",25,"Abogada");
Person p2=new Person(2,"Abelardo Gomez",54,"Chatarrero");
Person p3=new Person(3,"Ismael Suarez",39,"Arquitecto");
person.add(p1);
person.add(p2);
person.add(p3);


try {
Person found=findByName("Ana Ortiz");
System.out.println("Found: "+found);
Person clone=clone(found);
System.out.println("Cloned: "+clone);

writePerson(clone);
}catch (IllegalArgumentException e){
System.err.println("Error: "+e.getMessage());
}





}






}
1 change: 1 addition & 0 deletions labExceptions/src/main/resources/Persons.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Person{id=565, name='Ana Ortiz', age=25, occupation='Abogada'}
26 changes: 26 additions & 0 deletions labExceptions/src/test/java/PersonListTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;


class PersonListTest {



@Test
void findByName_with_correct_format() {
assertThrows(IllegalArgumentException.class,()-> PersonList.findByName(
"Ana"));
}

@Test
void testClone_Other_Person() {
Person p2=new Person(2,"Abelardo Gomez",54,"Chatarrero");
Person cloned=PersonList.clone(p2);

assertEquals(p2.getName(),cloned.getName());
assertEquals(p2.getAge(),cloned.getAge());
assertEquals(p2.getOccupation(),cloned.getOccupation());

}
}
18 changes: 18 additions & 0 deletions labExceptions/src/test/java/PersonTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.DisplayName;
import static org.junit.jupiter.api.Assertions.*;


class PersonTest {
private Person person;
@BeforeEach
void StUpPerson(){
person=new Person(1,"Ana Ortiz",36,"Abogada");
}
@Test
@DisplayName("Check age cannot be less than zero")
void setAge_cannot_be_less__tahn_zero() {
assertThrows(IllegalArgumentException.class,()->person.setAge(-2));
}
}
Binary file added labExceptions/target/classes/Person.class
Binary file not shown.
Binary file added labExceptions/target/classes/PersonList.class
Binary file not shown.
1 change: 1 addition & 0 deletions labExceptions/target/classes/Persons.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Person{id=565, name='Ana Ortiz', age=25, occupation='Abogada'}
Binary file not shown.
Binary file not shown.