From: Sergej Pavlenko Date: Sat, 22 Nov 2025 19:40:11 +0000 (+0100) Subject: GUI für Unterbesetzung erstellt und Icon gewechselt X-Git-Url: https://git.eternal.ddnss.de/?a=commitdiff_plain;h=abf5cde8d8bc5dc5962b0b6d2999b41e0cf3d837;p=tankstelle.git GUI für Unterbesetzung erstellt und Icon gewechselt --- diff --git a/src/main/java/de/diejungsvondertanke/tankstelle/FuelStationUI.java b/src/main/java/de/diejungsvondertanke/tankstelle/FuelStationUI.java index b526d64..2d1a7c4 100644 --- a/src/main/java/de/diejungsvondertanke/tankstelle/FuelStationUI.java +++ b/src/main/java/de/diejungsvondertanke/tankstelle/FuelStationUI.java @@ -6,6 +6,7 @@ import javax.swing.*; import javax.swing.table.DefaultTableModel; import java.awt.*; import java.util.List; +import java.io.IOException; /** * GUI for fuel stations @@ -24,6 +25,7 @@ public class FuelStationUI extends JFrame { public FuelStationUI() { setContentPane(contentPane); + setIconImage(Toolkit.getDefaultToolkit().getImage(Main.class.getResource("/Tankstelle.png"))); setTitle("Fuel Station Management System"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(800, 600); @@ -60,11 +62,12 @@ public class FuelStationUI extends JFrame { JTabbedPane tabbedPane = new JTabbedPane(); tabbedPane.addTab("Result", createResultPanel()); + tabbedPane.addTab("Overview", createOverviewPanel()); + tabbedPane.addTab("New fuel station", createNewFuelStationPanel()); tabbedPane.addTab("Change price", createPricePanel()); tabbedPane.addTab("Change stock", createStockPanel()); tabbedPane.addTab("Search", createSearchPanel()); - tabbedPane.addTab("New fuel station", createNewFuelStationPanel()); - tabbedPane.addTab("Overview", createOverviewPanel()); + tabbedPane.addTab("Understaffed", createUnderstaffedPanel()); add(tabbedPane, BorderLayout.CENTER); @@ -239,6 +242,7 @@ public class FuelStationUI extends JFrame { appendOutput(String.format( "Price changed of %s at %s to %.3f €/L.", type, getDisplayName(station), newPrice)); + refreshFuelTable(); txtPrice.setText(""); } catch (NumberFormatException ex) { showError("Please enter a valid number for the price."); @@ -325,6 +329,7 @@ public class FuelStationUI extends JFrame { type, getDisplayName(station), value)); txtAmount.setText(""); } + refreshFuelTable(); } catch (NumberFormatException ex) { showError("Please enter a valid number for the amount."); } catch (ArithmeticException ex) { @@ -417,7 +422,7 @@ public class FuelStationUI extends JFrame { JLabel lblAtrributes = new JLabel("Special attribute:"); JTextField txtAtrributes = new JTextField(15); - JLabel lblHint = new JLabel("Small: amount of vending machines, Medium: m^2, Large: supermarket-company"); + JLabel lblHint = new JLabel("Small: amount of vending machines, Medium: m², Large: supermarket-company"); JButton btnAdd = new JButton("Add fuel station"); btnAdd.addActionListener(e -> { @@ -429,13 +434,14 @@ public class FuelStationUI extends JFrame { } else if (rbMedium.isSelected()) { float retailSpace = Float.parseFloat(txtAtrributes.getText().replace(",", ".").trim()); Main.addNewFuelStation(retailSpace); - appendOutput("Fuel station with " + retailSpace + " m^2 retail space has been added."); + appendOutput("Fuel station with " + retailSpace + " m² retail space has been added."); } else if (rbLarge.isSelected()) { String supermarketCompany = txtAtrributes.getText().trim(); Main.addNewFuelStation(supermarketCompany); appendOutput("Fuel station with supermarket-company" + supermarketCompany + " has been added."); } refreshStationNames(); + refreshFuelTable(); txtAtrributes.setText(""); } catch (NumberFormatException ex) { showError("Please enter a valid number (for small / medium)."); @@ -503,12 +509,8 @@ public class FuelStationUI extends JFrame { JScrollPane scrollPane = new JScrollPane(tableFuelTypesPerStation); - JButton btnRefresh = new JButton("Refresh"); - btnRefresh.addActionListener(e -> refreshFuelTable()); - JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); topPanel.add(new JLabel("Overview of all fuels per fuel station")); - topPanel.add(btnRefresh); panel.add(topPanel, BorderLayout.NORTH); panel.add(scrollPane, BorderLayout.CENTER); @@ -518,6 +520,124 @@ public class FuelStationUI extends JFrame { return panel; } + /** + * Create Panel for condition understaffed + * + * @author Sergej Pavlenko + */ + public JPanel createUnderstaffedPanel() { + JPanel panel = new JPanel(new GridBagLayout()); + + GridBagConstraints gbc = new GridBagConstraints(); + gbc.insets = new Insets(5, 5, 5, 5); + gbc.fill = GridBagConstraints.HORIZONTAL; + gbc.anchor = GridBagConstraints.WEST; + + JLabel lblUnderstaffedInfo = new JLabel("When understaffed:
" + "Please enter a PIN and the amount of employees left.
" + + "The system will give you a combination of fuel stations that can function with a reduced amount of employees."); + + JLabel lblPIN = new JLabel("PIN:"); + JPasswordField txtPin = new JPasswordField(10); + + JLabel lblEmployees = new JLabel("Available employees:"); + JTextField txtEmployees = new JTextField(10); + JButton btnUnderstaffed = new JButton("Enable understaffed"); + + btnUnderstaffed.addActionListener(e -> { + String pin = new String(txtPin.getPassword()).trim(); + String employees = txtEmployees.getText().trim(); + + if (pin.isEmpty()) { + showError("Please enter a PIN."); + return; + } + + if (employees.isEmpty()) { + showError("Please enter the available amount of employees."); + return; + } + + short availableEmployees; + try { + availableEmployees = Short.parseShort(employees); + } catch (NumberFormatException ex) { + showError("Please enter a valid amount of employees."); + return; + } + + int totalEmployees = Main.getTotalNumberOfEmployees(); + if (availableEmployees >= totalEmployees) { + showError("Please enter an amount of employees that is less than the total number of employees."); + return; + } + + if (availableEmployees <= 0) { + showError("The amount of employees must be greater than 0."); + return; + } + + try { + boolean ok = Main.isPINCorrect(pin); + if (!ok) { + showError("PIN is incorrect. Access denied."); + } + } catch (IOException ex) { + showError("Error trying to check the PIN: " + ex.getMessage()); + return; + } + + FuelStation[] possibleFuelStations = Main.getFuelStationListWhenUnderstaffed(availableEmployees); + if (possibleFuelStations.length == 0) { + appendOutput("No fuel stations can function with the available amount of employees."); + return; + } + + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("Understaffed active.\n"); + stringBuilder.append("Total number of employees: " + totalEmployees + "\n"); + stringBuilder.append("Available employees: " + availableEmployees + "\n"); + stringBuilder.append("Fuel stations that can function:\n"); + + for (FuelStation fuelStation : possibleFuelStations) { + int staff = fuelStation.getNumber_of_employees(); + stringBuilder.append(" - " + getDisplayName(fuelStation) + " (" + staff + " employees, fuel station size: " + + fuelStation.getClass().getSimpleName() + ")\n"); + } + + appendOutput(stringBuilder.toString()); + + txtPin.setText(""); + txtEmployees.setText(""); + }); + + int y = 0; + gbc.gridx = 0; gbc.gridy = y; + gbc.gridwidth = 2; + panel.add(lblUnderstaffedInfo, gbc); + y++; + gbc.gridx = 0; gbc.gridy = y; + gbc.gridwidth = 1; + panel.add(lblPIN, gbc); + gbc.gridx = 1; + panel.add(txtPin, gbc); + y++; + gbc.gridx = 0; gbc.gridy = y; + panel.add(lblEmployees, gbc); + gbc.gridx = 1; + panel.add(txtEmployees, gbc); + y++; + gbc.gridx = 0; gbc.gridy = y; + panel.add(lblEmployees, gbc); + gbc.gridx = 1; + panel.add(txtEmployees, gbc); + y++; + gbc.gridx = 0; gbc.gridy = y; + gbc.gridwidth = 2; + panel.add(btnUnderstaffed, gbc); + + return panel; + } + /** * Updates the selection of fuel stations for ComboBox and List * @@ -532,7 +652,7 @@ public class FuelStationUI extends JFrame { } /** - * Updates table to keep up with changes + * Updates overview table to keep up with changes * * @author Sergej Pavlenko */ diff --git a/src/main/resources/Tankstelle.png b/src/main/resources/Tankstelle.png new file mode 100644 index 0000000..6a6fc55 Binary files /dev/null and b/src/main/resources/Tankstelle.png differ