diff --git a/admin_user.h b/admin_user.h index 8bdca37..26a5dfb 100644 --- a/admin_user.h +++ b/admin_user.h @@ -10,10 +10,6 @@ #include #endif -#ifndef MENU_H -#define MENU_H -#include "menu.h" -#endif //MENU_H #ifndef Utils #define Utils #include "utils.h" @@ -226,50 +222,17 @@ void add_item(){ void update_item(struct inventory db,int index){ char temp[100]; printf("Update item(empty value to not change the value)\n"); - - printf("Please input the name(original:%s)\n>",db.row[index].product); - fflush(stdin); - scanf("%[^\n]", temp);//input until /n - if(strcmp(temp,"") != 0){//check if temp is not empty - strcpy(db.row[index].product,temp);//if yes, copy temp to product - }//else preserve the original value - - printf("Please input the brand(orginal:%s)\n>",db.row[index].brand); - fflush(stdin); - scanf("%[^\n]", temp); - if(strcmp(temp,"") != 0){ - strcpy(db.row[index].brand,temp); - } - - printf("Please input the category(orginal:%s)\n>",db.row[index].category); - fflush(stdin); - scanf("%[^\n]", temp); - if(strcmp(temp,"") != 0){ - strcpy(db.row[index].category,temp); - } - - printf("Please input the price(orginal:$%.1lf)\n>",db.row[index].price); - fflush(stdin); - scanf("%[^\n]", temp); - if(strcmp(temp,"") != 0){ - db.row[index].price = atof(temp); - } - - printf("Please input the stock(orginal:%d)\n>",db.row[index].stock); - fflush(stdin); - scanf("%[^\n]", temp); - if(strcmp(temp,"") != 0){ - db.row[index].stock = atoi(temp); - } - - printf("Please input the barcode(original:%ld)\n>",db.row[index].barcode); - fflush(stdin); - scanf("%[^\n]", temp); - if(strcmp(temp,"") != 0){ - db.row[index].barcode = atol(temp); - } - - + printf("Name: %s\n", db.row[index].product); + printf("Brand: %s\n", db.row[index].brand); + printf("Catergory: %s\n", db.row[index].category); + printf("Price: $%lf\n", db.row[index].price); + printf("Stock: %d\n", db.row[index].stock); + printf("Barcode: %ld\n",db.row[index].barcode); + item_inputer("name",INPUT_TYPE::STRING,db.row[index].product,true); + item_inputer("brand",INPUT_TYPE::STRING,db.row[index].brand,true); + item_inputer("category",INPUT_TYPE::STRING,db.row[index].category,true); + item_inputer("stock",INPUT_TYPE::FLOAT,db.row[index].price,true); + item_inputer("barcode",INPUT_TYPE::LONG,db.row[index].barcode,true); if(!update_db_invt(db)){ printf("Failed to update item\n"); exit(1); diff --git a/utils.h b/utils.h index a5482fa..b4ac1d9 100644 --- a/utils.h +++ b/utils.h @@ -13,7 +13,7 @@ void welcome_message(){ printf("><\n");//welcome messgae } -int choices_selecter(char * Items[],int items,char * welcome_messages ){ +int choices_selecter(char * Items[],int items,char * welcome_messages){ int choice = 0; do{ system("cls"); @@ -40,13 +40,13 @@ int choices_selecter(char * Items[],int items,char * welcome_messages ){ } -char* prompt_item(char* prompt){ +char* prompt_item(char* prompt,bool empty_allowed=false){ char* item = malloc(sizeof(char) * 100); do{ printf("%s",prompt); fflush(stdin); - scanf("%[^\n]",item); - if(strcmp(item,"") == 0){ + scanf("%[^\n]",item);//input until /n + if(!empty_allowed&&(item,"") == 0){//check if temp is not empty printf("Invalid input, try again?(y/n)\n"); char temp[100]; fflush(stdin); @@ -55,17 +55,17 @@ char* prompt_item(char* prompt){ return NULL; } } - }while(strcmp(item,"") == 0); + }while(!empty_allowed&&strcmp(item,"") == 0); return item; } typedef enum{ INT=1,STRING=2,LONG=3,FLOAT=4 }INPUT_TYPE; -bool item_inputer(char * varname,INPUT_TYPE type,void * item){ +bool item_inputer(char * varname,INPUT_TYPE type,void * item,bool empty_allowed=false){ char output[1024] = "Please input the "; strcat(output,varname); strcat(output,": \n>"); - char* temp = prompt_item(output); + char* temp = prompt_item(output,empty_allowed); if(temp == NULL){ return false; } @@ -87,6 +87,82 @@ bool item_inputer(char * varname,INPUT_TYPE type,void * item){ } return true; } + +void lister(void * db,struct Map* map, int row,char * lister_name,char * SortItems[],int NoSortItems,void (*page_printer)(void*,int,int,struct * Map) , struct * Map (*sorter)(void *,int),void * (*showitem)(void *,int)){ + int choice = -1; + int page = 0; + int page_size = PAGE_SIZE; + int total_pages = ceil((double)row / page_size); + do{ + system("cls"); + welcome_message(); + printf("%s list\n",lister_name); + printf("0 exit\n"); + for(int i=1;i<=NoSortItems*2;i+=2){ + printf("%d sort %s decending\n",i,SortItems[i/2-1]); + printf("%d sort %s ascending\n",i+1,SortItems[i/2-1]); + } + if(page+1 == total_pages){ + (*page_printer)(db,page*page_size,row,map); + }else{ + (*page_printer)(db,page*page_size,(page+1)*page_size,map); + } + //page control + int current_page_size = page_size+8; + if(page+1 == total_pages){ + current_page_size = row - page*page_size+8; + } + printf("%d next page\n", current_page_size+1); + printf("%d previous page\n", current_page_size+2); + printf("%d set page size\n", current_page_size+3); + printf("%d/%d/%d(page size/page number/total)\n",page_size, page+1,total_pages); + + bool valid = true; + do{ + valid = true; + printf("Please input your choice\n>"); + fflush(stdin); + scanf("%d",&choice); + if(choice <=NoSortItems && choice > 0){ + printf("sorting...\n"); + map = (*sorter)(db,choice); + }else if(choice == current_page_size+1 ){ + if(page + 1 < total_pages){ + page++; + }else{ + printf("Already at last page\n"); + valid = false; + } + }else if(choice == current_page_size+2){ + if(page > 0){ + page--; + }else{ + printf("Already at first page\n"); + valid = false; + } + }else if(choice == current_page_size+3){ + printf("Enter page size: "); + fflush(stdin); + scanf("%d", &page_size); + total_pages = ceil(row / page_size); + }else if(choice >= 9 && choice <= current_page_size){ + if(map == NULL){ + db = (*showitem)(db,choice - 9 + page_size*page); + }else{ + db = (*showitem)(db,map[choice - 9 + page_size*page].key); + } + }else if(choice != 0){ + printf("Invalid choice\n"); + valid = false; + } + + }while(!valid); + }while(choice != 0); + return; +} + + + //check if strcasestr is define in current environment //if not, define it #ifndef HAVE_STRCASESTR