Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
c2b145b
create cleanerTest test file and add one simple test to init needed c…
Jul 11, 2022
2ce6d06
Merge pull request #1 from quiches-group/feature/setup_cleaner_test
nicolasbarb Jul 11, 2022
7bd6f2f
add phone correction
Jul 11, 2022
9950c22
Does nothing if phone number is empty
Jul 11, 2022
4a1c81d
Merge pull request #2 from quiches-group/feature/phone-correction
nicolasbarb Jul 11, 2022
39326f9
Added test for checking if first and last name has first uppercase an…
zaktolba Jul 11, 2022
d72dcbe
Added condition & method to reformat firstName
zaktolba Jul 11, 2022
85555c1
Merge branch 'master' into feature/upperLowerCaseNames
zaktolba Jul 11, 2022
0a1138f
Changed formatting to a function & added condition for lastName
zaktolba Jul 11, 2022
6360cfa
Added check if first or last name are empty
zaktolba Jul 11, 2022
ce0e125
Fixed condition if last or first name are not formatted
zaktolba Jul 11, 2022
fa7b34f
Added back missing @Test tags
zaktolba Jul 11, 2022
d3db45e
Moved condition to private formatName function & separated test for e…
zaktolba Jul 11, 2022
4116aa3
Merge pull request #4 from quiches-group/feature/upperLowerCaseNames
zaktolba Jul 12, 2022
7639061
Start adding regex to validate email
Jul 11, 2022
8796f22
Add tests to ensure that email is correct
Jul 12, 2022
ff39ad7
Merge pull request #3 from quiches-group/feature/email-correction
nicolasbarb Jul 12, 2022
585ca5f
Added handling of all uppercase names + tests
zaktolba Jul 12, 2022
cd934d5
Added treatment & test for names with '-'
zaktolba Jul 12, 2022
1776d35
Renamed some name tests
zaktolba Jul 12, 2022
7a41d88
Merge pull request #5 from quiches-group/feature/name-tests
zaktolba Jul 12, 2022
9d068d3
Remove duplicates
Jul 12, 2022
dde84e7
Merge pull request #6 from quiches-group/feature/remove-duplicates
nicolasbarb Jul 12, 2022
f8328f4
Refactor tests into multiple files
Jul 12, 2022
5137db6
Add gesture of inverted last and first name
Jul 12, 2022
0a40cf3
Refactor in private func
Jul 12, 2022
af93532
Merge pull request #7 from quiches-group/feature/inverted-last-first-…
nicolasbarb Jul 12, 2022
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: 2 additions & 1 deletion src/main/java/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public static void main(String[] args) throws IOException {
.map(tokens -> new Volunteer(tokens.get(0), tokens.get(1), tokens.get(2), tokens.get(3), tokens.get(4)))
.collect(toList());

List<Volunteer> outputVolunteers = Cleaner.cleanUp(inputVolunteers);
Cleaner cleaner = new Cleaner();
List<Volunteer> outputVolunteers = cleaner.cleanUp(inputVolunteers);

PrintWriter writer = new PrintWriter(new FileWriter("src/main/resources/output.csv"));
outputVolunteers.forEach(writer::println);
Expand Down
122 changes: 120 additions & 2 deletions src/main/java/org/example/volunteers/Cleaner.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,129 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Pattern;

public class Cleaner {
public static List<Volunteer> cleanUp(List<Volunteer> volunteers) {

private List<Volunteer> cleanedVolunteersList = new ArrayList<Volunteer>();

public List<Volunteer> cleanUp(List<Volunteer> volunteers) {
// This function should contain your dark magic.
// For now, it simply returns a copy of the initial list.
return new ArrayList<>(volunteers);

for (Volunteer volunteer : volunteers) {

// first & last name
volunteer.firstName = formatName(volunteer.firstName);
volunteer.lastName = formatName(volunteer.lastName);

//phone
volunteer.phone = formatPhone(volunteer.phone);

//email
volunteer.eMail = formatMail(volunteer.eMail);

isVolunteerDuplicated(volunteer);
}
return new ArrayList<>(cleanedVolunteersList);
}

private void isVolunteerDuplicated(Volunteer volunteer) {

isVolunteerInverted(volunteer);

Optional<Volunteer> volunteerWithSameInformation = cleanedVolunteersList.stream()
.filter(
volunteerItem -> volunteerItem.firstName.equals(volunteer.firstName) &&
volunteerItem.lastName.equals(volunteer.lastName) &&
volunteerItem.nickName.equals(volunteer.nickName) &&
volunteerItem.eMail.equals(volunteer.eMail) &&
volunteerItem.phone.equals(volunteer.phone)
).findFirst();

if (!volunteerWithSameInformation.isPresent()) {
cleanedVolunteersList.add(volunteer);
}
}

private Volunteer isVolunteerInverted(Volunteer volunteer) {
Optional<Volunteer> volunteerWithInvertedInformation = this.cleanedVolunteersList.stream()
.filter(
volunteerItem -> volunteerItem.lastName.equals(volunteer.firstName) &&
volunteerItem.firstName.equals(volunteer.lastName)
).findFirst();

if (volunteerWithInvertedInformation.isPresent()) {
String firstname = volunteer.firstName;
String lastname = volunteer.lastName;

volunteer.firstName = lastname;
volunteer.lastName = firstname;
}

return volunteer;
}

private String formatPhone(String phone) {
if (!phone.isEmpty()) {
String correctedPhoneNumber = phone.replaceAll("[^0-9]", "");

if (correctedPhoneNumber.charAt(1) == '3') {
correctedPhoneNumber = correctedPhoneNumber.substring(2);
} else {
correctedPhoneNumber = correctedPhoneNumber.substring(1);
}

if (correctedPhoneNumber.length() == 10) {
correctedPhoneNumber = correctedPhoneNumber.substring(1);
}
correctedPhoneNumber = "+33" + correctedPhoneNumber;
return correctedPhoneNumber;
} else {
return "";
}
}

private static String formatMail(String email) {
if (!email.isEmpty()) {
String emailFormatRegex = "^(?=.{1,64}@)[A-Za-z0-9_-]+(\\.[A-Za-z0-9_-]+)*@[^-][A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$";

if (email.charAt(email.length() - 1) == '.') {
email = removeLastCharacter(email);
}
if (!Pattern.compile(emailFormatRegex).matcher(email).matches()) {
return "";
}
return email;
}
return "";
}

private static String formatName(String input) {
// Only non-empty names
if (!Objects.equals(input, "")) {
// Add uppercase on first character and rest lowercase
if (!Objects.equals(input.substring(1), input.substring(1).toUpperCase()) || Objects.equals(input, input.toUpperCase())) {
input = input.substring(0, 1).toUpperCase() + input.substring(1).toLowerCase();
}
}

// Format names with '-'
for (int i = 0; i < input.length(); i++) {
if (input.charAt(i) == '-') {
input = input.substring(0, i) + "-" + input.substring(i+1, i+2).toUpperCase() + input.substring(i+2);
}
}
return input;
}

public static String removeLastCharacter(String str) {
String result = Optional.ofNullable(str)
.filter(sStr -> sStr.length() != 0)
.map(sStr -> sStr.substring(0, sStr.length() - 1))
.orElse(str);
return result;
}
}
11 changes: 6 additions & 5 deletions src/main/java/org/example/volunteers/Volunteer.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package org.example.volunteers;

import java.util.Arrays;
import java.util.List;

import static java.util.stream.Collectors.joining;

public final class Volunteer {
public final String firstName;
public final String lastName;
public final String nickName;
public final String eMail;
public final String phone;
public String firstName;
public String lastName;
public String nickName;
public String eMail;
public String phone;

public Volunteer(
String firstName,
Expand Down
Loading