Write a Java program to read all the employee names and their basic salaries from a text file “employee.txt” and calculate and display the net salary of each employee as follows:
DA = 20% of basic
HRA = 8% of basic
Medical = ₹1000
IT = 8% of basic
Gross Salary = Basic + DA + HRA + Medical
Net Salary = Gross Salary – IT
Also create the main() method to create an object of the class and call the methods accordingly.
import java.io.*;
class Employee{
String name;
double basic;
double da;
double hra;
double medical;
double it;
double gross;
double net;
public Employee(){
medical = 1000;
}
public void calculate()throws IOException{
try{
FileReader fr = new FileReader("employee.txt");
BufferedReader br = new BufferedReader(fr);
while(true){
name = br.readLine();
if(name == null)
break;
basic = Double.parseDouble(br.readLine());
da = 0.2 * basic;
hra = 0.08 * basic;
medical = 1000;
it = 0.08 * basic;
gross = basic + da + hra + medical;
net = gross - it;
System.out.println(name + "\'s net salary: " + net);
}
br.close();
fr.close();
}catch(FileNotFoundException e){
System.out.println("FILE NOT FOUND!");
}
}
public static void main(String[] args)throws IOException{
Employee obj = new Employee();
obj.calculate();
}
}