我为什么要归还NaN?

时间:2015-03-30 01:22:00

标签: java object

这5个类构成了一个“购物车”,而在ShoppingTest.java中,我创建了一些项目,其中一些可能具有批量价格。我调试了几次,我无法弄清楚为什么NaN会被退回!感谢所有帮助,谢谢。 我知道它在这里:bulkPrice = bulkPrice / bulkAmount; 我想在如何解决非数字问题方面提供一些帮助,但仍然可以实现上面和下面的代码。 (运行ShoppingTest)

    public class ShoppingTest{
   public static void main(String[] args){
      Item sillyPutty = new Item("silly putty" , 3.95, 10, 19.99);
      Item sillyString = new Item("silly string" , 3.5, 10, 14.95);
      Item bottleOfBubbles = new Item("bottle o bubbles" , .99);
      Item nintendoWii = new Item("Nintendo Wii System" , 389.99);
      Item mario = new Item("Mario Computer Science Party 2(Wii)",49.99);
      Item jamChallenge = new Item("Don Knuth Code Jam Challenge(Wii)", 49.99);
      Item computerSciencePen = new Item("Computer Science Pen", 3.4);
      Item rubiksCube = new Item("Rubik's cube" , 9.1);
      Item computerScienceBarbie = new Item("Computer Science Barbie", 10.99);
      Item button = new Item("\'Java Rules!\'button" , .99, 10, 5);
      Item bumperSticker = new Item("\'Java Rules\'bumper sticker", .99, 20, 8.95);

      Catalog catalog = new Catalog("RHS catalog" );
      catalog.add(sillyPutty);
      catalog.add(sillyString);
      catalog.add(bottleOfBubbles);
      catalog.add(nintendoWii);
      catalog.add(mario);
      catalog.add(jamChallenge);
      catalog.add(computerSciencePen);
      catalog.add(rubiksCube);
      catalog.add(computerScienceBarbie);
      catalog.add(button);
      catalog.add(bumperSticker);

      ItemOrder order = new ItemOrder(sillyPutty, 2);
      ItemOrder order2 = new ItemOrder(bottleOfBubbles, 3);
      ItemOrder order3 = new ItemOrder(nintendoWii, 1);
      ItemOrder order4 = new ItemOrder(computerSciencePen, 1);
      ItemOrder order5 = new ItemOrder(computerScienceBarbie, 4);
      ItemOrder order6 = new ItemOrder(button,12);
      ShoppingCart cart = new ShoppingCart();
      cart.add(order);
      cart.add(order2);
      cart.add(order3);
      cart.add(order4);
      cart.add(order5);
      cart.add(order6);
      double totalPrice = 0;
      totalPrice = cart.getTotal();
      System.out.print(totalPrice);
   }

}

    import java.util.*;
//Stores information about the overall order
public class ShoppingCart{
   ArrayList<ItemOrder> itemOrder;
   double total;
   boolean discount;

   public ShoppingCart(){
      itemOrder = new ArrayList<ItemOrder>();
      total = 0.0;
      discount = false ;
   }

   public void add(ItemOrder order){
      for(int i =0; i< itemOrder.size(); i++){
         if (itemOrder.get(i).getItem() == order.getItem()){
            itemOrder.remove(itemOrder.get(i));
         }
      }
      itemOrder.add(order);
   }

   public void setDiscount(boolean value){    
      if (value == true ){
         discount = true ;
      }
   }

   public double getTotal(){
      for (int i = 0; i<itemOrder.size(); i++){
         total += itemOrder.get(i).getPrice();
      }
      if (discount == true ){
         total = total * .9;
      }
      return total;
   }
}

    //Stores information aout a particular item
// and the quantity ordered for that item
public class ItemOrder{
   int quantity;
   Item item;
   //double itemOrderCost;

   public ItemOrder(Item inItem, int inQuantity){
      quantity = inQuantity;
      item = inItem;
   }

   public double getPrice(){
      return item.priceFor(quantity);
   }

   public Item getItem(){
      return item; 
   }
}

    import java.util.*;
//stores information about a collection of these items
public class Catalog { 
   ArrayList<Item> itemList;
   String catalogName;
   Item item;

   public Catalog(String inName){
      catalogName = inName;
      itemList = new ArrayList<Item>();
   }

   public void add(Item itemInformation){
      itemList.add(itemInformation);
   }

   public int size(){
      return itemList.size();
   }

   public Item get(int index){
      Item itemAtIndex = itemList.get(index);
      return itemAtIndex;
   }

   public String getName(){
      return catalogName;
   }
}

    //stores information about specific item

public class Item{
   String name;
   double price;
   double bulkPrice;
   int bulkQuantity;
   boolean isBulkItem;

   public Item(String inName, double inPrice)throws IllegalArgumentException{
      name = inName;
      price = inPrice;
      isBulkItem = false ;
      if (price<0){
         throw new IllegalArgumentException("Price can't be negative");
      }
   }

   public Item(String inName, double inPrice, int inBulkQuantity, double inBulkPrice){
      name = inName;
      price = inPrice;
      bulkPrice = inBulkPrice;
      bulkQuantity = inBulkQuantity;
      isBulkItem = true ;
      if (price < 0 || bulkPrice < 0){
         throw new IllegalArgumentException("Can't be negative");
      }
   }

   public double priceFor(int quantity){
      int withNormalPrice = 0;
      int bulkAmount = 0;
      if (quantity <0){
         throw new IllegalArgumentException("quantity can't be negative");
      }
      if (isBulkItem==true ){
         withNormalPrice = quantity%bulkQuantity;
         bulkAmount = quantity-withNormalPrice;
         bulkPrice = bulkPrice / bulkAmount;
         price = (bulkAmount*bulkPrice) + (price * withNormalPrice);
      }
      else {
         price = price * quantity;
      }
      return price;
   }

   public String toString(){
      String amounts = "" ;
      amounts = name + ", " + price;
      if (bulkPrice > 0|| bulkQuantity > 0){
         amounts = amounts + " (" + bulkPrice + "for " + bulkQuantity;
      }
     // NumberFormat nf = NumberFormat.getCurrencyInstance();
     // String text = nf.format(price);
      return amounts;
   }
}

2 个答案:

答案 0 :(得分:1)

bulkPrice = bulkPrice / bulkAmount; 

当bulkAmount为零时将生成NaN

您以这种方式验证quantity

  if (quantity <0){
     throw new IllegalArgumentException("quantity can't be negative");
  }

考虑以同样的方式验证bulkAmount以禁止零。此外,如果您希望它是零以外的东西,您将需要给它一个不同的值。可能通过以与数量相同的方式传递它。

答案 1 :(得分:0)

这是因为你将0除以0

bulkPrice = bulkPrice / bulkAmount;

这意味着这些值必须都为0.如果使用调试,则应该能够确认quantity为0并跟踪原因。您需要回顾一下有助于此表达式的值。