From d893ea18993bfcf6e420b09da8b4b1c1415eafdd Mon Sep 17 00:00:00 2001 From: Justin Musgrove Date: Sat, 9 Aug 2014 15:05:35 -0500 Subject: [PATCH] Added name search exercise --- .../java/exercises/beginner/NameSearch.java | 96 +++++++++ .../java/exercises/beginner/BoyNames.txt | 200 ++++++++++++++++++ .../java/exercises/beginner/GirlNames.txt | 200 ++++++++++++++++++ 3 files changed, 496 insertions(+) create mode 100644 src/main/java/com/levelup/java/exercises/beginner/NameSearch.java create mode 100644 src/main/resources/com/levelup/java/exercises/beginner/BoyNames.txt create mode 100644 src/main/resources/com/levelup/java/exercises/beginner/GirlNames.txt diff --git a/src/main/java/com/levelup/java/exercises/beginner/NameSearch.java b/src/main/java/com/levelup/java/exercises/beginner/NameSearch.java new file mode 100644 index 0000000..cee6f83 --- /dev/null +++ b/src/main/java/com/levelup/java/exercises/beginner/NameSearch.java @@ -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 Name search + */ +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 boysNames = Files.lines(boysNamesPath).collect( + Collectors.toList()); + + // ready girls names + List 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 boysNames, List 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."); + } + } + +} diff --git a/src/main/resources/com/levelup/java/exercises/beginner/BoyNames.txt b/src/main/resources/com/levelup/java/exercises/beginner/BoyNames.txt new file mode 100644 index 0000000..ce99e8a --- /dev/null +++ b/src/main/resources/com/levelup/java/exercises/beginner/BoyNames.txt @@ -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 \ No newline at end of file diff --git a/src/main/resources/com/levelup/java/exercises/beginner/GirlNames.txt b/src/main/resources/com/levelup/java/exercises/beginner/GirlNames.txt new file mode 100644 index 0000000..dc273b5 --- /dev/null +++ b/src/main/resources/com/levelup/java/exercises/beginner/GirlNames.txt @@ -0,0 +1,200 @@ +Emily +Madison +Emma +Olivia +Hannah +Abigail +Isabella +Samantha +Elizabeth +Ashley +Alexis +Sarah +Sophia +Alyssa +Grace +Ava +Taylor +Brianna +Lauren +Chloe +Natalie +Kayla +Jessica +Anna +Victoria +Mia +Hailey +Sydney +Jasmine +Julia +Morgan +Destiny +Rachel +Ella +Kaitlyn +Megan +Katherine +Savannah +Jennifer +Alexandra +Allison +Haley +Maria +Kaylee +Lily +Makayla +Brooke +Mackenzie +Nicole +Addison +Stephanie +Lillian +Andrea +Zoe +Faith +Kimberly +Madeline +Alexa +Katelyn +Gabriella +Gabrielle +Trinity +Amanda +Kylie +Mary +Paige +Riley +Jenna +Leah +Sara +Rebecca +Michelle +Sofia +Vanessa +Jordan +Angelina +Caroline +Avery +Audrey +Evelyn +Maya +Claire +Autumn +Jocelyn +Ariana +Nevaeh +Arianna +Jada +Bailey +Brooklyn +Aaliyah +Amber +Isabel +Danielle +Mariah +Melanie +Sierra +Erin +Molly +Amelia +Isabelle +Madelyn +Melissa +Jacqueline +Marissa +Shelby +Angela +Leslie +Katie +Jade +Catherine +Diana +Aubrey +Mya +Amy +Briana +Sophie +Gabriela +Breanna +Gianna +Kennedy +Gracie +Peyton +Adriana +Christina +Courtney +Daniela +Kathryn +Lydia +Valeria +Layla +Alexandria +Natalia +Angel +Laura +Charlotte +Margaret +Cheyenne +Mikayla +Miranda +Naomi +Kelsey +Payton +Ana +Alicia +Jillian +Daisy +Mckenzie +Ashlyn +Caitlin +Sabrina +Summer +Ruby +Rylee +Valerie +Skylar +Lindsey +Kelly +Genesis +Zoey +Eva +Sadie +Alexia +Cassidy +Kylee +Kendall +Jordyn +Kate +Jayla +Karen +Tiffany +Cassandra +Juliana +Reagan +Caitlyn +Giselle +Serenity +Alondra +Lucy +Kiara +Bianca +Crystal +Erica +Angelica +Hope +Chelsea +Alana +Liliana +Brittany +Camila +Makenzie +Veronica +Lilly +Abby +Jazmin +Adrianna +Karina +Delaney +Ellie +Jasmin \ No newline at end of file