Skip to content
Advertisement

How to write a method which will take item array as input and return item with least price (considering discount as well)?

I am new to Java, and I am trying to declare another method in this class called getItemPrice, which will take item array as input and return item with least price (considering discount as well).

So far, I have declared an array of 2 item objects in main method.

This is my main method:

public static void main(String[] args) {

Item[] Item;

Item = new Item[2]

Item[0] = new Item(1, "Item One", 5, 2);
Item[1] = new Item(2, "Item Two", 10, 5);
}

As you can see above, each item array has an id, name, price and discount.

Now, using these item arrays, I am trying to declare another method in this class called getItemPrice, which will take item array as input and return item with least price (considering discount as well).

For example, the method will take in both Item One and Item 2, but will return Item 1 because 5 minus 2 gives 3, which is less than Item 2, which is 10 minus 5, giving 5.

Hence, Item 1 will be returned because it has the least price when adding the discount.

I am really unsure how to accomplish this, so any help would be appreciated.

Class Implementation:

public class Item {

int ItemId;
String itemName;
double itemPrice;
double itemDiscount;

public Item(int ItemId, String itemName, double itemPrice, double itemDiscount) {

super();
this.ItemId = itemId;
this.ItemName = itemName;
this.itemPrice = itemPrice;
this.itemDiscount = itemDiscount;
}

// Getters and setters follow after this
}

Advertisement

Answer

Yo may iterate on each Item, and save the reference to the current one if its price is lower than the previous lower price

static Item getItemPrice(Item[] items) {
    double lowerPrice = Double.MAX_VALUE;
    Item lower = null;
    for (Item item : items) {
        double finalPrice = item.itemPrice - item.itemDiscount;
        if (finalPrice < lowerPrice) {
            lowerPrice = finalPrice;
            lower = item;
        }
    }
    return lower;
}

public static void main(String[] args) {
    Item[] items = new Item[]{
            new Item(1, "Item One", 5, 2),
            new Item(2, "Item Two", 10, 5)};
    System.out.println(getItemPrice(items)); // Item{id=1, name='Item One', price=5, discount=2}
}

Advanced level with Stream

static Item getItemPrice(Item[] items) {
    return Arrays.stream(items)
            .min(Comparator.comparingDouble(item -> item.itemPrice - item.itemDiscount))
            .orElse(null);
}

Note that, conventions are lowerCamelCase for variable name, and don’t prefix with the class name, a good class would be

class Item {
    int id;
    String name;
    double price;
    double discount;

    public Item(int id, String name, double price, double discount) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.discount = discount;
    }
}
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement