Instantly share code, notes, and snippets.

@bytecodeman /BabyName.java Secret
Last active Oct 31, 2018

Embed
What would you like to do?
CSC-220 BabyName Application With Exceptions
/*
* Name: Prof. Antonio C. Silvestri (STCC)
* Date: 11/05/2017
* Course Number: CSC-220
* Course Name: Data Structures and Algorithms
* Problem Number: HW6
* Email: silvestri@stcc.edu
* Description: Baby Names Class to Support Application
* http://cs.stcc.edu/csc-220-baby-names-application-stcccsc220/
*/
public class BabyName implements Comparable<BabyName> {
private String name;
private int number;
public BabyName(String name, int number) {
this.name = name;
this.number = number;
}
public String getName() {
return name;
}
public int getNumber() {
return number;
}
public void addToNumber(int number) {
this.number += number;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
BabyName other = (BabyName) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
return String.format("%15s %,10d", this.name, this.number);
}
@Override
public int compareTo(BabyName arg) {
if (this.number < arg.number)
return 1;
if (this.number > arg.number)
return -1;
if (this.name.compareTo(arg.name) < 0)
return -1;
if (this.name.compareTo(arg.name) > 0)
return 1;
return 0;
}
}
/*
* Name: Prof. Antonio C. Silvestri (STCC)
* Date: 11/05/2017
* Course Number: CSC-220
* Course Name: Data Structures and Algorithms
* Problem Number: HW6
* Email: silvestri@stcc.edu
* Description: Baby Names Application
* http://cs.stcc.edu/csc-220-baby-names-application-stcccsc220/
*/
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
public class BabyNameApp {
private static void outputNames(ArrayList<BabyName> list, String header) {
System.out.println(header);
for (int i = 0; i < list.size(); i++) {
System.out.printf("%3d. %s\n", i + 1, list.get(i));
}
System.out.println();
}
// **********************************************
private static int[] getStartAndFinalYears(Scanner sc) {
do {
try {
System.out.print("Enter Start Year: ");
int startYear = sc.nextInt();
System.out.print("Enter Final Year: ");
int endYear = sc.nextInt();
return new int[] { startYear, endYear };
} catch (InputMismatchException ex) {
System.out.println("Years must be integers!");
} finally {
sc.nextLine();
}
} while (true);
}
// **********************************************
private static int getTopNoOfNames(Scanner sc) {
do {
try {
System.out.print("Enter Top Number of Names: ");
int top = sc.nextInt();
return top;
} catch (InputMismatchException ex) {
System.out.println("Top Number of Names must be an integer!");
} finally {
sc.nextLine();
}
} while (true);
}
// **********************************************
private static void process(Scanner sc, String args[]) {
try {
int years[] = getStartAndFinalYears(sc);
int top = getTopNoOfNames(sc);
System.out.println("Patience Please!");
System.out.println("V1.0 might take some time to generate results.");
System.out.println("V2.0 is in the works and will produce faster results.");
BabyNameStats stats = new BabyNameStats(years[0], years[1], top);
ArrayList<BabyName> topBoys = stats.getTopMaleNames();
outputNames(topBoys, "Top " + top + " Boy Names");
ArrayList<BabyName> topGirls = stats.getTopFemaleNames();
outputNames(topGirls, "Top " + top + " Girl Names");
}
catch (BabyNameException ex) {
System.out.println("Babyname Exception: " + ex.getMessage());
}
catch (Exception ex) {
ex.printStackTrace();
}
}
// **********************************************
private static boolean doThisAgain(Scanner sc, String prompt) {
System.out.print(prompt);
String doOver = sc.nextLine();
return doOver.equalsIgnoreCase("Y");
}
// **********************************************
public static void main(String args[]) {
final String TITLE = "Baby Name Application V1.0";
final String CONTINUE_PROMPT = "Do this again? [y/N] ";
System.out.println("Welcome to " + TITLE);
Scanner sc = new Scanner(System.in);
do {
process(sc, args);
} while (doThisAgain(sc, CONTINUE_PROMPT));
sc.close();
System.out.println("Thank you for using " + TITLE);
}
}
@SuppressWarnings("serial")
public class BabyNameException extends RuntimeException {
public BabyNameException(String message) {
super(message);
}
}
/*
* Name: Prof. Antonio C. Silvestri (STCC)
* Date: 10/28/2018
* Course Number: CSC-220
* Course Name: Data Structures and Algorithms
* Problem Number: HW6
* Email: silvestri@stcc.edu
* Description: Baby Names Class to Support Application
* http://cs.stcc.edu/csc-220-baby-names-application/
*/
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class BabyNameStats {
private int startYear;
private int endYear;
private int top;
private ArrayList<BabyName> boys, girls;
public BabyNameStats(int startYear, int endYear, int top) {
this.setStartEndYears(startYear, endYear);
this.setTop(top);
}
public int getStartYear() {
return startYear;
}
public int getEndYear() {
return endYear;
}
public int getTop() {
return top;
}
public void setStartEndYears(int startYear, int endYear) {
if (startYear > endYear)
throw new BabyNameException(
String.format("StartYear %d passed must be <= EndYear %d passed", startYear, endYear));
if (startYear < 1880)
throw new BabyNameException(String.format("StartYear %d passed must be >= 1880", startYear));
if (endYear > 2017)
throw new BabyNameException(String.format("EndYear %d passed must be <= 2017", endYear));
if (this.startYear != startYear) {
this.startYear = startYear;
this.boys = this.girls = null;
}
if (this.endYear != endYear) {
this.endYear = endYear;
this.boys = this.girls = null;
}
}
public void setTop(int top) {
if (top <= 0)
throw new BabyNameException(String.format("Top %d passed must be > 0", top));
this.top = top;
}
private void generateStats() throws MalformedURLException, IOException {
this.boys = new ArrayList<BabyName>();
this.girls = new ArrayList<BabyName>();
for (int year = startYear; year <= endYear; year++) {
System.out.print(".");
if ((year + 1 - startYear) % 10 == 0)
System.out.println();
try (Scanner sc = new Scanner(new URL("https://cs.stcc.edu/~silvestri/babynames/yob" + year + ".txt").openStream())) {
sc.useDelimiter("\\s*,\\s*|\\s+");
while (sc.hasNextLine()) {
String name = sc.next();
String sex = sc.next();
int number = sc.nextInt();
sc.nextLine();
BabyName baby = new BabyName(name, number);
ArrayList<BabyName> list = sex.equals("M") ? this.boys : this.girls;
int index = list.indexOf(baby);
if (index == -1)
list.add(baby);
else
list.get(index).addToNumber(baby.getNumber());
}
}
}
System.out.println();
Collections.sort(boys);
Collections.sort(girls);
}
private ArrayList<BabyName> getTopNames(boolean male) {
try {
ArrayList<BabyName> topNames = new ArrayList<>();
ArrayList<BabyName> list = male ? this.boys : this.girls;
if (list == null)
this.generateStats();
list = male ? this.boys : this.girls;
for (int i = 0; i < top; i++) {
topNames.add(list.get(i));
}
return topNames;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public ArrayList<BabyName> getTopMaleNames() {
return getTopNames(true);
}
public ArrayList<BabyName> getTopFemaleNames() {
return getTopNames(false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment