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