Skip to content
Advertisement

how to compare 2 set values in the same arraylist in java

I have an arraylist of employees. Within this the id and name are non changeable, while the hours contracted and planned are changeable.

I have managed the first 3 questions stated below, but stuck on the last one. Besides the 3 Classe below, i also have the Class Workspace.

I have the Class Employee.

public class Employee {
   
    public int id;
    public String name;
    public int HoursWork;
    public int HoursPlanned;   
}

This exends to Class Specialist

public class Specialist extends Employee { 
    
    public Specialist(int id, String name){
        this.id=id;
        this.naam=naam;
        this.UrenWerk=0;
        this.UrenPlan=0;        
    }
    
    public void InPlanning(int Planned){
        HoursPlanned+=Planned;
        Workspace.setTotalHoursPlanned(HoursPlanned);
    }
    
        public void Contract(int Available){
        HoursWork+=Available;
        Workspace.setTotalHoursAvailable(HoursWork);        
    }            
}

And in the main

public class Main {
     
    public static void main(String[] args) {
        
     Specialist H = new Specialist(1,"H");
     Specialist R = new Specialist(2,"R");
     Specialist P = new Specialist(3,"P");
     Specialist K = new Specialist(4,"K");
      
     List<Specialist> LijstSpecialist = new ArrayList <>();
     LijstSpecialist.add(H);
     LijstSpecialist.add(R);
     LijstSpecialist.add(P);
     LijstSpecialist.add(K);
     
     Workspace A = new Workspace (LijstSpecialist);

     H.InPlanning(1700);
     R.InPlanning(1400);
     P.InPlanning(1880);
     K.InPlanning(300);

     H.Contract(1880);
     R.Contract(1800);
     P.Contract(1890);
     K.Contract(1700); 

 System.out.println("available " + A.TotalHoursAvailable()+ " hours.");
             
 System.out.println("Planned " + A.getTotalHoursPlanned()+ " hours.");
                             
 System.out.println("To be planned " + (A.TotalHoursAvailable() - A.getTotalHoursPlanned())+ " hours.");
     
 System.out.println("Employees to be planned ");

In the last question i want the names of the employees to be printed that have less planned than the contract states.

Advertisement

Answer

The following should work as you need it:

public class Main {
     
    public static void main(String[] args) {
        Specialist H = new Specialist(1,"H");
        Specialist R = new Specialist(2,"R");
        Specialist P = new Specialist(3,"P");
        Specialist K = new Specialist(4,"K");

        List<Specialist> LijstSpecialist = new ArrayList <>();
        LijstSpecialist.add(H);
        LijstSpecialist.add(R);
        LijstSpecialist.add(P);
        LijstSpecialist.add(K);

        Workspace A = new Workspace (LijstSpecialist);

        H.InPlanning(1700);
        R.InPlanning(1400);
        P.InPlanning(1880);
        K.InPlanning(300);

        H.Contract(1880);
        R.Contract(1800);
        P.Contract(1890);
        K.Contract(1700); 

        List<String> specialistsToBePlanned = LijstSpecialist.stream().filter((specialist -> specialist.HoursPlanned < specialist.HoursWork))
            .map(Specialist::getName)
            .collect(Collectors.toList());

        System.out.println("available " + A.TotalHoursAvailable()+ " hours.");
               
        System.out.println("Planned " + A.getTotalHoursPlanned()+ " hours.");
                               
        System.out.println("To be planned " + (A.TotalHoursAvailable() - A.getTotalHoursPlanned())+ " hours.");

        System.out.println("Employees to be planned " + specialistsToBePlanned);
    }
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement