// editor5
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#define MAX_NAME_LENGTH 50
typedef struct Product{
    int id;
    char name[MAX_NAME_LENGTH + 1];
    int quantity;
    struct Product* next;
}Product;
Product* head = NULL;
void addProduct(int id, const char* name, int quantity){
    if(quantity < 0){
        printf("Invalid input\n");
        return;
    }
    Product* current = head;
    while(current != NULL){
        if(current->id == id){
            printf("Product with ID %d already exists. use UPDATE to change it.\n", id);
            return;
        }
        current = current->next;
    }
    Product* newProduct = (Product*)malloc(sizeof(Product));
    if(newProduct == NULL){
        printf("Memory allocation failed\n");
        return;
    }
    newProduct->id = id;
    strncpy(newProduct->name, name, MAX_NAME_LENGTH);
    newProduct->name[MAX_NAME_LENGTH] = '\0';
    newProduct->quantity = quantity;
     newProduct->next = NULL;
     
     if(head == NULL){
         head =  newProduct;
     }else{
         Product* temp = head;
         while (temp->next != NULL){
             temp = temp->next;
         }
             temp->next = newProduct;
         }
       // printf("Added\n");
     }
     void updateProduct(int id, int quantity){
         if(quantity < 0){
              printf("Invalid input\n");
              return;
         }
         Product* current = head;
         int found = 0;
         while(current != NULL){
             if(current->id == id){
                 current->quantity = quantity;
                 found = 1;
                 break;
             }
             current = current->next;
         }
         if(found){
             printf("Updated\n");
         }else{
             printf("Product not found.\n");
         }
     }
     void getProduct(int id){
         Product* current = head;
         int found = 0;
         while(current != NULL){
             if(current->id == id){
                 printf("%d %s %d\n", current->id, current->name, current->quantity);
                 found = 1;
                 break;
             }
             current = current->next;
         }
         if(!found){
             printf("Product not found.\n");
         }
     }
         void deleteProduct(int id){
             Product* current = head;
             Product* prev = NULL;
             int found = 0;
             
             while (current != NULL){
                 if(current->id == id){
                     if(prev == NULL){
                         head = current->next;
                         
                     } else{
                         prev->next = current->next;
                     }
                         free(current);
                         found = 1;
                         break;
                 }else{
                     prev = current;
                     current = current->next;
                 }
             }
             if(found){
                 printf("Deleted\n");
             }else{
                 printf("Product not found.\n");
             }
         }
         void freeList(){
             Product* temp;
             while (head != NULL){
                 temp = head;
                 head = head->next;
                 free(temp);
             }
         }
         int main(){
             char command[10];
             char name[MAX_NAME_LENGTH + 2];
             int id, quantity;
             
             while (1){
                if(scanf("%9s", command) != 1){
                    break;
                }
                 if(strcmp(command, "ADD") == 0){
                    if(scanf("%d %50s %d", &id, name, &quantity) == 3){
                     addProduct(id, name, quantity);
                 }else{
                     printf("Invalid ADD command format.\n");
                     while (getchar() != '\n');
                 }
             }else if(strcmp(command,"UPDATE") == 0){
                     if(scanf("%d %d", &id, &quantity) == 2){
                     updateProduct(id, quantity);
                     }else{
                        printf("Invalid UPDATE command format.\n");
                     while (getchar() != '\n'); 
                     }
             }else if(strcmp(command, "GET") == 0){
             if(scanf("%d", &id) == 1){
             getProduct(id);
             }else{
                        printf("Invalid GET command format.\n");
                     while (getchar() != '\n');
             }
             }else if(strcmp(command, "DELETE") == 0){
                 if(scanf("%d", &id)== 1){
                 deleteProduct(id);
                 }else{
                        printf("Invalid DELETE command format.\n");
                     while (getchar() != '\n'); 
                 }
             }else if(strcmp(command, "EXIT") == 0){
             break;
             }else{
        printf("Invalid command.\n");
        while (getchar() != '\n');
         }
             }
             freeList();
             return 0;
     }