Skip to content

Commit

Permalink
Added name search exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
justinmusgrove committed Aug 9, 2014
1 parent 1666b92 commit d893ea1
Show file tree
Hide file tree
Showing 3 changed files with 496 additions and 0 deletions.
96 changes: 96 additions & 0 deletions src/main/java/com/levelup/java/exercises/beginner/NameSearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.levelup.java.exercises.beginner;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

/**
* This java exercises will demonstrate how to search for name within a file.
*
* @author Justin Musgrove
* @see <a href='http://www.leveluplunch.com/java/exercises/name-search/'>Name search</a>
*/
public class NameSearch {

public static void main(String args[]) throws IOException {

Path boysNamesPath = Paths
.get("src/main/resources/com/levelup/java/exercises/beginner/BoyNames.txt")
.toAbsolutePath();

Path girlsNamePath = Paths
.get("src/main/resources/com/levelup/java/exercises/beginner/GirlNames.txt")
.toAbsolutePath();

// read boys names
List<String> boysNames = Files.lines(boysNamesPath).collect(
Collectors.toList());

// ready girls names
List<String> girlsNames = Files.lines(girlsNamePath).collect(
Collectors.toList());

// ask user get name from user
String searchName = getNamesFromUser();

displaySearchResults(searchName, boysNames, girlsNames);

}

/**
* Method should ask user to input a name
*
* @return name to be searched
*/
public static String getNamesFromUser() {

// Create a Scanner object to read input.
Scanner keyboard = new Scanner(System.in);

// Get a name from user
System.out.println("Popular Name Search");
System.out.print("Enter a name: ");

String name = keyboard.nextLine();

keyboard.close();

return name;
}

/**
* Method should determine and output if a name is contained with in lists
* passed
*
* @param searchName
* @param boysNames
* @param girlsNames
* @throws IOException
*/
public static void displaySearchResults(String searchName,
List<String> boysNames, List<String> girlsNames) throws IOException {

System.out.println("\nHere are the search results:\n");

boolean popularBoyName = boysNames.stream().anyMatch(
p -> p.equalsIgnoreCase(searchName));
boolean popularGirlName = girlsNames.stream().anyMatch(
p -> p.equalsIgnoreCase(searchName));

// Display the results.
if (popularBoyName) {
System.out.println(searchName + " is a popular boy's name.");
}
if (popularGirlName) {
System.out.println(searchName + " is a popular girl's name.");
}
if (!popularBoyName && !popularGirlName) {
System.out.println(searchName + " is not a popular name.");
}
}

}
200 changes: 200 additions & 0 deletions src/main/resources/com/levelup/java/exercises/beginner/BoyNames.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
Jacob
Michael
Joshua
Matthew
Daniel
Christopher
Andrew
Ethan
Joseph
William
Anthony
David
Alexander
Nicholas
Ryan
Tyler
James
John
Jonathan
Noah
Brandon
Christian
Dylan
Samuel
Benjamin
Zachary
Nathan
Logan
Justin
Gabriel
Jose
Austin
Kevin
Elijah
Caleb
Robert
Thomas
Jordan
Cameron
Jack
Hunter
Jackson
Angel
Isaiah
Evan
Isaac
Mason
Luke
Jason
Gavin
Jayden
Aaron
Connor
Aiden
Aidan
Kyle
Juan
Charles
Luis
Adam
Lucas
Brian
Eric
Adrian
Nathaniel
Sean
Alex
Carlos
Bryan
Ian
Owen
Jesus
Landon
Julian
Chase
Cole
Diego
Jeremiah
Steven
Sebastian
Xavier
Timothy
Carter
Wyatt
Brayden
Blake
Hayden
Devin
Cody
Richard
Seth
Dominic
Jaden
Antonio
Miguel
Liam
Patrick
Carson
Jesse
Tristan
Alejandro
Henry
Victor
Trevor
Bryce
Jake
Riley
Colin
Jared
Jeremy
Mark
Caden
Garrett
Parker
Marcus
Vincent
Kaleb
Kaden
Brady
Colton
Kenneth
Joel
Oscar
Josiah
Jorge
Cooper
Ashton
Tanner
Eduardo
Paul
Edward
Ivan
Preston
Maxwell
Alan
Levi
Stephen
Grant
Nicolas
Omar
Dakota
Alexis
George
Collin
Eli
Spencer
Gage
Max
Cristian
Ricardo
Derek
Micah
Brody
Francisco
Nolan
Ayden
Dalton
Shane
Peter
Damian
Jeffrey
Brendan
Travis
Fernando
Peyton
Conner
Andres
Javier
Giovanni
Shawn
Braden
Jonah
Cesar
Bradley
Emmanuel
Manuel
Edgar
Erik
Mario
Edwin
Johnathan
Devon
Erick
Wesley
Oliver
Trenton
Hector
Malachi
Jalen
Raymond
Gregory
Abraham
Elias
Leonardo
Sergio
Donovan
Colby
Marco
Bryson
Martin
Loading

0 comments on commit d893ea1

Please sign in to comment.