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-11-2011, 09:46 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 help

Im writing this code for a gas station program. I have been writing and improving my code for about 3 weeks now and its quite long. I have about classes , customer class, office class and a gas pumps classes. I need help on how to get started on finding the total output at the end of the day. Conceptually i know what i have to do but I don't know where to begin writing the code. I think each time there is a new customer i should add the amount of fuel purchased to a new total fuel purchases variable in the gas pump class. If any one could give me some advice or tips on my theory it would be great ! Should I create a method or can i be done without one.
Code:
import java.util.Random;
import java.util.Scanner;
public class Customer
{
	private String carTag;
	private double gasPurchased;
	private int gasType;
	
	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);
		//1:regular, 2:plus, 3:premium
		System.out.println ("enter type of gas to buy " + 
				"(1:regular, 2:plus, 3:premium): ");
		gasType = inputDevice.nextInt ();
		return gasType;
		
	}
	
	public double getGasPurchased ()
	{
		Random gen = new Random ();
		gasPurchased = gen.nextDouble ()* 50;
		return gasPurchased;
	}
	
	public String getCarTag ()
	{
		return carTag;
	}
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 ();
        //asuumint gas tanks always have gas, will upgrade later
        do
        {
            int whatHappens = gen.nextInt (100);
            if (whatHappens == 5)
            {
                System.out.println ("gas station closed");
                break;
            }
            else if ((whatHappens % 10) == 0)
            {
                if (pump1.pumpAvailable ())
                    pump1.customerArrival ();
                else
                    System.out.println ("a new customer arrives at pump 1, 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 ();
            }
            
        } while (true);
        
        do
system.exit ()
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;
	
	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");

            System.out.println("Pump has " + regularTank()); 

        }

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

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

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

                }

				printReceipt ( );
					resetPump ( );

       			}

       		public boolean pumpAvailable ()
       		{
       			return (who == null);
       		}

			}
Gomez is offline   Reply With Quote
Old 03-11-2011, 10:18 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Is this somehow different then your previous requests:
http://www.codingforums.com/showthread.php?t=221015
and
http://www.codingforums.com/showthread.php?t=220895
and
http://www.codingforums.com/showthread.php?t=220784
?
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu 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 12:42 PM.


Advertisement
Log in to turn off these ads.