this programm is a execption handling method example problem. this is a pretty b
ID: 3842159 • Letter: T
Question
this programm is a execption handling method example problem. this is a pretty begginer calss so we havent discussed any major tops we are using abstract methods and execption hadling to deal with this problem. I put this Question up already but the code was far to complex for the level i am at tright now and was hard to understand.. will include an example of a person Class as reference to how much we have used.
Write a program to enter employee data into an array, including each employee’s name, Social Security number, and salary. The maximum number of employees is 100, but your program should work with any number of employees less than 100.
Your program should have two exception classes, one called SSNLengthException which is thrown when the Social Security number entered – without the dashes or spaces – is not exactly nine characters, and the other called SSNCharacterException which is thrown when any character in the Social Security number is not a digit.
Note #1: In this program, you may use the abbreviation SSN for “Social Security Number” in both variables and class names.
Your program also should check that a valid number is entered for an employee’s salary. This would including checking for a numeric entry (NumberFormatException) and that a negative number is not entered (InvalidArgumentException). Both of the exceptions are already part of the Java language
Note #2: When an exception is thrown, the user should be reminded of what he or she entered, told why it is inappropriate, and asked to reenter the data that was rejected.
After all of the data has been entered, your program should display the records for all employees, with an annotation stating whether the employee’s salary is above or below the average.
You will need to define the classes Employee, SSNLengthException, and SSNCharacterException. Derive the class Employee from the class Person given on Blackboard along with the Homework Assignment. Every Employee object should record the empoyee’s name (recorded in Person), salary, Social Security Number, and any other data you think might be appropriate.
Here is an example of how you might enter data into your program (user input is underlined in blue):
Entering info for employee #1
Enter employee name (or Q to finish): G. Washington
Enter month name, day, year (no commas): February 22 1732
Enter 9-digit SSN: 0000000000001
Error: "11122333344" does not have 9 digits, please re-enter.
Enter 9-digit SSN: 000000001
Enter employee's salary: 90000
Entering info for employee #2
Enter employee name (or Q to finish): Oprah Winfrey
Enter month name, day, year (no commas): January 29 1954
Enter 9-digit SSN: 987654321ABC
Error: "987654321ABC" is not all numeric, please re-enter.
Enter 9-digit SSN: 987654321
Enter employee's salary: 125000
Entering info for employee #3
Enter employee name (or Q to finish): Pancho Villa
Enter month name, day, year (no commas): June 05 1878
Enter 9-digit SSN: 123456789
Enter employee's salary: 63500
Entering info for employee #4
Enter employee name (or Q to finish): Harry Potter
Enter month name, day, year (no commas): July 31 1980
Enter 9-digit SSN: 248492940
Enter employee's salary: 42,000
Error: "42,000" is not all numeric
Enter employee’s salary: -42000
Error: You cannot enter a negative salary
Enter employee’s salary: 42000
Entering info for employee #5
Enter employee name (or Q to finish): q
then output in a table using print f
listings of your Employee, SSNLengthException, and SSNCharacterException classes, along with the class that contains main().
EXAMPLE OF CODE FROM DIFFERENT PROJECT
Explanation / Answer
import java.util.*;
class Person {
private String name ;
private Date born ;
private Date died ; //null indicates still alive.
public Person(String initialName, Date birthDate, Date deathDate) {
if (consistent(birthDate, deathDate)) {
name = initialName ;
born = new Date(birthDate.getTime()) ;
if (deathDate == null)
died = null ;
else
died = new Date(deathDate.getTime()) ;
} else {
System.out.println("Inconsistent dates. Aborting.") ;
System.exit(0) ;
}
}
public Person(Person original) {
if (original == null) {
System.out.println("Fatal error.") ;
System.exit(0) ;
}
name = original.name ;
born = new Date(original.born.getTime()) ;
if (original.died == null)
died = null ;
else
died = new Date(original.died.getTime()) ;
}
public void set(String newName, Date birthDate, Date deathDate) {
if (consistent(birthDate, deathDate)) {
name = newName ;
born = new Date(birthDate.getTime()) ;
if (deathDate == null)
died = null ;
else
died = new Date(deathDate.getTime()) ;
} else {
System.out.println("Inconsistent dates. Aborting.") ;
System.exit(0) ;
}
}
public String toString( ) {
if (died == null) {
return (name + ", Born: " + born) ;
}
return (name + ", Born: " + born + "-" + died) ;
}
public boolean equals(Object anObject) {
if (anObject == null || getClass() != anObject.getClass()) {
return false ;
}
Person otherPerson = (Person) anObject ;
return (name.equals(otherPerson.name) &&
born.equals(otherPerson.born) &&
datesMatch(died, otherPerson.died) ) ;
}
private static boolean datesMatch(Date date1, Date date2) {
if (date1 == null)
return (date2 == null) ;
else if (date2 == null) //&& date1 != null
return false ;
else // both dates are not null.
return(date1.equals(date2)) ;
}
/**
* Precondition: newDate is a consistent date of birth.
* Postcondition: Date of birth of the calling object is newDate.
* */
public void setBirthDate(Date newDate) {
if (consistent(newDate, died))
born = new Date(newDate.getTime()) ;
else {
System.out.println("Inconsistent dates. Aborting.") ;
System.exit(0) ;
}
}
/**
* Precondition: newDate is a consistent date of death.
* Postcondition: Date of death of the calling object is newDate.
* */
public void setDeathDate(Date newDate) {
if (!consistent(born, newDate)) {
System.out.println("Inconsistent dates. Aborting.") ;
System.exit(0) ;
}
if (newDate == null)
died = null ;
else
died = new Date(newDate.getTime()) ;
}
public void setName(String newName) {
name = newName ;
}
/**
* Precondition: The date of birth has been set, and changing the year
* part of the date of birth will give a consistent date of birth.
* Postcondition: The year of birth is (changed to) newYear.
* */
public void setBirthYear(int newYear) {
if (born == null) { //Precondition is violated
System.out.println("Fata ; Error. Aborting.") ;
System.exit(0) ;
}
born.setYear(newYear) ;
if (!consistent(born, died)) {
System.out.println("Inconsistent dates. Aborting.") ;
System.exit(0) ;
}
}
/**
* Precondition: The date of death has been set, and changing the year
* part of the date of death will give a consistent date of death.
* Postcondition: The year of death is (changed to) newYear.
* */
public void setDeathYear(int newYear) {
if (died == null) { //Precondition is violated
System.out.println("Fata ; Error. Aborting.") ;
System.exit(0) ;
}
died.setYear(newYear) ;
if (!consistent(born, died)) {
System.out.println("Inconsistent dates. Aborting.") ;
System.exit(0) ;
}
}
public String getName() {
return name ;
}
public Date getBirthDate( ) {
return new Date(born.getTime()) ;
}
public Date getDeathDate( ) {
if (died == null)
return null ;
else
return new Date(died.getTime()) ;
}
/**
* To be consistent, birthDate must not be null. If there is no date of
* death (deathDate == null), that is consistent with any birthDate.
* Otherwise, the birthDate must come before or be equal to the deathDate.
* */
private static boolean consistent(Date birthDate, Date deathDate) {
if (birthDate == null)
return false ;
else if (deathDate == null)
return true ;
else
return (birthDate.before(deathDate)
|| birthDate.equals(deathDate )) ;
}
}
class SSNLengthException extends Exception{
private Long ssn;
SSNLengthException(Long num){
ssn = num;
}
public Long getSsn(){
return ssn;
}
}
class SSNCharacterException extends Exception{
private String ssn;
SSNCharacterException(String num){
ssn = num;
}
public String getSsn(){
return ssn;
}
}
public class Employee extends Person
{
Long ssn;
Double salary;
Employee(String name, Date brth, Date death, Long ssn_no, Double sal)
{
super(name, brth, death);
ssn = ssn_no;
salary = sal;
}
public Double getSalary(){
return salary;
}
public Long getSsn(){
return ssn;
}
public void setSalary(Double sal){
salary = sal;
}
public void setSsn(Long num){
ssn = num;
}
public static void main(String args[]){
Scanner in=new Scanner(System.in);
Employee []emp = new Employee[100];
char flag = 'y' ;
int count = 0;
String input;
String date;
Date brth;
String ssn = "";
Double sal = 0.0;
while(count < 100){
System.out.println("Entering info for employee #" + count + 1);
System.out.print("Enter employee name (or Q to finish):") ;
input = in.next();
if(input.equals("q") || input.equals("Q")){
break;
}
count++;
System.out.print("Enter month name, day, year (no commas) (Format mm dd yyyy: ");
String mo = in.next();
/*StringTokenizer st = new StringTokenizer(date," ");
*/Calendar calendar = Calendar.getInstance();
//System.out.println(st.nextToken());
//System.out.println(st.nextToken());
calendar.clear();
calendar.set(Calendar.MONTH, Integer.parseInt(mo));
calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(in.next()));
calendar.set(Calendar.YEAR, Integer.parseInt(in.next()));
brth = calendar.getTime();
flag = 'y';
while(flag == 'y'){
System.out.print("Enter 9-digit SSN: ");
try{
ssn = in.next();
try{
long l = Long.parseLong(ssn);
}
catch(NumberFormatException e){
throw new SSNCharacterException(ssn);
}
if(ssn.length() != 9){
throw new SSNLengthException(Long.parseLong(ssn));
}
flag = 'n';
}
catch(SSNCharacterException e){
System.out.println("Error: "" + e.getSsn() + """ + "is not all numeric, please re-enter.");
}
catch(SSNLengthException e){
System.out.println("Error: "" + e.getSsn() + "" " + "does not have 9 digits, please re-enter.");
}
}
flag = 'y';
while(flag == 'y'){
System.out.print("Enter employee's salary: ");
try{
sal = Double.parseDouble(in.next());
if(sal < 0){
throw new IllegalArgumentException();
}
flag = 'n';
}
catch(NumberFormatException n){
System.out.println("Please enter valid salary , salary can't be non-numeric");
}
catch(IllegalArgumentException inv){
System.out.println("Negative values not allowed");
}
}
emp[count-1] = new Employee(input, brth , null, Long.parseLong(ssn) , sal);
}
for(int i =0;i < count;i++){
System.out.println(emp[i].getName() + " " + emp[i].getBirthDate() + " " + emp[i].getSsn() + " " +emp[i].getSalary());
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.