2014-10-16 48 views
1

所以我應該創建一個程序,在那裏我有一個帶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 
    } 
} 

回答

7

不要對存儲在GasPump中的數據使用靜態字段。

靜態字段是單例,它們只有一個值在所有GasPump實例中共享。這意味着如果您有多個GasPump實例,那麼調用reset將會重置所有的加油泵。

通過從每個字段中刪除關鍵字static,然後會爲每個GasPump的字段保留一個單獨的副本。因此調用重置將只擦除GasPump的一個實例的字段。

下圖可以幫助您可視化的區別:

enter image description here

在這個例子中,計數已全面c1和CircleWithCount的C2實例共享。

你可以閱讀更多的細節有關使用static關鍵字上的字段位置:What does the 'static' keyword do in a class?

+0

我從實例變量改變static關鍵字就像你告訴我,但是當我打電話的serveAClient方法()方法,我得到一個錯誤表示「無法對非靜態字段_____進行靜態引用」,並且它提供的唯一解決方案是將其恢復爲靜態。 – Rocketsm46 2014-10-16 15:22:30

+0

@ Rocketsm46這是因爲你必須在整個其他方法中級聯改變。 serveAClient具有類似'GasPump.staticFieldName'的引用,並且由於該字段不再是靜態的,編譯器正確地標記錯誤。要修復,請改爲參考實例字段/方法。例如'GasPump.getCurrentGasType()'變成'getCurrentGasType()'。有時候人們使用以下相當於'this.getCurrentGasType()'的。 – 2014-10-16 15:30:29