我正在尝试检查汽油泵是否免费使用和/或是否充满了汽油,然后我试图使该泵在队列中被汽车使用。

Thread carThreads[]=new Thread[TOTAL_CARS];
  try {
    Pump pump1 = new Pump();
    pump1.setName("pump1");
    pump1.setFuelAmount(2000);
    pump1.setState(0);

    Pump pump2 = new Pump();
    pump2.setName("pump2");
    pump2.setFuelAmount(2500);
    pump2.setState(0);

    Pump chosenPump = new Pump();

    if( pump1.getState()==0 && pump1.getFuelAmount()<0 ){
      chosenPump = pump1;
      System.out.println("Pump1 is free and has a fuel amount of: "
       + (pump1.getFuelAmount()) );
    }

    else if ( pump2.getState()==0 && pump2.getFuelAmount()<0 ){
      chosenPump = pump2;
      System.out.println("Pump2 is free and has a fuel amount of: "
       + (pump2.getFuelAmount()) );
    }
    //else{
    //  System.out.println("Must wait for the tanker. It should be here soon");
    //}

    Random r = new Random();

    Car car;

    for(int i = 0; i<TOTAL_CARS; i++){
      car = new Car(i, chosenPump);
      System.out.println("car" + car.getID() + " was created");

      (carThreads[i] = new Thread(car)).start();
      Thread.currentThread().sleep(r.nextInt(10000));

      line.enqueue(car);

      chosenPump.usePump( (Car)line.getfirst(), chosenPump, line );

      System.out.println("this is the new line size for gas: " + line.size());
    }//end for
  }//end try
  catch (Exception e){
  }
}//end of main

最佳答案

您正在检查pump1.getFuelAmount()<0

这将检查泵的燃油量是否为负。如果要查看其燃油量是否为正,则需要执行pump1.getFuelAmount()>0pump2.getFuelAmount()>0

07-28 03:16