search also finished

This commit is contained in:
smarttommyau 2022-08-27 21:34:35 +08:00
parent 7b1ccd61d2
commit 6e95628734
No known key found for this signature in database
GPG Key ID: D7832FC94BD13B6A
3 changed files with 134 additions and 51 deletions

9
main.c
View File

@ -44,12 +44,15 @@ int main(){
check = false; check = false;
break; break;
default://invalid input default://invalid input
printf("Invalid choice\n"); printf("Invalid choice...press anykey to retry....\n");
check = false; fflush(stdin);
getchar();
check = true;
break; break;
} }
fflush(stdin);
} while (check); } while (check);
return 0; return 0;

View File

@ -19,9 +19,13 @@
#endif // !SORT_H #endif // !SORT_H
#define PAGE_SIZE 20 #define PAGE_SIZE 20
//for sort list //fucntions for menu
void list_items(); void list_items();
void search_item();
void print_page(struct inventory db, int cur, int end,struct Map* map);
int normal_menu_user_choices(); int normal_menu_user_choices();
//main of normal user
bool normal_menu(){ bool normal_menu(){
system("cls"); system("cls");
welcome_message(); welcome_message();
@ -34,7 +38,7 @@ bool normal_menu(){
list_items(); list_items();
break; break;
case 2://Search item case 2://Search item
// search_item(); search_item();
break; break;
case 3://scan barcode case 3://scan barcode
// scan_barcode(); // scan_barcode();
@ -51,7 +55,21 @@ bool normal_menu(){
return true; return true;
} }
//universal functions for normal user
struct Map* sortItems(struct inventory db, int sort);
void 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);
printf("Brand: %d\n", db.row[index].brand);
printf("Price: $%lf\n", db.row[index].price);
printf("Stock: %d\n", db.row[index].stock);
printf("Barcode: %ld\n",db.row[index].barcode);
printf("Press any key to return to the list\n");
fflush(stdin);
getchar();
return;
}
int normal_menu_user_choices(){ int normal_menu_user_choices(){
int choice; int choice;
printf("You can buy items inside the 2 option below\n"); printf("You can buy items inside the 2 option below\n");
@ -63,24 +81,11 @@ int normal_menu_user_choices(){
scanf("%d", &choice); scanf("%d", &choice);
return choice; return choice;
} }
void list_page(struct inventory db,struct Map* map,int row){//user for showing list result and search result
//list items
void show_item(struct inventory db,int index);
void print_page(struct inventory db, int cur, int end,struct Map* map);
struct Map* sortItems(struct inventory db, int sort);
void list_items(){
system("cls");
welcome_message();
//get the list of items from the database
//print the list of items
//prompt user to select an item
//if user selects an item, display the item's details
int choice = -1; int choice = -1;
int page = 0; int page = 0;
int page_size = PAGE_SIZE; int page_size = PAGE_SIZE;
struct inventory db = read_db_invt(); int total_pages = ceil((double)row / page_size);
int total_pages = (int)ceil((double)db.db.row_count / page_size);
struct Map* map = NULL;
do{ do{
//options //options
@ -98,7 +103,7 @@ void list_items(){
//print items //print items
if(page + 1 >= total_pages){ if(page + 1 >= total_pages){
print_page(db, page * page_size, db.db.row_count,map); print_page(db, page * page_size, row,map);
}else{//sorted) }else{//sorted)
print_page(db, page * page_size, (page + 1) * page_size,map); print_page(db, page * page_size, (page + 1) * page_size,map);
} }
@ -124,9 +129,8 @@ void list_items(){
}else if(choice == page_size+5){ }else if(choice == page_size+5){
printf("Enter page size: "); printf("Enter page size: ");
scanf("%d", &page_size); scanf("%d", &page_size);
total_pages = ceil(db.db.row_count / page_size); total_pages = ceil(row / page_size);
}else if(choice >= 9 && choice < db.db.row_count+9){ }else if(choice >= 9 && choice < row+9){
printf("s");
show_item(db,choice - 9 + page_size*page); show_item(db,choice - 9 + page_size*page);
}else if(choice != 0){ }else if(choice != 0){
printf("Invalid choice\n"); printf("Invalid choice\n");
@ -136,6 +140,35 @@ void list_items(){
}while(choice != 0); }while(choice != 0);
} }
//print the list for the page
void print_page(struct inventory db, int cur, int end,struct Map* map){
for (int i = cur; i < end; i++)
{
if(map != NULL){
printf("%d item: %s\n", i + 9, db.row[map[i].key].product);
}
else{
printf("%d item: %s\n", i + 9, db.row[i].product);
}
}
}
//list items
void list_items(){
system("cls");
welcome_message();
//get the list of items from the database
//print the list of items
//prompt user to select an item
//if user selects an item, display the item's details
struct inventory db = read_db_invt();
struct Map* map = NULL;
list_page(db,map,db.db.row_count);
}
//for sorting
struct Map* sortItems(struct inventory db, int sort){ struct Map* sortItems(struct inventory db, int sort){
struct Map *map = malloc(sizeof(struct Map) * db.db.row_count); struct Map *map = malloc(sizeof(struct Map) * db.db.row_count);
for (int i = 0; i < db.db.row_count; i++){ for (int i = 0; i < db.db.row_count; i++){
@ -177,33 +210,79 @@ struct Map* sortItems(struct inventory db, int sort){
return map; return map;
} }
void print_page(struct inventory db, int cur, int end,struct Map* map){
for (int i = cur; i < end; i++)
{
if(map != NULL){
printf("%d item: %s\n", i + 9, db.row[map[i].key].product); //search items
char* prompt_search();
char* searchItems(struct inventory db, char* searchstr);
void search_item(){
system("cls");
welcome_message();
//search for item
//prompt user to select an item
//if user selects an item, display the item's details
struct inventory db = read_db_invt();
int total_pages = 0;
struct Map* map = NULL;
do{
//options
int breakout = 0;
bool valid = false;
do{
system("cls");
welcome_message();
printf("0 exit\n");
printf("1 search\n");
scanf("%d", &breakout);
if(breakout == 1 || breakout == 0){
valid = true;
}else{
printf("Invalid choice\n");
} }
else{ }while(!valid);
printf("%d item: %s\n", i + 9, db.row[i].product); if(breakout == 0){
break;
}
char* searchstr = prompt_search();
printf("searching...\n");
map = searchItems(db,searchstr);
if(map[0].value == 0){
list_page(db,map+1,map[0].value);//ofset map, as it is use to store the size
}else{//empty search
printf("No result found\n");
printf("Press any key to continue\n");
fflush(stdin);
getchar();
}
}while(true);//break on top
}
char* prompt_search(){
printf("Enter search string:(largest length 100) ");
char* searchstr = malloc(sizeof(char) * 100);
scanf("%s", searchstr);
return searchstr;
}
char* searchItems(struct inventory db, char* searchstr){
struct Map* map = malloc(sizeof(struct Map) * (db.db.row_count+1));
int k = 1;
for (int i = 0; i < db.db.row_count; i++){
map[k].key = i;
if(strstr(db.row[i].product,searchstr) != NULL){
map[k].value = db.row[i].product;
k++;
} }
} }
map[0].value = k-1;
map[0].key = -1;
return map;
} }
void 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);
printf("Brand: %d\n", db.row[index].brand);
printf("Price: $%lf\n", db.row[index].price);
printf("Stock: %d\n", db.row[index].stock);
printf("Barcode: %ld\n",db.row[index].barcode);
printf("Press any key to return to the list\n");
fflush(stdin);
getchar();
return;
}
//

View File

@ -8,4 +8,5 @@ int compare_decending(const void *a, const void *b){
} }
int compare_ascending(const void *a, const void *b){ int compare_ascending(const void *a, const void *b){
return (*(struct Map *)a).value - (*(struct Map *)b).value; return (*(struct Map *)a).value - (*(struct Map *)b).value;
} }