In: Computer Science
Please add comments to this code!
Item Class:
import java.text.NumberFormat;
public class Item {
   private String name;
   private double price;
   private int bulkQuantity;
   private double bulkPrice;
   /***
   *
   * @param name
   * @param price
   * @param bulkQuantity
   * @param bulkPrice
   */
   public Item(String name, double price, int
bulkQuantity, double bulkPrice) {
       this.name = name;
       this.price = price;
       this.bulkQuantity =
bulkQuantity;
       this.bulkPrice =
bulkPrice;
}
   public Item(String name, double price) {
       if (price < 0) {
           throw
new IllegalArgumentException();
       }
       this.name = name;
       this.price = price;
}
   /***
   *
   * @param quantity
   * @return
   */
   public double priceFor(int quantity) {
       double actual = 0;
       if (quantity < 0) {
           throw
new IllegalArgumentException();
       } else {
           if
(bulkQuantity!=0) {
              
actual = (quantity / bulkQuantity) * bulkPrice
                      
+ (quantity % bulkQuantity) * price;
           } else
{
              
actual = quantity * price;
          
}
       }
return actual;
}
   public boolean equals(Item ietm) {
       return
this.name.equals(ietm.name);
   }
   @Override
   public String toString() {
       NumberFormat format =
(NumberFormat) NumberFormat.getCurrencyInstance();
      
format.setMinimumFractionDigits(2);
      
format.setMaximumFractionDigits(2);
       String str = "";
       str = name + ", " +
format.format(price);
       if (bulkPrice != 0) {
           str +=
" ( " + bulkQuantity + " for " + format.format(bulkPrice)
                  
+ " )";
       }
       return str;
   }
}
JAVA Code that needs comments:
public class ItemOrder {
   private final Item item;
   private final int quantity;
   /**
   *
   * @param item
   * @param quantity
   */
   public ItemOrder(Item item, int quantity) {
       this.item = item;
       this.quantity =
quantity;
   }
   public double getPrice() {
       return
item.priceFor(quantity);
   }
   public Item getItem() {
       return item;
   }
}
Thanks!!
Item.java :
//import java package
import java.text.NumberFormat;
//Java class with name Item
public class Item {
   //fields
   private String name;
   private double price;
   private int bulkQuantity;
   private double bulkPrice;
   /***
   *
   * @param name
   * @param price
   * @param bulkQuantity
   * @param bulkPrice
   */
   //constructor with parameters
   public Item(String name, double price, int
bulkQuantity, double bulkPrice) {
       this.name = name;//set name
       this.price = price;//set
price
       this.bulkQuantity =
bulkQuantity;//set bulk quantity
       this.bulkPrice = bulkPrice;//set
bulk price
   }
   //parameterized constructor
   public Item(String name, double price) {
       //checking price
       if (price < 0) {
           //if price is
less than 0 then throw exception
           throw new
IllegalArgumentException();
       }
       this.name = name;//set
name
       this.price = price;//set price
}
   /***
   *
   * @param quantity
   * @return
   */
   //method priceFor() with one integer parameter named
quantity
   public double priceFor(int quantity) {
       double actual = 0;//variable used
to store actual price
       //using if else checking
quantity
       if (quantity < 0) {
           //if quantity is
less than 0 then throw exception
           throw new
IllegalArgumentException();
       } else {
           //checking bulk
quantity
           if (bulkQuantity
!= 0) {
          
    //when bulkQuantity is not 0 then
          
    //calculate actual price
          
    actual = (quantity / bulkQuantity) * bulkPrice +
(quantity % bulkQuantity) * price;
           } else {
          
    //when bulkQuantity is zero
          
    actual = quantity * price;
           }
       }
return actual;//return variable actual
   }
   //equals() method , this method will compare item
name
   public boolean equals(Item ietm) {
       return
this.name.equals(ietm.name);
   }
   //method toString()
   @Override
   public String toString() {
       //instance of NumberFormat
class
       NumberFormat format =
(NumberFormat) NumberFormat.getCurrencyInstance();
       //setting format for fractional
digit
      
format.setMinimumFractionDigits(2);
      
format.setMaximumFractionDigits(2);
       String str = "";//declaring
variable for concatenating name and price
       str = name + ", " +
format.format(price);
       //checking bulkPrice
       if (bulkPrice != 0) {
           //when bulkPrice
is not zero then
           str += " ( " +
bulkQuantity + " for " + format.format(bulkPrice) + " )";
       }
       return str;//return variable
str
   }
}
**************************************************
ItemOrder.java :
//Java class named ItemOrder
public class ItemOrder {
   //private final fields
private final Item item;
private final int quantity;
/**
*
* @param item
* @param quantity
*/
//constructor with parameters
public ItemOrder(Item item, int quantity) {
this.item = item;//set item
this.quantity = quantity;//set quantity
}
//method to return price
public double getPrice() {
   //calling static method from item class
return item.priceFor(quantity);
}
//This is getter method for the item
public Item getItem() {
return item;
}
}