From c34d9a797a499e232a0b5ee075ad6e38958bd89a Mon Sep 17 00:00:00 2001 From: Sergej Pavlenko Date: Thu, 20 Nov 2025 01:34:23 +0100 Subject: [PATCH] =?utf8?q?Liste=20f=C3=BCr=20Treibstoffbestand=20bei=20ein?= =?utf8?q?er=20oder=20mehreren=20Tankstellen=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../tankstelle/FuelStationUI.java | 66 +++++++++++++++++-- 1 file changed, 60 insertions(+), 6 deletions(-) diff --git a/src/main/java/de/diejungsvondertanke/tankstelle/FuelStationUI.java b/src/main/java/de/diejungsvondertanke/tankstelle/FuelStationUI.java index abfbae9..a64f934 100644 --- a/src/main/java/de/diejungsvondertanke/tankstelle/FuelStationUI.java +++ b/src/main/java/de/diejungsvondertanke/tankstelle/FuelStationUI.java @@ -17,6 +17,7 @@ public class FuelStationUI extends JFrame { private JComboBox comboFuelTypes; private JTextArea outputArea; private JPanel contentPane; + private JList 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); -- 2.43.0