In: Computer Science
Design two sub- classes of Employee...SalariedEmployee and HourlyEmployee. A salaried employee has an annual salary attribute. An hourly employee has an hourly pay rate attribute, an hours worked attribute, and an earnings attribute. An hourly employee that works more than 40 hours gets paid at 1.5 times their hourly pay rate. You will decide how to implement constructors, getters, setters, and any other methods that might be necessary. 1. 2. (20 points) Draw a UML diagram for the classes. (80 points) Implement the classes, and write a test program that creates a salaried employee and two hourly employees. One of the hourly employees should have hours worked set to less than 40 and one should have hours worked set to more than 40. The test program should display all attributes for the three employees. To keep things simple, the employee classes don’t need to do any editing.
Here is my code so far:
import java.util.Scanner;
public class Assignment6_lgolden81 {
public static void main(String[] args) {
//Declaring the variables for the main method
String fname, lname, city, state, street;
int employeeNO, zip;
int month, day, year, size;
//This Scanner class object is used to get the input entered by the user
Scanner input = new Scanner(System.in);
System.out.println("Please Enter the Desired Number of Employees: ");
size = input.nextInt();
//This is the Array used to hold the employee class information
Employee emp[] = new Employee[size];
//This loop is used to get the employee information
for (int i = 0; i < size; i++) {
//This portion of the program gets the input entered by the user
//prints out the employee number
System.out.println("\nEmployee #" + (i + 1) + ":\n");
input.nextLine();
//Prints out the employee's first name
System.out.println("\nEnter the Employee's First Name:");
fname = input.nextLine();
if (fname.length() < 1) {
System.out.println("Invalid Input: Please Enter a Valid First Name!");
System.exit(0);
}
//Prints out the employee's last name
System.out.println("\nEnter the Employee's Last Name:");
lname = input.nextLine();
if (lname.length() < 1) {
System.out.println("Invalid Input: Please Enter a Valid Last Name!");
System.exit(0);
}
///Prints out the employee's ID number
System.out.println("\nEnter the Employee's ID Number:");
employeeNO = input.nextInt();
input.nextLine();
if (employeeNO < 1) {
System.out.println("Invalid Input: Please Enter a Valid Employee ID Number!");
System.exit(0);
}
//Prints out the employee's street address
System.out.println("\nEnter the Street Address:");
street = input.nextLine();
if (street.length() < 1) {
System.out.println("Invalid Input: Please Enter a Valid Street Name!");
System.exit(0);
}
//Prints out the name of the City
System.out.println("\nEnter the City:");
city = input.nextLine();
if (city.length() < 1) {
System.out.println("Invalid Input: Please Enter a Valid City Name!");
System.exit(0);
}
//Prints out the name of the State
System.out.println("\nEnter the State (ex. GA):");
state = input.nextLine();
if (state.length() < 1) {
System.out.println("Invalid Input: Please Enter a Valid State Name!");
System.exit(0);
}
//Prints out the employee's zip code
System.out.println("\nEnter the Zip Code:");
zip = input.nextInt();
if (zip == 0) {
System.out.println("Invalid Input: Please Enter a Valid Zip Code!");
System.exit(0);
}
//While loop used to get the employee's hire date (Month) and makes sure the input is valid
while (true) {
System.out.println("\nEnter Month the Employee Was Hired:");
month = input.nextInt();
if (month < 1 || month > 12) {
System.out.println(">> Invalid Input: Value Must be between 1-12 <<");
input.close();
continue;
} else
break;
}
//While loop used to get the employee's hire date (Day) and makes sure the input is valid
while (true) {
System.out.println("\nEnter the Day the Employee Was Hired:");
day = input.nextInt();
if (day < 1 || day > 31) {
System.out.println(">> Invalid Input: Value Must be between 1-31 <<");
continue;
} else
break;
}
//While loop used to get the employee's hire date (Year) and makes sure the input is valid
while (true) {
System.out.println("\nEnter the Year the Employee Was Hired:");
year = input.nextInt();
if (year < 1900 || year > 2020) {
System.out.println(">> Invalid Input. Value Must be between 1900-2020 <<");
continue;
} else
break;
}
//Initialized the MyDate method
MyDate md = new MyDate(year, month, day);
//Created an Employee class object and populated the data into an array
emp[i] = new Employee(fname, lname, md, street, city, state, zip, employeeNO, i);
}
//Displays all of the employee information stored in the Array along with the employee number
System.out.println("\nDisplaying the Employee's Information");
for (int i = 0; i < size; i++) {
System.out.println("\n[Employee #" + (i + 1) + "]:");
System.out.println(emp[i].toString());
}
}
}
// Program Classes
//This class initializes the variables for the year
class MyDate {
private int year;
private int month;
private int day;
//Parameterized constructor
public MyDate(int year, int month, int day) {
super();
this.year = year;
this.month = month;
this.day = day;
}
//Getters and Setters for the year, month, and day
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
@Override
public String toString() {
return month + "/" + day + "/" + year;
}
}
//Employee class used to initialize the employee information
class Employee {
// Declared employee variables
private int employeeNumber;
private int employeeNO;
private MyDate hireDate;
private String street;
private String city;
private String state;
private String fname;
private String lname;
private int zip;
//Parameterized constructor
public Employee(String fname, String lname, MyDate hireDate,
String street, String city, String state, int zip,int employeeNO, int employeeNumber) {
super();
this.employeeNumber = employeeNumber;
this.employeeNO = employeeNO;
this.fname = fname;
this.lname = lname;
this.hireDate = hireDate;
this.street = street;
this.city = city;
this.state = state;
this.zip = zip;
}
/* Getters and Setters for the Employee Number, ID, First Name, Last Name,
* Hire Date, Street, City, State, Zip Code
*/
public int getEmployeeNumber() {
return employeeNumber;
}
public void setEmployeeNumber(int employeeNumber) {
this.employeeNumber = employeeNumber;
}
public int getEmployeeNO() {
return employeeNumber;
}
public void setEmployeeNO(int employeeNO) {
this.employeeNO = employeeNO;
}
public String getNamefirst() {
return fname;
}
public void setNamefirst(String fname) {
this.fname = fname;
}
public String getNamelast() {
return lname;
}
public void setNamelast(String lname) {
this.lname = lname;
}
public MyDate getHireDate() {
return hireDate;
}
public void setHireDate(MyDate hireDate) {
this.hireDate = hireDate;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public int getZip() {
return zip;
}
public void setZip(int zip) {
this.zip = zip;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "\nEmployee ID Number = " + employeeNO + "\n\nFirst Name = " + fname + "\n\nLast Name = " +
lname + "\n\nHire Date = " + hireDate + "\n\nStreet = " + street + "\n\nCity = " + city + "\n\nState = "
+ state + "\n\nZip Code = " + zip + "\n";
}
}
Representative Class subtleties are not given in the inquiry depiction. Henceforth, the subtleties are accepted for advantageous understanding and utilized in the UML Diagram. Kindly don't hesitate to change according to the planned needs.
UML Diagram:
Employee Class:
HourlyEmployee is the sub class.:
SalariedEmployee Class: