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
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.

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

6 changes: 6 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 Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.io.IOException;

public class Main {
public static void main(String[] args) throws IOException {
Person person = new Person(0,"Rogerio Ceni",22,"Goleiro");
Person person1 = new Person(1,"Rodrigo Conde",40,"Software developer"); // ERROR NAME
Person person2 = new Person(2,"Olavo Carvalho",122,"Caveira");
Person person3 = new Person(3,"Rodrigo Conde",40,"Software developer"); // DUPLICATED

// ERROR CASES:
// Person person1 = new Person(1,"RodrigoConde",12,"Bugado"); // ERROR NAME
// Person person1 = new Person(1,"RodrigoConde",-2,"Bugado"); // ERROR AGE


PersonList personList = new PersonList();

personList.addPerson(person);
personList.addPerson(person1);
personList.addPerson(person2);
personList.addPerson(person3);
personList.clonePerson(person2); // CLONING PERSON 2
personList.findByName("Rogerio Ceni");
personList.findByName("Rodrigo Conde");
personList.findByName("Gorfo Douro"); // NOT FOUND
personList.personToFile(person); // WRITE FILE
System.out.println(person1.equals(person3)); // TRUE
System.out.println(person1.equals(person2)); // FALSE
}
}
71 changes: 71 additions & 0 deletions Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import java.util.Objects;

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) {
setAge(age);
setName(name);
this.id = id;
this.name = name;
this.age = age;
this.occupation = occupation;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return Objects.equals(getName(), person.getName()) &&
Objects.equals(getAge(), person.getAge()) &&
Objects.equals(getOccupation(), person.getOccupation());
}

// Getters e setters

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
String[] find;
find = name.split(" ");
if (find.length<2){
throw new RuntimeException("Name not correctly formatted, must be separated by a ' ' while creating!");
}
this.name = find[0];
}

public int getAge() {
return age;
}

public void setAge(int age) {
if (age < 0) {
System.out.println("Age cannot be under 0.");
throw new RuntimeException("Age cannot be under 0.");
}
this.age = age;
}

public String getOccupation() {
return occupation;
}

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

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

class PersonList {
private final List<Person> personList = new ArrayList<>();

public List<Person> getPersonList() {
return personList;
}

private String getName(String name) {
String[] find;
find = name.split(" ");
return find[0];
}

private String getLastName(String name) {
String[] find;
System.out.println(name);
find = name.split(" ");
try {
return find[1];
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Name not correctly formatted, must be separated by a ' ' while creating");
return "Name not correctly formatted, must be separated by a ' ' while creating";
}
}

public void findByName(String fullName) {
String firstName = this.getName(fullName);
String lastName = this.getLastName(fullName);
List<Person> people = getPersonList();
try {
for (Person currentPerson : people) {
String[] nameParts = currentPerson.getName().split(" ");
if (nameParts.length >= 2 && nameParts[0].equalsIgnoreCase(firstName) && nameParts[1].equalsIgnoreCase(lastName)) {
System.out.println("Nome: " + currentPerson.getName() + " ID: " + currentPerson.getId());
return;
}
}
} catch (IllegalArgumentException e) {
System.out.println("Name must be space separated. Ex.: John Doe");
return;
}
System.out.println("Person " + fullName + " not found.");
}
public void clonePerson(Person person){
Person newPerson = new Person(person.getId()+100,person.getName(), person.getAge(), person.getOccupation());
this.personList.add(newPerson);
System.out.println("Created person: " + newPerson.getName() + " - ID: " + newPerson.getId());
}
public void personToFile(Person person) throws IOException {
File filename = new File("person.txt");

try (FileWriter file = new FileWriter(filename)) {
file.write("ID: " + person.getId() + "\n");
file.write("Name: " + person.getName() + "\n");
file.write("Age: " + person.getAge() + "\n");
file.write("Occupation: " + person.getOccupation() + "\n");
} catch (Exception e) {
System.out.println(e.getMessage());
throw new RuntimeException(e);
}
}

public void addPerson(Person person) {
personList.add(person);
}
}
3 changes: 3 additions & 0 deletions out/production/lab-java-exceptions/.idea/.gitignore

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

11 changes: 11 additions & 0 deletions out/production/lab-java-exceptions/.idea/lab-java-exceptions.iml

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

6 changes: 6 additions & 0 deletions out/production/lab-java-exceptions/.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 out/production/lab-java-exceptions/.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 out/production/lab-java-exceptions/.idea/vcs.xml

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

Binary file added out/production/lab-java-exceptions/Main.class
Binary file not shown.
Binary file added out/production/lab-java-exceptions/Person.class
Binary file not shown.
Binary file not shown.
Loading