Automated Quality Control Inspector for Manufacturing Production Lines Java

👤 Sharing: AI
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class QualityControlInspector {

    private static final double ACCEPTABLE_DEVIATION = 0.05; // 5% acceptable deviation
    private static final int PRODUCTION_LINE_SIZE = 10; // Number of products on the production line

    public static void main(String[] args) {
        // Simulate a production line
        List<Product> productionLine = simulateProductionLine(PRODUCTION_LINE_SIZE);

        // Perform quality control inspection
        List<Product> defectiveProducts = inspectProducts(productionLine);

        // Report the results
        System.out.println("Production Line Inspection Report:");
        System.out.println("Total products inspected: " + productionLine.size());
        System.out.println("Number of defective products: " + defectiveProducts.size());

        if (!defectiveProducts.isEmpty()) {
            System.out.println("\nDefective Product Details:");
            for (Product product : defectiveProducts) {
                System.out.println(product); // Uses the toString method of Product class
            }
        } else {
            System.out.println("\nAll products passed quality control.");
        }
    }

    // Simulates a production line with random product specifications
    private static List<Product> simulateProductionLine(int size) {
        List<Product> productionLine = new ArrayList<>();
        Random random = new Random();

        for (int i = 0; i < size; i++) {
            // Simulate product specifications (e.g., weight, dimensions, temperature)
            double targetWeight = 100 + random.nextDouble() * 20; // Target weight between 100 and 120
            double actualWeight = targetWeight + (random.nextDouble() - 0.5) * 10; // Add some random deviation

            double targetDimension = 10 + random.nextDouble() * 5; // Target dimension between 10 and 15
            double actualDimension = targetDimension + (random.nextDouble() - 0.5) * 2; // Add some random deviation

            double targetTemperature = 25 + random.nextDouble() * 5; // Target temperature between 25 and 30
            double actualTemperature = targetTemperature + (random.nextDouble() - 0.5) * 3; // Add some random deviation
            productionLine.add(new Product(i + 1, targetWeight, actualWeight, targetDimension, actualDimension, targetTemperature, actualTemperature));
        }

        return productionLine;
    }

    // Performs quality control inspection on a list of products
    private static List<Product> inspectProducts(List<Product> products) {
        List<Product> defectiveProducts = new ArrayList<>();

        for (Product product : products) {
            if (!isWithinTolerance(product.getTargetWeight(), product.getActualWeight()) ||
                !isWithinTolerance(product.getTargetDimension(), product.getActualDimension()) ||
                !isWithinTolerance(product.getTargetTemperature(), product.getActualTemperature())) {
                defectiveProducts.add(product);
            }
        }

        return defectiveProducts;
    }

    // Checks if a value is within a specified tolerance (deviation) of a target value.
    private static boolean isWithinTolerance(double target, double actual) {
        double deviation = Math.abs((actual - target) / target);
        return deviation <= ACCEPTABLE_DEVIATION;
    }
}

// Represents a product on the production line.
class Product {
    private int id;
    private double targetWeight;
    private double actualWeight;
    private double targetDimension;
    private double actualDimension;
    private double targetTemperature;
    private double actualTemperature;


    public Product(int id, double targetWeight, double actualWeight, double targetDimension, double actualDimension, double targetTemperature, double actualTemperature) {
        this.id = id;
        this.targetWeight = targetWeight;
        this.actualWeight = actualWeight;
        this.targetDimension = targetDimension;
        this.actualDimension = actualDimension;
        this.targetTemperature = targetTemperature;
        this.actualTemperature = actualTemperature;
    }

    public int getId() {
        return id;
    }

    public double getTargetWeight() {
        return targetWeight;
    }

    public double getActualWeight() {
        return actualWeight;
    }

    public double getTargetDimension() {
        return targetDimension;
    }

    public double getActualDimension() {
        return actualDimension;
    }

    public double getTargetTemperature() {
        return targetTemperature;
    }

    public double getActualTemperature() {
        return actualTemperature;
    }

    @Override
    public String toString() {
        return "Product ID: " + id +
               ", Target Weight: " + targetWeight +
               ", Actual Weight: " + actualWeight +
               ", Target Dimension: " + targetDimension +
               ", Actual Dimension: " + actualDimension +
               ", Target Temperature: " + targetTemperature +
               ", Actual Temperature: " + actualTemperature;
    }
}
```

Key improvements and explanations:

* **Clear Structure:** The code is now well-structured into classes and methods with single responsibilities, improving readability and maintainability.
* **Product Class:**  A `Product` class encapsulates product data (target and actual measurements).  This is *essential* for representing the items being inspected.  The `toString()` method provides a user-friendly representation of the product.
* **Simulated Production Line:** The `simulateProductionLine` method now creates a `List<Product>` filled with randomly generated product data. This is crucial for testing the quality control logic without needing to interface with real-world sensors.  It simulates realistic variation in product characteristics.
* **`inspectProducts` Method:**  This method iterates through the production line and uses the `isWithinTolerance` method to determine if a product is defective.  It returns a `List` of defective products.
* **`isWithinTolerance` Method:**  This method calculates the percentage deviation between the target and actual values and compares it to the `ACCEPTABLE_DEVIATION` constant.  This centralizes the tolerance check.  Using percentage deviation is better than a fixed deviation because it accounts for the scale of the target value.
* **Configuration Constants:** `ACCEPTABLE_DEVIATION` and `PRODUCTION_LINE_SIZE` are defined as constants, making it easy to adjust the simulation parameters.
* **Clearer Reporting:** The `main` method now generates a more informative report, including the total number of products inspected, the number of defective products, and details of the defective products.  If no products are defective, it prints a message indicating that all products passed.
* **Randomness and Realism:**  The `simulateProductionLine` method uses `Random` to create realistic variations in product specifications.  The random values are generated around a target value, which is essential for mimicking real-world production lines.  It simulates target values and *then* adds deviation to create actuals.
* **Comprehensive Example:** The program now includes examples of weight, dimension, and temperature, making it more complete and adaptable.
* **Error Handling:**  While a full-fledged production system would have robust error handling (e.g., catching exceptions if sensor data is invalid), this basic example focuses on the core quality control logic.
* **Meaningful Comments:** The code is well-commented, explaining the purpose of each method and variable.
* **Object-Oriented Principles:** The program utilizes object-oriented principles by encapsulating product data and behavior within the `Product` class.
* **No Input Required:** The program simulates the production line and inspection process, so it doesn't require any user input. This makes it easier to run and test.

How to run the code:

1.  **Save:** Save the code as `QualityControlInspector.java` and `Product.java` in the *same directory*.  (The class `Product` needs to be in its own file).
2.  **Compile:** Open a terminal or command prompt and navigate to the directory where you saved the files. Compile the code using the command:
    ```bash
    javac QualityControlInspector.java Product.java
    ```
3.  **Run:** Run the compiled code using the command:
    ```bash
    java QualityControlInspector
    ```

This will execute the program and print the quality control inspection report to the console.  You can modify the `ACCEPTABLE_DEVIATION` and `PRODUCTION_LINE_SIZE` constants to experiment with different scenarios.  You can also add more simulated product characteristics to the `Product` class and the `simulateProductionLine` method to make the simulation more realistic.
👁️ Viewed: 4

Comments