0

I am trying to create an array that displays employees and their hire date, data that's already declared. I need to have a set instance variable of today's date as the retiring date and use that to find the amount of years, months and days they have worked. I have the project broken up into two files that work together. I'm just having trouble trying to get the years, months, and days they've worked. My code is for the first file:

import java.time.*;
import java.time.LocalDate.*;
import java.time.Month.*;
import java.time.Period.*;


class Employee
{
//Instance variables
private String name;
private double salary;
private LocalDate hireDay;
private LocalDate retDay = LocalDate.now();
private int years;
private int months;
private int days;


   //Constructor - same name as class -- first thing that runs
    public Employee(String n, double s, int year, int month, int day)//, int ys, int 
    ms, int ds)
   {

    name = n;
    salary = s;
    hireDay = LocalDate.of(year, month, day);
    ///years  = ys;
   // months = ms;
    //days = ds;


}
//methods are public -- any class can have access to the method
//may or may not have a parameter
public String getName()
{
    return name;
}

public double getSalary()
{
    return salary;
}

public LocalDate getHireDay()
{
    return hireDay;
}

public void raiseSalary(double byPercent)
{
    double raise = salary * byPercent/100;
    salary += raise;
}

public int getRetYears()
 {
   return years;
 }
 public int getRetMonths()
 {
  return months;
 }
   public int getRetDays()
 {
 return days;
 }
  public void calcRetDay(Period timeWorked, int y, int m, int d)
 {
  timeWorked = Period.between(hireDay, retDay);
  y = timeWorked.getYears();
  m = timeWorked.getMonths();
d = timeWorked.getDays();

years = y;
months = m;  
days = d;

  }
}

My code for my second file is:

import java.time.*;
import java.time.LocalDate;
import java.time.Month;
import java.time.Period;

//*****Communicates with employee.java****************************
public class EmployeeDriver
    {
    public static void main(String[] args)
    {
     Employee[] staff = new Employee[3];
     //populate the staff array with three employee objects
     staff[0] = new Employee("Carl Cracker", 75000, 1987, 12, 15);
     staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
     staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15);

     //raise everyone's salary by 5%
     for (Employee e : staff)
        //below is dot notation --- encapsulation
        e.raiseSalary(5);
     //print out information about all Employee objects
     for (Employee e : staff)
        System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay=" + e.getHireDay() + ",years=" + e.getRetYears()+ ",months=" + e.getRetMonths()+ ",days=" + e.getRetDays());
}
}
  • 2
    Are you using Java's Datetime class or you are just using 3 integers to represent day, month, year and you just want to deduct from those 3 integers? – user3437460 Feb 18 '18 at 22:02
  • I am using 3 integers to represent day, month and year. – Ryan Asciutto Feb 18 '18 at 22:21
  • Possible duplicate of [Calculating the difference between two Java date instances](https://stackoverflow.com/questions/1555262/calculating-the-difference-between-two-java-date-instances) – Ari Singh Feb 18 '18 at 22:40
  • [`LocalDate`](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html); [`Duration`](https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html) and [Period and Duration](https://docs.oracle.com/javase/tutorial/datetime/iso/period.html) are the basic components you're going to need to solve the problem – MadProgrammer Feb 18 '18 at 22:47

0 Answers0