
Java Programming Free Projects idea’s
Java Programming Free Projects idea’s and check for errors, try to solve them. If encounter any error reach me by clicking here on contacts: Keep practicing Keep learning.
या कोडमध्ये बदल करा आणि त्रुटी तपासा, त्या सोडवण्याचा प्रयत्न करा. काही त्रुटी आढळल्यास संपर्कांवर येथे क्लिक करून माझ्याशी संपर्क साधा: सराव करत रहा शिकत रहा.
Simple Salary Slip generator:
import java.util.Scanner;
class Employee
{
public String name;
public int id;
public int daysPresent;
public double dailyRate;
public double pf;
public double travelAllowance;
public double insurance;
public double incentive;
}
public class Main
{
public static void main(String[] args)
{
// Read the employee data from the standard input
Scanner scanner = new Scanner(System.in);
Employee employee = new Employee();
System.out.print("Enter employee name: ");
employee.name = scanner.nextLine();
System.out.print("Enter employee ID: ");
employee.id = scanner.nextInt();
System.out.print("Enter number of days present in the month: ");
employee.daysPresent = scanner.nextInt();
// Set the employee data
employee.dailyRate = 1000;
employee.pf = 0.1;
employee.travelAllowance = 500;
employee.insurance = 0.2;
employee.incentive = 1000;
// Calculate the gross salary and deductions
double grossSalary = employee.daysPresent * employee.dailyRate;
double pfDeduction = grossSalary * employee.pf;
double insuranceDeduction = grossSalary * employee.insurance;
double totalDeductions = pfDeduction + insuranceDeduction;
// Calculate the net salary
double netSalary = grossSalary + employee.travelAllowance + employee.incentive - totalDeductions;
// Print the salary slip
System.out.println();
System.out.println("Salary Slip");
System.out.println("------------");
System.out.println("Employee Name: " + employee.name);
System.out.println("Employee ID: " + employee.id);
System.out.println("Days Present: " + employee.daysPresent);
System.out.printf("Gross Salary: $%.2f\n", grossSalary);
System.out.printf("PF Deduction: $%.2f\n", pfDeduction);
System.out.printf("Insurance Deduction: $%.2f\n", insuranceDeduction);
System.out.printf("Total Deductions: $%.2f\n", totalDeductions);
System.out.printf("Travel Allowance: $%.2f\n", employee.travelAllowance);
System.out.printf("Incentive: $%.2f\n", employee.incentive);
System.out.printf("Net Salary: $%.2f\n", netSalary);
}
}
Same project with GUI and DataBase:
As you can see these topic are similar, but the one above gives output on the command prompt and the below code give a User Interface. We are using MySQL which is a free software you can download from official site. Remember to use the free community version link in click here MySQL community server installer.
जसे आपण पाहू शकता की हे विषय समान आहेत, परंतु वरील एक कमांड प्रॉम्प्टवर आउटपुट देतो आणि खालील कोड वापरकर्ता इंटरफेस देतो. आम्ही MySQL वापरत आहोत जे एक विनामूल्य सॉफ्टवेअर आहे जे तुम्ही अधिकृत साइटवरून डाउनलोड करू शकता. MySQL समुदाय सर्व्हर इंस्टॉलर येथे क्लिक करा येथे विनामूल्य समुदाय आवृत्ती दुवा वापरण्याचे लक्षात ठेवा.
For connecting the Server to Project follow here
File: Employee.java
public class Employee
{
public int id;
public String name;
public int daysPresent;
public double dailyRate;
public double pf;
public double travelAllowance;
public double insurance;
public double incentive;
}
Servlet File
<html>
<body>
<applet code="SalarySlipApplet.class" width="600" height="400"></applet>
</body>
</html>
File: SalarySlipApplet.java
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class SalarySlipApplet extends JApplet implements ActionListener
{
// GUI components
private JTextField employeeIDField;
private JLabel nameLabel;
private JLabel daysPresentLabel;
private JLabel grossSalaryLabel;
private JLabel pfDeductionLabel;
private JLabel insuranceDeductionLabel;
private JLabel totalDeductionsLabel;
private JLabel travelAllowanceLabel;
private JLabel incentiveLabel;
private JLabel netSalaryLabel;
private JButton showButton;
public void init()
{
// Initialize GUI components
employeeIDField = new JTextField(10);
nameLabel = new JLabel(" ");
daysPresentLabel = new JLabel(" ");
grossSalaryLabel = new JLabel(" ");
pfDeductionLabel = new JLabel(" ");
insuranceDeductionLabel = new JLabel(" ");
totalDeductionsLabel = new JLabel(" ");
travelAllowanceLabel = new JLabel(" ");
incentiveLabel = new JLabel(" ");
netSalaryLabel = new JLabel(" ");
showButton = new JButton("Show Salary Slip");
showButton.addActionListener(this);
// Create the layout
JPanel inputPanel = new JPanel();
inputPanel.add(new JLabel("Employee ID:"));
inputPanel.add(employeeIDField);
inputPanel.add(showButton);
JPanel outputPanel = new JPanel(new GridLayout(9, 2));
outputPanel.add(new JLabel("Name:"));
outputPanel.add(nameLabel);
outputPanel.add(new JLabel("Days Present:"));
outputPanel.add(daysPresentLabel);
outputPanel.add(new JLabel("Gross Salary:"));
outputPanel.add(grossSalaryLabel);
outputPanel.add(new JLabel("PF Deduction:"));
outputPanel.add(pfDeductionLabel);
outputPanel.add(new JLabel("Insurance Deduction:"));
outputPanel.add(insuranceDeductionLabel);
outputPanel.add(new JLabel("Total Deductions:"));
outputPanel.add(totalDeductionsLabel);
outputPanel.add(new JLabel("Travel Allowance:"));
outputPanel.add(travelAllowanceLabel);
outputPanel.add(new JLabel("Incentive:"));
outputPanel.add(incentiveLabel);
outputPanel.add(new JLabel("Net Salary:"));
outputPanel.add(netSalaryLabel);
// Add the panels to the applet
setLayout(new BorderLayout());
add(inputPanel, BorderLayout.NORTH);
add(outputPanel, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent event)
{
try
{
// Get the employee ID from the text field
int employeeID = Integer.parseInt(employeeIDField.getText().trim());
// Get the employee data from the database
Employee employee = DBConnection.getEmployee(employeeID);
// Calculate the salary slip
double grossSalary = employee.daysPresent * employee.salaryPerDay;
double pfDeduction = grossSalary * 0.1;
double insuranceDeduction = grossSalary * 0.05;
double totalDeductions = pfDeduction + insuranceDeduction;
double travelAllowance = employee.daysPresent * 100;
double incentive = 0;
if (employee.daysPresent == 31) incentive = 500;
double netSalary = grossSalary + travelAllowance + incentive - totalDeductions;
// Update the labels
nameLabel.setText(employee.name);
daysPresentLabel.setText(String.valueOf(employee.daysPresent));
grossSalaryLabel.setText(String.format("%.2f", grossSalary));
pfDeductionLabel.setText(String.format("%.2f", pfDeduction));
insuranceDeductionLabel.setText(String.format("%.2f", insuranceDeduction));
totalDeductionsLabel.setText(String.format("%.2f", totalDeductions));
travelAllowanceLabel.setText(String.format("%.2f", travelAllowance));
incentiveLabel.setText(String.format("%.2f", incentive));
netSalaryLabel.setText(String.format("%.2f", netSalary));
}
catch (NumberFormatException exception)
{
JOptionPane.showMessageDialog(this, "Invalid employee ID", "Error", JOptionPane.ERROR_MESSAGE);
}
catch (SQLException exception)
{
JOptionPane.showMessageDialog(this, "Database error", "Error", JOptionPane.ERROR_MESSAGE);
}
}
File: DBConnection.java
Before this you should download JDBC connector for MySQL server click here . After installing these driver make sure you edit the USERNAME and PASSWORD field with the username and password you have set during installation process.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBConnection
{
private static final String URL = "jdbc:mysql://localhost:3306/employees";
private static final String USERNAME = "username";
private static final String PASSWORD = "password";
public static Connection getConnection() throws SQLException
{
return DriverManager.getConnection(URL, USERNAME, PASSWORD);
}
public static Employee getEmployee(int id) throws SQLException
{
String sql = "SELECT * FROM employees WHERE id = ?";
try (Connection connection = getConnection();
PreparedStatement statement = connection.prepareStatement(sql))
{
statement.setInt(1, id);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next())
{
Employee employee = new Employee();
employee.id = resultSet.getInt("id");
employee.name = resultSet.getString("name");
employee.daysPresent = resultSet.getInt("days_present");
employee.dailyRate = resultSet.getDouble("daily_rate");
employee.pf = resultSet.getDouble("pf");
employee.travelAllowance = resultSet.getDouble("travel_allowance");
employee.insurance = resultSet.getDouble("insurance");
employee.incentive = resultSet.getDouble("incentive");
return employee;
}
else
{
return null;
}
}
}
}
Comment down any query, I will try to solve them as soon as possible. Do subscribe on YouTube for the tutorials on Java Programming Free Projects idea’s and from more code go to my GitHub profile.