From 8f40ed195b37e80eaf2f3ddb5d7a42ec1131c00f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C5=A1per=20Dobrovoljc?= Date: Mon, 22 Apr 2024 13:41:36 +0200 Subject: [PATCH] DN07 --- src/DN07.java | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/DN07.java diff --git a/src/DN07.java b/src/DN07.java new file mode 100644 index 0000000..4680298 --- /dev/null +++ b/src/DN07.java @@ -0,0 +1,59 @@ +import java.io.File; +import java.io.FileNotFoundException; +import java.util.Scanner; + +public class DN07 { + public static void main(String[] args) { + Planet[] planeti; + try { + planeti = preberiPlanete(args[0]); + } catch (FileNotFoundException e) { + System.out.println("Datoteka ne obstaja"); + return; + } + + String[] imena = args[1].toLowerCase().split("\\+"); + + double sum = 0; + for (String ime : imena) { + for (Planet planet :planeti) { + if (planet.ime.equals(ime)) { + sum += planet.povrsina(); + } + } + } + + System.out.printf("Povrsina planetov \"%s\" je %d milijonov km2\n", args[1], Math.round(sum / 1000000)); + } + + static Planet[] preberiPlanete(String datoteka) throws FileNotFoundException { + File file = new File(datoteka); + Scanner scanner = new Scanner(file); + + Planet[] planeti = new Planet[8]; + for (int i = 0; i < 8; i++) { + if (!scanner.hasNextLine()) break; + + String line = scanner.nextLine(); + String[] parts = line.split(":"); + + planeti[i] = new Planet(parts[0].toLowerCase(), Integer.parseInt(parts[1])); + } + + return planeti; + } +} + +class Planet { + String ime; + int radij; + + public Planet(String ime, int radij) { + this.ime = ime; + this.radij = radij; + } + + public double povrsina() { + return 4*Math.PI*radij*radij; + } +} \ No newline at end of file