switch statement trouble

i am having problem with my switch statement in case 4 and maybe 5

Page 1 of 1

3 Replies - 1087 Views - Last Post: 07 January 2010 - 08:53 AM Rate Topic: -----

#1 john009  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 23-December 09

switch statement trouble

Posted 07 January 2010 - 07:07 AM

here is my code

my cases can be seen down below in bottom of code :)



package pkgjAssignmentThree;
import keyb.InOut;
public class MonthlyWages 
{
	
	/** Creates a new instance of Main */
	public MonthlyWages() 
	{
	}
	
	 static final int DeductionPercentage = 35; // Global int Variables
	 static final double StaffBasic = 600.00, HighComrate = 12.5, LowComrate = 7.5; // Global double Variables
	 static int NumberOfEmployees = 6;
	
	
   static void displayMenu()
	{
	
	   System.out.println("*****************************************************************************");
	   
		System.out.println("1 - All Employee Details");
		System.out.println("2 - Employees whose monthly sales where above 10,000");
		System.out.println("3 - Employees whose monthly sales are below the average");
		System.out.println("4 - Employee with the highest monthly sales");
		System.out.println("5 - Exit");
		
		System.out.println("*****************************************************************************");

	}// End of displayMenu module
	
  static void displayEmployees(int spRefNo[],String spName[],int spMonthlySales[], double spGrossPay[], double spDeductions[], double spNetPay[],int lowValue,double averageSales,String string)
  {
  
	  System.out.println(string);
	  
	  System.out.println(("Ref No") + "\t\t" + ("Name") + "\t" + ("Monthly Sales") + "\t" + ("Gross Pay") + "\t\t" + ("Deductions") + "\t\t" + ("Net Pay"));
	  
	  for (int empCount = 0;empCount < NumberOfEmployees; empCount++)
	  {
				 
		  if ( (spMonthlySales[empCount] >= lowValue) && ( spMonthlySales[empCount] < averageSales) )
		  {
			  
			  System.out.println("*************************************************************************************************");
			  System.out.println("");
			  System.out.println((spRefNo[empCount]) + "\t" + (spName[empCount]) + "\t  " + (spMonthlySales[empCount]) + "\t\t " + (InOut.format(spGrossPay[empCount],2)) + "\t\t" + (InOut.format(spDeductions[empCount],2)) + "\t\t\t" + (InOut.format(spNetPay[empCount],2)));		  
			  System.out.println("");
			  System.out.println("*************************************************************************************************");
			  
		  }// end if
		  
	  }// end repeat
	  
  }// end displayEmployees module
  
  
  static double averageResult(int[] spMonthlySales, int HowMany)
  {
  
	  int totalSales = 0;
	  double averageTotalSales = 0;		
	  
	  for (int index = 0; index < HowMany; HowMany = HowMany - 1 )
	  {
	  
		  totalSales = (int) (totalSales + spMonthlySales[index]);

	  }
	  
	  averageTotalSales = totalSales/NumberOfEmployees;
	  return averageTotalSales;

  }
  
  


	
	public static void main(String[] args) 
	{
	   
		
		int highestSales = 0; // Local Variables
		int empCount = 0;
		
		
		double[] spCommission; // Commission Arrays
		spCommission = new double[NumberOfEmployees];
		
		double[] spGrossPay; // Gross Pay Arrays
		spGrossPay = new double[NumberOfEmployees];
		
		double[] spDeductions; // Deductions Arrays
		spDeductions = new double[NumberOfEmployees];
		
		double[] spNetPay; // Monthly sales Arrays
		spNetPay = new double[NumberOfEmployees];
		
		int[] spMonthlySales; // Monthly sales Arrays
		spMonthlySales = new int[NumberOfEmployees];
		
		final int[] spRefNo; // Ref Number Arrays 
		spRefNo = new int [NumberOfEmployees];
		spRefNo[0] = 1000;
		spRefNo[1] = 2571;
		spRefNo[2] = 4002;
		spRefNo[3] = 1303;
		spRefNo[4] = 1004;
		spRefNo[5] = 1005;
		
		final String[] spName; // Name Arrays
		spName = new String[NumberOfEmployees];
		spName[0] = "Chris Barnes";
		spName[1] = "Daniel Pollock";
		spName[2] = "John McManus";
		spName[3] = "Jean Martin";
		spName[4] = "Rosemary Goody";
		spName[5] = "Liam Cage";
		
		empCount = 0;
		
		for (empCount = 0; empCount < NumberOfEmployees;)
		{
		
		System.out.println("*****************************************************************************");	
			
		System.out.println("Ref.No: " + spRefNo[empCount]);
		
		System.out.println("");
		
		System.out.println("Name : " + spName[empCount]);
		
		System.out.println("");
		
		System.out.println("Please enter monthly sales for employee:");
				
		spMonthlySales[empCount] = InOut.readInt();
		
		System.out.println("*****************************************************************************");
		System.out.println("");
		
		
		while(spMonthlySales[empCount] < 0 || spMonthlySales[empCount] >20000)
		{

		 System.out.println("Invalid value. Should be in the range 0 - 20000!");
		 System.out.println("Re-enter the salespersons monthly sales");  
		 System.out.println("Enter monthly sales");
		 spMonthlySales[empCount]=InOut.readInt();
			 
		} 
		
		if (spMonthlySales[empCount] >= 10000)
		{
			
			spCommission[empCount] = spMonthlySales[empCount] * (HighComrate/100.00); // working out Commisio
			spGrossPay[empCount] = StaffBasic + spCommission[empCount]; // working out Gross Pay
			spDeductions[empCount] = spGrossPay[empCount] * (DeductionPercentage/100.00); // working out Deductions
			spNetPay[empCount] = spGrossPay[empCount] - spDeductions[empCount]; // working out Net Pay		   
		
		}// end of Calculations
		else
		{
		
			spCommission[empCount] = spMonthlySales[empCount] * (LowComrate/100.00); // working out Commision
			spGrossPay[empCount] = StaffBasic + spCommission[empCount];// working out Gross Pay
			spDeductions[empCount] = spGrossPay[empCount] * (DeductionPercentage/100.00);// working out Deductions
			spNetPay[empCount] = spGrossPay[empCount] - spDeductions[empCount];// working out Net Pay
			
		}// end of Calculations
			
		
		if (spMonthlySales[empCount] > highestSales)
		{
		
			highestSales = spMonthlySales[empCount];
		
		}// end of if 
		
	   empCount = empCount + 1;
		
		}// end of details of employees
		
		int menuOption = 0;
		boolean validOption = true;
		
		
	   while (validOption = true)
		{

			displayMenu();
			
		   System.out.println("Please choose a menu option number");
		   menuOption = InOut.readInt();

		while((menuOption < 1) || (menuOption > 5))
		{

		   System.out.println("Invalid value. Should be in the range 1 - 5!");
		   System.out.println("Re-enter a menu option number");  
		   menuOption=InOut.readInt();
			 
		} // end of faild menu number
		
				
		switch (menuOption)
		{
		
			case 1:
			displayEmployees(spRefNo,spName, spMonthlySales, spGrossPay, spDeductions, spNetPay, 0,20000,"All Employees Details");

				
				  break;
			case 2:
			displayEmployees(spRefNo,spName,spMonthlySales,spGrossPay,spDeductions,spNetPay, 10000,20000, "Employees whose monthly sales are 10,000 or above");
				   
					break; 
			case 3:
				double averageSales;
		   averageSales = averageResult(spMonthlySales,NumberOfEmployees);
		   displayEmployees(spRefNo,spName,spMonthlySales,spGrossPay,spDeductions,spNetPay,0,averageSales,"Employees whose monthly sales are below the average");
				   
					break;
					
			case 4:
				
				for (int index = 0; NumberOfEmployees < 6; NumberOfEmployees = NumberOfEmployees-1)
				{
				
				   if ((int)spMonthlySales[index] == highestSales)
					{
					   
					   spMonthlySales[index] = (int) spNetPay[index] + 500;
					   
					   System.out.println((spRefNo[index]) + (spGrossPay[index]) + (spDeductions[index]) + (spNetPay[index]));
					
					}// end of if

				}// end of for 
				
				   break;
				   
			case 5:
				validOption = false;
 
		}// switch statement
				
	   }// end of while
   
	}// end of main module


 


Is This A Good Question/Topic? 0
  • +

Replies To: switch statement trouble

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10567
  • View blog
  • Posts: 39,121
  • Joined: 27-December 08

Re: switch statement trouble

Posted 07 January 2010 - 07:09 AM

Please respect rule #6 of this forum.

Quote

Describe any errors you are encountering. Help us help you!

Was This Post Helpful? 0
  • +
  • -

#3 john009  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 23-December 09

Re: switch statement trouble

Posted 07 January 2010 - 07:18 AM

the problem i am having is that when i enter 4 the case 4 does not show the highest value

the values i enter are

10000
9990
4900
15000
4000
6000
Was This Post Helpful? 0
  • +
  • -

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: switch statement trouble

Posted 07 January 2010 - 08:53 AM

Are you calculating the largest value inside the "case 4:" of the switch statement?
if yes, i dont understand the logic.
given an array of int values, suppose int[] arr, the way to print the biggest value of this array is:
(suppose it is inside a "case" block)

case 4:
int largest = arr[0];
for(int i = 1; i< arr.length;i++)
{
	 if(arr[i] > largest)
	 {
		   largest = arr[i];
	  }
}
System.out.println(largest);
break;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1