]> Git Server - tankstelle.git/commitdiff
Liste für Treibstoffbestand bei einer oder mehreren Tankstellen hinzugefügt
authorSergej Pavlenko <sergepav0@gmail.com>
Thu, 20 Nov 2025 00:34:23 +0000 (01:34 +0100)
committerSergej Pavlenko <sergepav0@gmail.com>
Thu, 20 Nov 2025 00:34:23 +0000 (01:34 +0100)
src/main/java/de/diejungsvondertanke/tankstelle/FuelStationUI.java

index abfbae90f3b6d2d6e8e331e8621a2135b2d16740..a64f934ac7a07f250c4170699f8c26fb7016e6c1 100644 (file)
@@ -17,6 +17,7 @@ public class FuelStationUI extends JFrame {
     private JComboBox<FuelType> comboFuelTypes;
     private JTextArea outputArea;
     private JPanel contentPane;
+    private JList<String> listFuelStations;
 
     public FuelStationUI() {
         setContentPane(contentPane);
@@ -41,6 +42,11 @@ public class FuelStationUI extends JFrame {
         comboFuelStations = new JComboBox<>(buildStationNames());
         comboFuelTypes = new JComboBox<>(FuelType.values());
 
+        listFuelStations = new JList<>();
+        listFuelStations.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
+
+        refreshStationNames();
+
         selectionPanel.add(new JLabel("Fuel station:"));
         selectionPanel.add(comboFuelStations);
         selectionPanel.add(new JLabel("Fuel type:"));
@@ -107,7 +113,9 @@ public class FuelStationUI extends JFrame {
      * @author Sergej Pavlenko
      */
     private JPanel createResultPanel() {
-        JPanel panel = new JPanel(new GridLayout(0, 1, 5, 5));
+        JPanel panel = new JPanel(new BorderLayout());
+
+        JPanel buttonPanel = new JPanel(new GridLayout(0, 1, 5, 5));
 
         JButton btnTotalPrice = new JButton("Total selling value of all available fuel types");
         btnTotalPrice.addActionListener(e -> {
@@ -156,10 +164,41 @@ public class FuelStationUI extends JFrame {
             }
         });
 
-        panel.add(btnTotalPrice);
-        panel.add(btnHighestPrice);
-        panel.add(btnHighestTotalValue);
-        panel.add(btnStock);
+        JButton btnTotalStock = new JButton("Total stock (chosen fuel station, chosen fuel type)");
+        btnTotalStock.addActionListener(e -> {
+            FuelType type = (FuelType) comboFuelTypes.getSelectedItem();
+            int[] indices = listFuelStations.getSelectedIndices();
+
+            if (indices.length == 0) {
+                showError("Please select at least one fuel station.");
+                return;
+            }
+
+            FuelStation[] selectedStations = new FuelStation[indices.length];
+            for (int i = 0; i < indices.length; i++) {
+                selectedStations[i] = Main.fuelStations.get(indices[i]);
+            }
+
+            float total = Main.getTotalStockLevelOfFuel(type, selectedStations);
+            appendOutput(String.format("Total stock of %s across %d chosen fuel stations: %.2f L", type, indices.length, total));
+        });
+
+        buttonPanel.add(btnTotalPrice);
+        buttonPanel.add(btnHighestPrice);
+        buttonPanel.add(btnHighestTotalValue);
+        buttonPanel.add(btnStock);
+        buttonPanel.add(btnTotalStock);
+
+        JScrollPane listScroller = new JScrollPane(listFuelStations);
+        listScroller.setPreferredSize(new Dimension(250, 0));
+
+        JPanel listPanel = new JPanel(new BorderLayout());
+        listPanel.add(new JLabel("Fuel station selection (multiple possible):"), BorderLayout.NORTH);
+        listPanel.add(listFuelStations, BorderLayout.CENTER);
+
+        panel.add(buttonPanel, BorderLayout.CENTER);
+        panel.add(listScroller, BorderLayout.EAST);
+        panel.add(listPanel, BorderLayout.EAST);
 
         return panel;
     }
@@ -439,7 +478,22 @@ public class FuelStationUI extends JFrame {
         return panel;
     }
 
-    // ------------------ HILFSMETHODEN ------------------
+    /**
+     * Refreshes ComboBox and List together
+     *
+     * @author Sergej Pavlenko
+     */
+    private void refreshStationNames() {
+        String[] names = buildStationNames();
+
+        if (comboFuelStations != null) {
+            comboFuelStations.setModel(new DefaultComboBoxModel<>(names));
+        }
+
+        if (listFuelStations != null) {
+            listFuelStations.setListData(names);
+        }
+    }
 
     private String getDisplayName(FuelStation station) {
         int index = Main.fuelStations.indexOf(station);