View Single Post
Old 03-12-2011, 07:48 PM   PM User | #1
Gomez
New to the CF scene

 
Join Date: Mar 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Gomez is an unknown quantity at this point
Java Code Producing Error

Hello
I finished writing my code for a gas station project. Where we have to simulate the operations that happen in a gas station. My code is sort of solid and I have completed it. I have two loops in my Office class. How can I send a cutomer to pump 2 if pump 1 is busy. Do i need to make 1 loop instead of two ? .........any advice or tips would be great

Code:
import java.util.Random;
public class Office
{
    public static void main (String [ ] args)
    {
        double regularTank = 1000;
        double plusTank = 1000;
        double primiumTank = 1000;
        GasPump pump1 = new GasPump ( );
        GasPump2 pump2 = new GasPump2 ( );
        Random gen  = new Random ();
        double output1 = 0, output2 = 0, gastotal = 0, gastotal2 = 0;
        
        //asuumint gas tanks always have gas, will upgrade later
        do
        {
            int whatHappens = gen.nextInt (100);
            if (whatHappens == 5)
            {
                System.out.println ("gas station closed");
                System.out.println ("The Total Revenue from Pump1 :" + output1);
                
                break;
            }
            else if ((whatHappens % 10) == 0)
            {
                if (pump1.pumpAvailable ())
                {
                	System.out.println( "Pump 1 is open");
                    pump1.customerArrival ();}
                else
                    System.out.println ("customer arrives, but is turned away, gas pump is busy");
            }
            else
            {
                if (pump1.pumpAvailable ())
                    System.out.println ("no customer; waiting.........");
            }

            if (!pump1.pumpAvailable())
            {
                if (pump1.updateClock () == 0)
                	   pump1.saleComplete ();
            }
            output1 = output1 + pump1.saleComplete ();
           	
            	
        } while (true);
        
        do
        {
        	
        	 int whatHappens = gen.nextInt (100);
             if (whatHappens == 5)
             {
                 System.out.println ("gas station closed");
                 System.out.println ("The total revenue from Pump 2 is" + output2);
                 break;
             }
             else if ((whatHappens % 10) == 0)
             {
                 if (pump2.pumpAvailable2 ())
                     pump2.customerArrival2 ();
                 else
                     System.out.println ("a new customer arrives, but is turned away, gas pump is busy");
             }
             else
             {
                 if (pump2.pumpAvailable2 ())
                     System.out.println ("no customer; waiting.........");
             }

             if (!pump2.pumpAvailable2())
             {
                 if (pump2.updateClock2 () == 0)
                     pump2.saleComplete2 ();
             }
             
             	output2 = output2 + pump2.saleComplete2 ();
             	
             	
         } while (true);
       
        System.exit (0);
        
    }
   
}
Code:
     
import java.util.Random;
import java.util.Scanner;
public class Customer
{
	private String carTag;
	private double gasPurchased, gasPurchased2;
	private int gasType, gasType2;

	public Customer ()
	{
		Random gen = new Random ( );
		carTag = "";
		for (int i = 1; i <= 6; i++)
		{

			//a random number between 48 and 90
			int code = gen.nextInt (43)+ 48;
			if ((code <= 64) && (code >= 58))
			{
				i--;
				continue;
			}

			carTag += ((char)(code));

		}

		System.out.println ("a car arrives; tag number:" + carTag);

	}

	public int getGasType ()
	{
		Scanner inputDevice = new Scanner (System.in);
		
		System.out.println ("enter type of gas to buy " +
				"(1:regular, 2:plus, 3:premium): ");
		gasType = inputDevice.nextInt ();
		return gasType;

	}
        	public int getGasType2 ()
                {
		Scanner inputDevice = new Scanner (System.in);

		System.out.println ("enter type of gas to buy " +
				"(1:regular, 2:plus, 3:premium): ");
		gasType2 = inputDevice.nextInt ();
		return gasType2;

	}


	public double getGasPurchased ()
	{
		Random gen = new Random ();
		gasPurchased = gen.nextDouble ()* 50;
		return gasPurchased;
	}

        public double getGasPurchased2 ()
	{
		Random gen = new Random ();
		gasPurchased2 = gen.nextDouble ()* 50;
		return gasPurchased2;
	}

	public String getCarTag ()
	{
		return carTag;
	}
}
Code:
public class GasPump
{
	final private double REGULAR_PRICE = 3.15;
	final private double PLUS_PRICE = 3.5;
	final private double PREMIUM_PRICE = 4.5;
        final private double HOSE_FLOW_RATE = 0.85;
	
