frachtfluege/src/gfn/marc/Routen.java

158 lines
6.5 KiB
Java

package gfn.marc;
// Hilfsklasse zur Simulation der Aufgabenstellung
import java.util.*;
public class Routen {
static Ort a = new Ort("A", 1600, 5.6);
static Ort b = new Ort("B", 1800, 5.2);
static Ort c = new Ort("C", 1300, 5.5);
static Ort d = new Ort("D", 1450, 4.9);
static Ort e = new Ort("E", 1400, 5.1);
static {
a.setKnoten(b, c, d);
b.setKnoten(a, c, e);
c.setKnoten(a, b);
d.setKnoten(a, e);
e.setKnoten(b, d);
a.berechneRouten();
b.berechneRouten();
c.berechneRouten();
d.berechneRouten();
e.berechneRouten();
}
public static String[][] getRoute(String SB, String SE) {
for (Ort ort : Ort.orte) {
if (ort.id.matches(SB)) {
return ort.routen.get(SB.concat(SE));
}
}
return null;
}
}
class Ort {
static ArrayList<Ort> orte = new ArrayList<>();
static HashMap<String, Ort> orteMap = new HashMap<>();
String id;
int frachtkapazitaet;
double frachtkosten; // EUR/kg
ArrayList<Ort> knoten;
HashMap<String, Ort> knotenMap;
HashMap<String, String[][]> routen;
public Ort(String id, int frachtkapazitaet, double frachtkosten) {
this.id = id;
this.frachtkapazitaet = frachtkapazitaet;
this.frachtkosten = frachtkosten;
Ort.orte.add(this);
Ort.orteMap.put(this.id, this);
}
public void setKnoten(Ort... knotenArray) {
ArrayList<Ort> knoten = new ArrayList<>();
HashMap<String, Ort> knotenMap = new HashMap<>();
for (Ort k : knotenArray) {
knoten.add(k);
knotenMap.put(k.id, k);
}
this.knoten = knoten;
this.knotenMap = knotenMap;
}
void berechneRouten() {
HashMap<String, String[][]> routen = new HashMap<>();
Ort start = this;
for (Ort ziel : orte) {
if (!start.id.equals(ziel.id)) {
routen.put(start.id.concat(ziel.id), new String[orte.size()][orte.size() + 1]);
String[][] tabelle = routen.get(start.id.concat(ziel.id));
for (int i = 0; i < tabelle.length; i++) {
tabelle[i][0] = start.id;
}
int i = 0;
int j = 1;
for (Ort startKnoten : start.knoten) {
if (startKnoten.id.equals(ziel.id)) {
tabelle[i][j] = ziel.id;
i++;
} else if (startKnoten.knoten.contains(ziel)) {
tabelle[i][j] = startKnoten.id;
tabelle[i][j + 1] = ziel.id;
i++;
} else {
for (Ort folgeKnoten1 : startKnoten.knoten) {
if (folgeKnoten1.knoten.contains(ziel)) {
if (folgeKnoten1.id.equals(startKnoten.id)) continue;
if (folgeKnoten1.id.equals(start.id)) continue;
tabelle[i][j] = startKnoten.id;
tabelle[i][j + 1] = folgeKnoten1.id;
tabelle[i][j + 2] = ziel.id;
i++;
} else {
for (Ort folgeKnoten2 : folgeKnoten1.knoten) {
if (folgeKnoten2.knoten.contains(ziel)) {
if (folgeKnoten2.id.equals(folgeKnoten1.id) ||
folgeKnoten2.id.equals(startKnoten.id) ||
folgeKnoten2.id.equals(start.id) ||
folgeKnoten1.id.equals(startKnoten.id) ||
folgeKnoten1.id.equals(start.id)) continue;
tabelle[i][j] = startKnoten.id;
tabelle[i][j + 1] = folgeKnoten1.id;
tabelle[i][j + 2] = folgeKnoten2.id;
tabelle[i][j + 3] = ziel.id;
i++;
} else {
for (Ort folgeKnoten3 : folgeKnoten2.knoten) {
if (folgeKnoten3.knoten.contains(ziel)) {
if (folgeKnoten3.id.equals(folgeKnoten2.id) ||
folgeKnoten3.id.equals(folgeKnoten1.id) ||
folgeKnoten3.id.equals(startKnoten.id) ||
folgeKnoten3.id.equals(start.id) ||
folgeKnoten2.id.equals(folgeKnoten1.id) ||
folgeKnoten2.id.equals(startKnoten.id) ||
folgeKnoten2.id.equals(start.id) ||
folgeKnoten1.id.equals(startKnoten.id) ||
folgeKnoten1.id.equals(start.id)) continue;
tabelle[i][j] = startKnoten.id;
tabelle[i][j + 1] = folgeKnoten1.id;
tabelle[i][j + 2] = folgeKnoten2.id;
tabelle[i][j + 3] = folgeKnoten3.id;
tabelle[i][j + 4] = ziel.id;
i++;
}
}
}
}
}
}
}
}
System.out.println("-----------------------------------");
System.out.println("Route: " + start.id.concat(ziel.id));
for (int x = 0; x < tabelle.length; x++) {
for (int y = 0; y < tabelle[x].length; y++) {
if (tabelle[x][y] != null) {
System.out.print(tabelle[x][y] + " ");
}
}
System.out.println();
}
}
}
this.routen = routen;
}
}