I have modelled the task of super market pricing as per the below task.
http://codekata.com/kata/kata01-supermarket-pricing/
Please review my code and suggest any improvements.
class Product
{
String Name; //Name of the product
int unit_price;
UnitType unit_type;
boolean groupOffer; //Is this product eligible for a group offer?
int buy,get; //If so..what are the values of buy and get..
public Product(String name, int unit_price, UnitType unit_type, boolean groupOffer) {
// TODO Auto-generated method stub
Name = name;
this.unit_price = unit_price;
this.unit_type = unit_type;
this.groupOffer = groupOffer;
}
public Product(String name, int unit_price, UnitType unit_type,boolean groupOffer, int buy, int get) {
this(name,unit_price,unit_type,groupOffer);
this.buy = buy;
this.get = get;
}
}
enum UnitType
{
NUMERIC, //to represent non-breakable units.
POUND,
KG,
GRAM;
}
public class SuperMarket {
public static void main(String args[])
{
//Configuring the price of products.
Product pen = new Product("Pen",5,UnitType.NUMERIC,Boolean.FALSE);
Product potato = new Product("Potato",2,UnitType.POUND,Boolean.FALSE);
Product soda = new Product("Soda",1,UnitType.NUMERIC,Boolean.TRUE,4,1);
//Calculating the prices
System.out.println(getPrice(pen,4));
System.out.println(getPrice(pen,4.1f)); //Pen cannot be a fraction
System.out.println(getPrice(potato,3));
System.out.println(getPrice(potato,3.1f));//Potato can be a fraction
System.out.println(getPrice(soda,10)); // Eligible for group offer
}
//Get the price of products
public static float getPrice(Product product,float quantity)
{
if(product.unit_type == UnitType.NUMERIC)
{
if(quantity != Math.round(quantity))
return -1000;
}
float price = product.unit_price * quantity;
//To decrease the price of free items.
if(product.groupOffer)
{
price = (quantity/(product.buy+product.get)) * product.buy * product.unit_price;
}
return price;
}
}
String Name; //Name of the product
It is a variable called name in a class called Product it should be clear what this variable holds. Same withboolean groupOffer; //Is this product eligible for a group offer?
and//Get the price of products public static float getPrice
.// TODO Auto-generated method stub
Is also an annoying auto generated comment by eclipse \$\endgroup\$