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;
break;
default://invalid input
printf("Invalid choice\n");
check = false;
printf("Invalid choice...press anykey to retry....\n");
fflush(stdin);
getchar();
check = true;
break;
}
fflush(stdin);
} while (check);
return 0;

View File

@ -19,9 +19,13 @@
#endif // !SORT_H
#define PAGE_SIZE 20
//for sort list
//fucntions for menu
void list_items();
void search_item();
void print_page(struct inventory db, int cur, int end,struct Map* map);
int normal_menu_user_choices();
//main of normal user
bool normal_menu(){
system("cls");
welcome_message();
@ -34,7 +38,7 @@ bool normal_menu(){
list_items();
break;
case 2://Search item
// search_item();
search_item();
break;
case 3://scan barcode
// scan_barcode();
@ -51,7 +55,21 @@ bool normal_menu(){
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 choice;
printf("You can buy items inside the 2 option below\n");
@ -63,24 +81,11 @@ int normal_menu_user_choices(){
scanf("%d", &choice);
return choice;
}
//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
void list_page(struct inventory db,struct Map* map,int row){//user for showing list result and search result
int choice = -1;
int page = 0;
int page_size = PAGE_SIZE;
struct inventory db = read_db_invt();
int total_pages = (int)ceil((double)db.db.row_count / page_size);
struct Map* map = NULL;
int total_pages = ceil((double)row / page_size);
do{
//options
@ -98,7 +103,7 @@ void list_items(){
//print items
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)
print_page(db, page * page_size, (page + 1) * page_size,map);
}
@ -124,9 +129,8 @@ void list_items(){
}else if(choice == page_size+5){
printf("Enter page size: ");
scanf("%d", &page_size);
total_pages = ceil(db.db.row_count / page_size);
}else if(choice >= 9 && choice < db.db.row_count+9){
printf("s");
total_pages = ceil(row / page_size);
}else if(choice >= 9 && choice < row+9){
show_item(db,choice - 9 + page_size*page);
}else if(choice != 0){
printf("Invalid choice\n");
@ -136,6 +140,35 @@ void list_items(){
}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 *map = malloc(sizeof(struct Map) * db.db.row_count);
for (int i = 0; i < db.db.row_count; i++){
@ -177,33 +210,79 @@ struct Map* sortItems(struct inventory db, int sort){
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{
printf("%d item: %s\n", i + 9, db.row[i].product);
}while(!valid);
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){
return (*(struct Map *)a).value - (*(struct Map *)b).value;
}
}