JohnnyB
JohnnyB

Reputation: 3

How to stop when a certain case is entered?

I am working on an assignment that is basically a payroll calendar where employee information is entered and then calculated. I stored the last name, first name, and net wages into an array so when it is terminated, the array will display the last name and net wages. I am a beginner in java and have been trying to fix this for a while now.

Here is my code:

    /*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package cuyamacapayroll;
import java.util.*;
import java.util.Arrays;


class CuyamacaPayroll {

public static void main(String[] args) {    
Scanner sc = new Scanner(System.in);

    double grossWages, taxesWithheld, retirementWithheld, netWages, salary;
    String firstName, lastName;
    int typing;
    int numOfHours;        
        String employeeFirstName[] = new String[5];
        String employeeLastName[] = new String[5];
        double employeeNetWages[] = new double[5];

System.out.println("Cuyamaca Travel Agency Weekly Payroll Calculator");

int count = 0;
while(true) {
//Prompt the user for the employee type, followed by the employee name (first and last).
    System.out.print("Enter Employee Type [S]alaried, [H]ourly, [T]emporary, [Q]uit, [D]isplay Information: ");
    char typeOfEmployee = sc.next().charAt(0);
//After the employee name has been entered, you will use a switch statement
//to determine the next course of action dependent on the employee type.
//Your switch statement must be able to support both upper-case and lower-case characters,
//representing employee type. Depending on the employee type,
//you will prompt the user for either their yearly or hourly salary.

switch(typeOfEmployee) {

    case 's':
    case 'S':

        System.out.print("Enter Employee First Name: ");
              firstName = sc.next();
              employeeFirstName[ count ] = firstName;

        System.out.print("Enter Employee Last Name : ");
              lastName = sc.next();
              employeeLastName[ count ] = lastName;

        System.out.print(firstName + " " + lastName + "'s Yearly Salary: ");
              salary = sc.nextDouble();

        System.out.print("Enter " + firstName + " " + lastName + "'s Number of Hours: ");
              numOfHours = sc.nextInt();

        System.out.println("Employee: "+firstName+" "+lastName);
              grossWages = salary / 52;
              taxesWithheld = grossWages *.18;
              retirementWithheld = grossWages * .04;
              netWages = grossWages - taxesWithheld - retirementWithheld;
              employeeNetWages[ count ] = netWages;
                  count = count + 1; // moves the array to next index

        System.out.printf("Gross Wages: $%.2f\n", grossWages);
        System.out.printf("Taxes Withheld: $%.2f\n", taxesWithheld);
        System.out.printf("Retirement Withheld: $%.2f\n", retirementWithheld);
        System.out.printf("Net Wages: $%.2f\n", netWages);
              typeOfEmployee = 0;
break;
case 'h':
case 'H':
          System.out.print("Enter Employee First Name: ");
              firstName = sc.next();
              employeeFirstName[ count ] = firstName;

              System.out.print("Enter Employee Last Name : ");
              lastName = sc.next();
              employeeLastName[ count ] = lastName;


              System.out.print(firstName + " " + lastName + "'s Hourly Salary: ");
          salary = sc.nextDouble();

          System.out.print("Enter " + firstName + " " + lastName + "'s Number of Hours: ");
          numOfHours = sc.nextInt();

          System.out.println("Employee: "+firstName+lastName);
          grossWages = salary * numOfHours;
          taxesWithheld = grossWages *.18;
          netWages = grossWages - taxesWithheld;
          employeeNetWages[ count ] = netWages;

                  count = count + 1; // moves the array to next index
          System.out.println("Gross Wages: $"+grossWages);
          System.out.println("Taxes Withheld: $"+taxesWithheld);
          System.out.println("Net Wages: $"+netWages);
          break;
case 't':
case 'T':
          System.out.print("Enter Employee First Name: ");
              firstName = sc.next();
              employeeFirstName[ count ] = firstName;

              System.out.print("Enter Employee Last Name : ");
              lastName = sc.next();
              employeeLastName[ count ] = lastName;


              System.out.print(firstName + " " + lastName + "'s Hourly Salary: ");
          salary = sc.nextDouble();
          System.out.print("Enter " + firstName + " " + lastName + "'s Number of Hours: ");
          numOfHours = sc.nextInt();
          System.out.println("Employee: "+firstName+lastName);
          grossWages = salary * numOfHours;
          if(numOfHours > 35)
          grossWages += salary * (numOfHours - 35) * 0.5;
          System.out.println("Gross Wages: $"+grossWages);
          System.out.println("NO Taxes Deducted");
          break;

case 'd':
case 'D':  

    System.out.println("Display Employees: Sort By [L]ast Name, [W]ages or [X] To Quit: ");
    break;

case 'l':
case 'L':

    System.out.println("Last Names Are : " + Arrays.toString(employeeLastName));
    break;

case 'w':
case 'W':

    System.out.println("Net Wages Are : " + Arrays.toString(employeeNetWages));
    break;

case 'q':
case 'Q':
    return;

default  :  System.out.println("Invalid Employee Type");
}



}

}

          }

Whenever I enter "D" I want it to stop asking for employee information.

Also, is there a way to sort the arrays in alphabetical order?

Any help would be appreciated thanks!

Upvotes: 0

Views: 66

Answers (2)

Danda
Danda

Reputation: 390

Instead of contentiously running the while loop with while(true) use a boolean variable to control the loop.

declare it as : boolean check = true;

now While condition is : while(check){ }

In switch case 'D' you want to terminate so if you enter case 'D' make this boolean as FALSE. so you will come out of while loop.

I hope this will help you.

Upvotes: 2

modify the while condition, while(true) will keep you there looping forever..

create a boolean variable and set or clear the value depending of the key pressed

like:

int count = 0;
boolean flag = false;
while(!flag) {
...
case 'd':
case 'D':  

    System.out.println("Display Employees: Sort By [L]ast Name, [W]ages or [X] To Quit: ");
flag = true;
    break;

Upvotes: 1

Related Questions