Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Java code assistance needed

    hello all
    i have written this program about the proceses that take place at a fuel station. My program is good but it never seems to end. I would like to print the total revenue once the day is over but my program never seems to end and I dont know why. ANY HELP WOULD BE GREAT !!!!!!
    Code:
                                                                                                         
    import java.util.Random;
    import java.util.Scanner;
    public class Office {
        public static void main (String [ ] args)
        {
            double FinalOutput = 0, output = 0, output2=0;
            GasPump pump1 = new GasPump ( );
            GasPump pump2 = new GasPump ( );
            Random gen  = new Random ();
            do
            {
                
                int whatHappens = gen.nextInt (100);
               
                if (pump1.pumpAvailable() && (pump1.pumpAvailable())&& (whatHappens == 5))
                {
                    System.out.println ("gas station closed");
                    System.out.println("TOTAL REVENUE: $"+ FinalOutput);
                    break;
                }
    
                else if ((whatHappens % 10) == 0)
                {
                    if (pump1.pumpAvailable ())
                    {
                        System.out.println("Pump 1 is open");
                        pump1.customerArrival ();}
                    else if(!pump1.pumpAvailable())
                    {
                        System.out.println("Pump 1 occupied please go to Pump 2");
                        pump2.customerArrival();
                    } 
                    else
                        System.out.println("Sorry both pump is occupied");
    
                }
                else
                {
                    if ((pump1.pumpAvailable ())&& (pump1.pumpAvailable()) )
                        System.out.println ("no customer; waiting.........");
                }
    
                if (!pump1.pumpAvailable())
                {
              
                    if (pump1.updateClock () == 0)
                    {
                    output = output + pump1.saleComplete();}
                }
    
                if(!pump2.pumpAvailable())
                {
                    
                    if(pump1.updateClock() == 0)
                    {
    
                     output2= output2 + pump2.saleComplete();}
                }
                FinalOutput = output + output2;
    
            } 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 output2;
    	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()
           {
        	   output2 = output2 + saleComplete2();
        	   
        	   return output2;
        	   
           }
    }

  • #2
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    okay the main problem with your code is that, u just messed up the pump numbering in your office class. so i just rewrote that for you. in most cases u just used pump1 and forgot to use pump2's clock counter.
    the other problem u have is that u are generating a number between 1 to 100 and the only chance of it being 5 is very rare, and not only that both pumps have to be available as well. this is very rare and very hard to come by so i changed ur random generator to produce numbers between 1 to 10 and customer can go through if whatHappens is even. like this u have better chance of getting five and ending ur program.
    Also i changed ur resetPump() in GasPump class and added some more stuff to it. u also should just call it in ur constructor as well.
    and i removed all of the stuff with the "2" and the end. because they are all unnecessary.

    so go through all the codes that i added it should help u with all the problems.

    Code:
    import java.util.Random;
    
    public class Office {
    	public static void main(String[] args) {
    		double FinalOutput = 0;
    		GasPump pump1 = new GasPump();
    		GasPump pump2 = new GasPump();
    		Random rand = new Random(System.currentTimeMillis());
    		do {
    			int whatHappens = rand.nextInt(9)+1;
    			if (pump1.pumpAvailable() && pump2.pumpAvailable() && whatHappens == 5) {
    				System.out.println("gas station closed");
    				System.out.println("TOTAL REVENUE: $" + FinalOutput);
    				break;
    			} else if ((whatHappens % 2) == 0) {
    				if (pump1.pumpAvailable()) {
    					System.out.println("Pump 1 is open");
    					pump1.customerArrival();
    				} else if (pump2.pumpAvailable()) {
    					System.out.println("Pump 1 occupied please go to Pump 2");
    					pump2.customerArrival();
    				} else
    					System.out.println("Sorry both pump is occupied");
    			} else {
    				if ((pump1.pumpAvailable()) && (pump2.pumpAvailable()))
    					System.out.println("no customer; waiting.........");
    			}
    			if (!pump1.pumpAvailable()) {
    				if (pump1.updateClock() == 0) {
    					FinalOutput += pump1.saleComplete();
    				}
    			}
    			if (!pump2.pumpAvailable()) {
    				if (pump2.updateClock() == 0) {
    					FinalOutput += pump2.saleComplete();
    				}
    			}
    		} while (true);
    	}
    }
    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(System.currentTimeMillis());
    		carTag = "";
    		for (int i = 0; i < 6; i++) {
    			// a random number between 48 and 90
    			int code = gen.nextInt(43) + 48;
    			if ((58 <= code) && (code <= 64)) {
    				i--;
    			}else{
    				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 double getGasPurchased() {
    		Random gen = new Random(System.currentTimeMillis());
    		gasPurchased = gen.nextDouble() * 50;
    		return gasPurchased;
    	}
    
    	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 double gasPurchased;
    	private double output;
    	private int timeLeftToFinish;
    	private int gasType;
    	private String carTag;
    	private Customer who;
    
    	public GasPump() {
    		resetPump();
    	}
    
    	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
    				+ " units of time 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;
    		carTag = null;
    		output = 0;
    		gasType = 0;
    		totalPayment = 0;
    		gasPurchased = 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;
    		}
    		printReceipt();
    		resetPump();
    		return totalPayment;
    	}
    
    	public boolean pumpAvailable() {
    		return (who == null);
    	}
    
    	public double output() {
    		output = output + saleComplete();
    		return output;
    	}
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •