private JComboBox<FuelType> comboFuelTypes;
private JTextArea outputArea;
private JPanel contentPane;
+ private JList<String> listFuelStations;
public FuelStationUI() {
setContentPane(contentPane);
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:"));
* @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 -> {
}
});
- 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;
}
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);