package PAyroll;


import javax.swing.JOptionPane;
import javax.swing.UIManager;

import java.awt.Color;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.swing.UIManager;

public class EmployeeProfile {

	

	
	public static void main(String[] args){
		  
		
	    
		  JOptionPane.showMessageDialog(null,"Welcome to Payroll System");
	
		  
		 payroll();
	
	
	}
   


		 public static void payroll(){ 	
			 int conf=0; 
			 
		
		//	do{ 
				 
			       String name="";
			       String add= "";
			       String contact= "";
			       String email= "";
			       String word="";	
			       String mainmenu="";
			       int rh=0;
			       int employee=0;
			       int r=0;
			mainmenu=JOptionPane.showInputDialog(null,"A. Employee \nB. Compute Payroll \nC. Display Payslip \nE.Exit ");     
			
			int size[]= new int[employee];
			employee =Integer.parseInt(JOptionPane.showInputDialog(null,"Enter number of employees: "));
		
			
			for(int x=0;x<=size.length;x++){ 
	     
		 name = JOptionPane.showInputDialog(null, "Enter Name:","Employee ["+(x+1)+"];",JOptionPane.PLAIN_MESSAGE);
	      add = JOptionPane.showInputDialog(null,"Enter Home Address:","Employee ["+(x+1)+"];",JOptionPane.PLAIN_MESSAGE);
    	  contact = JOptionPane.showInputDialog(null,"Enter contact:","Employee ["+(x+1)+"];",JOptionPane.PLAIN_MESSAGE);
		  email = JOptionPane.showInputDialog(null,"Enter Email Address:","Employee ["+(x+1)+"];",JOptionPane.PLAIN_MESSAGE);
		  

		if ( name == null || name.equals("") || add == null || add.equals("") || contact == null || contact.equals("") ||
				  email ==null || email.equals("")){
		  
		  JOptionPane.showMessageDialog(null,"invalid information","Student Enrollment",JOptionPane.PLAIN_MESSAGE);
		 }		
		  rh = Integer.parseInt(JOptionPane.showInputDialog("Enter Rate Per Hour:"));
			
	      
	         JOptionPane.showMessageDialog(null,"Name: "+name+"\n\nHome Address:"+add+"\n\nContact:"+contact+"\n\nEmail Address:"+email);  
		     
			
			}
		  }  
		  
	 }

in this code, if the user inputs 2 employees, the whole code must be repeated twice.


thanks
waiting for ur reply guys!

Recommended Answers

All 2 Replies

Swap these two line.

employee =Integer.parseInt(JOptionPane.showInputDialog(null,"Enter number of employees: "));
int size[]= new int[employee];

the logic is flawed to start with, the for should never loop from 0 to the size of the employee array, correction int array for the moment. it should loop from 0 to the number of employees you would like to create at a time.

Even tho standard payroll programs wouldnt even suggest creating multiple employees at the same time, it would be A)Create Employee , and if you wanna create 2 then you are going to go through this menu twice.

but to fix your problem :

1)loop from 0 to employee
2)create the array AFTER saving a user input in employee

int size[]= new int[employee];
employee =Integer.parseInt(JOptionPane.showInputDialog(null,"Enter number of employees: "));

heres a few suggestions unrelated to your looping problem;

3)use more meaningfull names for your variables (ex:"nbEmployees" instead of "employee" when saving an input that asks "how many employees do you want to create?")

4)put part of your code in a try{}catch(Exception e){} block, those "Integer.parseInt(...)" methods can throw exceptions if you enter letters or invalid characters that cant be parsed to numbers. At the moment, these are unhandled and will make your program crash saying "Unhandlled Exception : blah blah blah" ~

5)your array named "size" (refer to tip #3) doesnt contain anything yet because you never actually save anything in int, but if you were to, you couldnt because it expects values of type "int" and you would like to save a bunch of Strings and numbers that represent an Employee. Ideally, i would say "create an Employee class that contains all the variables you need to represent one, then create an array of 'Employee' items instead of ints" , but im assuming your course hasn't taught you the principles of object oriented programming yet.

This means that you will want to create many arrays, all of the same size, representing each of the values that make up an Employee, and because it would be a tidious task to resize them all once you add a new Employee, simply start them at a hardcoded size, like 25 or 50, and keep a variable for the ammount of Employees you currently have stored, example "nbTotalEmployees" that would start at 0 and increase by 1 every time you add a new one. and a variable for the max number that was hardcoded.

the idea behind this would be to have like :

public class CarInventory{
    //arrays representing a car :
    int nbMaxCars = 25;
    int nbCars = 0;
    String[] model = new String[nbMaxCars];
    String[] color = new String[nbMaxCars];
    int[] seats = new int[nbMaxCars];

    public addCar(String newModel, String aColor , int nbSeats){
        //time to add values into the first empty spot for a new car
        //only if we havent reached the max
        if(nbCars < nbMaxCars){
            //store the arguments we received in the arrays that they belong in,
            //because arrays a 0 based (start at index 0), nbCars will always represent
            //the first available spot in the array, unless it is equal to nbMaxCars
            model[nbCars] = newModel;
            color[nbCars] = aColor;
            seats[nbCars] = nbSeats;

            //increment nbCars because we added a new car to the arrays
            nbCars++;
        }
    }
}

here is a little try-catch example :

public void someMethod(){
    String s = "this is not a number";
    int x;

    //this creates an unhandled exception, comment this line to test the try catch
    x = Integer.parseInt(s); 

    //this will not crash because you will handle the Exception
    try{
        x = Integer.parseInt(s);
    }
    catch(Exception e){
        //you will only enter the catch, if an Exception hapenned inside the "try" block
    
        //first way to get info about the error :
        System.out.println("Some error happenned, here is some generic message : " + e.getMessage());

        //second way to get info about the error :
        e.printStackTrace();
    }
}
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.