Manage warehouse inventory in C using structures to add products, update quantities, and print inventory with error handling and simple code.
#include <stdio.h>
#include <string.h>
#define INVENTARIO_MAX 100
#define NOME_MAX_LEN 50
// Structure representing a product in the warehouse
typedef struct {
char nome[NOME_MAX_LEN]; // Product name
int codice; // Unique identification code
int quantita; // Available quantity
} Prodotto;
// Array to hold the products
Prodotto inventario[INVENTARIO_MAX];
int num_prodotti = 0; // Current number of products in the inventory
// Function to add a new product to the inventory
// Returns 1 if added successfully, 0 if inventory is full
int aggiungiProdotto(const char* nome, int codice, int quantita) {
if (num_prodotti >= INVENTARIO_MAX) {
printf("Error: inventory full, cannot add more products.\n");
return 0;
}
// Check if the code already exists
for (int i = 0; i < num_prodotti; i++) {
if (inventario[i].codice == codice) {
printf("Error: product with code %d already exists.\n", codice);
return 0;
}
}
// Add the product
strncpy(inventario[num_prodotti].nome, nome, NOME_MAX_LEN - 1);
inventario[num_prodotti].nome[NOME_MAX_LEN - 1] = '\0'; // null terminator safety
inventario[num_prodotti].codice = codice;
inventario[num_prodotti].quantita = quantita;
num_prodotti++;
return 1;
}
// Function to update the quantity of a product given its code
// delta can be positive (addition) or negative (removal)
// Returns 1 if updated successfully, 0 if product not found or negative quantity
int aggiornaQuantita(int codice, int delta) {
for (int i = 0; i < num_prodotti; i++) {
if (inventario[i].codice == codice) {
int nuova_quantita = inventario[i].quantita + delta;
if (nuova_quantita < 0) {
printf("Error: negative quantity not allowed for code %d.\n", codice);
return 0;
}
inventario[i].quantita = nuova_quantita;
return 1;
}
}
printf("Error: product with code %d not found.\n", codice);
return 0;
}
// Function to print the entire inventory
void stampaInventario() {
printf("\nCurrent inventory (%d products):\n", num_prodotti);
printf("%-10s %-20s %-10s\n", "Code", "Name", "Quantity");
printf("-------------------------------------------\n");
for (int i = 0; i < num_prodotti; i++) {
printf("%-10d %-20s %-10d\n", inventario[i].codice, inventario[i].nome, inventario[i].quantita);
}
printf("-------------------------------------------\n");
}
// Example main function
int main() {
// Adding products
aggiungiProdotto("Vite M3", 101, 500);
aggiungiProdotto("Bullone M6", 102, 300);
aggiungiProdotto("Rondella", 103, 1000);
// Print initial inventory
stampaInventario();
// Update quantities (remove 50 screws)
aggiornaQuantita(101, -50);
// Add 200 bolts
aggiornaQuantita(102, 200);
// Attempt to remove invalid quantity
aggiornaQuantita(103, -1500); // should fail
// Print updated inventory
stampaInventario();
return 0;
}'Prodotto' structure containing name, code, and quantity
Array of structures to store the inventory
Functions to add a product, update quantity, and print inventory
Configuration: maximum inventory size defined by INVENTARIO_MAX
No external dependencies, standard C code
Arduino: not applicable (generic C code for console)
Compilation: gcc or other standard C compiler