class Pump_Tank_simulation
{

public static void main(String args[])
{
  Tank h=new Tank();
	Pump1 p1 = new Pump1(h);
	Pump2 p2 = new Pump2(h);
      p1.start();
      p2.start();
    }
}

class Pump1 extends Thread
{
    private Tank pTut;
    private double flow_rate;

    public Pump1(Tank h)
    {
        pTut=h;
    }

    public void run()
    {
        while(true)
        {
		while((flow_rate = Math.abs((Math.random()*5))) == 0);
            pTut.addWater(flow_rate);
           try{
                 sleep( (int) (Math.random()*3000 ) );
             }
             catch(InterruptedException e)
            {
               System.err.println("Exeption "+e.toString());
           }
        }
    }
}

class Pump2 extends Thread
{
    private Tank cTut;
    private double flow_rate;

    public Pump2(Tank h)
    {
        cTut=h;
    }

    public void run()
    {
        while (true)
        {
            try{
                sleep( (int) (Math.random()*3000));
               }
            catch(InterruptedException e)
            {
                System.err.println("Exeption "+e.toString());
            }
            while((flow_rate = Math.abs((Math.random()*5))) == 0);
            cTut.cikarSu(flow_rate);
       }
    }
}

class Tank
{
    private double TankWater = 0.0;
    private boolean Pump1 = true;
    private boolean Pump2 = false;
    private final double kapasite = 10.0;

    public synchronized void addWater(double amount_of_water)
    {
        while (!Pump1)
        {
          try{
            wait();
             }
          catch (InterruptedException e)
          {
            System.err.println("Exception:"+e.toString() );
          }
        }

        TankWater += amount_of_water;
	  if (TankWater >= kapasite){
	        Pump1 = false;
		  System.out.println("Flow rate of pump 1 " + amount_of_water + " Storage tank is Full(10).");
		  TankWater = kapasite;
	  }
	  else
		  System.out.println("Flow rate of pump 1 " + amount_of_water + "water content of Storage tank is " + TankWater);

	  Pump2 = true;
        notify();
    }

    public synchronized void cikarSu(double amount_of_water)
    {
         while (!Pump2)
        {
          try{
            wait();
             }
          catch (InterruptedException e)
          {
            System.err.println("Exception:"+e.toString() );
          }
        }

    TankWater -= amount_of_water;
	  if (TankWater <= 0)
    {
      Pump2=false;
		  System.out.println("Flow rate of pump 2 " + amount_of_water + " Storage tank is empty(0)");
		  TankWater = 0;
	  }
	  else
		  System.out.println("Flow rate of pump 2 " + amount_of_water + "water content of Storage tank is " + TankWater);
	  Pump1 = true;
    notify();
    }
}
