macboyme95 macboyme95 - 5 months ago 112
Java Question

Java Discount Calculation

I'm new to coding hence the doubt.
The main objective is to get the LeastPrice of items (including the discount).
The following code works fine but only shows least price ignoring discount.
How to calculate discount?

public class ItemDemo {
public static void main(String[] args) {
Scanner sc1= new Scanner(System.in);
Scanner sc2=new Scanner(System.in);
Scanner sc3=new Scanner(System.in);

Item[] item= new Item[5];
for(int i=0;i<item.length;i++)
{
System.out.println("Enter Item Id");
int itemId=sc1.nextInt();
System.out.println("Enter Item name");
String itemName=sc2.nextLine();
System.out.println("Enter Item price");
double itemPrice=sc3.nextDouble();
System.out.println("Enter item Discount");
double itemDiscount=sc3.nextDouble();
item[i]= new Item(itemId,itemName,itemPrice,itemDiscount);
}

double minValue= getLeastPriceItem(item);
System.out.println("Least item price "+minValue);
}

public static double getLeastPriceItem(Item[] item)
{
double min;
min=item[0].getItemPrice();
for(int i=1;i<item.length;i++)
{
if(item[i].getItemPrice()<min)
{
min=item[i].getItemPrice();
}
}
return min;
}
}


class Item
{
private int itemId;
private String itemName;
private double itemPrice;
private double itemDiscount;
public Item(int itemId, String itemName, double itemPrice,double itemDiscount) {
this.itemId = itemId;
this.itemName = itemName;
this.itemPrice = itemPrice;
this.itemDiscount = itemDiscount;
}
public int getItemId() {
return itemId;
}

public double getItemPrice() {
return itemPrice;
}
public void getItemPrice(double itemPrice) {
this.itemPrice = itemPrice;
}
public double getItemDiscount() {
return itemDiscount;
}
public void setItemDiscount(double itemDiscount) {
this.itemDiscount = itemDiscount;
}
public String getName()
{
return itemName;
}

}

Answer

There is a bit of logic error in your getLeastPriceItem function.

Problem #1:
for(int i=1;i<item.length;i++)

The first index should be 0 not 1. Otherwise you are not processing the first item.

Problem #2:
if (item[i].getItemPrice() < min)

You are not including the discount price for comparison.

Assuming discount is by whole number and not percentage then the code should be something like double afterDiscount = item[i].getItemPrice() - item[i].getItemDiscount();

Problem #3:
public static double getLeastPriceItem

Not so much of a problem but I will recommend returning the entity Item instead.

So here is my proposed solution;

public static void main(String[] args) {

        Item[] item= new Item[5];
        item[0] = new Item(1, "a", 100.0, 20.0);
        item[1] = new Item(2, "b", 80.0, 30.0);
        item[2] = new Item(3, "c", 300.0, 40.0);
        item[3] = new Item(4, "d", 400.0, 390.0);
        item[4] = new Item(5, "e", 500.0, 60.0);

        Item cheapest = getLeastPriceItem(item);
        System.out.println("Least item price: "+ cheapest.getItemPrice() + " with discount of: " + cheapest.getItemDiscount());
    }

    public static Item getLeastPriceItem(Item[] item)
    {
        double currMin = Double.MAX_VALUE;
        Item cheapestItem = null;
        for(int i=0;i<item.length;i++)
        {
            double afterDiscount = item[i].getItemPrice() - item[i].getItemDiscount();
            if(afterDiscount < currMin)
            {
                currMin = afterDiscount;
                cheapestItem = item[i];
            }
        }
        return cheapestItem;
    }

Output:

Least item price: 400.0 with discount of: 390.0

The algorithm here is to track the current cheapest item's value and also the cheapest item object. Loop through all of the items in the array and compare them accordingly, then return the cheapest item object.

Returning the item is a more elegant solution because once you figured out the instance of the cheapest object, you can do/print whatever you like from it's properties. E.g. finding how much discount value it has etc.