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