-1

These 5 classes make a "shopping cart" and in ShoppingTest.java I create Items, some which have possible bulk prices. I've debugged it several times and I can't figure out why NaN is getting returned! All help is appreciated, thank you. I know it is here: bulkPrice = bulkPrice / bulkAmount; I would like some help on how to get around the Not a number problem, but still implement the code above and below. (Run 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;
   }
}
Marina Claire
  • 103
  • 1
  • 1
  • 11
  • FYI `NaN` is short for "Not a Number", it could possible be associated with a division by 0 – MadProgrammer Mar 30 '15 at 01:24
  • 2
    You've got a lot of code there, perhaps you could indicate *where* you are getting a `NaN` that you don't expect? – Greg Hewgill Mar 30 '15 at 01:25
  • Right about here `bulkPrice = bulkPrice / bulkAmount;` for the first item. I suggest you add some `System.out.println` statements into you code to track the values as they change and even crack open the debugger to see what's going on... – MadProgrammer Mar 30 '15 at 01:29
  • To track this down, take a close look at your stack trace and work downward. If that doesn't give you enough information, enter some simple System.out.println statements. Start with the getTotal() method and then keep working back to each item in your cart. Keep tracing back until you see where the value is not getting calculated correctly. It could be a "type" issue. – riddle_me_this Mar 30 '15 at 01:30
  • I'd also reduce the number of test elements to see which one is giving you the issues – MadProgrammer Mar 30 '15 at 01:31
  • 3
    I'm voting to close this question as off-topic because it's asking for debugging services which should have been done first – MadProgrammer Mar 30 '15 at 01:31

2 Answers2

1
bulkPrice = bulkPrice / bulkAmount; 

Will produce a NaN when bulkAmount is zero.

You validate quantity this way:

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

Consider validating bulkAmount the same way to disallow zero. Also, if you ever want it to be something besides zero you will need to give it a different value. Possibly by passing it in the same way you do quantity.

candied_orange
  • 7,036
  • 2
  • 28
  • 62
  • It should be noted that with formula currently implemented, when choosing random `bulkQuantity` and `quantity`, this exception will be thrown roughly *50%* of the time. Therefore I would suggest finding another solution to avoid this problem. – Marko Gresak Mar 30 '15 at 01:57
0

This is because you are dividing 0 by 0

bulkPrice = bulkPrice / bulkAmount;

This means these values must both be 0. If you use your debug you should be able to confirm that quantity is 0 and trace why. You need to look back through the values which contribute to this expression.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130