模拟加油站

时间:2014-10-16 14:54:49

标签: java

所以我应该创建一个程序,我有一个带有5个气泵的加油站,而且我不知道如何跟踪统计数据,程序正确地减去剩余的气体量从燃气罐的类型,但当下一个客户抽气时,统计数据会被删除?任何帮助将不胜感激,这是我的代码。请记住,这是5级课程的一部分,这只是其中的一部分。

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

1 个答案:

答案 0 :(得分:7)

不要将静态字段用于存储在GasPump中的数据。

静态字段是单例,它们只在GasPump的所有实例中共享一个值。这意味着如果您有多个GasPump实例,则调用reset将重置所有气泵。

通过从每个字段中删除关键字static,将为每个GasPump提供单独的字段副本。因此,调用reset只会擦除GasPump的一个实例的字段。

下图可以帮助您看到差异:

enter image description here

在此示例中,计数已在CircleWithCount的实例c1和c2之间共享。

您可以在此处阅读有关在字段中使用static关键字的更多详细信息:What does the 'static' keyword do in a class?