So I need to convert the Date to a string in the DayofBirth JOPtionPane areas that starts on lines 43 . it is originally defined as a date and must remain so. The book I am reading recommended using .toString() well that was useless or I just could not figure it out. so how would I keep the Date as is but just convert it so that I can use it in my JOptionPayne for user input and output?

package payrollsystemtest;

import javax.swing.JOptionPane;

/**
 *
 * 
 */
public class PayrollSystemTest
{
   public static void main( String args[] )
   {
      //create a four element employee array
       Employee employees[]=new Employee[4];
       String first;
       String last;
       String ssn;
       Date DayofBirth;
       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;

       for(count=0;count<5;count++)
       {
           empTypeString=
JOptionPane.showInputDialog(null, "What is your employment status?\n\n 1. Salaried\n2.\nHourly\n3.Commissioned\n4.Base Plus Commissioned", "Employee Type", JOptionPane.QUESTION_MESSAGE);
empType=Integer.parseInt(empTypeString);

             switch(empType)
             {
                 case 1:
first=JOptionPane.showInputDialog(null,"What is your first name?","First name",JOptionPane.QUESTION_MESSAGE);
last=JOptionPane.showInputDialog(null,"What is your last name?","last name",JOptionPane.QUESTION_MESSAGE);
ssn= JOptionPane.showInputDialog(null,"What is your social security number?","SSN",JOptionPane.QUESTION_MESSAGE);   
DayofBirth.toString() =JOptionPane.showInputDialog(null,"What is your date of birth?","Day of Birth",JOptionPane.QUESTION_MESSAGE);
salaryString=JOptionPane.showInputDialog(null,"What is your weekly pay?","salary",JOptionPane.QUESTION_MESSAGE);
salary=Double.parseDouble(salaryString);
                     break;

                case 2:

first=JOptionPane.showInputDialog(null,"What is your first name?","First name",JOptionPane.QUESTION_MESSAGE);
last=JOptionPane.showInputDialog(null,"What is your last name?","last name",JOptionPane.QUESTION_MESSAGE);
ssn= JOptionPane.showInputDialog(null,"What is your social security number?","SSN",JOptionPane.QUESTION_MESSAGE);
DayofBirth =JOptionPane.showInputDialog(null,"What is your date of birth?","Day of Birth",JOptionPane.QUESTION_MESSAGE);
wageString=JOptionPane.showInputDialog(null,"What is hourly wage?","Hourly wage",JOptionPane.QUESTION_MESSAGE);
wage=Double.parseDouble(wageString);
hoursString=JOptionPane.showInputDialog(null,"Please enter the hours worked each week?","Hours worked",JOptionPane.QUESTION_MESSAGE);
hours=Integer.parseInt(hoursString);
                    break;
                case 3:
first=JOptionPane.showInputDialog(null,"What is your first name?","First name",JOptionPane.QUESTION_MESSAGE);
last=JOptionPane.showInputDialog(null,"What is your last name?","last name",JOptionPane.QUESTION_MESSAGE);
ssn= JOptionPane.showInputDialog(null,"What is your social security number?","SSN",JOptionPane.QUESTION_MESSAGE);
DayofBirth =JOptionPane.showInputDialog(null,"What is your date of birth?","Day of Birth",JOptionPane.QUESTION_MESSAGE);
salesString=JOptionPane.showInputDialog(null,"What is your weekly  sales?"," sales",JOptionPane.QUESTION_MESSAGE);
sales=Double.parseDouble(salesString);
rateString=JOptionPane.showInputDialog(null,"What is commission rate on your sales?","Commission Rate ",JOptionPane.QUESTION_MESSAGE);
rate=Double.parseDouble(rateString);
                    break;
                case 4:
first=JOptionPane.showInputDialog(null,"What is your first name?","First name",JOptionPane.QUESTION_MESSAGE);
last=JOptionPane.showInputDialog(null,"What is your last name?","last name",JOptionPane.QUESTION_MESSAGE);
ssn= JOptionPane.showInputDialog(null,"What is your social security number?","SSN",JOptionPane.QUESTION_MESSAGE);
DayofBirth =JOptionPane.showInputDialog(null,"What is your date of birth?","Day of Birth",JOptionPane.QUESTION_MESSAGE);
salesString=JOptionPane.showInputDialog(null,"What is your weekly  sales?"," sales",JOptionPane.QUESTION_MESSAGE);
sales=Double.parseDouble(salesString);                    
rateString=JOptionPane.showInputDialog(null,"What is commission rate on your sales?","Commission Rate ",JOptionPane.QUESTION_MESSAGE);
rate=Double.parseDouble(rateString);  
 salaryString=JOptionPane.showInputDialog(null,"What is your weekly pay?","salary",JOptionPane.QUESTION_MESSAGE);
salary=Double.parseDouble(salaryString);
                    break;

                default:
                    JOptionPane.showMessageDialog(null,"Invalid entry, please try again","Try Again" ,JOptionPane.ERROR_MESSAGE);
                    break;

             }
             switch(count)
             {
                 case 0:  Employees[0]=new 
 SalariedEmployee(first,last,ssn,DayofBirth,salary);
                     break;
                 case 1: Employees[1]=new
HourlyEmployee(first,last,ssn,DayofBirth,wage,hours);
                     break;
                 case 2: Employees[2]=new
CommissionEmployee(first,last,ssn,DayofBirth,sales,rate);
                     break;
                 case 3:  Employee[3]=new
BasePlusCommissionemployee(first,last,ssn,DayofBirth,sales,rate,salary);

             }

       }

      for ( Employee currentEmployee : employees )
      {
         System.out.println( currentEmployee ); // invokes toString

         // determine whether element is a BasePlusCommissionEmployee
         if ( currentEmployee instanceof BasePlusCommissionEmployee )
         {
            // downcast Employee reference 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

         if(currentDate.getMonth()==currentEmployee.getBirthday().getMonth())
         {
             System.out.printf("earned $%,.2f. %s\n\n", currentEmployee.earnings() + 100.00,
                     "Note: added a $100 bonus to your payroll amount for birthday!!!" );
         }else{
             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().getSimpleName() );
   } // end main

String.valueOf(date) this might help

Member Avatar for 1stDAN

hi,

I think that the result of JOptionPane.showInputDialog(...) is of type String (also Object might be possible). Therefore, instead of

DayofBirth.toString() = JOptionPane.showInputDialog(null, "What is your date of birth?","Day of Birth",JOptionPane.QUESTION_MESSAGE);

it should be

String dateString = JOptionPane.showInputDialog(null,"What is your date of birth?","Day of Birth",JOptionPane.QUESTION_MESSAGE);

Then you can convert dateString into Date-type:

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/mm/dd");
    Date DayofBirth = dateFormat.parse(dateString);

I have chosen ISO "yyyy/mm/dd". Depending on your location you might choose your own local date formatting, see SimpleDateformat.

(Btw, follow this famous SUN-Java convention: Click Here, variables should always start with small letters.)

my advice, a few simple refactoring would make that code a lot easier to read and maintain.
a 'for instance'

                 case 1:
first=JOptionPane.showInputDialog(null,"What is your first name?","First name",JOptionPane.QUESTION_MESSAGE);
last=JOptionPane.showInputDialog(null,"What is your last name?","last name",JOptionPane.QUESTION_MESSAGE);
ssn= JOptionPane.showInputDialog(null,"What is your social security number?","SSN",JOptionPane.QUESTION_MESSAGE);   
DayofBirth.toString() =JOptionPane.showInputDialog(null,"What is your date of birth?","Day of Birth",JOptionPane.QUESTION_MESSAGE);
salaryString=JOptionPane.showInputDialog(null,"What is your weekly pay?","salary",JOptionPane.QUESTION_MESSAGE);
salary=Double.parseDouble(salaryString);
                     break;
                case 2:
first=JOptionPane.showInputDialog(null,"What is your first name?","First name",JOptionPane.QUESTION_MESSAGE);
last=JOptionPane.showInputDialog(null,"What is your last name?","last name",JOptionPane.QUESTION_MESSAGE);
ssn= JOptionPane.showInputDialog(null,"What is your social security number?","SSN",JOptionPane.QUESTION_MESSAGE);
DayofBirth =JOptionPane.showInputDialog(null,"What is your date of birth?","Day of Birth",JOptionPane.QUESTION_MESSAGE);
wageString=JOptionPane.showInputDialog(null,"What is hourly wage?","Hourly wage",JOptionPane.QUESTION_MESSAGE);
wage=Double.parseDouble(wageString);
hoursString=JOptionPane.showInputDialog(null,"Please enter the hours worked each week?","Hours worked",JOptionPane.QUESTION_MESSAGE);
hours=Integer.parseInt(hoursString);
                    break;

and, for now I'm only guessing that the

DayofBirth.toString() =JOptionPane.showInputDialog(null,"What is your date of birth?","Day of Birth",JOptionPane.QUESTION_MESSAGE);

was supposed to be:

DayofBirth =JOptionPane.showInputDialog(null,"What is your date of birth?","Day of Birth",JOptionPane.QUESTION_MESSAGE);

you can re-write the above just as:

 case 1:
setBasics();
salaryString=JOptionPane.showInputDialog(null,"What is your weekly pay?","salary",JOptionPane.QUESTION_MESSAGE);
salary=Double.parseDouble(salaryString);
                     break;
                case 2:
                setBasics();
wageString=JOptionPane.showInputDialog(null,"What is hourly wage?","Hourly wage",JOptionPane.QUESTION_MESSAGE);
wage=Double.parseDouble(wageString);
hoursString=JOptionPane.showInputDialog(null,"Please enter the hours worked each week?","Hours worked",JOptionPane.QUESTION_MESSAGE);
hours=Integer.parseInt(hoursString);
                    break;

and then just add the following method:

public static void setBasics(){
    first=JOptionPane.showInputDialog(null,"What is your first name?","First name",JOptionPane.QUESTION_MESSAGE);
    last=JOptionPane.showInputDialog(null,"What is your last name?","last name",JOptionPane.QUESTION_MESSAGE);
    ssn= JOptionPane.showInputDialog(null,"What is your social security number?","SSN",JOptionPane.QUESTION_MESSAGE);
    DayofBirth =JOptionPane.showInputDialog(null,"What is your date of birth?","Day of Birth",JOptionPane.QUESTION_MESSAGE);
}

now, if I'm not mistaken, you call those lines in each and every possible outcome (for each case), so, it would be even better to do:

if (emType > 0 && emType < 5)
setBasics(); // so it won't run if you hit the 'default case'
switch(emType){
 case 1:
setBasics();
salaryString=JOptionPane.showInputDialog(null,"What is your weekly pay?","salary",JOptionPane.QUESTION_MESSAGE);
salary=Double.parseDouble(salaryString);
                     break;
                case 2:
                setBasics();
wageString=JOptionPane.showInputDialog(null,"What is hourly wage?","Hourly wage",JOptionPane.QUESTION_MESSAGE);
wage=Double.parseDouble(wageString);
hoursString=JOptionPane.showInputDialog(null,"Please enter the hours worked each week?","Hours worked",JOptionPane.QUESTION_MESSAGE);
hours=Integer.parseInt(hoursString);
                    break;
...
}

I still having a problem and its just the dang Date on line 140 it needs a string but it must be a date to pass to my employee superclass. It needs to be a date because of the employee superclass it is used on line 15. here I will post both . sigh thanks in advance I am pulling out my dang teeth and punching the ground here. It is probably really something very easy I am just overlooking.

package payrollsystemtest;

import java.text.SimpleDateFormat;
import javax.swing.JOptionPane;

/**
 *
 * 
 */
public class PayrollSystemTest
{
    private static String first;
    private static String last;
    private static String ssn;
    private static Date DayofBirth ;
    private static final int currentDate = 11;


   public static void main( String args[] )

   {
      //create a four element employee array
       Employee employees[]=new Employee[4];

       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;

       for(count=0;count<5;count++)
       {
           empTypeString=
JOptionPane.showInputDialog(null, "What is your employment status?\n\n 1. Salaried\n2.\nHourly\n3.Commissioned\n4.Base Plus Commissioned", "Employee Type", JOptionPane.QUESTION_MESSAGE);
empType=Integer.parseInt(empTypeString);

             switch(empType)
             {
                 case 1:
setEmpData();
salaryString=JOptionPane.showInputDialog(null,"What is your weekly pay?","salary",JOptionPane.QUESTION_MESSAGE);
salary=Double.parseDouble(salaryString);
                     break;

                case 2:

setEmpData();
wageString=JOptionPane.showInputDialog(null,"What is hourly wage?","Hourly wage",JOptionPane.QUESTION_MESSAGE);
wage=Double.parseDouble(wageString);
hoursString=JOptionPane.showInputDialog(null,"Please enter the hours worked each week?","Hours worked",JOptionPane.QUESTION_MESSAGE);
hours=Integer.parseInt(hoursString);
                    break;
                case 3:
setEmpData();
salesString=JOptionPane.showInputDialog(null,"What is your weekly  sales?"," sales",JOptionPane.QUESTION_MESSAGE);
sales=Double.parseDouble(salesString);
rateString=JOptionPane.showInputDialog(null,"What is commission rate on your sales?","Commission Rate ",JOptionPane.QUESTION_MESSAGE);
rate=Double.parseDouble(rateString);
                    break;
                case 4:
setEmpData();
salesString=JOptionPane.showInputDialog(null,"What is your weekly  sales?"," sales",JOptionPane.QUESTION_MESSAGE);
sales=Double.parseDouble(salesString);                    
rateString=JOptionPane.showInputDialog(null,"What is commission rate on your sales?","Commission Rate ",JOptionPane.QUESTION_MESSAGE);
rate=Double.parseDouble(rateString);  
 salaryString=JOptionPane.showInputDialog(null,"What is your weekly pay?","salary",JOptionPane.QUESTION_MESSAGE);
salary=Double.parseDouble(salaryString);
                    break;

                default:
 JOptionPane.showMessageDialog(null,"Invalid entry, please try again","Try Again" ,JOptionPane.ERROR_MESSAGE);
                    break;

             }
             switch(count)
             {
                 case 0:  Employee[0]=new 
 SalariedEmployee(first,last,ssn,DayofBirth,salary);
                     break;
                 case 1: Employees[1]=new
HourlyEmployee(first,last,ssn,DayofBirth,wage,hours);
                     break;
                 case 2: Employees[2]=new
CommissionEmployee(first,last,ssn,DayofBirth,sales,rate);
                     break;
                 case 3:  Employee[3]=new
BasePlusCommissionEmployee(first,last,ssn,DayofBirth,sales,rate,salary);

             }

       }

      for ( Employee currentEmployee : employees )
      {
         System.out.println( currentEmployee ); // invokes toString

         // determine whether element is a BasePlusCommissionEmployee
         if ( currentEmployee instanceof BasePlusCommissionEmployee )
         {
            // downcast Employee reference 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

         if(currentDate.getMonth()==currentEmployee.getBirthday().getMonth())
         {
             System.out.printf("earned $%,.2f. %s\n\n", currentEmployee.earnings() + 100.00,
                     "Note: added a $100 bonus to your payroll amount for birthday!!!" );
         }else{
             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().getSimpleName() );


   } // end main
  public static void setEmpData(){
    first=JOptionPane.showInputDialog(null,"What is your first name?","First name",JOptionPane.QUESTION_MESSAGE);
    last=JOptionPane.showInputDialog(null,"What is your last name?","last name",JOptionPane.QUESTION_MESSAGE);
    ssn= JOptionPane.showInputDialog(null,"What is your social security number?","SSN",JOptionPane.QUESTION_MESSAGE);
   DayofBirth =JOptionPane.showInputDialog(null,"What is your date of birth?","Day of Birth",JOptionPane.QUESTION_MESSAGE);

}


} // end class PayrollSystemTest

And here is the employee superclass

package payrollsystemtest;

/**
 *
 * 
 */
public abstract class Employee
{
   private String firstName;
   private String lastName;
   private String socialSecurityNumber;
   private Date birthday;

   // three-argument constructor
   public Employee( String first, String last, String ssn , Date DayOfBirth)
   {
      firstName = first;
      lastName = last;
      socialSecurityNumber = ssn;
      birthday = DayOfBirth;
   } // end three-argument Employee constructor

   // set first name
   public void setFirstName( String first )
   {
      firstName = first;
   } // end method setFirstName

   // return first name
   public String getFirstName()
   {
      return firstName;
   } // end method getFirstName

   // set last name
   public void setLastName( String last )
   {
      lastName = last;
   } // end method setLastName

   // return last name
   public String getLastName()
   {
      return lastName;
   } // end method getLastName

   // set social security number
   public void setSocialSecurityNumber( String ssn )
   {
      socialSecurityNumber = ssn; // should validate
   } // end method setSocialSecurityNumber

   // return social security number
   public String getSocialSecurityNumber()
   {
      return socialSecurityNumber;
   } // end method getSocialSecurityNumber

   // set birthday
   public void setBirthday(Date DayOfBirth)
   {
       birthday = DayOfBirth;
   }// end method

   // get birthday
   public Date getBirthday()
   {
       return birthday;
   }

   // return String representation of Employee object
    @Override
   public String toString()
   {
      return String.format( "%s %s\nsocial security number: %s\nbirthday: %s\n",
         getFirstName(), getLastName(), getSocialSecurityNumber(), getBirthday());
   } // end method toString

   // abstract method overridden by subclasses
   public abstract double earnings(); // no implementation here
} // end abstract class Employee

Take a short break, then go back to 1stDAN's post above, which spells out exactly what you need to do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.