Java 模拟加油站
因此,我应该创建一个程序,其中我有一个加油站,有5个加油泵,我不知道如何跟踪统计数据,该程序正确地减去了油箱类型中剩余的汽油量,但当下一个客户加油时,统计数据会被删除吗?任何帮助都将不胜感激,这是我的代码。请记住,这是5节课计划的一部分,这只是其中的一部分Java 模拟加油站,java,Java,因此,我应该创建一个程序,其中我有一个加油站,有5个加油泵,我不知道如何跟踪统计数据,该程序正确地减去了油箱类型中剩余的汽油量,但当下一个客户加油时,统计数据会被删除吗?任何帮助都将不胜感激,这是我的代码。请记住,这是5节课计划的一部分,这只是其中的一部分 import java.util.Random; import java.util.Scanner; public class GasPump { static Scanner Input = new Scanner (System.
import java.util.Random;
import java.util.Scanner;
public class GasPump
{
static Scanner Input = new Scanner (System.in);
private static double totalRevenue;
private static double currentPurchase;
//private int currentGasType;
private static double currentGasType;
private static double currentGasAmount;
private Client currentClient;
private int clock;
private static double regularTank;
private static double plusTank;
private static double supremeTank;
//private static double amountLeftInTank;
public void updatePump()
{
if (currentClient != null)
{
clock--;
if (clock == 0)
{
//totalRevenue += currentPurchase;
totalRevenue = totalRevenue + currentPurchase;
resetPump ( );
}
}
}
public void resetPump ( )
{
clock = 0;
currentClient = null;
currentPurchase = 0;
//currentGasType = "";
currentGasType = 0;
currentGasAmount = 0;
}
public boolean pumpOpen()
{
return (clock == 0) && (currentClient == null);
}
public static double getCurrentGasType()
{
regularTank = 1000;
plusTank = 1000;
supremeTank = 1000;
//Scanner Input = new Scanner(System.in);
System.out.println("What type of gas would you like?");
System.out.println("Regular , Plus or Premium");
System.out.println("1)Regular: $2.99 per gallon; 2)Plus: $3.99 per gallon; 3)Premium: $4.99 per gallon");
currentGasType = Input.nextDouble();
Random gen = new Random ();
//currentGasAmount = gen.nextDouble ()* 45;
currentGasAmount = gen.nextDouble()*50;
double roundOff = (double) Math.round(currentGasAmount * 100) / 100;
//System.out.println("How many gallons would you like?");
//currentGasAmount = Input.nextDouble();
if (currentGasType == 1)
{
currentGasType = 2.99;
regularTank = regularTank - currentGasAmount;
}
else if (currentGasType == 2)
{
currentGasType = 3.99;
plusTank = plusTank - currentGasAmount;
}
else
{
currentGasType = 4.99;
supremeTank = supremeTank - currentGasAmount;
}
System.out.println("# of gallons purchased: " + currentGasAmount);
System.out.println("Amount of regular gas left: " + regularTank);
System.out.println("Amount of plus gas left: " + plusTank);
System.out.println("Amount of supreme gas left: " + supremeTank);
return currentGasType;
}
/*public static double getCurrentGasAmount() {
Random gen = new Random ();
currentGasAmount = gen.nextDouble ()* 50;
//System.out.println("How many gallons would you like?");
//currentGasAmount = Input.nextDouble();
System.out.println("# of gallons purchased: " + currentGasAmount);
return currentGasAmount;
}
*/
public static double getCurrentPurchase()
{
currentPurchase = currentGasType * currentGasAmount;
return currentPurchase;
}
public static double getTotalRevenue() {
totalRevenue += currentPurchase;
System.out.println("Total revenue so far is " + totalRevenue);
return totalRevenue;
}
/*public static double getAmountLeftInTank()
{
regularTank = 1000;
plusTank = 1000;
supremeTank = 1000;
if (currentGasAmount == 1)
if (currentGasType == 1)
{
//regularTank = regularTank - currentGasAmount;
}
else if (currentGasType == 2)
else if (currentGasAmount == 2)
{
//plusTank = plusTank - currentGasAmount;
}
else
{
supremeTank = supremeTank - currentGasAmount;
}
System.out.println("Amount of regular gas left: " + regularTank);
System.out.println("Amount of plus gas left: " + plusTank);
System.out.println("Amount of supreme gas left: " + supremeTank);
return amountLeftInTank;
}
*/
public void serveAClient (Client aClient)
{
clock = 10;
currentClient = aClient;
GasPump.getCurrentGasType();
System.out.println("Your total is " + GasPump.getCurrentPurchase());
GasPump.getTotalRevenue();
//GasPump.getAmountLeftInTank();
/*
* design get methods
* ask client what type of gas he wants
* add more code here
*/
// add the total here
}
}
不要对存储在GasPump中的数据使用静态字段 静态字段是单例字段,它们只有一个值在GasPump的所有实例中共享。这意味着,如果您有多个GasPump实例,则调用reset将重置所有气泵 通过从每个字段中删除关键字static,那么每个GasPump都会有一个单独的字段副本。因此,调用reset只会擦除GasPump的一个实例的字段 下图可能有助于您将差异可视化: 在本例中,计数已在CircleWithCount的实例c1和c2之间共享
您可以在此处阅读有关在字段上使用static关键字的更多详细信息:不要对存储在GasPump中的数据使用静态字段 静态字段是单例字段,它们只有一个值在GasPump的所有实例中共享。这意味着,如果您有多个GasPump实例,则调用reset将重置所有气泵 通过从每个字段中删除关键字static,那么每个GasPump都会有一个单独的字段副本。因此,调用reset只会擦除GasPump的一个实例的字段 下图可能有助于您将差异可视化: 在本例中,计数已在CircleWithCount的实例c1和c2之间共享
您可以在此处阅读有关在字段上使用static关键字的更多详细信息:我更改了实例变量中的static关键字,就像您告诉我的那样,但是当我调用serveClient()方法中的方法时,我收到一个错误,说“无法对非静态字段进行静态引用”它给出的唯一解决方案是将其恢复为静态。@Rocketsm46,这是因为您必须在其他方法中级联更改。ServeClient具有类似“GasPump.staticFieldName”的引用,并且由于该字段不再是静态的,编译器可以正确地标记错误。若要修复,请改为引用实例字段/方法。例如,“GasPump.getCurrentGasType()”变为“getCurrentGasType()”。有时人们会使用与“this.getCurrentGasType()”等效的以下命令。我更改了实例变量中的static关键字,就像您告诉我的那样,但是当我调用ServeClient()方法中的方法时,我收到一个错误,说“无法对非静态字段进行静态引用”它给出的唯一解决方案是将其恢复为静态。@Rocketsm46,这是因为您必须在其他方法中级联更改。ServeClient具有类似“GasPump.staticFieldName”的引用,并且由于该字段不再是静态的,编译器可以正确地标记错误。若要修复,请改为引用实例字段/方法。例如,“GasPump.getCurrentGasType()”变为“getCurrentGasType()”。有时,人们会使用与“this.getCurrentGasType()”等效的以下命令。