Advertisement
LeonidR

Payroll ex2, 3.6.2013po

Jan 6th, 2014
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.05 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Payroll{
  4.     public List<Employee> employess = new ArrayList<Employee>();
  5.  
  6.     public Iterator<Employee> aboveAverageIterator(){
  7.         double sum = 0;
  8.  
  9.         for(Employee a: employess){
  10.             sum += a.getSalary();
  11.         }
  12.  
  13.         return new AboveAverageIterator(sum/(employess.size()));
  14.     }
  15.  
  16.    
  17.  
  18.     class AboveAverageIterator implements Iterator<Employee>{
  19.         private double average;
  20.         private Employee cur = null;
  21.         private final Iterator<Employee> it = employess.iterator();
  22.  
  23.         public AboveAverageIterator(double average){
  24.             this.average = average;
  25.             this.reach_next();
  26.         }
  27.  
  28.         private void reach_next(){
  29.             boolean flag = false;
  30.             Employee t = null;
  31.             while(!flag && it.hasNext()){
  32.                 t = it.next();
  33.                 if(t.getSalary() > average)
  34.                     flag = true;
  35.             }
  36.  
  37.             cur = flag ? t : null;
  38.         }
  39.  
  40.         public boolean hasNext(){
  41.             return cur != null;
  42.         }
  43.  
  44.         public Employee next(){
  45.             Employee temp = cur;
  46.             this.reach_next();
  47.             return temp;
  48.         }
  49.  
  50.         public void remove(){
  51.             throw new UnsupportedOperationException();
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement