import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.List;
+import java.io.IOException;
/**
* GUI for fuel stations
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);
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);
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.");
type, getDisplayName(station), value));
txtAmount.setText("");
}
+ refreshFuelTable();
} catch (NumberFormatException ex) {
showError("Please enter a valid number for the amount.");
} catch (ArithmeticException ex) {
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 -> {
} 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).");
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);
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
*
}
/**
- * Updates table to keep up with changes
+ * Updates overview table to keep up with changes
*
* @author Sergej Pavlenko
*/