2016-04-15 53 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

我该怎么做?我已经尝试完成一个新的燃料交易类,它工作正常,直到我必须找到两个日期之间的燃料量,所以我把第一行'公共静态无效betweenTwoDates(字符串startDate,字符串endDate,fuelTransaction [] thisFuelTransactions)“,但它说这个符号找不到? –

+0

@ Paddy McKenna您必须将上次燃料交易存储在车辆对象中。所以你现在可以计算 - vehicle.lastFuelTransaction; – PeerNet