Skoči na vsebino

P2 - 2021/22 - DN09 Planeti - resitev Aljaz Starc

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

class Planet {

    public String name;
    public int radius;


    public Planet (String name, int radius) {
        this.name = name;
        this.radius = radius;
    }

    public double povrsina () {
        return 4 * Math.PI * Math.pow(this.radius, 2);
    }

    public String slug () {
        return this.name.toLowerCase();
    }

    public Boolean matchesSlug (List<String> names) {
        return names.stream().map((String name) -> name.toLowerCase()).filter((String name) -> name.equals(this.slug())).count() == 1;
    }
}

public class DN09 {

    public static ArrayList<Planet> planets = new ArrayList<Planet>();

    public static void main (String[] args) throws Exception {

        // Load data from file
        try (Stream<String> linesStream = Files.lines(Paths.get(args[0]))) {
            List<String> lines = linesStream.collect(Collectors.toList());
            for (String line: lines) {
                String[] parts = line.split(":");
                String ime = parts[0];
                int radij = Integer.parseInt(parts[1]);

                Planet planet = new Planet(ime, radij);
                planets.add(planet);
            }
        }

        // filtered
        int povrsina = (int) Math.floor(planets.stream()
            .filter((Planet p) -> p.matchesSlug(Arrays.asList(args[1].split("\\+"))))
            .mapToDouble((Planet p) -> p.povrsina()
        ).sum() / 1000 / 1000);

        System.out.format("Povrsina planetov \"%s\" je %d milijonov km2\n", args[1], povrsina);
    }
}

Zadnja posodobitev: May 3, 2022