Skip to content

Instantly share code, notes, and snippets.

@jananpatel2002
Created November 1, 2021 18:52
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/f7fe3232a4008ea765f6785acb078ab7 to your computer and use it in GitHub Desktop.
Save jananpatel2002/f7fe3232a4008ea765f6785acb078ab7 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 "Name: " + getName() + " \n Sex: " + getSex() + " \n Number: " + getNumber() + "\n";
}
@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 other = (Name) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (number != other.number)
return false;
if (sex == null) {
if (other.sex != null)
return false;
} else if (!sex.equals(other.sex))
return false;
return true;
}
public void addToNumber(int addingNumber) throws NameAppException {
if (number >= 0) {
setNumber(getNumber() + addingNumber);
} else
throw new NameAppException("Number must be positive");
}
}
/*
* 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: Exception class
*/
public class NameAppException extends Exception {
private static final long serialVersionUID = 1L;
public NameAppException(String thrownString) {
super(thrownString);
}
}
/*
* 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());
break;
}
case "P": {
System.out.println("Print the List");
Collections.sort(people);
System.out.println(people);
break;
}
case "T": {
System.out.println("Terminating List Processing");
break;
}
default:
System.out.println("Bad Option Specified.");
break;
}
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