views:

141

answers:

5

I am trying to check if a gas pump is free for use && full of gas, and then I am trying to make that pump the pump to be used by the cars in a queue.

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
+6  A: 

Perhaps you want

if(pump1.getState() == 0 && pump1.getFuelAmount() > 0) {

instead of

if(pump1.getState() == 0 && pump1.getFuelAmount() < 0) {

(you've mixed up > and <)

bemace
+13  A: 

You are checking pump1.getFuelAmount()<0

This will check if the pump has a negative amount of fuel. If you want to see if it has a positive amount of fuel, you need to do pump1.getFuelAmount()>0 and pump2.getFuelAmount()>0

Alan Geleynse
oh my goodness thank you!!!!!!!!!!!!
Luron
A: 

change pump1.getFuelAmount()<0 to pump1.getFuelAmount()> 0 same for pump2

frictionlesspulley
Why post your own answer of something that someone already posted?
Joe Philllips
+3  A: 

Rewrite your if() statement like so:

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()));
}

You may also want to include a catchall else at the end so you can handle if neither pump is available or if neither has fuel.

mway
A: 

A couple of comments:

First, pump1 and pump2. Wouldn't an array of pumps reduce the amount of code? (You can just use loops to check all your pumps for anything)

On your if statement: In theory, you cannot have a negative amount of fuel, so all you want to do is see if you have more than 0. (If you have less than 0, you can't give anymore fuel anyways, so it doesn't matter how far below 0 it is)

Karl