Home > database >  How to print lowest price brand name in search results
How to print lowest price brand name in search results

Time:08-05

I have all prices in a list and all titles in a list from this two list i want to get lowest price brand name for that which condition i need to use

CodePudding user response:

With little code I can't get an exact feel, but finding the lowest cost index would work like so:

List<String> brandNames = /*some initialization*/;
List<Integer> brandCosts = /*some initialization*/;

int cheapestIndex = 0;
// can start from first index because we are already storing index 0 by default
for(int i=1; i<brandCosts.size(); i  )
    if(brandCosts.get(i) < brandCosts.get(cheapestIndex))
        cheapestIndex = i;
System.out.println("Cheapest Brand: "   brandNames.get(cheapestIndex));

CodePudding user response:

One way would be the following, as long as the values are sorted in both arrays. it is recommended to implement two-dimensional arrays.

String[] products={"lenovo", "huawei", "oppo", "hp", "mac", "msi", "asus", "dell"};
int[] price ={4500000, 1700000, 6500000, 1000000, 3000000, 14000000, 183000000, 44000000};
        
        int lowerValue = 0;
        int position = 0;
        for(int unitPrice:price) {
            if (unitPrice < lowerValue){
                System.out.println("the cheapest product is: "   products[position]   " with a price of: " price[position]);
            }
            position  ;
        }
  •  Tags:  
  • java
  • Related