Demo - Employee - Nested if Statements

 

package NestedIfDemo; // Author: Kevin Roark // Employee class Demo //Libraries for date functionality import java.time.LocalDate; public class Employee { private LocalDate hireDate; private String firstName; private String lastName; private double salary; public Employee(LocalDate hireDate, String firstName, String lastName, double salary) { this.hireDate = hireDate; this.firstName = firstName; this.lastName = lastName; this.salary = salary; } //Getters and Setters************* public LocalDate getHireDate() { return hireDate; } public void setHireDate(LocalDate hireDate) { this.hireDate = hireDate; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } }

This is a Java class for an Employee object. The class is within the package NestedIfDemo. Let's break down its components:

  1. Private instance variables: The Employee class has four private instance variables hireDate, firstName, lastName, and salary.

    • hireDate is an instance of the LocalDate class, which is part of Java's built-in time and date handling system. This represent the date when the employee was hired.

    • firstName and lastName are String instances, representing the employee's first and last names.

    • salary is a double, representing the employee's salary.

  2. Constructor: The constructor of this class (public Employee(LocalDate hireDate, String firstName, String lastName, double salary)) accepts four parameters and assigns them to the respective instance variables. This constructor is used to create a new instance of the Employee class, setting the values of the instance variables to the arguments provided when the constructor is called.

  3. Getters and Setters: This class has 'get' and 'set' methods for each of the instance variables.

    • Getter methods (getHireDate(), getFirstName(), getLastName(), getSalary()) return the current value of the respective instance variables. They take no parameters and return the respective data types of the instance variables.

    • Setter methods (setHireDate(LocalDate hireDate), setFirstName(String firstName), setLastName(String lastName), setSalary(double salary)) are used to set or change the values of the respective instance variables. Each of these methods accepts a single parameter of the respective instance variable type and returns no value.

These methods provide controlled access to the instance variables. Since the variables are declared as private, they cannot be accessed directly from outside the class. This concept is known as encapsulation, one of the four fundamental principles of Object Oriented Programming (OOP), and it is used to maintain the integrity of the data within the class.

package NestedIfDemo; // Author Kevin Roark // Demo of nested if statements import java.time.LocalDate; import java.time.Period; public class EmployeeDriver { public static void main(String[] args) { // variable needed to determine todays date LocalDate currentDate = LocalDate.now(); //create an instance of an Employee Employee myEmployee = new Employee(LocalDate.of(2019, 8, 30), "Kevin", "Roark", 60000); //calculate period of work Period period = Period.between(myEmployee.getHireDate(), currentDate); if(myEmployee.getSalary() > 50000) { if(period.getYears() >= 2) { System.out.println("You qualify for the loan."); } else { System.out.println("You must have been on your current job for at least two years to qualify"); } } else { System.out.println("You must earn at least $50,000 per year to qualify."); } } }

This Java code demonstrates the use of an Employee object in a program. The program also demonstrates a concept known as nested if-else statements. Here's a breakdown of what the code does:

  1. Current date: The LocalDate class's now() method is used to get the current date and it is stored in the variable currentDate.

  2. Create Employee object: An Employee object myEmployee is created using the Employee class's constructor. The constructor takes four parameters: hireDate, firstName, lastName, and salary. In this case, the hireDate is set to August 30, 2019, the firstName is set to "Kevin", the lastName is set to "Roark", and the salary is set to 60,000.

  3. Calculate period of work: The Period.between() method is used to calculate the duration between the employee's hire date and the current date. This duration is stored in the Period object period.

  4. Nested if-else statements: The nested if-else statements evaluate the following:

    • The outer if-else checks whether the Employee's salary is greater than 50,000. If not, it prints "You must earn at least $50,000 per year to qualify."

    • If the Employee's salary is greater than 50,000, the program enters the nested if-else block. This block checks whether the number of years in period is greater than or equal to 2 (i.e., if the employee has been working for at least 2 years).

      • If the condition is met, it prints "You qualify for the loan."

      • If the condition is not met (i.e., the employee has been working for less than 2 years), it prints "You must have been on your current job for at least two years to qualify."

Overall, this program seems to be evaluating whether an employee qualifies for a loan based on their salary and duration of employment.

 

COSC-1437 / ITSE-2457 Computer Science Dept. - Author: Dr. Kevin Roark