<repo_name>hitjedi/Oil<file_sep>/src/name/babkov/oilproject/Pipe.java
package name.babkov.oilproject;

import java.util.ArrayList;
import java.util.List;
import name.babkov.oilproject.event.PipeEvent;
import name.babkov.oilproject.listener.PipeListener;

public class Pipe {

    private final double thresh;
    
    private double volume;
    
    private List<PipeListener> listOfPipeListener;

    public Pipe(double thresh) {
        
        this.volume = 0;
        
        this.thresh = thresh;
        
        this.listOfPipeListener = new ArrayList<PipeListener>();
        
    }       
    
    public double getVolume() {
        return volume;
    }
    
    
    
    public synchronized void addPipeListener(PipeListener listener){
        
        listOfPipeListener.add(listener);
        
    }
    
    public synchronized void removePipeListener(PipeListener listener){
        
        listOfPipeListener.remove(listener);
        
    }
        
    
    
    public synchronized void addOilToPipe(double volume){

        if(this.volume != thresh){
            
            if(this.volume+volume>thresh){
            
                this.volume = thresh;                           
                
                firePipeEvent();
            
            }    
            else{
            
                this.volume+= volume;
                
                System.out.println("Volume in pipe = " + String.format("%6.2f",this.volume));
            }
            
        }                                        
    }
    
    private void firePipeEvent(){
        
        PipeEvent event = new PipeEvent(this);
        
        for(PipeListener listener:listOfPipeListener){
            
            listener.pipeFulled(event);
            
        }
        
    }
}
<file_sep>/src/name/babkov/oilproject/listener/InstallationEngineListener.java
package name.babkov.oilproject.listener;

import java.util.EventObject;

public interface InstallationEngineListener {

    void failureOccured(EventObject event);
    
}
<file_sep>/src/name/babkov/oilproject/InstallationEngine.java
package name.babkov.oilproject;

import name.babkov.oilproject.event.InstallationEngineEvent;
import name.babkov.oilproject.listener.InstallationEngineListener;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;


public class InstallationEngine extends Thread{

    private final int interval = 500;
    
    private Installation installation;
    
    private Pipe pipe; 
    
    private List<InstallationEngineListener> listOfInstallationEngineListener;
    
    private Date failureDate;
    
    public InstallationEngine(Installation installation, Pipe pipe) {
        
        this.installation = installation;
        
        this.pipe = pipe;
        
        this.listOfInstallationEngineListener = new ArrayList<InstallationEngineListener>();
    }

    public Installation getInstallation() {
        return installation;
    }
    
    
    
    public synchronized void addInstallationEngineListener(InstallationEngineListener listener){
        
        listOfInstallationEngineListener.add(listener);
        
    }

    
    public synchronized void removeInstallationEngineListener(InstallationEngineListener listener){
        
        listOfInstallationEngineListener.remove(listener);
        
    }
    
        
    @Override
    public void run() {
        
        boolean isWorked = true;
        
        Random random  = new Random();
        
        while(isWorked && !Thread.interrupted()){
    
            double volume = installation.produceVolume();
            
            System.out.println(
                    installation.getName() + 
                    " produced " + 
                    String.format("%6.2f",installation.getFullVolume()) 
                    + " of oil");
            
            pipe.addOilToPipe(volume);
                                                                             
            try {
                
                Thread.sleep(interval);
            
            } catch (InterruptedException ex) {
            
                break;
            }
            
            if(random.nextInt(20)==10){
                
                fireFailure();
                
                isWorked = false;
                
            }
            
        }
        
    }
    
    public boolean isFailure()
    {
        
        return failureDate != null;
        
    }

    public Date getFailureDate() {
        return failureDate;
    }
    
    
    
    private void fireFailure(){
        
        failureDate = new Date();
        
        InstallationEngineEvent event = new InstallationEngineEvent(this);
        
        for(InstallationEngineListener listener: listOfInstallationEngineListener){
                            
            listener.failureOccured(event);
            
        }
        
    }
        
    
}
<file_sep>/src/name/babkov/oilproject/Installation.java
package name.babkov.oilproject;

public class Installation {

    private double volume;
    
    private double distance;
    
    private String name;

    private double fullVolume;
    
    public Installation(double volume, double distance, String name) {
        this.volume = volume;
        this.distance = distance;
        this.name = name;
        fullVolume = 0;
    }

    public double getDistance() {
        return distance;
    }

    public String getName() {
        return name;
    }

    public double getFullVolume() {
        return fullVolume;
    }

    
    
    public double produceVolume() {
        
        fullVolume+=volume;
                        
        return volume;
    }        
           
}
<file_sep>/src/name/babkov/oilproject/OilProject.java
package name.babkov.oilproject;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class OilProject {

    private final int numOfInstallation = 5;
    
    private final int numOfWorkInstallation = 3;
    
    private final int volumeInterval = 50;
    
    private final int distanceInterval = 10;
    
    private List<Installation> listOfInstallation; 
            
    private List<InstallationEngine> listOfInstallationEngine;
       
    private Dispatcher dispatcher;   
    
    private Pipe pipe;
    
    
    public static void main(String[] args) {
        
        
       new OilProject().Start(); 
                     
    }
        
    public void Start(){
             
       Random random = new Random();              
               
       listOfInstallation = new ArrayList<Installation>(numOfInstallation);
       
       listOfInstallationEngine = new ArrayList<InstallationEngine>(numOfWorkInstallation);
       
              
       for(int i = 0;i<numOfInstallation;i++){
       
           listOfInstallation.add(new Installation(random.nextDouble()*volumeInterval, 
                                                       random.nextDouble()*distanceInterval,
                                                       "Installation #"+Integer.toString(i)));
           
       }
       
       pipe = new Pipe(500);
       
       dispatcher = new Dispatcher(listOfInstallation, numOfWorkInstallation, pipe);
       
       dispatcher.start();
       
       
        
    }
}
