diff --git a/admin_user.h b/admin_user.h index b76e375..b1bedea 100644 --- a/admin_user.h +++ b/admin_user.h @@ -302,7 +302,7 @@ void tran_control(){ "quantity", "total", }; - lister(&tran,NULL,tran.db.row_count,"Transaction list",SortItems,4,print_tran,sortTrans,showTran); + lister(&tran,NULL,tran.db.row_count,"Transaction",SortItems,4,print_tran,sortTrans,showTran); break; case 2: add_tran(tran); @@ -319,13 +319,14 @@ void print_tran(void * ddb, int cur, int end,struct Map* map){ for (int i = cur; i < end; i++) { if(map != NULL){ - double total = db.row[map[i].key].price * db.row[map[i].key].quantity; + int index = map[i].key; + double total = db.row[index].price * db.row[index].quantity; //reconstuct date and time and print all transaction info out char date[11]; char time[9]; - sprintf(date,"%02d-%02d-%04d",db.row[map[i].key].date.day,db.row[map[i].key].date.month,db.row[map[i].key].date.year); - sprintf(time,"%02d:%02d:%02d",db.row[map[i].key].time.hour,db.row[map[i].key].time.minute,db.row[map[i].key].time.second); - printf("%-5d%-15s%-10s%-10d%-10d%-10.2lf%-10d%-10.2lf\n",i+9,date,time,db.row[map[i].key].barcode,db.row[map[i].key].id,db.row[map[i].key].price,db.row[map[i].key].quantity,total); + sprintf(date,"%02d-%02d-%04d",db.row[index].date.day,db.row[index].date.month,db.row[index].date.year); + sprintf(time,"%02d:%02d:%02d",db.row[index].time.hour,db.row[index].time.minute,db.row[index].time.second); + printf("%-5d%-15s%-10s%-10d%-10d%-10.2lf%-10d%-10.2lf\n",i+9,date,time,db.row[index].barcode,db.row[index].id,db.row[index].price,db.row[index].quantity,total); }else{ double total = db.row[i].price * db.row[i].quantity; //reconstuct date and time and print all transaction info out @@ -484,8 +485,9 @@ struct transaction remove_tran(struct transaction db,int index){ struct Map* sortTrans(void * ddb,int choice){ struct transaction db = *((struct transaction*)ddb); struct Map *map = malloc(sizeof(struct Map) * db.db.row_count); + printf("l"); for (int i = 0; i < db.db.row_count; i++){ - + map[i].key = i; switch(choice){ case 1: case 2:; @@ -561,9 +563,9 @@ void user_control(){ "ID", "Role", }; - lister(&db,NULL,db.db.row_count,"User list",SortItems,3,print_user,SortUser,showUser); + lister(&db,NULL,db.db.row_count,"User",SortItems,3,print_user,SortUser,showUser); db = read_db_user(); - // db = list_user(db); + db = list_user(db); break; case 3: break; @@ -855,7 +857,7 @@ void * showUser(void * ddb,int index){ getchar(); break; } - }while(choice != 0); + }while(choice != 0&&choice!=2); void * re = malloc(sizeof(db)); memcpy(re,&db,sizeof(db)); return re; diff --git a/normal_user.h b/normal_user.h index 4f8a841..283dc7e 100644 --- a/normal_user.h +++ b/normal_user.h @@ -202,7 +202,7 @@ void list_items(){ struct Map* sortItems(void * ddb, int sort){ struct inventory db = *((struct inventory*)ddb); - struct Map *map = malloc(sizeof(struct Map) * db.db.row_count-1); + struct Map *map = malloc(sizeof(struct Map) * db.db.row_count -1); for (int i = 0; i < db.db.row_count-1; i++){ map[i].key = i; diff --git a/utils.h b/utils.h index 50de476..fbe99d3 100644 --- a/utils.h +++ b/utils.h @@ -117,9 +117,9 @@ void lister(void * db,struct Map* map, int row,char * lister_name,char * SortIte 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]); - printf("%d sort %s ascending\n",i+1,SortItems[i/2]); + for(int i=0;i"); fflush_stdin(); scanf("%d",&choice); - if(choice <=NoSortItems && choice > 0){ + if(choice <=NoSortItems*2 && choice > 0){ printf("sorting...\n"); map = (*sorter)(db,choice); }else if(choice == current_page_size+1 ){ @@ -164,11 +164,11 @@ void lister(void * db,struct Map* map, int row,char * lister_name,char * SortIte fflush_stdin(); scanf("%d", &page_size); total_pages = ceil((double)row / page_size); - }else if(choice >= 9 && choice <= current_page_size){ + }else if(choice > NoSortItems*2 && choice <= current_page_size){ if(map == NULL){ - db = (*showitem)(db,choice - 9 + page_size*page); + db = (*showitem)(db,choice - NoSortItems*2-1 + page_size*page); }else{ - db = (*showitem)(db,map[choice - 9 + page_size*page].key); + db = (*showitem)(db,map[choice - NoSortItems*2-1 + page_size*page].key); } }else if(choice != 0){ printf("Invalid choice\n");