diff --git a/src/gfn/marc/Frachtfluege.java b/src/gfn/marc/Frachtfluege.java index f82f7a2..71c294a 100644 --- a/src/gfn/marc/Frachtfluege.java +++ b/src/gfn/marc/Frachtfluege.java @@ -1,11 +1,79 @@ package gfn.marc; +import java.sql.SQLOutput; + +import static gfn.marc.Routen.holeStreckeGewicht; +import static gfn.marc.Routen.holeStreckePreis; + public class Frachtfluege { + // Route für Ausgabsort und Zielort + static String[][] tabelle = Routen.getRoute("A", "E"); + + public static void main(String[] args) { + // beste Route finden + int gewicht = 1300; + int besteRoute = findeRoute(gewicht); + + // beste Route ausgeben + System.out.print("Beste Route: "); + for (int i = 0; i < tabelle[besteRoute].length; i++) { + if (tabelle[besteRoute][i] != null) { + System.out.print(tabelle[besteRoute][i] + " "); + } + } + System.out.println(); - String[][] route = Routen.getRoute("C", "E"); } + + + // Funktion zur Berechnung der besten Route + static int findeRoute(int gewicht) { + + int[] gewichte = new int[tabelle.length]; + double[] preise = new double[tabelle.length]; + for (int i = 0; i < tabelle.length; i++) { + int gewichtMax = Integer.MAX_VALUE; + for (int j = 0; j < tabelle[i].length - 1; j++) { + String sb = tabelle[i][j]; + String se = tabelle[i][j + 1]; + + if (tabelle[i][j + 1] != null && j != tabelle[i].length - 1) { + if (holeStreckeGewicht(sb, se) < gewichtMax) { + gewichtMax = holeStreckeGewicht(sb, se); + } + gewichte[i] = gewichtMax; + } + + + + } + + for (int j = 0; j < tabelle[i].length - 1; j++) { + String sb = tabelle[i][j]; + String se = tabelle[i][j + 1]; + if (gewichte[i] >= gewicht) { + if (tabelle[i][j + 1] != null && j != tabelle[i].length - 1) { + preise[i] += gewichte[i] * holeStreckePreis(sb, se); + } + } + } + + } + + double guenstigsterPreis = Integer.MAX_VALUE; + int index = 0; + for (int i = 0; i < preise.length; i++) { + if (preise[i] < guenstigsterPreis && preise[i] != 0) { + guenstigsterPreis = preise[i]; + index = i; + } + } + return index; + } } + + diff --git a/src/gfn/marc/Routen.java b/src/gfn/marc/Routen.java index 07fa904..224dcca 100644 --- a/src/gfn/marc/Routen.java +++ b/src/gfn/marc/Routen.java @@ -6,12 +6,14 @@ 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); + // Orte mit von mir erfundenen Frachtkapazitäten und -kosten. + static Ort a = new Ort("A", 1600, 2.6); + static Ort b = new Ort("B", 1800, 2.2); + static Ort c = new Ort("C", 1300, 2.5); + static Ort d = new Ort("D", 1450, 1.9); + static Ort e = new Ort("E", 1400, 2.1); + // Verknüpfung der Orte miteinander static { a.setKnoten(b, c, d); b.setKnoten(a, c, e); @@ -19,6 +21,7 @@ public class Routen { d.setKnoten(a, e); e.setKnoten(b, d); + // Berechnung aller möglicher Routen zwischen den einzelnen Orten --> ACHTUNG! BUG! a.berechneRouten(); b.berechneRouten(); c.berechneRouten(); @@ -26,17 +29,29 @@ public class Routen { e.berechneRouten(); } - public static String[][] getRoute(String SB, String SE) { + // gibt alle möglichen Routen für eine Verbindung als zweidimensionales Array zurück + 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)); + if (ort.id.matches(sb)) { + return ort.routen.get(sb.concat(se)); } } return null; } + // gibt maximale Frachtkapazität zwischen zwei Orten zurück + public static int holeStreckeGewicht(String sb, String se) { + return Math.min(Ort.orteMap.get(sb).frachtkapazitaet, Ort.orteMap.get(se).frachtkapazitaet); + } + + // gibt Frachtkosten (pro Kilo) zwischen zwei Orten zurück + public static double holeStreckePreis(String sb, String se) { + return Ort.orteMap.get(sb).frachtkosten + Ort.orteMap.get(se).frachtkosten; + } + } +// Klasse für Orte class Ort { static ArrayList orte = new ArrayList<>(); static HashMap orteMap = new HashMap<>(); @@ -47,6 +62,7 @@ class Ort { HashMap knotenMap; HashMap routen; + // Konstruktor für Orte public Ort(String id, int frachtkapazitaet, double frachtkosten) { this.id = id; this.frachtkapazitaet = frachtkapazitaet; @@ -66,6 +82,7 @@ class Ort { this.knotenMap = knotenMap; } + // berechnet alle möglichen Routen eines Ortes zu allen anderen Orten void berechneRouten() { HashMap routen = new HashMap<>(); Ort start = this; @@ -87,7 +104,7 @@ class Ort { } else if (startKnoten.knoten.contains(ziel)) { tabelle[i][j] = startKnoten.id; tabelle[i][j + 1] = ziel.id; - i++; + i++; // TODO: Bug beheben! Einige Routen fehlen, wenn die zweite Verzweigung mehr Knoten hat als die erste. } else { for (Ort folgeKnoten1 : startKnoten.knoten) { if (folgeKnoten1.knoten.contains(ziel)) { @@ -136,7 +153,9 @@ class Ort { } } } - System.out.println("-----------------------------------"); + + // Alle berechneten Routen ausgeben + /*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++) { @@ -144,7 +163,13 @@ class Ort { System.out.print(tabelle[x][y] + " "); } } - System.out.println(); + System.out.println(); */ + + // unvollständige Routen entfernen + for (int k = 0; k < tabelle.length; k++) { + if (tabelle[k][1] == null) { + tabelle[k][0] = null; + } } } }