2016-04-15 11 views
-3

ユーザーが自動車に燃料を入れるたびにストレージシステムを作成しなければなりません。格納される必要があるデータは、日付、自動車の走行距離、リットルの数およびリットル当たりのコストです。これらを記録するために別のクラスを作成する必要があります。私はオブジェクトを格納し、管理するために配列を使うように頼まれました。

燃料が手に入るたびに、すべての取引の詳細を追加できるはずです。

誰もが適切な線に沿って手伝ってくれますか?以下は私の燃料ロガークラスです。私が話していた燃料取引クラスの作成方法はわかりません。たぶん

public class FuelLogger 
{ 
public static void main (String [] arguments) 
    { 
     FuelTransaction Ft1 = new FuelTransaction("10/01/2016", 500, 10,   0.99); 
     FuelTransaction Ft2 = new FuelTransaction("15/01/2016", 560, 10, 0.99); 
     FuelTransaction Ft3 = new FuelTransaction(); 

     Ft3.setDate("24/01/2016"); 
     Ft3.setCarMileage(670); 
     Ft3.setNumberOfLitres(15); 
     Ft3.setCostPerLitre(1.01); 

     Ft1.displayDetails(); 
     Ft2.displayDetails(); 
     Ft3.displayDetails(); 

     //Amount of fuel bought between 2 dates 
     //System.out.println("The total amount of fuel between the two dates is " + FuelTransaction.getFuelAmount(Ft1, Ft3)); 

     System.out.println("The total number of FuelTransactions is " + FuelTransaction.getTotalNum()); 
    } 
} 
+1

何か試しましたか? クラス、名前、プロパティは何ですか... – Charly

+0

fuelTransaction型の配列を作成する必要があります。ここで、fuelTransactionは、日付、自動車の走行距離などのすべての情報を含むクラスです。これに答えるか、この質問を完成させること。 –

+0

学校や面接は? – Kayaman

答えて

0

このような何か:

public class FuelTransaction { 

    // Class variables 
    String date; 
    int mileage, numberOfLitres, costPerLitre; 

    // Constructor where we instantiate the FuelTransaction object 
    public FuelTransaction(String date, int mileage, int numberOfLitres, int costPerLitre) { 

    // Takes all the variables passed in and stores them to the class variable 
    this.date = date; 
    this.mileage = mileage; 
    this.numberOfLitres = numberOfLitres; 
    this.costPerLitre = costPerLitre; 
    } 

    public FuelTransaction() { 

    // Empty constructor, sets everything to "" or 0 
    this.date = ""; 
    mileage = numberOfLitres = costPerLitre = 0; 
    } 

    public void setDate(String date) { 

    // Setter to set the date 
    this.date = date; 
    } 

} 

これは完全なクラスではありません、あなたはそれに追加する必要がありますが、これらは基本です。 2つのコンストラクタがあるに注意してください、これはあなたがnew FuelTransaction(DATE, MILEAGE, NUMBEROFLITRES, COSTPERLITRE)ような変数のすべてを指定することにより、FuelTransactionクラスを初期化することができますが、それはまた、あなたがnew FuelTransaction()を呼び出してから、手動でsetDate()セッターでインスタンス化した後にデータを追加することができます:

FuelTransaction ft = new FuelTransaction(); 
ft.setDate("01-23-45"); 
+0

ああ、私はそれが完全に動作するように追加した他のビットを回避する必要がある作業の一部を得ました。どうもありがとう! –

1

あなたに新しいLogオブジェクトを作成し、コンストラクタ内のオブジェクトに詳細を追加して保存することができます。

+0

どうすればいいですか?私は燃料取引のための新しいクラスを完成しようとしました、そして、それは2つの日付の間に燃料の量を見いだすまでうまくいくので、最初の行の 'public static void betweenTwoDates(String startDate、String endDate、fuelTransaction []) thisFuelTransactions) 'が、シンボルが見つからないと言っていますか? –

+0

@Paddy McKenna前回の燃料取引を車両オブジェクトに保存する必要があります。だから今すぐ計算することができます - vehicle.lastFuelTransaction; – PeerNet

関連する問題