Help me with a simple error in my code dealing with ArrayLists: Here\'s my code:
ID: 3727414 • Letter: H
Question
Help me with a simple error in my code dealing with ArrayLists:
Here's my code:
import java.util.ArrayList;
public class Checkout
{
private int size;
private int amount;
private int sum;
private double taxRate;
private ArrayList <DessertItem> dessertItems;
public Checkout()
{
size = 100;
amount = 0;
sum = 0;
dessertItems = new ArrayList <DessertItem>();
taxRate = DessertShoppe.TAX_RATE;
}
public void enterItem(DessertItem item)
{
dessertItems.add (item);
amount++;
}
public int numberOfItems()
{
return amount;
}
public int totalCost()
{
sum = 0;
for (int i = 0 ; i < amount ; i++)
{
sum += dessertItems[i].getCost ();
}
return sum;
}
public int totalTax()
{
return (int) (Math.round(this.totalCost() * taxRate / 100));
}
public void clear()
{
for (DessertItem item : dessertItems)
{
item = null;
}
amount = 0;
sum = 0;
}
public String toString()
{
String receipt = "Thank You!";
receipt += DessertShoppe.STORE_NAME + " ";
receipt += "Purchased: ";
String totalPay = DessertShoppe.cents2dollarsAndCents(totalCost() + totalTax());
if (totalPay.length() > DessertShoppe.COST_WIDTH)
{
totalPay = totalPay.substring(0, DessertShoppe.COST_WIDTH);
}
receipt += "$" + totalPay;
return receipt;
}
}
This is the error code I get for the bolded line of code:
The type of the expression must be an array type but it resolved to ArrayList<DessertItem>
Explanation / Answer
Since you have not posted class DessertItem, so i can not know what is returned by : dessertItems[i].getCost ()
But It seems that it is returning FLOAT type. But "sum" is of type "Integer". So, if you change type of "sum" variable then it may work
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.