]> Git Server - tankstelle.git/commitdiff
GUI für Unterbesetzung erstellt und Icon gewechselt
authorSergej Pavlenko <sergepav0@gmail.com>
Sat, 22 Nov 2025 19:40:11 +0000 (20:40 +0100)
committerSergej Pavlenko <sergepav0@gmail.com>
Sat, 22 Nov 2025 19:40:11 +0000 (20:40 +0100)
src/main/java/de/diejungsvondertanke/tankstelle/FuelStationUI.java
src/main/resources/Tankstelle.png [new file with mode: 0644]

index b526d64b3ab2514828504b3d22565adb51935957..2d1a7c4a08f3817b8a6a07883461ef541893a5af 100644 (file)
@@ -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("<html>When understaffed:<br>" + "Please enter a PIN and the amount of employees left.<br>" +
+                "The system will give you a combination of fuel stations that can function with a reduced amount of employees.<html>");
+
+        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 (file)
index 0000000..6a6fc55
Binary files /dev/null and b/src/main/resources/Tankstelle.png differ