In: Computer Science
Please add comments to this code!
JAVA Code:
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;
}
}
Thanks!!
import java.text.NumberFormat;
public class Item {
// instance variale of string name
private String name;
// instance variale of double price
private double price;
// instance variale of int quantity
private int bulkQuantity;
// instance variale of double price
private double bulkPrice;
/***
*
*Constructor takes 4 arguments and assigns to instance variables
* @param name
* @param price
* @param bulkQuantity
* @param bulkPrice
*/
public Item(String name, double price, int bulkQuantity, double bulkPrice) {
// assinging to name
this.name = name;
//assigning to price
this.price = price;
//assigning to bulkQuantity
this.bulkQuantity = bulkQuantity;
//assigning to bulkPrice
this.bulkPrice = bulkPrice;
}
/**
* Another constructor which takes name and price as arguments
* @param name
* @param price
*/
public Item(String name, double price) {
//checking if price is negative than throwing the exception
if (price < 0) {
throw new IllegalArgumentException();
}
// if everything is good assigning the data
this.name = name;
this.price = price;
}
/***
*
* @param quantity
* @return totalPrice for given quantity
*/
public double priceFor(int quantity) {
double actual = 0;
//if quantity is negative than throwing exception
if (quantity < 0) {
throw new IllegalArgumentException();
} else {
if (bulkQuantity!=0) {
// calculating the actual price if bulkQuantity is not zero
actual = (quantity / bulkQuantity) * bulkPrice
+ (quantity % bulkQuantity) * price;
} else {
// calculating the actual price if bulkQuantity is zero
actual = quantity * price;
}
}
//returning actual price
return actual;
}
//checks the equality of 2 items
public boolean equals(Item ietm) {
//checking equality based on the name
return this.name.equals(ietm.name);
}
@Override
public String toString() {
//creating NumberFormat to format the price to 2 fraction digits
NumberFormat format = (NumberFormat) NumberFormat.getCurrencyInstance();
format.setMinimumFractionDigits(2);
//setting the fractions 2
format.setMaximumFractionDigits(2);
String str = "";
//formating the price and name
str = name + ", " + format.format(price);
if (bulkPrice != 0) {
str += " ( " + bulkQuantity + " for " + format.format(bulkPrice)
+ " )";
}
return str;
}
}