In: Computer Science
In JAVA (eclipse).... Extract both files and place them in your program’s folder. You will be using them. In your driver class: • Create an array called “boyNames” and store all names from the BoyNames.txt file • Similarly, create an array called “girlsNames” and store all names from the GirlNames.txt file • Create a text menu that allowing users to: 1. Enter a name and the application must display a message indicating if the name is among the most popular (found on either array) If the name is found, tell the user if it is a boy’s or girl’s name 2. Option to suggest a name. Program must suggest/display a name to the user by randomly selecting one from the appropriate array depending if the user wants a boy’s or girl’s name 3. Option to display all names starting with a specific letter. Display all names starting with a specific letter (e.g. all names starting with the letter R) depending if the user wants a boy’s or girl’s name • After a user either enters a name from (1) in the menu or is suggested a name from (2), the program must ask if the name is acceptable for the newborn or not 1. If the name is not acceptable, user must be given the option to exit the program or repeat the procedure. Either ask for a new name to search if user is in (1), or suggest a new name randomly if in (2). 2. If the name is acceptable, program must ask the user to enter the newborn’s last name and expected date of birth. 3. Then, the program must instantiate an object of the class called NewBorn using its constructor with the accepted name for the newborn, last name, and expected date of birth. 4. Finally, program must display the state of the NewBorn object
Below is our code for the given problem
1. Main.java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class Main {
static Scanner scan = new Scanner(System.in);
public static String boysFile =
"C:/Users/mohammadasif/Desktop/java/BoysNames.txt";
public static String girlsFile =
"C:/Users/mohammadasif/Desktop/java/GirlsNames.txt";
public static String name = null ;
public static void main(String[] args) {
selectFromOption();
System.out.println("Is name acceptable for newBorn [Press Y if Yes]
?");
String userChoice = scan.next();
if(userChoice.equalsIgnoreCase("Y"))
{
System.out.print("Enter the last name : ");
String lastName = scan.next();
System.out.print("Enter the expected DOB (dd/MM/yyyy) : ");
String dob = scan.next();
NewBorn newborn = new NewBorn(name, lastName, dob);
System.out.println("First Name : "+ newborn.getFirstName() +
"\nLast Name : "+ newborn.getLastName() + "\nDOB : "+
newborn.getDateofBirth());
System.out.println("Press R to Repeat Or Press Any Other Button to
Exit");
userChoice = scan.next();
if(userChoice.equalsIgnoreCase("R"))
{
main(null);
}
}
else
{
main(null);
}
}
private static void selectFromOption() {
System.out.println("1. Check a Name ");
System.out.println("2. Get Suggested name ");
System.out.println("3. Check all name starts with specific letter
");
int choice = scan.nextInt();
switch(choice)
{
case 1:
enteredName();
break;
case 2:
getSuggestedName();
break;
case 3:
checkNamewithLetter();
break;
default :
System.out.println("Wrong Choice, try Again");
selectFromOption();
}
}
private static void enteredName() {
System.out.print("Enter name : ");
name = scan.next();
boolean popular = true;
File file1 = new File(boysFile);
File file2 = new File(girlsFile);
FileReader fr1, fr2;
try {
fr1 = new FileReader(file1);
fr2 = new FileReader(file2);
BufferedReader br1 = new BufferedReader(fr1);
BufferedReader br2 = new BufferedReader(fr2);
String boys = null;
String girls = null;
while((boys = br1.readLine()) != null || (girls = br2.readLine())
!= null){
if(name.equalsIgnoreCase(boys))
{
System.out.println(name + " is a popular name among Boys");
popular = false;
break;
}
else if(name.equalsIgnoreCase(girls))
{
System.out.println(name + " is a popular name among Girls");
popular = false;
break;
}
}
if(popular)
{
System.out.println(name + " is not a popular name ");
}
} catch (FileNotFoundException ex) {
System.out.println("No Fle Found");
} catch (IOException ex) {
System.out.println("Unale to read the file");
}
}
private static void getSuggestedName() {
System.out.println("1. For Boys");
System.out.println("2. For Girls");
String choice = scan.next();
File file1 = new File(boysFile);
File file2 = new File(girlsFile);
FileReader fr1, fr2;
ArrayList<String> allNames = new
ArrayList<String>();
Random r = new Random();
if("1".equals(choice))
{
try {
fr1 = new FileReader(file1);
BufferedReader br1 = new BufferedReader(fr1);
String boys = null;
while((boys = br1.readLine()) != null ){
allNames.add(boys);
}
String randomName = allNames.get(r.nextInt(allNames.size()));
System.out.println(randomName);
} catch (Exception ex) {
System.out.println("No Fle Found");
}
}
else if("2".equals(choice))
{
try {
fr2 = new FileReader(file2);
BufferedReader br1 = new BufferedReader(fr2);
String girls = null;
while((girls = br1.readLine()) != null ){
allNames.add(girls);
}
String randomName = allNames.get(r.nextInt(allNames.size()));
System.out.println(randomName);
} catch (Exception ex) {
System.out.println("No Fle Found");
}
}
else {
System.out.println("Wrong Input, Try Again");
getSuggestedName();
}
}
private static void checkNamewithLetter() {
System.out.println("1. For Boys");
System.out.println("2. For Girls");
String choice = scan.next();
System.out.print("Enter Letter : ");
String name = scan.next();
File file1 = new File(boysFile);
File file2 = new File(girlsFile);
FileReader fr1, fr2;
try {
fr1 = new FileReader(file1);
fr2 = new FileReader(file2);
BufferedReader br1 = new BufferedReader(fr1);
BufferedReader br2 = new BufferedReader(fr2);
String boys = null;
String girls = null;
if(choice.equals("1"))
{
name = name.substring(0,1).toUpperCase();
while((boys = br1.readLine()) != null)
{
if(boys.startsWith(name))
{
System.out.println(boys);
}
}
}
else if( choice.equals("2"))
{
name = name.substring(0,1).toLowerCase();
while((girls = br2.readLine()) != null)
{
if(girls.startsWith(name))
{
System.out.println(girls);
}
}
}
main(null);
return;
} catch (Exception ex) {
System.out.println("No File Found");
}
}
}
2. NewBorn.java
public class NewBorn {
private String name;
private String lastName;
private String dob;
NewBorn(String name, String lastName, String dob) {
this.name = name ;
this.lastName = lastName;
this.dob = dob;
}
public String getFirstName()
{
return name;
}
public String getLastName()
{
return lastName;
}
public String getDateofBirth()
{
return dob;
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------
Here is some of the output that I run for the above code, if you feel any problem in the code feel free to ask