我有一些代码要查看一个类,我理解其中的大部分内容,但对这种方法感到困惑。使用给定的代码,返回更改不会总是导致 0,因为最后输入的是 totalOfItems 和 totalGiven 为 0.0。有人告诉我,运行它时不会发生,但我想了解原因。谁能帮我?
public SelfCheckout () {
this.totalOfItems = 0.0;
this.totalGiven = 0.0;
}
/**
* This method will scan the item.
* @param amount The amount the items cost.
*/
public void scanItem (double amount){
this.totalOfItems = this.totalOfItems + amount;
}
/** The method will add the items scanned and return the total due.
*
* @return getTotalDue The getTotalDue is the total amount of items scanned.
*/
public double getTotalDue(){
return this.totalOfItems;
}
/** The method will show the amount that is received from the consumer.
*
*/
public void receivePayment(double amount){
this.totalGiven = this.totalGiven + amount;
}
/**The method will calculate the amount of change due.
*
*/
public double produceChange(){
double change = this.totalGiven - this.totalOfItems;
this.totalGiven = 0.0;
this.totalOfItems = 0.0;
return change;