Skip to content
Advertisement

How to determine whether a switch case was chosen?

I’ve got a project that I am working on and it requires I have an array with a set size of 5. I then need to gather data from 4 different cases, and store them 5 times. (One of the cases can be used multiple times, case gets specified by user input). Here is what I have tried:

for (count = 0; count < 5; count++) {
     employeeTypeString = JOptionPane.showInputDialog(null, "What type of employee are you?nnEnter the number that corresponds to your answer.n1. Salariedn2. Hourlyn3. Commissionedn4. Base Plus Commissioned", "Employee Type", JOptionPane.QUESTION_MESSAGE);
     employeeType = Integer.parseInt(employeeTypeString);

     switch (employeeType) {
        case 1:
           firstName = JOptionPane.showInputDialog(null, "What is your first name?", "First Name", JOptionPane.QUESTION_MESSAGE);
           lastName = 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);
           salaryString = JOptionPane.showInputDialog(null, "What is your weekly salary?", "Salary", JOptionPane.QUESTION_MESSAGE);
           salary = Double.parseDouble(salaryString);
           break;

        case 2:
           firstName = JOptionPane.showInputDialog(null, "What is your first name?", "First Name", JOptionPane.QUESTION_MESSAGE);
           lastName = 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);
           wageString = JOptionPane.showInputDialog(null, "What is your hourly wage?", "Hourly Wage", JOptionPane.QUESTION_MESSAGE);
           wage = Double.parseDouble(wageString);
           hoursString = JOptionPane.showInputDialog(null, "How many hours per week do you work?", "Hours Worked", JOptionPane.QUESTION_MESSAGE);
           hours = Integer.parseInt(hoursString);
           break;

        case 3:
           firstName = JOptionPane.showInputDialog(null, "What is your first name?", "First Name", JOptionPane.QUESTION_MESSAGE);
           lastName = 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);
           salesString = JOptionPane.showInputDialog(null, "What are your weekly gross sales?", "Gross Sales", JOptionPane.QUESTION_MESSAGE);
           sales = Double.parseDouble(salesString);
           rateString = JOptionPane.showInputDialog(null, "What percent of commission do you receive?", "Commsission Rate", JOptionPane.QUESTION_MESSAGE);
           rate = Double.parseDouble(rateString);
           break;

        case 4:
           firstName = JOptionPane.showInputDialog(null, "What is your first name?", "First Name", JOptionPane.QUESTION_MESSAGE);
           lastName = 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);
           salesString = JOptionPane.showInputDialog(null, "What are your weekly gross sales?", "Gross Sales", JOptionPane.QUESTION_MESSAGE);
           sales = Double.parseDouble(salesString);
           rateString = JOptionPane.showInputDialog(null, "What percent of commission do you receive?", "Commsission Rate", JOptionPane.QUESTION_MESSAGE);
           rate = Double.parseDouble(rateString);
           salaryString = JOptionPane.showInputDialog(null, "What is your base pay?", "Base Pay", JOptionPane.QUESTION_MESSAGE);
           salary = Double.parseDouble(salaryString);
           break;

        default:
           JOptionPane.showMessageDialog(null, "Invalid entry!", "Error", JOptionPane.ERROR_MESSAGE);
           break;
     }
     switch (count) {
        case 0:
           employees[0] = new SalariedEmployee(firstName, lastName, ssn, salary);
           break;
        case 1:
           employees[1] = new HourlyEmployee(firstName, lastName, ssn, wage, hours);
           break;
        case 2:
           employees[2] = new CommissionEmployee(firstName, lastName, ssn, sales, rate);
           break;
        case 3:
           employees[3] = new BasePlusCommissionEmployee(firstName, lastName, ssn, sales, rate, salary);
           break;
        case 4:
           employees[4] = new SalariedEmployee(firstName, lastName, ssn, salary);
     }
  }

I planned on replacing the last Switch(count) with something else, but am not sure how to go about it. The issue I have, is that no matter what I input, that Switch(count) will always iterate to the next one and does not care what I chose. So if I chose the employeeType of 1, then chose employee type of 3, it would ask me the info for type 3, but display all the info for type 2 (leading to blank results).

salaried employee: john smith
social security number: 111-11-1111
date of birth: null
weekly salary: $800.00
earned $3,200.00

hourly employee: sue jones
social security number: 222-22-2222
date of birth: null
hourly wage: $0.00; hours worked: 0.00
earned $0.00

commission employee: karen price
social security number: 333-33-3333
date of birth: null
gross sales: $10,000.00; commission rate: 0.06
earned $2,400.00

base-salaried commission employee: bob lewis
social security number: 444-44-4444
date of birth: null
gross sales: $5,000.00; commission rate: 0.04; base salary: $300.00
new base salary with 10% increase is: $330.00
earned $2,120.00

salaried employee: jomes allen
date of birth: null
weekly salary: $300.00
earned $1,200.00

Employee 0 is a SalariedEmployee
Employee 1 is a HourlyEmployee
Employee 2 is a CommissionEmployee
Employee 3 is a BasePlusCommissionEmployee
Employee 4 is a SalariedEmployee

As shown above, the output displays a Salaried, Hourly, Commission, BasePlus, then Salaried employee, when I actually input a Salaried, then Salaried again.

Advertisement

Answer

If I understand correctly, this should work:

for (count = 0; count < 5; count++) {
     employeeTypeString = JOptionPane.showInputDialog(null, "What type of employee are you?nnEnter the number that corresponds to your answer.n1. Salariedn2. Hourlyn3. Commissionedn4. Base Plus Commissioned", "Employee Type", JOptionPane.QUESTION_MESSAGE);
     employeeType = Integer.parseInt(employeeTypeString);

     switch (employeeType) {
        case 1:
           // input data (removed for brevity)
           employees[count] = new SalariedEmployee(firstName, lastName, ssn, salary);
           break;

        case 2:
           // input data (removed for brevity)
           employees[count] = new HourlyEmployee(firstName, lastName, ssn, wage, hours);
           break;

        case 3:
           // input data (removed for brevity)
           employees[count] = new CommissionEmployee(firstName, lastName, ssn, sales, rate);
           hours);
           break;

        case 4:
           // input data (removed for brevity)
           employees[count] = new BasePlusCommissionEmployee(firstName, lastName, ssn, sales, rate, salary);
           break;

        default:
           JOptionPane.showMessageDialog(null, "Invalid entry!", "Error", JOptionPane.ERROR_MESSAGE);
           count--;   // Subtract 1 so the loop repeats
           break;
     }
  }
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement