top of page

Java Coding Practice - Employee Self-Service Application

Updated: Dec 17, 2023


Java Coding Practice

Sharpen your programming skills with Java coding practice. Explore a variety of hands-on exercises and challenges to enhance your proficiency in Java programming. Level up your coding abilities and stay ahead in the world of Java development through practical coding practice sessions. Start coding now for a stronger foundation and mastery of Java programming concepts. Create a Java application Employee Self-Service where employees can view their pay stubs, update personal information, and access tax-related documents.


// Model class: Employee.java

import java.util.List;

class Employee {

private int employeeId;

private String name;

private String address;

private double salary;

private List<PayStub> payStubs;

private List<TaxDocument> taxDocuments;

public int getEmployeeId() {

return employeeId;

}

public void setEmployeeId(int employeeId) {

this.employeeId = employeeId;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getAddress() {

return address;

}

public void setAddress(String address) {

this.address = address;

}

public double getSalary() {

return salary;

}

public void setSalary(double salary) {

this.salary = salary;

}

public List<PayStub> getPayStubs() {

return payStubs;

}

public void setPayStubs(List<PayStub> payStubs) {

this.payStubs = payStubs;

}

public List<TaxDocument> getTaxDocuments() {

return taxDocuments;

}

public void setTaxDocuments(List<TaxDocument> taxDocuments) {

this.taxDocuments = taxDocuments;

}

}


Core Java Programming

Java Back-End Development


PayStub.java class PayStub {

private int payPeriod;

private double grossSalary;

private double deductions;

private double netSalary;

public PayStub(int payPeriod, double grossSalary, double deductions, double netSalary) {

super();

this.payPeriod = payPeriod;

this.grossSalary = grossSalary;

this.deductions = deductions;

this.netSalary = netSalary;

}

public int getPayPeriod() {

return payPeriod;

}

public void setPayPeriod(int payPeriod) {

this.payPeriod = payPeriod;

}

public double getGrossSalary() {

return grossSalary;

}

public void setGrossSalary(double grossSalary) {

this.grossSalary = grossSalary;

}

public double getDeductions() {

return deductions;

}

public void setDeductions(double deductions) {

this.deductions = deductions;

}

public double getNetSalary() {

return netSalary;

}

public void setNetSalary(double netSalary) {

this.netSalary = netSalary;

}

}


TaxDocument.java class TaxDocument {

private int year;

private String documentType;

private String content;

public TaxDocument(int year, String documentType, String content) {

super();

this.year = year;

this.documentType = documentType;

this.content = content;

}

public int getYear() {

return year;

}

public void setYear(int year) {

this.year = year;

}

public String getDocumentType() {

return documentType;

}

public void setDocumentType(String documentType) {

this.documentType = documentType;

}

public String getContent() {

return content;

}

public void setContent(String content) {

this.content = content;

}

} EmployeeService.java class EmployeeService {

public void viewPayStubs(Employee employee) {

for (PayStub payStub : employee.getPayStubs()) {

// Display pay stub information

System.out.println("Pay Period: " + payStub.getPayPeriod());

System.out.println("Gross Salary: $" + payStub.getGrossSalary());

System.out.println("Deductions: $" + payStub.getDeductions());

System.out.println("Net Salary: $" + payStub.getNetSalary());

System.out.println("----------------------");

}

}


public void updatePersonalInformation(Employee employee, String address) {

// Update employee's address

employee.setAddress(address);

System.out.println("Personal information updated successfully.");

}


public void viewTaxDocuments(Employee employee) {

for (TaxDocument taxDocument : employee.getTaxDocuments()) {

// Display tax document information

System.out.println("Year: " + taxDocument.getYear());

System.out.println("Document Type: " + taxDocument.getDocumentType());

System.out.println("Content: " + taxDocument.getContent());

System.out.println("----------------------");

}

}

}


Core Java Programming

Java Back-End Development

// Main program EmployeeSelfServiceApp.java import java.util.ArrayList;

import java.util.List;


public class EmployeeSelfServiceApp {

public static void main(String[] args) {

// Sample data for initializing an Employee

Employee employee = new Employee();

employee.setEmployeeId(1);

employee.setName("John Doe");

employee.setAddress("123 Main St");

employee.setSalary(60000.0);

// Set up some initial pay stubs (assuming you have a constructor in PayStub class)

List<PayStub> payStubs = new ArrayList<>();

payStubs.add(new PayStub(1, 5000.0, 500.0, 4500.0));

payStubs.add(new PayStub(2, 5500.0, 600.0, 4900.0));


employee.setPayStubs(payStubs);


// Set up some initial tax documents (assuming you have a constructor in TaxDocument class)

List<TaxDocument> taxDocuments = new ArrayList<>();

taxDocuments.add(new TaxDocument(2022, "W-2", "Content for W-2"));

taxDocuments.add(new TaxDocument(2021, "W-2", "Content for W-2"));


employee.setTaxDocuments(taxDocuments);

EmployeeService employeeService = new EmployeeService();


// Employee views pay stubs

System.out.println("=== View Pay Stubs ===");

employeeService.viewPayStubs(employee);


// Employee updates personal information

System.out.println("=== Update Personal Information ===");

employeeService.updatePersonalInformation(employee, "New Address");


// Employee views tax documents

System.out.println("=== View Tax Documents ===");

employeeService.viewTaxDocuments(employee);

}

}

8 views0 comments

Comments


bottom of page