Beginner programmer here and I am lost on a project for a class. I have to develop a Java application that calculates the monthly paychecks for a numbe of different types of employees. The employee types are created in a subclass array based on parent base class Employee. The initial code is provided for each class and for a driver class.
This is what I have so far:
The program is supposed to ask me what kind of employee I am, for my first name, last name, ssn, and depending on my employee type my salary, etc. I have no idea what I am doing, could someone please take a look and let me know where my goofs are or if I am on the right path. Thus far it does not keep my information so I think my loop is goofed, and it does not print out the information I supplied.
This is what happens when I run the program and enter 1 for salaried employee, it pops up with the box for the information, I imput all that it asks for then it ask me what kind of employee I am again, when I press cancel I get this:
----jGRASP exec: java PayrollSystemTest1
Exception in thread "main" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:454)
at java.lang.Integer.parseInt(Integer.java:527)
at PayrollSystemTest1.main(PayrollSystemTest1.java:47)
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
Thanks for any help.
//PayrollSystemTest.java
//Employee hierarchy test program.
import javax.swing.*;
public class PayrollSystemTest1
{
public static void main( String args[])
{
SalariedEmployee salariedEmployee=
new SalariedEmployee("John", "Smith", "111-11-1111", 800.00);
HourlyEmployee hourlyEmployee=
new HourlyEmployee("Karen", "Price", "222-22-2222", 16.75, 40);
CommissionEmployee commissionEmployee=
new CommissionEmployee("Sue", "Jones", "333-33-3333", 10000, .06);
BasePlusCommissionEmployee basePlusCommissionEmployee=
new BasePlusCommissionEmployee("Bob", "Lewis", "444-44-4444", 5000, .04, 300);
//create four-element Employee array
Employee employees[] = new Employee[4];
//declarations
String first = " ";
String last = " ";
String ssn = " ";
String salaryString;
double salary = 0;
String wageString;
double wage = 0;
String hoursString;
int hours = 0;
String salesString;
double sales = 0;
String rateString;
double rate =0;
String empTypeString = " ";
int empType = 0;
int count = 0;
for (count = 0; count < 5; count++)
{
empTypeString = JOptionPane.showInputDialog
(null, "Please enter the type of employee you are: \n\nEnter the number that is your employee type\n1. Salaried\n2. Hourly\n3. Commission\n 4. Base Pay Plus Commission", JOptionPane.QUESTION_MESSAGE);
empType = Integer.parseInt(empTypeString);
switch(empType)
{
case 1:
first = JOptionPane.showInputDialog
(null, "Please enter your first name: ", "First Name", JOptionPane.QUESTION_MESSAGE);
last = JOptionPane.showInputDialog
(null, "Please enter your last name: ", "Last Name", JOptionPane.QUESTION_MESSAGE);
ssn = JOptionPane.showInputDialog
(null, "Please enter your Social Security Number: ", JOptionPane.QUESTION_MESSAGE);
salaryString = JOptionPane.showInputDialog
(null, "Please enter your weekly salary: ", "Salary", JOptionPane.QUESTION_MESSAGE);
salary = Double.parseDouble(salaryString);
break;
case 2:
first = JOptionPane.showInputDialog
(null, "Please enter your first name: ", "First Name", JOptionPane.QUESTION_MESSAGE);
last = JOptionPane.showInputDialog
(null, "Please enter your last name: ", "Last Name", JOptionPane.QUESTION_MESSAGE);
ssn = JOptionPane.showInputDialog
(null, "Please enter your Social Security Number: ", JOptionPane.QUESTION_MESSAGE);
wageString = JOptionPane.showInputDialog
(null, "Please enter your hourly wage: ", "Hourly Wage", JOptionPane.QUESTION_MESSAGE);
wage = Double.parseDouble(wageString);
hoursString = JOptionPane.showInputDialog
(null, "Please enter the hours worked: ", JOptionPane.QUESTION_MESSAGE);
hours = Integer.parseInt(hoursString);
break;
case 3:
first = JOptionPane.showInputDialog
(null, "Please enter your first name: ", "First Name", JOptionPane.QUESTION_MESSAGE);
last = JOptionPane.showInputDialog
(null, "Please enter your last name: ", "Last Name", JOptionPane.QUESTION_MESSAGE);
ssn = JOptionPane.showInputDialog
(null, "Please enter your Social Security Number: ", JOptionPane.QUESTION_MESSAGE);
salesString = JOptionPane.showInputDialog
(null, "Please enter your gross weekly sales: ", "GrossSales",JOptionPane.QUESTION_MESSAGE);
sales = Double.parseDouble(salesString);
rateString = JOptionPane.showInputDialog
(null, "Plese enter the % of commission you receive: ", "Commission Rate", JOptionPane.QUESTION_MESSAGE);
rate = Double.parseDouble(rateString);
break;
case 4:
first = JOptionPane.showInputDialog
(null, "Please enter your first name: ", "First Name", JOptionPane.QUESTION_MESSAGE);
last = JOptionPane.showInputDialog
(null, "Please enter your last name: ", "Last Name", JOptionPane.QUESTION_MESSAGE);
ssn = JOptionPane.showInputDialog
(null, "Please enter your Social Security Number: ", JOptionPane.QUESTION_MESSAGE);
salesString = JOptionPane.showInputDialog
(null, "Please enter your gross weekly sales: ", "GrossSales",JOptionPane.QUESTION_MESSAGE);
sales = Double.parseDouble(salesString);
rateString = JOptionPane.showInputDialog
(null, "Please enter the % of commission you receive: ", "Commission Rate", JOptionPane.QUESTION_MESSAGE);
rate = Double.parseDouble(rateString);
salaryString = JOptionPane.showInputDialog
(null, "Please enter your base pay: ", "Base Pay", JOptionPane.QUESTION_MESSAGE);
salary = Double.parseDouble(salaryString);
break;
default:
JOptionPane.showInputDialog
(null, "That is invalid", JOptionPane.ERROR_MESSAGE);
}
switch (count)
{
case 1:
employees[0] = new SalariedEmployee(first, last, ssn, salary);
break;
case 2:
employees[1] = new HourlyEmployee(first, last, ssn, wage, hours);
break;
case 3:
employees[2] = new CommissionEmployee(first, last, ssn, sales, rate);
break;
case 4:
employees[3] = new BasePlusCommissionEmployee(first, last, ssn, sales, rate, salary);
break;
}
}
//initialize array with Employees
employees[0] = salariedEmployee;
employees[1] = hourlyEmployee;
employees[2] = commissionEmployee;
employees[3] = basePlusCommissionEmployee;
System.out.println( "Employees processed polymorphically: \n" );
//generically process each element in array employees
for (Employee currentEmployee : employees)
{
System.out.println( currentEmployee); //invokes toString
// determine whether element is a BasePlusCommissionEmployee
if (currentEmployee instanceof BasePlusCommissionEmployee)
{
//downcast Employee referene to
//BasePlusCommissionEmployee reference
BasePlusCommissionEmployee employee =
(BasePlusCommissionEmployee) currentEmployee;
double oldBaseSalary = employee.getBaseSalary();
employee.setBaseSalary ( 1.10 * oldBaseSalary);
System.out.printf(
"new base salary with 10%% increase is: $%,.2f\n",
employee.getBaseSalary() );
}//end if
System.out.printf(
"earned $%, .2f\n\n", currentEmployee.earnings() );
}//end for
//get type name of each object in employees array
for (int j=0; j<employees.length; j++)
System.out.printf ( "Employee %d is a %s\n", j,
employees [j].getClass().getName() );
}// end main
}// end class PayrollSystemTest