diff --git a/admin_user.h b/admin_user.h index 43f0963..4964ebc 100644 --- a/admin_user.h +++ b/admin_user.h @@ -123,7 +123,7 @@ void add_item(); void remove_item(struct inventory db,int index); void update_item(struct inventory db,int index); void admin_list_pages(struct inventory db); -void item_control(struct inventory db,int index); +struct inventory item_control(struct inventory db,int index); int prompt_inv_control(){ int choice = 0; @@ -145,7 +145,6 @@ int prompt_inv_control(){ } void inv_control(){ - struct inventory db = read_db_invt(); int choice = 0; do{ choice = prompt_inv_control(); @@ -153,6 +152,7 @@ void inv_control(){ case 1://list //add a new element with product name new item //for item control + struct inventory db = read_db_invt(); db.row = (struct inventory_row *)realloc(db.row, sizeof(struct inventory_row) * (db.db.row_count + 1)); db.db.row_count += 1; strcpy(db.row[db.db.row_count - 1].product,"CREATE NEW ITEM"); @@ -183,11 +183,16 @@ void show_item_admin(struct inventory db,int index){ return; } -void item_control(struct inventory db,int index){ +struct inventory item_control(struct inventory db,int index){ int choice = 0; if(db.row[index].barcode == -1024){//new item add_item(); - return; + struct inventory temp = read_db_invt();//reappend new item at back + temp.row = (struct inventory_row *)realloc(temp.row, sizeof(struct inventory_row) * (temp.db.row_count + 1)); + temp.db.row_count += 1; + strcpy(temp.row[temp.db.row_count - 1].product,"CREATE NEW ITEM"); + temp.row[temp.db.row_count - 1].barcode = -1024;//IMPORTANT: -1024 is reserved for new item + return temp; } do { @@ -212,6 +217,12 @@ void item_control(struct inventory db,int index){ break; } } while (choice != 0); + struct inventory temp = read_db_invt(); + temp.row = (struct inventory_row *)realloc(temp.row, sizeof(struct inventory_row) * (temp.db.row_count + 1)); + temp.db.row_count += 1; + strcpy(temp.row[temp.db.row_count - 1].product,"CREATE NEW ITEM"); + temp.row[temp.db.row_count - 1].barcode = -1024;//IMPORTANT: -1024 is reserved for new item + return temp; } char* prompt_item(char* prompt){ @@ -222,9 +233,10 @@ char* prompt_item(char* prompt){ scanf("%[^\n]",item); if(strcmp(item,"") == 0){ printf("Invalid input, try again?(y/n)\n"); + char temp[100]; fflush(stdin); - char c = getchar(); - if(c == 'n'){ + scanf("%[^\n]",temp); + if(strcmp(temp,"n") == 0){ return NULL; } } diff --git a/normal_user.h b/normal_user.h index b6b1035..a2b9b08 100644 --- a/normal_user.h +++ b/normal_user.h @@ -60,7 +60,7 @@ void normal_menu(){ //universal functions for normal user struct Map* sortItems(struct inventory db, int sort); -void show_item(struct inventory db,int index){ +struct inventory show_item(struct inventory db,int index){ system("cls"); printf("Product: %s\n", db.row[index].product); printf("Catergory: %s\n", db.row[index].category); @@ -71,7 +71,7 @@ void show_item(struct inventory db,int index){ printf("Press any key to return to the list\n"); fflush(stdin); getchar(); - return; + return db; } int normal_menu_user_choices(){ int choice; @@ -87,7 +87,7 @@ int normal_menu_user_choices(){ } //user for showing list result and search result -void list_page(struct inventory db,struct Map* map,int row,void (*showitem)(struct inventory,int)){//showitem is a function pointer for showing item,allow customization for better flexibilty +void list_page(struct inventory db,struct Map* map,int row,struct inventory (*showitem)(struct inventory,int)){//showitem is a function pointer for showing item,allow customization for better flexibilty int choice = -1; int page = 0; int page_size = PAGE_SIZE; @@ -140,7 +140,7 @@ void list_page(struct inventory db,struct Map* map,int row,void (*showitem)(stru scanf("%d", &page_size); total_pages = ceil(row / page_size); }else if(choice >= 9 && choice < row+9){ - (*showitem)(db,choice - 9 + page_size*page); + db = (*showitem)(db,choice - 9 + page_size*page); }else if(choice != 0){ printf("Invalid choice\n"); valid = false;