#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAX_PRODUCS 100;

typedef struct {
    int id;
    char name[50];
    int quantity;
} Product;

Product products[MAX_PRODUCTS];
int productCount = 0;

void addProduct(int id, char *name, int quantity) {
    if (quantity < 0) {
        printf("Invalid input\n");
        return;
    }
    for (int i = 0; i < productCount; i++) {
        if (products[i].id == id) {
            printf("Product with ID %d already exists.\n", id);
            return;
        }
    }
    if (productCount < MAX_PRODUCTS) {
        products[productCount].id = id;
        strcpy(products[productCount].name, name);
        products[productCount].quantity = quantity;
        productCount++;
        
    }
}

void updateStock(int id, int quantity) {
    if (quantity < 0) {
        printf("Invalid input\n");
        return;
    }
    for (int i = 0; i < productCount; i++) {
        if (products[i].id == id) {
            products[i].quantity = quantity;
            printf("Updated\n");
            return;
        }
    }
    printf("Product not found.\n");
}

void getProduct(int id) {
    for (int i = 0; i < productCount; i++) {
        if (products[i].id == id) {
            printf("%d %s %d\n", products[i].id, products[i].name, products[i].quantity);
            return;
        }
    }
    printf("Product not found.\n");
}

void deleteProduct(int id) {
    for (int i = 0; i < productCount; i++) {
        if (products[i].id == id) {
            for (int j = i; j < productCount - 1; j++) {
                products[j] = products[j + 1];
            }
            productCount--;
            printf("Deleted\n");
            return;
        }
    }
    printf("Product not found.\n");
}

int main() {
    char command[50];
    while (1){
        printf("> ");
        fgets(command, sizeof(command), stdin);
        command[strcspn(command, "\n")] =0;
        
        char *token = strtok(command, " ");
        if (strcmp(token, "ADD") == 0) {
            int id = atoi(strtok(NULL, " "));
            char *name = strtok(NULL, " ");
            int quantity = atoi(strtok(NULL, " "));
            addProduct(id, name, quantity);
        } else if (strcmp(token, "UPDATE") == 0) {
            int id = atoi(strtok(NULL, " "));
            int quantity = atoi(strtok(NULL, " "));
            updateStock(id, quantity);
        } else if (strcmp(token, "GET") == 0) {
            int id = atoi(strtok(NULL, " "));
            getProduct(id);
        } else if (strcmp(token, "DELETE") == 0) {
            int id = atoi(strtok(NULL, " "));
            deleteProduct(id);
        } else if (strcmp(token, "EXIT") == 0) {
            break;
        } else {
            printf("Invalid command.\n");
        }
    }
    return 0;
}