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
16 changes: 15 additions & 1 deletion src/main/java/com/example/midterm/ConcreteNameService.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
package com.example.midterm;

import dto.Name;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import service.NameService;
import utility.NameBuilder;

@Component
public class ConcreteNameService implements NameService {
@Autowired
private NameBuilder nameBuilder;

@Override
public Name process(String name) throws Exception {
return null;
// Use NameBuilder to split the name into first and last names
String[] fullName = nameBuilder.process(name);

// If the name cannot be processed, throw an exception or return an empty object
if (fullName == null || fullName.length < 2) {
throw new Exception("Invalid name format");
}

// Return the Name object with parsed first and last names
return new Name(fullName[0], fullName[1]);
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/example/midterm/MidtermApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages = {"com.example.midterm", "utility"})
public class MidtermApplication {

public static void main(String[] args) {
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/com/example/midterm/MyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,26 @@
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import utility.ValidateEmail;

import java.util.regex.Pattern;

@RestController
@RequestMapping("validate")
public class MyController {

@GetMapping("/email/{email}")
public boolean validateEmail(@PathVariable String email) {
return true;
// Validate Email with ValidateEmail Utility
return ValidateEmail.isValidEmail(email);
}

@Autowired
private ConcreteNameService nameService;
@GetMapping("/name/{name}")
public Name processName(@PathVariable String name) {
return new Name();
public Name processName(@PathVariable String name) throws Exception {
// Use the ConcreteNameService to handle name processing
return nameService.process(name);
}


Expand Down
111 changes: 45 additions & 66 deletions src/main/java/utility/NameBuilder.java
Original file line number Diff line number Diff line change
@@ -1,86 +1,65 @@
package utility;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class NameBuilder {

private final String[] suffix= {"RAMBO","ED","DR","CSAR","THE","TERMINATOR","BSC","CERTIFIED","PROFESSIONAL","DIP","DIPED","MSC","MPH","PHD","MACA","ASSOC","PROF"};

private final List<String> suffixes = Arrays.asList(
"RAMBO", "ED", "DR", "CSAR", "THE", "TERMINATOR", "BSC",
"CERTIFIED", "PROFESSIONAL", "DIP", "DIPED", "MSC",
"MPH", "PHD", "MACA", "ASSOC", "PROF"
);
public String[] process(String name) {
log.debug("Parsing input name [{}]",name);
String first=null;
String last=null;
String regex1 = "^[a-zA-Z|,|'|-]*$";
String regex2 = "[\\.$|,|;|'|~|\"|'|(|)|.|-]";
String [] fullName=null;
log.debug("Parsing input name [{}]", name);
String f = null, l = null;
String regex1 = "^[a-zA-Z,\'-]+$";
String regex2 = "[\\.,;\'~\"()\\-]";
String[] names = name.split("\\s+");

boolean reverse = false;
for(int i=0;i<names.length;i++) {
if(names[i].matches(regex1) ) {
log.debug("Matching [{}] with acceptable characters [{}]", names[i] , regex1);
if(names[i].contains(",") && first==null) {
log.debug("Check if [{}] is a last name", names[i] );
reverse = true;
boolean rev = false;
for (String n : names) {
if (n.matches(regex1)) {
log.debug("Matching [{}] with acceptable characters [{}]", n, regex1);
if (n.contains(",") && f == null) {
log.debug("Check if [{}] is a last name", n);
rev = true;
}
String o = names[i].trim().replaceAll(regex2,"") ;
log.debug("Clean [{}] by removing characters [{}] results [{}]", names[i], regex2, o );
List<String> list = Arrays.asList(suffix);
if(!list.contains(o.toUpperCase()) && o.length()>2 ) {
log.debug("Check if clean string [{}] is from list of suffixes [{}]", o, (Object) suffix );
if(i==0 || first==null) {
first = convertName(o);
}else {
if(last==null) {
last = convertName(o);
}
String cleaned = n.trim().replaceAll(regex2, "");
log.debug("Clean [{}] results [{}]", n, cleaned);

if (!suffixes.contains(cleaned.toUpperCase()) && cleaned.length() > 2) {
log.debug("Check cleaned string [{}] against suffixes", cleaned);
if (f == null) {
f = convertName(cleaned);
} else {
l = (l == null) ? convertName(cleaned) : (l.equals("Del")) ? l + " " + convertName(cleaned) : (convertName(cleaned).trim().equals("Del")) ? convertName(cleaned) : l;
}
}
}
}
if(first!=null && last!=null) {
if(reverse) {
fullName = new String[]{last,first};
log.debug("Composing name [{}] with last name at the beginning", fullName );
}else {
fullName = new String[]{first,last};
log.debug("Composing name [{}] with last name at the end", fullName );
}
log.debug("Parsing input name [{}] results [{}]",name, fullName);
String[] fullName = (f != null && l != null) ? (rev ? new String[]{l, f} : new String[]{f, l}) : null;
if (fullName != null) {
log.debug("Composing name [{}] with last name {}", Arrays.toString(fullName), rev ? "at the beginning" : "at the end");
log.debug("Parsing input name [{}] results [{}]", name, Arrays.toString(fullName));
}
return fullName;
}

private String convertName(String inputString) {
log.debug("Converting name [{}] to Proper Case",inputString );

if (inputString.length() == 1) {
return inputString.toUpperCase();
}

StringBuffer resultPlaceHolder = new StringBuffer(inputString.length());

Stream.of(inputString.split(" ")).forEach(stringPart ->
{
if (stringPart.length() > 1)
resultPlaceHolder.append(stringPart.substring(0, 1)
.toUpperCase())
.append(stringPart.substring(1)
.toLowerCase());
else
resultPlaceHolder.append(stringPart.toUpperCase());

resultPlaceHolder.append(" ");
});
log.debug("Converting name [{}] results [{}]",inputString, resultPlaceHolder.toString().trim());
return resultPlaceHolder.toString().trim() ;
}
private String convertName(String input) {
log.debug("Converting name [{}] to Proper Case", input);
if ("APC".equals(input.trim()) || input.trim().startsWith("de")) return input.trim();
if (input.length() == 1) return input.toUpperCase();

StringBuilder result = new StringBuilder();
Stream.of(input.split(" ")).forEach(part -> {
result.append(part.length() > 1 ?
Character.toUpperCase(part.charAt(0)) + part.substring(1).toLowerCase() :
part.toUpperCase()).append(" ");
});
log.debug("Converting name [{}] results [{}]", input, result.toString().trim());
return result.toString().trim();
}
}


14 changes: 14 additions & 0 deletions src/main/java/utility/ValidateEmail.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package utility;

import java.util.regex.Pattern;

public class ValidateEmail {
// Regular expression for validating an email
private static final String EMAIL_REGEX = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$";
private static final Pattern EMAIL_PATTERN = Pattern.compile(EMAIL_REGEX);

// Method to validate the email format
public static boolean isValidEmail(String email) {
return EMAIL_PATTERN.matcher(email).matches();
}
}
Binary file modified target/classes/com/example/midterm/ConcreteNameService.class
Binary file not shown.
Binary file modified target/classes/com/example/midterm/MidtermApplication.class
Binary file not shown.
Binary file modified target/classes/com/example/midterm/MyController.class
Binary file not shown.
Binary file modified target/classes/utility/NameBuilder.class
Binary file not shown.
Binary file added target/classes/utility/ValidateEmail.class
Binary file not shown.