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