	private double totalPayment;
	private Customer who;
        private int timeLeftToFinish;
        private int gasType;
        private double gasPurchased;
        private String carTag;
        private double output;
        
	public GasPump ()
	{
		who = null;
		totalPayment = 0;
                timeLeftToFinish = 0;
	}
	
	public void customerArrival ()
	{
		who = new Customer ();
		System.out.println ("welcome to jack's gas station PUMP 1 ");
		
		gasType = who.getGasType ();
		gasPurchased = who.getGasPurchased ();

                timeLeftToFinish =  (int)(gasPurchased/HOSE_FLOW_RATE);

                System.out.println (timeLeftToFinish + " total time units needed for this transaction...");

		carTag = who.getCarTag ();
		
	}

       private void printReceipt ( )
        {
            System.out.println ("****Sale Receipt");
            switch (gasType)
            {
                case 1: System.out.println ("regular gas"); break;
                case 2: System.out.println ("plus gas"); break;
                case 3: System.out.println ("premium gas"); break;
            }

            System.out.println ("gas amount: " + gasPurchased +
            "\ntotal payment" + totalPayment + "\nthank you");

          
        }

       private void resetPump ( )
       {
            who = null;
            totalPayment = 0;
            timeLeftToFinish = 0;
       }

       public int updateClock ()
       {
           timeLeftToFinish--;
           System.out.println (timeLeftToFinish + " time units left to finish pump ");
           return timeLeftToFinish;
       }

       public double saleComplete ( )
       {
		switch (gasType)
                {
                    case 1: totalPayment = REGULAR_PRICE * gasPurchased; break;
                    case 2: totalPayment = PLUS_PRICE * gasPurchased; break;
                    case 3: totalPayment  = PREMIUM_PRICE * gasPurchased; break;

                }
				double total = totalPayment;
				printReceipt ( );
				
				resetPump ( );
				return total;
       			}

       public boolean pumpAvailable ()
       {
            return (who == null);
       }
	
       public double output1()
       {
    	   output = output + saleComplete();
    	   return output;
}
}
Code:
                                                                          
public class GasPump2
{
	final private double REGULAR_PRICE = 3.15;
	final private double PLUS_PRICE = 3.5;
	final private double PREMIUM_PRICE = 4.5;
        final private double HOSE_FLOW_RATE = 0.85;
	
	private double totalPayment2;
	private double output;
	private Customer who2;
        private int timeLeftToFinish2;
        private int gasType2;
        private double gasPurchased2;
        private String carTag;
        
	public GasPump2 ()
	{
		who2 = null;
		totalPayment2 = 0;
                timeLeftToFinish2 = 0;
	}
	
	public void customerArrival2 ()
	{
		who2 = new Customer ();
		System.out.println ("welcome to jack's gas station PUMP 2 ");
		
		gasType2 = who2.getGasType2 ();
		gasPurchased2 = who2.getGasPurchased2 ();

                timeLeftToFinish2 =  (int)(gasPurchased2/HOSE_FLOW_RATE);

                System.out.println (timeLeftToFinish2 + " total time units needed for this transaction...");

		carTag = who2.getCarTag ();
		
	}

       private void printReceipt2 ( )
        {
            System.out.println ("****Sale Receipt");
            switch (gasType2)
            {
                case 1: System.out.println ("regular gas"); break;
                case 2: System.out.println ("plus gas"); break;
                case 3: System.out.println ("premium gas"); break;
            }

            System.out.println ("gas amount: " + gasPurchased2 +
            "\ntotal payment" + totalPayment2 + "\nthank you");

          
        }

       private void resetPump2 ( )
       {
            who2 = null;
            totalPayment2 = 0;
            timeLeftToFinish2 = 0;
       }

       public int updateClock2 ()
       {
           timeLeftToFinish2--;
           System.out.println (timeLeftToFinish2 + " time units left to finish pump ");
           return timeLeftToFinish2;
       }

       public double saleComplete2 ( )
       {
		switch (gasType2)
                {
                    case 1: totalPayment2 = REGULAR_PRICE * gasPurchased2; break;
                    case 2: totalPayment2 = PLUS_PRICE * gasPurchased2; break;
                    case 3: totalPayment2  = PREMIUM_PRICE * gasPurchased2; break;

                }
				double total2 = totalPayment2;
				printReceipt2 ( );
				
				resetPump2 ( );
				return total2;
       			}

       public boolean pumpAvailable2 ()
       {
            return (who2 == null);
       }
       
       public double output2()
       {
    	   output = output + saleComplete2();
    	   return output;
    	   
       }
}

Last edited by Gomez; 03-12-2011 at 10:39 PM..
Gomez is offline   Reply With Quote