-
-
Save bytecodeman/d9e3740278e51b91bf90ff7f05c41ea2 to your computer and use it in GitHub Desktop.
Name Class for Names Application Project Part 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Name: Prof. Antonio C. Silvestri (STCC) | |
* Date: 10/26/2020 | |
* 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.trim().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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Name: Prof. Antonio C. Silvestri (STCC) | |
* Date: 10/26/2020 | |
* 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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Name: Prof. Antonio C. Silvestri (STCC) | |
* Date: 10/24/2020 | |
* Course Number: CSC-220 | |
* Course Name: Data Structures and Algorithms | |
* Problem Number: HW6 | |
* Email: silvestri@stcc.edu | |
* Description: Names Class Driver/Tester | |
*/ | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.Scanner; | |
public class NameClassTesterApp { | |
private static final String TITLE = "Name Class Driver/Tester V1.0"; | |
private static final String CONTINUE_PROMPT = "Do this again? [y/N] "; | |
// ********************************************** | |
private static String getFirstCharacter(String str) { | |
str = str.trim().toUpperCase(); | |
return str.isEmpty() ? "" : str.substring(0, 1); | |
} | |
// ********************************************** | |
private static void process(Scanner sc, String args[]) { | |
try { | |
ArrayList<Name> people = new ArrayList<>(); | |
while (true) { | |
System.out.print("Enter [A]dd Name, [P]rint List, or [T]erminate List Processing: "); | |
String strShape = getFirstCharacter(sc.nextLine()); | |
switch (strShape) { | |
case "A" -> { | |
System.out.println("Add a Name to List"); | |
System.out.print("Enter name sex number (space separated): "); | |
String name = sc.next(); | |
String sex = sc.next(); | |
int number = sc.nextInt(); | |
sc.nextLine(); | |
Name person = new Name(name, sex, number); | |
int index = people.indexOf(person); | |
if (index == -1) | |
people.add(person); | |
else | |
people.get(index).addToNumber(person.getNumber()); | |
} | |
case "P" -> { | |
System.out.println("Print the List"); | |
Collections.sort(people); | |
System.out.println(people); | |
} | |
case "T" -> { | |
System.out.println("Terminating List Processing"); | |
} | |
default -> | |
System.out.println("Bad Option Specified."); | |
} | |
if (strShape.equals("T")) | |
break; | |
} | |
} | |
catch (NameAppException ex) { | |
System.out.println("Name Exception: " + ex.getMessage()); | |
} | |
catch (Exception ex) { | |
ex.printStackTrace(); | |
} | |
} | |
//********************************************** | |
// Do not change the doThisAgain method | |
private static boolean doThisAgain(Scanner sc, String prompt) { | |
System.out.print(prompt); | |
String doOver = sc.nextLine(); | |
return doOver.trim().equalsIgnoreCase("Y"); | |
} | |
//********************************************** | |
// Do not change the main method | |
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment