Search code examples
javastrategy-pattern

Java Strategy Min-Max


Learning Java on hyperskill. Get stuck on strategy topic. Community there is non responsive and I do not understand why I am not getting correct result. Please help. I assume that I am not getting what this part actually mean :"If the array is empty, the Finder should return Integer.MAX_VALUE in case of finding the min value and Integer.MIN_VALUE in case of finding the max value."

import java.util.Scanner;
import java.util.Arrays;
import java.util.Collections;
import java.util.ArrayList;

class Finder {

    private FindingStrategy strategy;

    public Finder(FindingStrategy strategy) {
        this.strategy = strategy;
    }

    /**
     * It performs the search algorithm according to the given strategy
     */
    public int find(int[] numbers) {
       return this.strategy.getResult(numbers);
    }
}

interface FindingStrategy {

    /**
     * Returns search result
     */
    int getResult(int[] numbers);

}

class MaxFindingStrategy implements FindingStrategy {
    @Override
    public int getResult(int[] numbers) {
        if (numbers.length > 0 ){
            Arrays.sort(numbers);
            return numbers[0];
        } else {
            return Integer.MIN_VALUE;
        }

    }
}

class MinFindingStrategy implements FindingStrategy {
    @Override
    public int getResult(int[] numbers) {
        if (numbers.length > 1 ){
            Arrays.sort(numbers);
            return numbers[numbers.length-1];
        } else {
            return Integer.MAX_VALUE;  
        }
    }
}

/* Do not change code below */
public class Main {

    public static void main(String[] args) {

        final Scanner scanner = new Scanner(System.in);

        final String[] elements = scanner.nextLine().split("\\s+");
        int[] numbers = null;

        if (elements[0].equals("EMPTY")) {
            numbers = new int[0];   
        } else {
            numbers = new int[elements.length];
            for (int i = 0; i < elements.length; i++) {
                numbers[i] = Integer.parseInt(elements[i]);
            }
        }

        final String type = scanner.nextLine();

        Finder finder = null;

        switch (type) {
            case "MIN":
                finder = new Finder(new MinFindingStrategy());
                break;
            case "MAX":
                finder = new Finder(new MaxFindingStrategy());
                break;
            default:
                break;
        }

        if (finder == null) {
            throw new RuntimeException(
                    "Unknown strategy type passed. Please, write to the author of the problem.");
        }

        System.out.println(finder.find(numbers));
    }
}

Solution

  • I would use streams for this job:

    Arrays.stream(numbers).min().orElse(Integer.MIN_VALUE);
    
    Arrays.stream(numbers).max().orElse(Integer.MAX_VALUE);
    

    Since with this stream you do not have to check numbers.length it also solves your issue of checking numbers.length > 1 instead of numbers.length > 0.