Home > Back-end >  How to display least and most expensive from array in Java
How to display least and most expensive from array in Java

Time:11-02

So I have an array of 15 Flying Objects, The flyingObjects class consists of 1 variable (Price: Double) with its getters and setters. I also have an airplane class that extends FlyingObjects, a helicopter class that extends Airplane and quadrotor and multirotor class that extends helicopter. On the other side of the tree I have a UAV class that extends FlyingObjects, a MAV class that extends UAV and a AD class that extends UAV.

Here is the array:

    FlyingObjects [] test = new FlyingObjects[7];
    test[0] = new Uav(10,43);
    test[1] = new AgriculturalDrone(8000,780000,"Chase",2400);
    test[2] = new Uav(10,5);
    test[3] = new Mav(0.5,140000,"trooper",10);
    test[4] = new Multirotor("Hexa",140000,200,185,2021,1,4);
    test[5] = new Helicopter("Robinson",199000,250,100,2018,7);
    test[6] = new Airplane("Boeing",350000,450);

Now I need to write a method that will get me the most expensive and least expensive UAV in my array (note that the price is always the second attribute in the UAV's constructors). for some reason my method always return the first UAV in the array as the least expensive and the last UAV in the array as the most expensive. Any tip on how to fix this issue?

    public static void findLeastAndMostExpensiveUAV(FlyingObjects[] flyingObjects) {
    int mostExpensive = -1;
    int leastExpensive =1000000000;
    boolean hasUav = false;
    
    if(flyingObjects == null) {
        System.out.println("There is no UAV");
    }
    for(int i = 0;i<flyingObjects.length;i  ) {
        if (flyingObjects[i] instanceof Uav) {
          Uav a = (Uav) flyingObjects[i];
          if (a.getPrice() >= mostExpensive) {
              mostExpensive = i;
          }if (a.getPrice() <= leastExpensive){
              leastExpensive = i;

          }
          if(!hasUav) {
              hasUav = true;
          }
        }
    }
    if(!hasUav) {
        System.out.println("There is no UAV");
    }
    else {
        System.out.println("\nInformation about the most expensive UAV: \n"  flyingObjects[mostExpensive] "\n");
        System.out.println("Information about the least expensive UAV: \n" flyingObjects[leastExpensive]);
    }
}

CodePudding user response:

Your mostExpensive and leastExpensive variables seem to be representing both the index of your max and min priced Uav objects in the array and the price of the max and min price of all Uav objects.

You should track the min/max price (type double) and index of min/max priced Uav object (type int) separately, perhaps in variables that look like these:

int maxPricedUavIndex;
int minPricedUavIndex;
double maxPrice = 0d;
double minPrice = Double.MAX_VALUE;

You would assign maxPricedUavIndex and minPricedUavIndex to the value of the index i where appropriate, and you would assign maxPrice and minPrice to a.getPrice() where appropriate

CodePudding user response:

Another way using StreamAPI:

var stat = Arrays.stream(flyingObjects)
                .filter(o -> o instanceof UAV)
                .collect(Collectors.teeing(
                        Collectors.minBy(Comparator.comparingDouble(FlyingObject::getPrice)),
                        Collectors.maxBy(Comparator.comparingDouble(FlyingObject::getPrice)),
                        (min, max) -> new MinMaxStatistic<>(min.orElse(null), max.orElse(null))));

FlyingObject min = stat.min;
FlyingObject max = stat.max;

and you'll need a class to collect statistics

class MinMaxStatistic<T> {
    T min;
    T max;

    public MinMaxStatistic(T min, T max) {
        this.min = min;
        this.max = max;
    }
}
  • Related