lister fix, list user fin

This commit is contained in:
stmctommyau 2023-09-08 00:48:29 +08:00
parent b6eb8d8756
commit b15ed51c8d
No known key found for this signature in database
GPG Key ID: 87B1991E1277F054
3 changed files with 21 additions and 19 deletions

View File

@ -302,7 +302,7 @@ void tran_control(){
"quantity", "quantity",
"total", "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; break;
case 2: case 2:
add_tran(tran); 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++) for (int i = cur; i < end; i++)
{ {
if(map != NULL){ 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 //reconstuct date and time and print all transaction info out
char date[11]; char date[11];
char time[9]; 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(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[map[i].key].time.hour,db.row[map[i].key].time.minute,db.row[map[i].key].time.second); 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[map[i].key].barcode,db.row[map[i].key].id,db.row[map[i].key].price,db.row[map[i].key].quantity,total); 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{ }else{
double total = db.row[i].price * db.row[i].quantity; double total = db.row[i].price * db.row[i].quantity;
//reconstuct date and time and print all transaction info out //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 Map* sortTrans(void * ddb,int choice){
struct transaction db = *((struct transaction*)ddb); struct transaction db = *((struct transaction*)ddb);
struct Map *map = malloc(sizeof(struct Map) * db.db.row_count); struct Map *map = malloc(sizeof(struct Map) * db.db.row_count);
printf("l");
for (int i = 0; i < db.db.row_count; i++){ for (int i = 0; i < db.db.row_count; i++){
map[i].key = i;
switch(choice){ switch(choice){
case 1: case 1:
case 2:; case 2:;
@ -561,9 +563,9 @@ void user_control(){
"ID", "ID",
"Role", "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 = read_db_user();
// db = list_user(db); db = list_user(db);
break; break;
case 3: case 3:
break; break;
@ -855,7 +857,7 @@ void * showUser(void * ddb,int index){
getchar(); getchar();
break; break;
} }
}while(choice != 0); }while(choice != 0&&choice!=2);
void * re = malloc(sizeof(db)); void * re = malloc(sizeof(db));
memcpy(re,&db,sizeof(db)); memcpy(re,&db,sizeof(db));
return re; return re;

View File

@ -202,7 +202,7 @@ void list_items(){
struct Map* sortItems(void * ddb, int sort){ struct Map* sortItems(void * ddb, int sort){
struct inventory db = *((struct inventory*)ddb); 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++){ for (int i = 0; i < db.db.row_count-1; i++){
map[i].key = i; map[i].key = i;

18
utils.h
View File

@ -117,9 +117,9 @@ void lister(void * db,struct Map* map, int row,char * lister_name,char * SortIte
welcome_message(); welcome_message();
printf("%s list\n",lister_name); printf("%s list\n",lister_name);
printf("0 exit\n"); printf("0 exit\n");
for(int i=1;i<=NoSortItems*2;i+=2){ for(int i=0;i<NoSortItems*2;i+=2){
printf("%d sort %s decending\n",i,SortItems[i/2]); printf("%d sort %s decending\n",i+1,SortItems[i/2]);
printf("%d sort %s ascending\n",i+1,SortItems[i/2]); printf("%d sort %s ascending\n",i+2,SortItems[i/2]);
} }
if(page+1 == total_pages){ if(page+1 == total_pages){
(*page_printer)(db,page*page_size,row,map); (*page_printer)(db,page*page_size,row,map);
@ -127,9 +127,9 @@ void lister(void * db,struct Map* map, int row,char * lister_name,char * SortIte
(*page_printer)(db,page*page_size,(page+1)*page_size,map); (*page_printer)(db,page*page_size,(page+1)*page_size,map);
} }
//page control //page control
int current_page_size = page_size+8; int current_page_size = page_size+NoSortItems*2;
if(page+1 == total_pages){ if(page+1 == total_pages){
current_page_size = row - page*page_size+8; current_page_size = row - page*page_size+NoSortItems*2;
} }
printf("%d next page\n", current_page_size+1); printf("%d next page\n", current_page_size+1);
printf("%d previous page\n", current_page_size+2); printf("%d previous page\n", current_page_size+2);
@ -142,7 +142,7 @@ void lister(void * db,struct Map* map, int row,char * lister_name,char * SortIte
printf("Please input your choice\n>"); printf("Please input your choice\n>");
fflush_stdin(); fflush_stdin();
scanf("%d",&choice); scanf("%d",&choice);
if(choice <=NoSortItems && choice > 0){ if(choice <=NoSortItems*2 && choice > 0){
printf("sorting...\n"); printf("sorting...\n");
map = (*sorter)(db,choice); map = (*sorter)(db,choice);
}else if(choice == current_page_size+1 ){ }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(); fflush_stdin();
scanf("%d", &page_size); scanf("%d", &page_size);
total_pages = ceil((double)row / 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){ if(map == NULL){
db = (*showitem)(db,choice - 9 + page_size*page); db = (*showitem)(db,choice - NoSortItems*2-1 + page_size*page);
}else{ }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){ }else if(choice != 0){
printf("Invalid choice\n"); printf("Invalid choice\n");