Warehouse Inventory Management with Structures and Quantity Updates

Manage warehouse inventory in C using structures to add products, update quantities, and print inventory with error handling and simple code.

Brief Introduction

Warehouse Inventory Management with Structures and Quantity Updates (c)
DESCRIPTION:
This example shows how to manage a warehouse inventory using structures in C. You can add products, update quantities, and print the inventory status. The code is simple yet robust, suitable for those who want to learn how to manipulate arrays of structures and update functions.

ADDITIONAL NOTES:
The code is written in standard C, without external dependencies. It can be compiled with any modern C compiler. The inventory is limited to 100 products for simplicity but can be changed by modifying INVENTARIO_MAX. The functions handle common errors such as duplicate codes or negative quantities.

EXPLANATIONS:
- The 'Prodotto' structure contains essential data for each item: name, code, and quantity.
- The 'inventario' array stores all products, while 'num_prodotti' keeps track of how many have been added.
- The 'aggiungiProdotto' function checks that capacity is not exceeded and that the code is unique before adding a new product.
- The 'aggiornaQuantita' function modifies the quantity of an existing product, ensuring it does not become negative.
- The 'stampaInventario' function displays all products with their details in a tabular format.
- The main demonstrates the use of the functions with examples of adding and updating, including error cases.

Example Code: c

#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;
}
×
×
×
Do you want to regenerate the answer?
×
Export chat
Choose export format
Generazione PDF in corso…
Allegati
×
You are about to close the chat and switch to the image generator. If you are not logged in, you will lose our chat. Do you confirm?
You are viewing a shared chat in temporary mode. It will not be saved.
×
Saved prompts
×
Private note
×
Label
×
Search all chats
×
Your insights
Analyzing…
×
AI Preferences
Assistant name 30
Answer length
AI model
Structured answers
Include examples
Use emojis
Guide me, don't solve Hints and questions instead of a ready answer
More settings →
×
Share this chat
Anyone opening this link can view the chat or add it to their profile as their own chat.
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
Shared chat
Someone shared a chat with you. Do you want to just view it or add it to your chats?
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
×

Saved messages

Loading...

×

Chat History

example-programs · CHAT HISTORY

Loading...

AI Preferences

×
  • BasicQuick and essential answers for study
  • MediumHigher quality for study and programming
  • AdvancedComplex reasoning and detailed analysis
Explain Steps

What do you need?

'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

Similar Topics

Counting Characters in a String Without Using strlen
Learn how to count characters in a C string manually without using strlen by iterating until the terminator for better understanding of string handling.
Program that reads a character and displays its ASCII code
This C program reads a single character from the user and displays its ASCII code using getchar and printf functions in a simple beginner-friendly way.
Reading and Printing an Array of 5 Integers
C program that reads and prints an array of 5 integers using loops and arrays, ideal for beginners learning basic input output in C programming.
Conversion of minutes into hours and minutes
C program that converts user input minutes into hours and minutes with input validation using standard stdio library for clear output and error handling.
Calculation of the average grade of a student with 4 subjects
C program to read four subject grades, validate inputs between and 1, calculate the average, and display it with two decimal precision.
Conversion of a Number of Days into Years, Weeks, and Days
C program that converts an integer number of days into years weeks and days using integer division and modulo with input validation and console output.
Program that replaces spaces with underscores in a string
This C program replaces spaces with underscores in a string using a modular function. It demonstrates safe input handling and simple string manipulation techniq
Simple calculator with switch ( +, -, *, / )
Simple C console calculator program using switch for operations plus minus multiply divide with input validation and division by zero handling.