0% found this document useful (0 votes)
51 views2 pages

Employee Salary Calculation in Java

The document contains the code for a Java program that stores employee data like ID, name, department, designation and salary in arrays. It takes an employee ID as input, searches for it in the employee ID array and if found, prints the employee details like name, department, designation and salary calculated based on the designation code. If the ID is invalid, it prints an error message.

Uploaded by

Debasis Dutta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views2 pages

Employee Salary Calculation in Java

The document contains the code for a Java program that stores employee data like ID, name, department, designation and salary in arrays. It takes an employee ID as input, searches for it in the employee ID array and if found, prints the employee details like name, department, designation and salary calculated based on the designation code. If the ID is invalid, it prints an error message.

Uploaded by

Debasis Dutta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

public class Project1 {

public static void main(String args[])


{
int[] empid=new int[] {1001,1002,1003,1004,1005,1006,1007};
String[] empname=new String[]
{"Ashish","Sushma","Rahul","Chahat","Ranjan","Suman","Tanmay"};
String[] joindate=new String[]
{"01/04/1999","23/08/2012","12/11/2008","29/01/2013","16/07/2005","01/01/2000","
12/06/2006"};
char[] desigcode=new char[]{'e','c','k','r','m','e','c'};
String[] dept=new String[]{"R&D","PM","Accounts","Front
Desk","Engineering","Manufacturing","PM"};
int[] basic=new int[]{20000,30000,10000,12000,50000,23000,29000};
int[] hra=new int[]{8000,12000,8000,6000,20000,9000,12000};
int[] it=new int[]{3000,9000,1000,20000,4400,10000};
int empno=[Link](args[0]);
int a=-1;
for(int i=0;i<[Link];i++)
{
if(empno==empid[i])
{
a=i;
break;
}
}
if(a!=-1)
{
String Name=empname[a];
String Dept=dept[a];
String desig=null;
int salary=basic[a]+hra[a]-it[a];
switch(desigcode[a])
{
case 'e':
desig="Engineering";
salary=salary+20000;
break;
case 'c':
desig="Consultancy";
salary=salary+32000;
break;
case 'k':
desig="clerk";
salary=salary+12000;
break;
case 'r':
desig="Receptionist";
salary=salary+15000;
break;
case 'm':
desig="Manager";
salary=salary+40000;
break;
}
[Link]("empno \t Name \t Department \t Designation \t
Salary");
[Link](empid[a]+" "+empname[a]+" "+dept[a]+" "+desig+"
"+salary);
}
else
{
[Link]("invalid empid");

This study source was downloaded by 100000834089164 from [Link] on 05-14-2022 21:50:52 GMT -05:00

[Link]
}
}
}

This study source was downloaded by 100000834089164 from [Link] on 05-14-2022 21:50:52 GMT -05:00

[Link]
Powered by TCPDF ([Link])

Common questions

Powered by AI

The program uses a for loop to iterate over the empid array and checks if any element matches the provided employee ID. If a match is found, the index of the matched employee ID is stored in the variable 'a', which is then used to access other employee details .

Converting the input employee ID from a string to an integer without validation poses security risks such as input exploitation through unexpected characters that could cause parsing errors or exceptions. Implementing input validation to check if the input is numeric and lies within valid ID ranges can prevent potential manipulation or accidental crashes, enhancing program robustness and security .

If the employee ID provided does not exist in the dataset, the output will be 'invalid empid'. This is because the variable 'a' remains -1, indicating that no match was found in the for loop iterating over empid, triggering the else statement that produces this output .

Using only arrays to manage employee data can lead to scalability issues, as adding new attributes requires modifications across multiple arrays and hardlands data manipulation. Arrays do not dynamically resize, posing difficulties when handling larger datasets or frequent updates. Additionally, lack of encapsulation mechanism increases risk of data inconsistency, suggests alternatives like objects or database structures for better data integrity and management .

While this specific program does not contain inline comments, their inclusion would potentially enhance the code's readability and maintainability by explaining the purpose and functionality of different blocks of code. Inline comments serve as a guide for both current and future developers, enabling them to quickly understand logic and any special considerations without tracing through the logic directly, thereby easing debugging and updates .

To optimize the program's salary calculation, one could implement a more generic approach by abstracting designation-specific operations into method calls or using data structures, like maps, to dynamically associate designation codes with allowances. This reduces redundant switch cases, eases the addition of new designations, and maintains separation of concerns by decoupling salary calculation from designation logic .

The switch statement enhances the program's structure by reducing the complexity and improving readability when determining salaries based on designation codes. Instead of multiple if-else statements, it offers a cleaner and more efficient way to assign designation-specific allowances, which simplifies maintenance and reduces the risk of errors in logic .

The program calculates an employee's net salary by first identifying the employee via their ID from the provided arguments. Then, it retrieves corresponding details (basic salary, HRA, and IT deductions). The net salary is calculated by adding the basic salary and HRA, subtracting IT deductions, and finally adding a designation-specific allowance to this result. The allowance is determined by a switch statement based on the designation code ('e', 'c', 'k', 'r', 'm'), which maps to Engineering, Consultancy, Clerk, Receptionist, and Manager roles respectively .

Refactoring the program into an object-oriented approach would involve creating a class, e.g., 'Employee', encapsulating all related data fields (ID, name, join date, department, etc.) and methods for salary calculation. This encapsulation would replace the multiple arrays with instances of the Employee class, improving maintainability, scalability, and reducing redundancy. The program logic would shift from procedural loops over arrays to method invocations on Employee objects, promoting polymorphism, inheritance, and leading to more robust design patterns .

The program utilizes arrays as the key data structure. Specifically, it uses arrays to store employee IDs (int[] empid), names (String[] empname), joining dates (String[] joindate), designation codes (char[] desigcode), departments (String[] dept), basic salaries (int[] basic), HRA (int[] hra), and IT deductions (int[] it). These arrays enable the program to efficiently map correlated employee data across the same index for retrieval and processing .

You might also like