Skip to content

Instantly share code, notes, and snippets.

@jananpatel2002
Created November 8, 2021 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jananpatel2002/e4805ef883ca0a8ac18fd29d1070628e to your computer and use it in GitHub Desktop.
Save jananpatel2002/e4805ef883ca0a8ac18fd29d1070628e to your computer and use it in GitHub Desktop.
/*
* Name: Janan Patel
* Date: 11/1/2021
* Course Number: CSC-220
* Course Name: Data Structures and Algorithms
* Problem Number: HW6
* Email: jkpatel2001@student.stcc.edu
* Description: Names methods
*/
public class Name implements Comparable<Name> {
private String name;
private String sex;
private int number;
public String getName() {
return name;
}
public String getSex() {
return sex;
}
public int getNumber() {
return number;
}
public void setName(String name) throws NameAppException {
if (name == null || name == " ")
throw new NameAppException("The name cannot be blank");
else
this.name = name.substring(0, 1).toUpperCase() + name.substring(1);
}
public void setSex(String sex) throws NameAppException {
if (sex.toUpperCase().equals("M") || sex.toUpperCase().equals("F"))
this.sex = sex.toUpperCase();
else
throw new NameAppException("Invalid sex");
}
public void setNumber(int number) throws NameAppException {
if (number >= 0)
this.number = number;
else
throw new NameAppException("The number cannot be negative");
}
public Name(String name, String sex, int number) throws NameAppException {
setName(name);
setSex(sex);
setNumber(number);
}
@Override
public String toString() {
return String.format("%15s, %1s, %,5d\n", getName(), getSex(), getNumber());
}
@Override
public int compareTo(Name o) {
if (this.number < o.number) {
return this.number * -1;
}
if (this.number > o.number) {
return this.number;
}
return 0;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + number;
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 b = (Name) obj;
var a = this;
if (a.name == null) {
if (b.name != null)
return false;
} else if (!a.name.equals(b.name))
return false;
if (a.sex == null) {
if (b.sex != null)
return false;
} else if (!a.sex.equals(b.sex))
return false;
return true;
}
public void addToNumber(int addingNumber) throws NameAppException {
if (addingNumber >= 0) {
setNumber(getNumber() + addingNumber);
} else
throw new NameAppException("Number must be positive");
}
}
public class NameAppException extends Exception {
private static final long serialVersionUID = 1L;
public NameAppException(String thrownString) {
super(thrownString);
}
}
/*
* Name: Janan Patel
* Date: 11/8/2021
* Course Number: CSC-220
* Course Name: Data Structures and Algorithms
* Problem Number: HW6
* Email: jkpatel2001@student.stcc.edu
* Description: Names methods
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
public class NamesApp {
private final static String TITLE = "The Name Listing Application";
private final static String CONTINUE_PROMPT = "Do this again? [y/N] ";
static int num = 0;
// **********************************************
// Put as many methods you need here
public static ArrayList<Name> getTopMaleNames(ArrayList<Name> males, int num) {
ArrayList<Name> topmales = new ArrayList<>();
Collections.sort(males);
Collections.reverse(males);
for (int i = 0; i < num; i++) {
topmales.add(males.get(i));
}
return topmales;
}
public static ArrayList<Name> getTopFemaleNames(ArrayList<Name> females, int num) {
ArrayList<Name> topfemales = new ArrayList<>();
Collections.sort(females);
Collections.reverse(females);
for (int i = 0; i < num; i++) {
topfemales.add(females.get(i));
}
return topfemales;
}
public static void readingData(Scanner input, ArrayList<Name> males, ArrayList<Name> females)
throws NameAppException, IOException, MalformedURLException {
try {
int startYear = 0;
int endYear = 0;
int count = 0;
System.out.println("Please enter the Start year, End year, and the amount of top names to list");
startYear = input.nextInt();
if ((startYear > 2020) || (startYear < 1880)) {
throw new NameAppException("Start Year is not valid");
}
endYear = input.nextInt();
if ((startYear > endYear) || ((endYear < 1880) || (endYear > 2020))) {
throw new NameAppException("End Year is not valid");
}
num = input.nextInt();
input.nextLine();
for (int i = startYear; i <= endYear; i++) {
Scanner sc = new Scanner(new URL("https://cs.stcc.edu/~silvestri/names/yob" + i + ".txt").openStream());
sc.useDelimiter("\\s*,\\s*|\\s+");
while (sc.hasNextLine()) {
while (count == 0) {
System.out.println(
"Processing... Please give it a couple seconds (This process can take longer. It depends on what the year gap is) : ");
count++;
}
String name = sc.next();
String sex = sc.next();
int number = sc.nextInt();
Name person = new Name(name, sex, number);
if (sex.equals("M")) {
int index = males.indexOf(person);
if (index == -1)
males.add(person);
else
males.get(index).addToNumber(person.getNumber());
} else if (sex.equals("F")) {
int index = females.indexOf(person);
if (index == -1)
females.add(person);
else
females.get(index).addToNumber(person.getNumber());
}
sc.nextLine();
}
sc.close();
}
} catch (NameAppException ex) {
System.out.println("Name Exception: " + ex.getMessage());
}
}
// **********************************************
// Start your logic coding in the process method
private static void process(Scanner input, String args[]) throws Exception {
ArrayList<Name> males = new ArrayList<>();
ArrayList<Name> females = new ArrayList<>();
readingData(input, males, females);
System.out.println("\nMales:");
System.out.println("\n" + getTopMaleNames(males, num));
System.out.println("\nFemales:");
System.out.println("\n" + getTopFemaleNames(females, num));
}
// **********************************************
// Do not change the doThisAgain method
private static boolean doThisAgain(Scanner input, String prompt) {
System.out.print(prompt);
String doOver = input.nextLine();
return doOver.trim().equalsIgnoreCase("Y");
}
// **********************************************
// Do not change the main method
public static void main(String args[]) throws Exception {
System.out.println("Welcome to " + TITLE);
Scanner input = new Scanner(System.in);
do {
process(input, args);
} while (doThisAgain(input, CONTINUE_PROMPT));
input.close();
System.out.println("Thank you for using " + TITLE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment