Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- //define constant for item per page in display()
- #define ITEM_PER_PAGE 10
- typedef struct{
- char barcode[5];
- char name[20];
- int quantity;
- char price[10];
- char category[10];
- }ITEM;
- int main();
- void menu();
- void add();
- void ded();
- void display();
- void search();
- void update();
- void login();
- void error();
- void login1();
- void error2();
- void login2();
- void error2();
- void login3();
- //add new item
- void add() {
- system("cls");
- ITEM item;
- FILE *ptr;
- ptr = fopen("sample1.txt", "a");
- //file checking. Check if file existed or not
- if (!ptr) {
- printf("ERROR OPENING FILE\n");
- exit(-1);
- }
- printf("\n\n\n\t\tEnter Barcode: ");
- scanf(" %[^\n]", &item.barcode);
- printf("\t\tEnter Item name: ");
- scanf(" %[^\n]", &item.name);
- printf("\t\tEnter Quantity: ");
- scanf(" %d", &item.quantity);
- printf("\t\tEnter Price(Php): ");
- scanf(" %[^\n]", &item.price);
- printf("\t\tEnter Category: ");
- scanf(" %[^\n]", &item.category);
- fprintf(ptr,"\n%s|%s|%d|%s|%s\n",
- item.barcode, item.name, item.quantity, item.price, item.category);
- fclose(ptr);
- printf("\n\t\tItem Successfully Added!\n");
- system("pause");
- }
- //search for item's information by using barcode
- void search()
- {
- char userInputSearch[20];
- int i = 0, arrCount= 0, existed = 0;
- FILE *ptr;
- ITEM item[1000];
- ptr = fopen("sample1.txt", "r");
- //file checking. Check if file existed or not
- if(!ptr){
- printf("ERROR OPENING FILE");
- exit(-1);
- }
- //read the file content and store inside structure array
- i = 0;
- while (!feof(ptr))
- {
- fscanf(ptr ,"%[^|]|%[^|]|%d|%[^|]|%[^\n]\n"
- ,&item[i].barcode
- ,&item[i].name
- ,&item[i].quantity
- ,&item[i].price, &item[i].category);
- i++;
- arrCount++;
- }
- fclose(ptr);
- printf("\n\n\tPlease enter barcode: ");
- scanf(" %[^\n]", &userInputSearch);
- printf("\tSEARCH RESULT\n");
- printf("\n\n\n\tฺฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฟ\n");
- printf("\tณ%-7s ณ%-20s ณ%-8s ณ%-7s ณ%-10sณ\n"
- ,"BARCODE"
- ,"NAME"
- ,"QUANTITY"
- ,"PRICE"
- ,"CATEGORY");
- printf("\tณฤฤฤฤฤฤฤฤณฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤณฤฤฤฤฤฤฤฤฤณฤฤฤฤฤฤฤฤณฤฤฤฤฤฤฤฤฤฤณ\n");
- for(i = 0; i < arrCount; i++){
- if(strcmp(userInputSearch, item[i].barcode) == 0){
- printf("\tณ%-7s ณ%-20s ณ%-8d ณ%-7s ณ%-10sณ\n"
- ,item[i].barcode
- ,item[i].name
- ,item[i].quantity
- ,item[i].price, item[i].category);
- existed = 1;
- }
- }
- if(!existed){
- printf("%-62s\n", "NO RESULT");
- }
- printf("\tภฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤู\n");
- printf("\n\n\n");
- system("\t\tpause");
- }
- //display all the items information
- void display(){
- ITEM item;
- int i = 0, count = 0, page = 1; //count for item count
- FILE *ptr;
- ptr = fopen("sample1.txt", "r");
- //file checking. Check file exist or not
- if(!ptr){
- printf("ERROR OPENING FILE");
- exit(-1);
- }
- printf("\n\n\n\n\tPAGE %d\n", page);
- printf("\tฺฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฟ\n");
- printf("\tณ%-7s ณ%-20s ณ%-8s ณ%-7s ณ%-10sณ\n"
- ,"BARCODE"
- ,"NAME"
- ,"QUANTITY"
- ,"PRICE"
- ,"CATEGORY");
- printf("\tภฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤู\n");
- while(!feof(ptr)){
- if (count == ITEM_PER_PAGE) {
- count = 0; //reset the value of count
- //output end of line
- printf("\tภฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤู\n");
- //pause for user to enter any key to continue for next page
- system("pause");
- system("cls");
- page++;
- //output page number
- printf("\n\n\n\n\tPAGE %d\n", page);
- printf("\tฺฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฟ\n");
- printf("\tณ%-7s ณ%-20s ณ%-8s ณ%-7s ณ%-10sณ\n"
- , "BARCODE"
- , "NAME"
- , "QUANTITY"
- , "PRICE"
- , "CATEGORY");
- printf("\tภฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤู\n");
- }
- fscanf(ptr ,"%[^|]|%[^|]|%d|%[^|]|%[^\n]\n"
- ,&item.barcode
- ,&item.name
- ,&item.quantity
- ,&item.price, &item.category);
- printf("\tณ%-7s ณ%-20s ณ%-8d ณ%-7s ณ%-10sณ\n"
- ,item.barcode
- ,item.name
- ,item.quantity
- ,item.price, item.category);
- count++;
- }
- printf("\tภฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤู\n");
- fclose(ptr);
- system("\t\tpause");
- }
- //update the item's quantity (adding)
- void update() {
- char userInputBarcode[5];
- int i = 0, arrCount = 0, quantity = 0;
- ITEM item[1000];
- FILE *ptr;
- ptr = fopen("sample1.txt", "r");
- //file checking.Check if file existed or not
- if (!ptr) {
- printf("ERROR OPENING FILE\n");
- exit(-1);
- }
- //read the content of the file and store the content to the structure array
- while (!feof(ptr))
- {
- fscanf(ptr, "%[^|]|%[^|]|%d|%[^|]|%[^\n]\n",
- &item[i].barcode, &item[i].name,
- &item[i].quantity, &item[i].price, &item[i].category);
- i++;
- arrCount++;
- }
- fclose(ptr);
- printf("\n\n\n\t\tEnter barcode: ");
- scanf(" %[^\n]", &userInputBarcode);
- //read the content of the structure array
- for (i = 0; i<arrCount; i++) {
- //compare user input barcode with structure barcode
- //if is true then minus the quantity
- if (strcmp(userInputBarcode, item[i].barcode) == 0) {
- //check if user input quantity is valid or not
- //if is not valid, it will loop for user to reenter the value
- do {
- printf("\n\t\t\tEnter Quantity you wanted: ");
- scanf(" %d", &quantity);
- } while (quantity <= 0); //to prevent user to enter negative value or 0
- printf("\t\t\tItem Quantity Updated! \n\n\n\n");
- system("pause");
- item[i].quantity = item[i].quantity + quantity;
- break;
- }
- }
- fclose(ptr);
- //clear the text file
- ptr = fopen("sample1.txt", "w");
- fclose(ptr);
- ptr = fopen("sample1.txt", "a");
- //write the text file (updated content)
- for (i = 0; i<arrCount; i++) {
- fprintf(ptr, "%s|%s|%d|%s|%s\n",
- item[i].barcode, item[i].name,
- item[i].quantity, item[i].price, item[i].category);
- }
- fclose(ptr);
- }
- //deduct item's quantity
- void ded()
- {
- char userInputBarcode[5];
- int i=0, arrCount=0, quantity=0;
- ITEM item[1000];
- FILE *ptr;
- ptr = fopen("sample1.txt","r");
- //file checking.Check if file existed or not
- if(!ptr) {
- printf("ERROR OPENING FILE\n");
- exit(-1);
- }
- //read the content of the file and store the content to the structure array
- while (!feof(ptr))
- {
- fscanf(ptr,"%[^|]|%[^|]|%d|%[^|]|%[^\n]\n",
- &item[i].barcode,&item[i].name,
- &item[i].quantity,&item[i].price,&item[i].category);
- i++;
- arrCount++;
- }
- fclose(ptr);
- printf("\n\t\tEnter barcode: ");
- scanf(" %[^\n]", &userInputBarcode);
- //read the content of the structure array
- for (i=0;i<arrCount;i++){
- //compare user input barcode with structure barcode
- //if is true then minus the quantity
- if(strcmp(userInputBarcode, item[i].barcode) == 0){
- //check if user input quantity is valid or not
- //if is not valid, it will loop for user to reenter the value
- do{
- printf("\n\t\tEnter Quantity you wanted: ");
- scanf(" %d",&quantity);
- }while(quantity > item[i].quantity || quantity <= 0); //to prevent user for enter value bigger than item's quantity and negative value
- printf("Item's Quantity Updated!\n");
- system("\t\tpause");
- item[i].quantity=item[i].quantity-quantity;
- break;
- }
- }
- fclose(ptr);
- //clear the text file
- ptr = fopen("sample1.txt","w");
- fclose(ptr);
- ptr = fopen("sample1.txt","a");
- //write the text file (updated content)
- for (i=0;i<arrCount;i++){
- fprintf(ptr,"%s|%s|%d|%s|%s\n",
- item[i].barcode,item[i].name,
- item[i].quantity,item[i].price,item[i].category);
- }
- fclose(ptr);
- }
- void del()
- {
- char userInputBarcode[5];
- int i = 0, arrCount = 0, quantity = 0;
- ITEM item[1000];
- FILE *ptr;
- ptr = fopen("sample1.txt", "r");
- //file checking.Check if file existed or not
- if (!ptr) {
- printf("ERROR OPENING FILE\n");
- exit(-1);
- }
- //read the content of the file and store the content to the structure array
- while (!feof(ptr))
- {
- fscanf(ptr, "%[^|]|%[^|]|%d|%[^|]|%[^\n]\n",
- &item[i].barcode, &item[i].name,
- &item[i].quantity, &item[i].price, &item[i].category);
- i++;
- arrCount++;
- }
- fclose(ptr);
- printf("\n\n\n\t\tEnter barcode: ");
- scanf(" %[^\n]", &userInputBarcode);
- //clear the text file
- ptr = fopen("sample1.txt", "w");
- fclose(ptr);
- ptr = fopen("sample1.txt", "a");
- //read the content of the structure array
- for (i = 0; i<arrCount; i++) {
- //compare user input barcode with structure barcode
- if (strcmp(userInputBarcode, item[i].barcode) == 0) {
- printf("Item deleted\n");
- continue; //skip the record
- }
- else {
- fprintf(ptr, "%s|%s|%d|%s|%s\n",
- item[i].barcode, item[i].name,
- item[i].quantity, item[i].price, item[i].category);
- }
- }
- fclose(ptr);
- system("pause");
- }
- void menu(){
- system("cls");
- int ad=0;
- printf("\n\n\n\t\tWhat would you like to do? \n");
- printf("\n\t\t [1] Display All Item");
- printf("\n\t\t [2] Search Item");
- printf("\n\t\t [3] Add New Item");
- printf("\n\t\t [4] Update/Deduct Item's Quantity");
- printf("\n\t\t [5] Delete Item");
- printf("\n\t\t [6] Exit\n");
- printf("\n\t\t Select your choice: ");
- scanf(" %d", &ad);
- switch(ad){
- case 1:
- system("cls");
- display();
- menu();
- break;
- case 2:
- system("cls");
- search();
- menu();
- break;
- case 3:
- system("cls");
- add();
- menu();
- break;
- case 4:
- system("cls");
- ad = 0; //reset the ad variable
- printf("\n\n\n\tUPDATE/DEDUCT QUANTITY FUNCTION\n");
- printf("\n\t\t [1] Update Quantity");
- printf("\n\t\t [2] Deduct Quantity");
- printf("\n\t\t [3] Back");
- printf("\n\t\t Select your choice: ");
- scanf(" %d", &ad);
- switch (ad)
- {
- case 1: update(); break;
- case 2: ded(); break;
- case 3: menu(); break;
- default:
- printf("\n Wrong Choice. Please select again.");
- break;
- }
- menu();
- break;
- case 5:
- system("cls");
- del();
- menu();
- break;
- case 6:
- main();
- break;
- default:
- system("cls");
- printf("\n Wrong Choice. Please select again.");
- }
- }
- int main()
- {
- system("cls");
- int log;
- printf("\n\n\n\t\t\t\tIgnacio's Storage\n");
- printf("\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t [1] Login ");
- printf("\n\t\t\t\t [2] Exit Program ");
- printf("\n\t\t\t\t Enter your choice: ");
- scanf("%d", &log);
- switch(log)
- {
- case 1:
- login();
- break;
- case 2:
- exit(-1);
- break;
- default:
- system("cls");
- printf("\n Wrong input. Please try again.");
- main();
- }
- return 0;
- }
- void error()
- {
- system("cls");
- int log;
- printf("\n\n\n\t\t\t\tIgnacio's Storage\n");
- printf("\n\n\n\n\n\n\t\tLogin Failed. Please try again... (3 Attempts Left)");
- printf("\n\n\n\n\n\n\t\t\t\t [1] Login ");
- printf("\n\t\t\t\t [2] Exit Program ");
- printf("\n\t\t\t\t Enter your choice: ");
- scanf("%d", &log);
- switch(log)
- {
- case 1:
- login1();
- break;
- case 2:
- exit(-1);
- break;
- default:
- system("cls");
- printf("\n Wrong input. Please try again.");
- main();
- }
- }
- void error1()
- {
- system("cls");
- int log;
- printf("\n\n\n\t\t\t\tIgnacio's Storage\n");
- printf("\n\n\n\n\n\n\t\tLogin Failed. Please try again...(2 Attempts Left)");
- printf("\n\n\n\n\n\n\t\t\t\t [1] Login ");
- printf("\n\t\t\t\t [2] Exit Program ");
- printf("\n\t\t\t\t Enter your choice: ");
- scanf("%d", &log);
- switch(log)
- {
- case 1:
- login2();
- break;
- case 2:
- exit(-1);
- break;
- default:
- system("cls");
- printf("\n Wrong input. Please try again.");
- main();
- }
- }
- void error2()
- {
- system("cls");
- int log;
- printf("\n\n\n\t\t\t\tIgnacio's Storage\n");
- printf("\n\n\n\n\n\n\t\tLogin Failed. Please try again...(1 Attempts Left)");
- printf("\n\n\n\n\n\n\t\t\t\t [1] Login ");
- printf("\n\t\t\t\t [2] Exit Program ");
- printf("\n\t\t\t\t Enter your choice: ");
- scanf("%d", &log);
- switch(log)
- {
- case 1:
- login3();
- break;
- case 2:
- exit(-1);
- break;
- default:
- system("cls");
- printf("\n Wrong input. Please try again.");
- main;
- }
- }
- void login()
- {
- char username[10];
- char password[10];
- system("cls");
- printf("\n\n\n\t\t\t\tIgnacio's Storage\n");
- printf("\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t USERNAME : ");
- scanf("%s", &username);
- printf("\t\t\t\t PASSWORD : ");
- scanf("%s", &password);
- if(strcmp(username, "Omsss") == 0)
- {
- if(strcmp(password, "123456") == 0)
- {
- menu();
- }
- else
- {
- printf("\nPassword is Incorrect.");
- }
- }
- else
- {
- printf("\nUsername is Incorrect.");
- }
- error();
- }
- void login1()
- {
- char username[10];
- char password[10];
- system("cls");
- printf("\n\n\n\t\t\t\tIgnacio's Storage\n");
- printf("\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t USERNAME : ");
- scanf("%s", &username);
- printf("\t\t\t\t PASSWORD : ");
- scanf("%s", &password);
- if(strcmp(username, "Omsss") == 0)
- {
- if(strcmp(password, "123456") == 0)
- {
- menu();
- }
- else
- {
- printf("\nPassword is Incorrect.");
- }
- }
- else
- {
- printf("\nUsername is Incorrect.");
- }
- error1();
- }
- void login2()
- {
- char username[10];
- char password[10];
- system("cls");
- printf("\n\n\n\t\t\t\tIgnacio's Storage\n");
- printf("\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t USERNAME : ");
- scanf("%s", &username);
- printf("\t\t\t\t PASSWORD : ");
- scanf("%s", &password);
- if(strcmp(username, "Omsss") == 0)
- {
- if(strcmp(password, "123456") == 0)
- {
- menu();
- }
- else
- {
- printf("\nPassword is Incorrect.");
- }
- }
- else
- {
- printf("\nUsername is Incorrect.");
- }
- error2();
- }
- void login3()
- {
- char username[10];
- char password[10];
- system("cls");
- printf("\n\n\n\t\t\t\tIgnacio's Storage\n");
- printf("\n\n\n\n\n\n\t\t\t\t(WARNING: Last Attempt!)\n");
- printf("\n\n\n\n\n\n\n\n\n\t\t\t\t USERNAME : ");
- scanf("%s", &username);
- printf("\t\t\t\t PASSWORD : ");
- scanf("%s", &password);
- if(strcmp(username, "Omsss") == 0)
- {
- if(strcmp(password, "123456") == 0)
- {
- menu();
- }
- else
- {
- printf("\nPassword is Incorrect.");
- }
- }
- else
- {
- printf("\nUsername is Incorrect.");
- }
- exit(-1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement