Skip to content

Instantly share code, notes, and snippets.

@bytecodeman bytecodeman/Name.java Secret
Created Oct 26, 2019

Embed
What would you like to do?
Names Application Using Just One ArrayList (10/26/2019)
/*
* Name: Prof. Antonio C. Silvestri (STCC)
* Date: 10/26/2019
* Course Number: CSC-220
* Course Name: Data Structures and Algorithms
* Problem Number: HW6
* Email: silvestri@stcc.edu
* Description: Names Class to Support Application
* https://cs.stcc.edu/baby-names-application/
*/
public class Name implements Comparable<Name> {
private String name;
private String sex;
private int number;
public Name(String name, String sex, int number) {
this.setName(name);
this.setSex(sex);
this.setNumber(number);
}
// Getters
public String getName() {
return name;
}
public String getSex() {
return sex;
}
public int getNumber() {
return number;
}
// Setters
public void setName(String name) {
if (name == null || name.equals(""))
throw new NameAppException("Blank name Setting Attempted");
this.name = name;
}
public void setSex(String sex) {
if (sex == null || !sex.equalsIgnoreCase("M") && !sex.equalsIgnoreCase("F"))
throw new NameAppException("Bad Gender Setting Attempted: " + sex);
this.sex = sex;
}
public void setNumber(int number) {
if (number < 0)
throw new NameAppException("Negative Number Setting Attempted: " + number);
this.number = number;
}
// System Utility Methods
@Override
public String toString() {
return String.format("%15s, %1s, %,10d", this.name, this.sex, this.number);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((sex == null) ? 0 : sex.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;
Name other = (Name) obj;
if (name == null) {
if (other.name != null)
return false;
}
else if (!name.equals(other.name))
return false;
if (sex == null) {
if (other.sex != null)
return false;
}
else if (!sex.equals(other.sex))
return false;
return true;
}
@Override
public int compareTo(Name arg) {
return arg.number - this.number;
}
// General User Methods
public void addToNumber(int number) {
if (number < 0)
throw new NameAppException("Negative Number Setting Attempted in addToNumber: " + number);
this.setNumber(this.getNumber() + number);
}
}
/*
* Name: Prof. Antonio C. Silvestri (STCC)
* Date: 10/26/2019
* Course Number: CSC-220
* Course Name: Data Structures and Algorithms
* Problem Number: HW6
* Email: silvestri@stcc.edu
* Description: Dedicated Exception Class to Support Application
* https://cs.stcc.edu/baby-names-application/
*/
@SuppressWarnings("serial")
public class NameAppException extends RuntimeException {
public NameAppException(String message) {
super(message);
}
}
/*
* Name: Prof. Antonio C. Silvestri (STCC)
* Date: 10/26/2019
* Course Number: CSC-220
* Course Name: Data Structures and Algorithms
* Problem Number: HW6
* Email: silvestri@stcc.edu
* Description: Baby Names Application
* https://cs.stcc.edu/baby-names-application/
*/
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
public class NameApplication {
private static final String TITLE = "Name Application V1.0";
private static final String CONTINUE_PROMPT = "Do this again? [y/N] ";
private static void outputResults(ArrayList<Name> malePop, ArrayList<Name> femalePop) {
System.out.println("Top " + malePop.size() + " Male names:");
for (int i = 0; i < malePop.size(); i++) {
System.out.println(malePop.get(i));
}
System.out.println();
System.out.println("Top " + femalePop.size() + " Female names:");
for (int i = 0; i < femalePop.size(); i++) {
System.out.println(femalePop.get(i));
}
System.out.println();
}
// **********************************************
private static int getStartYear(Scanner sc) {
do {
try {
System.out.print("Enter Start Year: ");
return sc.nextInt();
} catch (InputMismatchException ex) {
System.out.println("Start Year must be integers!");
} finally {
sc.nextLine();
}
} while (true);
}
// **********************************************
private static int getEndYear(Scanner sc) {
do {
try {
System.out.print("Enter End Year: ");
return sc.nextInt();
} catch (InputMismatchException ex) {
System.out.println("Start Year must be integers!");
} finally {
sc.nextLine();
}
} while (true);
}
// **********************************************
private static int getTopNames(Scanner sc) {
do {
try {
System.out.print("Enter Top Number of Names: ");
return sc.nextInt();
} catch (InputMismatchException ex) {
System.out.println("Top Number of Names must be integers!");
} finally {
sc.nextLine();
}
} while (true);
}
// **********************************************
private static void process(Scanner sc, String args[]) {
try {
int startYear = getStartYear(sc);
int endYear = getEndYear(sc);
int topNames = getTopNames(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.");
NameStats stats = new NameStats(startYear, endYear, topNames);
stats.generateStats();
ArrayList<Name> malePop = stats.getTopMaleNames();
ArrayList<Name> femalePop = stats.getTopFemaleNames();
outputResults(malePop, femalePop);
}
catch (NameAppException ex) {
System.out.println("Name 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[]) {
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);
}
}
/*
* Name: Prof. Antonio C. Silvestri (STCC)
* Date: 10/26/2019
* Course Number: CSC-220
* Course Name: Data Structures and Algorithms
* Problem Number: HW6
* Email: silvestri@stcc.edu
* Description: Names Class to Support Application
* https://cs.stcc.edu/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 NameStats {
private static final String BASE_URL = "https://cs.stcc.edu/~silvestri/names/";
private int startYear;
private int endYear;
private int top;
private ArrayList<Name> names;
public NameStats(int startYear, int endYear, int top) {
this.setStartEndYears(startYear, endYear);
this.setTop(top);
}
// Getters
public int getStartYear() {
return startYear;
}
public int getEndYear() {
return endYear;
}
public int getTop() {
return top;
}
// Setters
public void setStartEndYears(int startYear, int endYear) {
if (startYear > endYear)
throw new NameAppException(
String.format("StartYear %d passed must be <= EndYear %d passed", startYear, endYear));
if (startYear < 1880)
throw new NameAppException(String.format("StartYear %d passed must be >= 1880", startYear));
if (endYear > 2018)
throw new NameAppException(String.format("EndYear %d passed must be <= 2018", endYear));
this.startYear = startYear;
this.endYear = endYear;
this.names = null; // Invalidate the names list
}
public void setTop(int top) {
if (top <= 0)
throw new NameAppException(String.format("Top %d passed must be > 0", top));
this.top = top;
}
public void generateStats() throws MalformedURLException, IOException {
this.names = new ArrayList<Name>();
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(BASE_URL + "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();
Name baby = new Name(name, sex, number);
int index = this.names.indexOf(baby);
if (index == -1)
this.names.add(baby);
else
this.names.get(index).addToNumber(baby.getNumber());
}
}
}
System.out.println();
Collections.sort(this.names);
}
private ArrayList<Name> getTopNames(String sex) {
ArrayList<Name> topNames = new ArrayList<>();
int i = 0;
int count = 0;
while (i < this.names.size() && count < this.top) {
if (this.names.get(i).getSex().equals(sex)) {
topNames.add(this.names.get(i));
count++;
}
i++;
}
return topNames;
}
public ArrayList<Name> getTopMaleNames() {
return getTopNames("M");
}
public ArrayList<Name> getTopFemaleNames() {
return getTopNames("F");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.