我正在创建一个程序,其中有一个名为GroceryItem的类,另一个名为GroceryList,第三个是运行程序的主方法。
我在这个节目中做了很多,但我现在被困住了。请查看我的代码并帮助我。
GroceryItem类:
public class GroceryItem {
private String name;
private double pricePerUnit;
private int quantity;
public GroceryItem(int quantity, String name, double pricePerUnit) {
this.name = name;
this.pricePerUnit = pricePerUnit;
this.quantity = quantity;
}
public double getCost() {
return (this.quantity * this.pricePerUnit);
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
GroceryList类:
public class GroceryList {
private GroceryItem[] list = null;
int num;
public GroceryList() {
list = new GroceryItem[10];
this.num = 0;
}
// Constructs a new empty grocery list.
public void add(GroceryItem item) {
list.add(item);
}
// Adds the given item order to this list, if the list is not full (has
// fewer than 10 items).
public double getTotalCost() {
double totalcost = 0;
for(int i = 0; i < list.length; i++){
totalcost += getGroceryItemOrder(getCost());
}
return totalcost;
}
// Returns the total sum cost of all grocery item orders in this list.
}
主要方法:
public static void main(String[] args) {
GroceryList list = new GroceryList();
GroceryItem carrots = new GroceryItem(5,"Carrots", 0.40);
list.add(carrots);
GroceryItem apples = new GroceryItem( 4,"Apples", 0.15);
list.add(apples);
GroceryItem rice = new GroceryItem( 1,"Rice", 1.10);
list.add(rice);
GroceryItem tortillas = new GroceryItem(10,"Tortillas", .05);
list.add(tortillas);
GroceryItem strawberries = new GroceryItem(1,"Strawberries", 4.99);
list.add(strawberries);
GroceryItem chicken = new GroceryItem( 1,"Chicken", 5.99);
list.add(chicken);
GroceryItem lettuce = new GroceryItem( 1,"Lettuce", 0.99);
list.add(lettuce);
GroceryItem milk = new GroceryItem( 2,"Milk", 2.39);
list.add(milk);
GroceryItem yogurt = new GroceryItem( 3,"Yogurt", 0.60);
list.add(yogurt);
GroceryItem chocolate = new GroceryItem(1,"Chocolate", 3.99);
list.add(chocolate);
}
}
答案 0 :(得分:1)
您正尝试使用方法add
将内容添加到数组中。
您应该使用ArrayList
或类似的数据结构,或使用索引添加项目:
list[ index++ ] = item
原因是简单数组没有add
方法。 ArrayList
以及其他几个集合类。
另外,在原始代码中,您有以下行:
totalcost += getGroceryItemOrder(getCost());
此代码中没有定义方法getGroceryItemOrder(...)
。并且,在此表单中,它将在getCost()
类上调用GroceryItemList
。 GroceryItemList
没有这样的方法,所以你得到一个错误
您想在当前列表项上调用getCost()
,因此您需要的行是:
totalcost += list[i].getCost();
答案 1 :(得分:0)
使用此选项添加您的Grocery元素。
GroceryList类
public class GroceryList {
List<GroceryItem> list = null;
int num;
public GroceryList() {
list = new ArrayList<GroceryItem>();
this.num = 0;
}
// Constructs a new empty grocery list.
public void add(GroceryItem item) {
list.add(item);
System.out.println("Added Grocery :::: >>>> NAME:" +item.getName()+ " ::::: PRICE PER UNIT: "+item.getPricePerUnit()+" :::::: QUANTITY: "+item.getQuantity()+" ::::: FINALCOST: "+(item.getQuantity()*item.getPricePerUnit()) );}
// Adds the given item order to this list, if the list is not full (has
// fewer than 10 items).
public double getTotalCost() {
double totalcost = 0;
for(int i = 0; i < list.size(); i++){
totalcost += list.get(i).getCost();
}
return totalcost;
}
// Returns the total sum cost of all grocery item orders in this list.
}
/// Add this function as toString class
@Override
public String toString() {
return "GroceryList [list=" + list + ", num=" + num + "]";
}
// add a function for finding cost per item like this
public HashMap<String,Double> getCostPerItem(int i) {
HashMap<String, Double> itemPriceName= new HashMap<String, Double>();
itemPriceName.put(list.get(i).getName(),
(list.get(i).getPricePerUnit()*list.get(i).getQuantity()));
return itemPriceName;
}
//在主函数中,您可以调用上面指定的函数
for(int i=0;i<list.list.size();i++) {
System.out.println("Cost Per Item "+list.getCostPerItem(i));
}