增强了循环不执行

时间:2017-09-23 00:59:08

标签: java for-loop

更新:请阅读本文的第二部分,内容为"更新"。自原帖以来,我已经获得了部分代码。谢谢!

我的toString方法中有一个增强的for循环,它返回一个字符串:

public String toString() {
    for (SelectedItem item: cartItems) {
        return "Item: " + item.getItemNumber() + "\nQuantity: " + item.getQuantity + "\nUnit Price: $" + item.getUnitPrice + "\nTotal Price: $" + (item.getUnitPrice * item.getQuantity);
    }
        return "";
}

但是,我的所有代码都返回:

" []

[]&#34 ;.我相信我的代码只执行返回""部分而不是"返回'项目:......'"部分。作为参考,这是我的完整课程:

import java.util.*;

public class ShoppingCart implements Cart  {

private double total;
private double tax;
private double shipping;
private int totalQuantity = 0;
ArrayList<SelectedItem> cartItems = new ArrayList<SelectedItem>();

public ShoppingCart() {
}

public void addItem(SelectedItem newItem) {     
    for (SelectedItem item: cartItems) {
        if (newItem.getItemNumber() == item.getItemNumber()) {
            totalQuantity = newItem.getQuantity() + totalQuantity;
        } else {
            totalQuantity = newItem.getQuantity();
            cartItems.add(new SelectedItem(newItem.getItemNumber(), newItem.getDescription(), newItem.getUnitPrice(), newItem.getQuantity()));
        }

    }
}

public void deleteItem(int deleteItemNumber) {

}

public double getTotal() {
    return total;
}

public double getTax() {
    tax = total * 0.045;
    return tax;
}

public double getShipping() {
    if (total <= 10) {
        shipping = 2.50;
    } else {
        shipping = total * 0.15;
    }
    return shipping;
}

public String toString() {
    for (SelectedItem item: cartItems) {
        return "Item: " + item.getItemNumber() + "\nQuantity: " + item.getQuantity + "\nUnit Price: $" + item.getUnitPrice + "\nTotal Price: $" + (item.getUnitPrice * item.getQuantity);
    }
        return "";
}

}

这是 SelectedItem 类:

public class SelectedItem implements Item {

private int itemNumber;
private String description;
private double unitPrice;
private int quantity;

public SelectedItem(int itemNumber, String description,
        double unitPrice, int quantity) {
    this.itemNumber = itemNumber;
    this.description = description;
    this.unitPrice = unitPrice;
    this.quantity = quantity;
}

public int getItemNumber() {
    return itemNumber;
}

public double getUnitPrice() {
    return unitPrice;
}

public String getDescription() {
    return description;
}

public int getQuantity() {
    return quantity;
}

public void setQuantity(int newQuantity) {
    quantity = newQuantity;
}

}

最后,我的测试人员应该创建SelectedItem对象的实例:

public class CartTester {

public static void main(String[] args) {

    ShoppingCart cart1 = new ShoppingCart();
    cart1.addItem(new SelectedItem(1, "widget", 0.95, 5));
    cart1.addItem(new SelectedItem(2, "gadget", 0.25, 10));
    cart1.addItem(new SelectedItem(3, "doohickey", 0.35, 3));
    cart1.deleteItem(2);
    cart1.addItem(new SelectedItem(3, "doohickey", 0.35, 2));
    cart1.addItem(new SelectedItem(4, "thingamabob", 0.12, 7));
    System.out.println(cart1.toString());

    ShoppingCart cart2 = new ShoppingCart();
    for (int i = 1; i <= 1000; i++) {
        String nameString = "Volume " + i;
        cart2.addItem(new SelectedItem(i, nameString, 10.99, i));
    }
    System.out.println("**********");
    System.out.println(cart2.toString());
}

}

请帮帮我!!以下是输出结果的片段:

Item: widget
Quantity: 5
Unit Price: $0.95
Total Price: $4.75

更新:我已将代码打印出以下内容:

aItem: widget
Quantity: 5
Unit Price: $0.95
Total Price: $4.75

**********
Item: Volume 1
Quantity: 1
Unit Price: $10.99
Total Price: $10.99

..但它仍然没有打印出ShoppingCart中的所有商品。我修改了我的ShoppingCart类,我认为问题是AddItem方法或toString方法。这是我更新的ShoppingCart类:

import java.util.*;

public class ShoppingCart implements Cart  {

private double total;
private double tax;
private double shipping;
private int totalQuantity = 0;
ArrayList<SelectedItem> compareTo = new ArrayList<SelectedItem>();
ArrayList<SelectedItem> cartItems = new ArrayList<SelectedItem>();
String itemDescription = "";
int itemQuantity;
double itemUnitPrice;
double itemTotal;

public ShoppingCart() {

    }

public void addItem(SelectedItem newItem) { 
    boolean found = false;
    if (cartItems != null && !cartItems.isEmpty()) {
       for (SelectedItem item : cartItems) {
           if (newItem.getItemNumber() == item.getItemNumber()) {
               found = true;
               item.setQuantity(newItem.getQuantity() + item.getQuantity());
           } 
       }
    } else {

    }
    if (found) {

    } else {
        cartItems.add(new SelectedItem(newItem.getItemNumber(), newItem.getDescription(), newItem.getUnitPrice(), newItem.getQuantity()));
    }

}

public void deleteItem(int deleteItemNumber) {
    for (SelectedItem item: cartItems) {
        if (deleteItemNumber == item.getItemNumber()) {
            item.setQuantity(0);
        }
    }
}

public double getTotal() {
    for (SelectedItem item: cartItems) {
        total = item.getQuantity() * item.getUnitPrice();
    }
    return total;
}

public double getTax() {
    tax = total * 0.045;
    return tax;
}

public double getShipping() {
    if (total <= 10) {
        shipping = 2.50;
    } else {
        shipping = total * 0.15;
    }
    return shipping;
}

public String toString() {
        for (SelectedItem item: cartItems) {
            return ("Item: " + item.getDescription() + "\nQuantity: " + item.getQuantity() + "\nUnit Price: $" 
        + item.getUnitPrice() + "\nTotal Price: $" + (item.getQuantity()*item.getUnitPrice()) + "\n");
        }
            return "\nTotal: $" + getTotal() + "\nTax: $" + getTax() + "\nShipping: $" + getShipping() +
                    "\nGRAND TOTAL: $" + (getTotal() + getTax() + getShipping());
    }
}

1 个答案:

答案 0 :(得分:-1)

正如在评论中已经提到的,使用return语句是错误的,除非您只想打印出第一个项目。所以你最好这样做:

  1. 将以下toString()方法添加到ShoppingCart

    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (SelectedItem item : cartItems) {
            sb.append("Item: " + item.getDescription() + "\nQuantity: " + item.getQuantity() + "\nUnit Price: $" + item.getUnitPrice() + "\nTotal Price: $" + (item.getQuantity()*item.getUnitPrice()) + "\n\n");
        }
        return sb.toString();
    }
    
  2. 最后,您的addItems方法应更改如下:

    public void addItem(SelectedItem newItem) {
        boolean found = false;
        if (cartItems != null && !cartItems.isEmpty()) {
           for (SelectedItem item : cartItems) {
               if (newItem.getItemNumber() == item.getItemNumber()) {
                   found = true;
               } 
           }
        } 
        if (found) {
            totalQuantity += newItem.getQuantity();
        } else {
            totalQuantity = newItem.getQuantity();
            cartItems.add(new SelectedItem(newItem.getItemNumber(), newItem.getDescription(),newItem.getUnitPrice(), newItem.getQuantity()));
        }
    }