Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
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
Old 03-13-2011, 05:26 PM   PM User | #2
Aradon
Moderator-san


 
Aradon's Avatar
 
Join Date: Jun 2005
Location: USA
Posts: 734
Thanks: 0
Thanked 20 Times in 19 Posts
Aradon is on a distinguished road
I'm pretty confused by this assignment, but I think I understand the basics.

First off you do not need to generate a second class for a second customer or gas pump. You can reuse the first class over again (with a 2nd variable type).

I'm also not sure why you have doing a random number for simulation of when the pump is open or closed. It seems like what would happen is you would have a random number generator that would tell you when a customer is appearing. You would then do a check if any pumps are open (you can have X number of pumps by using an array or a linked list). For example:

Code:
 

GasPump[] gasPumps = new GasPump[numberOfPumps];
for(int i = 0; i < numberOfPumps; i++)
  gasPumps[i] = new GasPump();
Then you can use as many gas pumps as you like.

I think that fixes the problem with using multiple gas pumps.

The next part depends on the assignment. But it should be as simple checking each placement in the array (this is horrible for performance, but whatever!) if one is empty. And if you reach numberOfPumps then you know it's full.

Hope that makes sense.
__________________
"To iterate is human, to recurse divine." -L. Peter Deutsch
Aradon is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:50 PM.


Advertisement
Log in to turn off these ads.