P2 2021/22 - 11 Zemljevidi¶
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class Vozlisce {
private UUID uuid;
private double x;
private double y;
private List<Cesta> ceste;
private int index;
public Vozlisce (int index, double x, double y) {
this.x = x;
this.y = y;
this.index = index;
this.uuid = UUID.randomUUID();
this.ceste = new ArrayList<Cesta>();
}
public UUID getUuid () { return this.uuid; }
public int getIndex () { return this.index; }
public double getX () { return this.x; }
public double getY () { return this.y; }
public void dodajCesto (Cesta cesta) {
this.ceste.add(cesta);
}
public double distanceFrom (Vozlisce vozlisce) {
return Math.sqrt(
Math.pow(vozlisce.getX() * 111.12 - this.x * 111.12, 2) +
Math.pow(vozlisce.getY() * 77.4 - this.y * 77.4, 2)
);
}
}
class Kraj extends Vozlisce {
private String ime;
public Kraj (int index, double x, double y, String ime) {
super(index, x, y);
this.ime = ime;
}
}
class Crpalka extends Vozlisce {
private double cena95;
private double cenaDizel;
public Crpalka (int index, double x, double y, double cena95, double cenaDizel) {
super(index, x, y);
this.cena95 = cena95;
this.cenaDizel = cenaDizel;
}
}
class Cesta {
private UUID uuid;
private Vozlisce vozlisce1;
private Vozlisce vozlisce2;
private int maxspeed;
public Cesta (Vozlisce vozlisce1, Vozlisce vozlisce2, int maxspeed) {
this.vozlisce1 = vozlisce1;
this.vozlisce2 = vozlisce2;
this.maxspeed = maxspeed;
this.uuid = UUID.randomUUID();
vozlisce1.dodajCesto(this);
vozlisce2.dodajCesto(this);
}
public UUID getUuid () { return this.uuid; }
public int getMaxSpeed () { return this.maxspeed; }
public Vozlisce getVozlisce1 () { return this.vozlisce1; }
public Vozlisce getVozlisce2 () { return this.vozlisce2; }
public double getDolzina () { return this.vozlisce1.distanceFrom(this.vozlisce2); }
public String toString () {
return String.format(
"Cesta (%d, %d): dolzina=%.2f km, omejitev=%d km/h",
this.vozlisce1.getIndex(),
this.vozlisce2.getIndex(),
this.getDolzina(),
this.maxspeed
);
}
}
class CestnoOmrezje {
private UUID uuid;
private List<Cesta> ceste;
private List<Vozlisce> vozlisca;
public CestnoOmrezje (List<Cesta> ceste, List<Vozlisce> vozlisca) {
this.uuid = UUID.randomUUID();
this.ceste = ceste;
this.vozlisca = vozlisca;
}
public UUID getUuid () { return this.uuid; }
public List<Cesta> getCeste () { return this.ceste; }
public List<Vozlisce> getVozlisca () { return this.vozlisca; }
public Vozlisce getVozlisce (int index) { return this.vozlisca.stream().filter((Vozlisce v) -> v.getIndex() == index).findFirst().orElse(null); }
public Cesta getCesta (Vozlisce v1, Vozlisce v2) {
return this.ceste
.stream()
.filter((Cesta c) -> {
return (
(v1.getIndex() == c.getVozlisce1().getIndex() && v2.getIndex() == c.getVozlisce2().getIndex()) ||
(v1.getIndex() == c.getVozlisce2().getIndex() && v2.getIndex() == c.getVozlisce1().getIndex())
);
})
.findFirst().orElse(null);
}
public static CestnoOmrezje izDatoteke(String imeDatoteke) throws Exception {
List<Cesta> ceste = new ArrayList<>();
List<Vozlisce> vozlisca = new ArrayList<>();
try (Stream<String> linesStream = Files.lines(Paths.get(imeDatoteke))) {
AtomicInteger vIndex = new AtomicInteger(0);
linesStream.collect(Collectors.toList()).stream().skip(1).forEach((String s) -> {
String[] p = s.split(" ");
if (p[0].matches("vozlisce")) {
vozlisca.add(new Vozlisce(
vIndex.getAndIncrement(),
Double.parseDouble(p[1]),
Double.parseDouble(p[2])
));
} else if (p[0].matches("kraj")) {
String krajName = "";
for (int i = 3; i < p.length; i++) krajName += p[i];
vozlisca.add(new Kraj(
vIndex.getAndIncrement(),
Double.parseDouble(p[1]),
Double.parseDouble(p[2]),
krajName
));
} else if (p[0].matches("crpalka")) {
vozlisca.add(new Crpalka(
vIndex.getAndIncrement(),
Double.parseDouble(p[1]),
Double.parseDouble(p[2]),
Double.parseDouble(p[3]),
Double.parseDouble(p[4])
));
} else {
ceste.add(new Cesta(
vozlisca.stream().filter((Vozlisce v) -> v.getIndex() == Integer.parseInt(p[0])).findFirst().orElse(null),
vozlisca.stream().filter((Vozlisce v) -> v.getIndex() == Integer.parseInt(p[1])).findFirst().orElse(null),
Integer.parseInt(p[2])
));
}
});
}
CestnoOmrezje omrezje = new CestnoOmrezje(ceste, vozlisca);
return omrezje;
}
public void dolzinaPoti(int[] pot) {
System.out.print("Pot: " + pot[0]);
for (int i = 1; i < pot.length; i++) System.out.print(" - " + pot[i]);
System.out.println("");
double skupnaDolzina = 0.0;
double skupenCasvMin = 0.0;
for (int i = 1; i < pot.length; i++) {
Vozlisce v1 = this.getVozlisce(pot[i-1]);
Vozlisce v2 = this.getVozlisce(pot[i]);
Cesta cesta = this.getCesta(v1, v2);
skupnaDolzina += v1.distanceFrom(v2);
skupenCasvMin += v1.distanceFrom(v2) / cesta.getMaxSpeed() * 60;
}
System.out.format("Skupna dolzina: %.2f km\n", skupnaDolzina);
System.out.format("Predviden cas voznje: %.0fh %.0fmin\n", skupenCasvMin / 60, skupenCasvMin % 60);
}
}
class DN11 {
public static void main (String[] args) throws Exception {
CestnoOmrezje omrezje = CestnoOmrezje.izDatoteke(args[1]);
switch (args[0]) {
case "ceste":
System.out.println("Omrezje vsebuje naslednje ceste:");
omrezje.getCeste().forEach((Cesta c) -> System.out.println(c.toString()));
break;
case "dolzinaPoti":
int[] pot = new int[args.length - 2];
for(int i=2; i<args.length; i++) pot[i-2] = Integer.parseInt(args[i]);
omrezje.dolzinaPoti(pot);
break;
default:
break;
}
}
}
Zadnja posodobitev:
May 24, 2022