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
8 changes: 8 additions & 0 deletions .idea/.gitignore

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

9 changes: 9 additions & 0 deletions .idea/lab2.04.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.

10 changes: 10 additions & 0 deletions src/main/java/org/example/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.example;

/**
* Hello world!
*/
public class App {
public static void main(String[] args) {
System.out.println("Aloha");
}
}
17 changes: 17 additions & 0 deletions src/main/java/org/example/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.example;

//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
public static void main(String[] args) {
//TIP Press <shortcut actionId="ShowIntentionActions"/> with your caret at the highlighted text
// to see how IntelliJ IDEA suggests fixing it.
System.out.printf("Hello and welcome!");

for (int i = 1; i <= 5; i++) {
//TIP Press <shortcut actionId="Debug"/> to start debugging your code. We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/> breakpoint
// for you, but you can always add more by pressing <shortcut actionId="ToggleLineBreakpoint"/>.
System.out.println("i = " + i);
}
}
}
90 changes: 90 additions & 0 deletions src/main/java/org/example/classes/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package org.example.classes;

import org.example.utils.Validator;

import java.util.Objects;

public class Person {
private static int identifierCounter = 0;
private int identifier;
private String fullName;
private int years;
private String jobTitle;

public Person (String fullName, int years, String jobTitle) {
this.identifier = identifierCounter++;
setFullName(fullName);
setYears(years);
this.jobTitle = jobTitle;
}

public Person () {
}

public Person (int identifier, String fullName, int years, String jobTitle) {
this.identifier = identifier;
this.fullName = fullName;
this.years = years;
this.jobTitle = jobTitle;
}

@Override
public Person clone() {
return new Person (fullName, years, jobTitle);
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Person that = (Person ) obj;
return getYears() == that.getYears() && getFullName().equals(that.getFullName()) && getJobTitle().equals(that.getJobTitle());
}

@Override
public int hashCode() {
return Objects.hash(getFullName(), getYears(), getJobTitle());
}

@Override
public String toString() {
return "Individual: " + fullName +
"\nidentifier=" + identifier +
"\nyears=" + years +
"\njobTitle='" + jobTitle + '\n';
}

public int getIdentifier() {
return identifier;
}

public void setIdentifier(int identifier) {
this.identifier = identifier;
}

public String getFullName() {
return fullName;
}

public void setFullName(String fullName) {
Validator.checkName(fullName);
this.fullName = fullName;
}

public int getYears() {
return years;
}

public void setYears(int years) {
Validator.checkAge(years);
this.years = years;
}

public String getJobTitle() {
return jobTitle;
}

public void setJobTitle(String jobTitle) {
this.jobTitle = jobTitle;
}
}
51 changes: 51 additions & 0 deletions src/main/java/org/example/classes/PersonList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.example.classes;

import org.example.utils.Validator;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class PersonList {
private ArrayList<Person> persons;

public PersonList () {
persons = new ArrayList<>();
}

public void addIndividual(Person person) {
persons.add(person);
}

public void clearAll() {
persons.clear();
}

public Person searchByName(String fullName) {
Validator.checkName(fullName);
Person result;

for (Person person : persons) {
if (person.getFullName().equals(fullName)) {
result = person;
return result;
}
}
throw new UnsupportedOperationException("FullName not found");
}

public Person clone(Person p) {
return new Person(p.getFullName(),p.getYears(),p.getJobTitle());
}

public void getInformation (Person p) throws IOException {
File file = new File("src/main/resources/personFiles/" + p.getFullName() + ".csv");
FileWriter fileWriter = new FileWriter(file);

fileWriter.write(p.toString());
fileWriter.close();
}


}
48 changes: 48 additions & 0 deletions src/main/java/org/example/utils/Validator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.example.utils;

import org.example.classes.Person;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Validator {

public static boolean checkName(String name) {
// this is a regex pattern, a pattern (or regular expression) used to validate different inputs like email patterns,
// IDs, etc. FInd more in README
Pattern pattern = Pattern.compile("^[a-zA-ZÀ-ÿ\\u00f1\\u00d1]+\\s[a-zA-ZÀ-ÿ\\u00f1\\u00d1]+$");
Matcher matcher = pattern.matcher(name);

if (!matcher.matches()) throw new IllegalArgumentException("Name format must be Name Surname");
return true;

}

public static boolean checkAge(int age) {
if (age < 0) throw new IllegalArgumentException("Age must be greater than zero or zero");
return true;
}

public static boolean writePerson(Person person) {
File dir = new File("src/main/resources/personFiles/" + person.getFullName() + ".csv");
try {
createFile(person, dir);
} catch (IOException e) {
System.err.println("Couldn't create file");
e.printStackTrace();
return false;
}
return true;
}

private static void createFile(Person person, File dir) throws IOException {
File file = new File(dir.getPath());
FileWriter fileWriter = new FileWriter(file);

fileWriter.write(person.toString());
fileWriter.close();
}
}
51 changes: 51 additions & 0 deletions src/test/java/org/example/classes/PersonListTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.example.classes;


import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

class PersonListTest {
Person p1;
Person p2;
Person p3;
Person p4;
PersonList pl;

@BeforeEach
void init() {
pl = new PersonList();
p1 = new Person("Ana Sánchez", 17, "Pintora");
p2 = new Person("Laura Garcia", 42, "Escultora");
p3 = new Person("Alberto Miranda", 23, "Trabajador de Amazon");
p4 = new Person("Lisa Medina", 51, "Música");
pl.addIndividual(p1);
pl.addIndividual(p2);
pl.addIndividual(p3);
pl.addIndividual(p4);
}

@Test
void findByName_sameNameOK() {
assertEquals(p1, pl.searchByName("Ana Sánchez"));
}

@Test
void findByName_wrongName() {
assertThrows(UnsupportedOperationException.class, () -> pl.searchByName("Ana Sanchez"));
}

@Test
void findByName_emptyList() {
pl.clearAll();
assertThrows(UnsupportedOperationException.class, () -> pl.searchByName("Ana Sánchez"));
}

@Test
void findByName_nameMultipleTimes_returnsFirst() {
p4.setFullName("Ana Sanchez");
assertEquals(p1.getIdentifier(), pl.searchByName("Ana Sánchez").getIdentifier());
}
}
36 changes: 36 additions & 0 deletions src/test/java/org/example/classes/PersonTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.example.classes;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class PersonTest {
Person person1;

@BeforeEach
void init() {
person1 = new Person("Jaume Sánchez", 31, "Programmer");
}

@Test
void testClone_differentID_ok() {

assertEquals(person1.getIdentifier() + 1, person1.clone().getIdentifier());
}

@Test
void testClone_clone_ok() {
assertEquals(person1, person1.clone());

}


@Test
void testClone_sameNameAgeAndOccupation_ok() {
Person person2 = person1.clone();
assertEquals(person1.getFullName(), person2.getFullName());
assertEquals(person1.getYears(), person2.getYears());
assertEquals(person1.getJobTitle(), person2.getJobTitle());
}
}
Loading