Thursday, January 24, 2013

C Programming



#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<dos.h>
#include<stdlib.h>

struct inventory{
char productCode[15];
int items;
};

int ctr=0;

int search(inventory product[],char *searchCode)
{
                for(int i=0;i<ctr;i++)
                                if(strnicmp(searchCode,product[i].productCode,20)==0)
                                return i;

                return -1;
}

void add(inventory product[])
{
                char tempCode[15];
                int tag;

                system("cls");
                printf("Enter New Product Code: ");
                gets(tempCode);
                tag=(search(product,tempCode));
                if(tag<0)
                {
                                strcpy(product[ctr].productCode,tempCode);
                                product[ctr].items=0;
                                ctr++;
                                printf("New Product Saved...");
                                getch();
                                getch();
                }
                else
                {
                                printf("Product Code Already Exist!!!");
                                getch();
                }
}

void edit(inventory product[])
{
                char editRecord[15];
                int tagEdit,tag;

                system("cls");
                printf("Enter Product Code that you want to edit: ");
                gets(editRecord);
                tagEdit=(search(product,editRecord));

                if(tagEdit>=0)
                {
                                system("cls");
                                printf("Product: %s\n",product[tagEdit].productCode);
                                printf("Items:   %d\n\n",product[tagEdit].items);

                                printf("Enter New Product Code: ");
                                gets(editRecord);
                                tag=(search(product,editRecord));
                                if(tag<0)
                                {
                                                strcpy(product[tagEdit].productCode,editRecord);
                                                printf("\n\nProduct Updated...");
                                                getch();
                                                getch();
                                }
                                else
                                {
                                                printf("Product Code Already Exist!!!");
                                                getch();
                                }
                }

                else
                {
                                printf("Product Code Do Not Exist!!!");
                                getch();
                }
}

void delContinue(inventory product[],int tagDel)
{
                system("cls");
                if(tagDel==ctr-1)
                {
                                printf("Product: %s\n",product[tagDel].productCode);
                                printf("Items:   %d\n\n",product[tagDel].items);

                                printf("Product Deleted...");
                                ctr--;
                                getch();
                }
                else
                {
                                system("cls");
                                printf("Product: %s\n",product[tagDel].productCode);
                                printf("Items:   %d\n\n",product[tagDel].items);

                                strcpy(product[tagDel].productCode,product[ctr-1].productCode);
                                product[tagDel].items=product[ctr-1].items;

                                printf("Product Deleted...");
                                ctr--;
                                getch();
                }
}

void deleteProduct(inventory product[])
{
                char delRecord[10];
                char choice;
                int tagDel;

                system("cls");
                printf("Enter Product Code that you want to delete: ");
                gets(delRecord);
                tagDel=search(product,delRecord);

                if(tagDel>=0)
                {
                                if(product[tagDel].items>0)
                                {
                                                printf("\n\nProduct Have Items!!!\n");
                                                printf("Do you want to continue? [y][n]: ");
                                                scanf("%c",&choice);
                                                if(choice=='y' || choice=='Y')
                                                {
                                                                delContinue(product,tagDel);
                                                }
                                }
                                else
                                {
                                                delContinue(product,tagDel);
                                }
                }
                else
                {
                                printf("Product Code Do Not Exist!!!");
                                getch();
                }

}

void displayProducts(inventory product[])
{

    int i,j,tempItem;
    char tempCode[15];

    if(ctr>0)
    {
                system("cls");
                printf("Product Code\t\tItems\n");
                printf("--------------------------------\n\n");

                                for(i=0;i<ctr;i++)
                                {
                                                for(j=i;j<ctr;j++)
                                                {
                                                                if(strcmp(product[i].productCode,product[j].productCode)>0)
                                                                {
                                                                                strcpy(tempCode,product[i].productCode);
                                                                                strcpy(product[i].productCode,product[j].productCode);
                                                                                strcpy(product[j].productCode,tempCode);

                                                                                tempItem=product[i].items;
                                                                                product[i].items=product[j].items;
                                                                                product[j].items=tempItem;
                                                                }
                                                }
                                                printf("%s\n",product[i].productCode);
                                                printf("%25d\n",product[i].items);
                                }
                                getch();
    }
    else
    {
       system("cls");
       printf("There is no product!!!");
       getch();
    }
    getch();
}

void addItems(inventory product[])
{
                char tempCode[15];
                int tagAdd;

                system("cls");
                printf("Enter Product Code that you want to add items: ");
                gets(tempCode);
                tagAdd=(search(product,tempCode));

                if(tagAdd>=0)
                {
                                system("cls");
                                product[tagAdd].items++;
                                printf("Product: %s\n",product[tagAdd].productCode);
                                printf("Items:   %d\n\n",product[tagAdd].items);

                                printf("Product Updated!!!");
                                getch();
                }
                else
                {
                                printf("Product Code Do Not Exist!!!");
                                getch();
                }
}

void deleteItems(inventory product[])
{
                char tempCode[15];
                int tagDel;

                system("cls");
                printf("Enter Product Code that you want to add items: ");
                gets(tempCode);
                tagDel=(search(product,tempCode));

                if(tagDel>=0)
                {
                                if(product[tagDel].items>0)
                                {
                                                system("cls");
                                                product[tagDel].items--;
                                                printf("Product: %s\n",product[tagDel].productCode);
                                                printf("Items:   %d\n\n",product[tagDel].items);

                                                printf("Product Updated!!!");
                                                getch();
                                }
                                else
                                {
                                                printf("This Product has no item yet!!!");
                                                getch();
                                }
                }
                else
                {
                                printf("Product Code Do Not Exist!!!");
                                getch();
                }
}

void accessProduct(inventory product[])
{
                char choice;
                system("cls");

                printf("<<Access Products>>\n\n");
                printf("[1]Display Products\n");
                printf("[2]Add Items\n");
                printf("[3]Delete Items\n");
                printf("Choice: ");
                choice=getche();
                switch(choice)
                {
                                case '1': displayProducts(product);break;
                                case '2': addItems(product);break;
                                case '3': deleteItems(product);break;
                                default: printf("\n\nInvalid Selection!!!");getch();break;
                }
}

main()
{
                char choice;
                char user[20];
                inventory product[100];

                system("cls");
                printf("Enter your name: ");
                gets(user);
                do
                {
                                system("cls");
                                printf("<<Basic Inventory System>>\n\n");
                                printf("[1]Add Product\n");
                                printf("[2]Edit Product\n");
                                printf("[3]Delete Product\n");
                                printf("[4]Access Products\n");
                                printf("[5]Exit\n");
                                printf("Choice: ");
                                choice=getche();
                                switch(choice)
                                {
                                                case '1': add(product);break;
                                                case '2': edit(product);break;
                                                case '3': deleteProduct(product);break;
                                                case '4': accessProduct(product);break;
                                                case '5': system("cls");printf("Thank you so much %s, for using my system!!!",user);getch();break;
                                                default: printf("\n\nInvalid Selection!!!");getch();break;
                                }
                }while(choice!='5');
}

No comments:

Post a Comment