From b6eb8d87566be41d86c7cccf9cd55b1102b0b01f Mon Sep 17 00:00:00 2001 From: stmctommyau Date: Fri, 8 Sep 2023 00:14:06 +0800 Subject: [PATCH] list tran fin testing list user --- admin_user.h | 189 +++++++++++++++++++++++---------------------------- 1 file changed, 85 insertions(+), 104 deletions(-) diff --git a/admin_user.h b/admin_user.h index 88595fa..b76e375 100644 --- a/admin_user.h +++ b/admin_user.h @@ -280,8 +280,11 @@ void remove_item(struct inventory db,int index){ } //tran control -void list_tran(struct transaction db); void add_tran(); +void print_tran(void * ddb, int cur, int end,struct Map* map); +void * showTran(void * ddb,int index); +struct Map* sortTrans(void * ddb,int choice); + void tran_control(){ struct transaction tran = read_db_tran(); int choice; @@ -299,8 +302,7 @@ void tran_control(){ "quantity", "total", }; - lister(&tran,NULL,tran.db.row_count,"Transaction list",SortItems,4,print_tran,sortTrans,showTran);) - list_tran(tran); + lister(&tran,NULL,tran.db.row_count,"Transaction list",SortItems,4,print_tran,sortTrans,showTran); break; case 2: add_tran(tran); @@ -311,92 +313,8 @@ void tran_control(){ }while(choice != 3); } - -void print_tran(struct transaction db, int cur, int end,struct Map* map); -struct Map* sortTrans(struct transaction db,int choice); -struct transaction showTran(struct transaction db,int index); - -void list_tran(struct transaction db){ - int choice = -1; - int page = 0; - int page_size = PAGE_SIZE; - int total_pages = ceil((double)db.db.row_count / page_size); - int row = db.db.row_count; - struct Map* map = NULL; - do{ - Cls(); - welcome_message(); - printf("Transaction list\n"); - printf("0 exit\n"); - printf("1 sort Date&Time decending\n"); - printf("2 sort Date&Time ascending\n"); - printf("3 sort product(barcode) decending\n"); - printf("4 sort product(barcode) ascending\n"); - printf("5 sort quantity decending\n"); - printf("6 sort quantity ascending\n"); - printf("7 sort total decending\n"); - printf("8 sort total ascending\n"); - if(page+1 == total_pages){ - print_tran(db,page*page_size,row,map); - }else{ - print_tran(db,page*page_size,(page+1)*page_size,map); - } - //page control - int current_page_size = page_size+8; - if(page+1 == total_pages){ - current_page_size = row - page*page_size+8; - } - printf("%d next page\n", current_page_size+1); - printf("%d previous page\n", current_page_size+2); - printf("%d set page size\n", current_page_size+3); - printf("%d/%d/%d(page size/page number/total)\n",page_size, page+1,total_pages); - - bool valid = true; - do{ - valid = true; - printf("Please input your choice\n>"); - fflush_stdin(); - scanf("%d",&choice); - if(choice <=8 && choice > 0){ - printf("sorting...\n"); - map = sortTrans(db,choice); - }else if(choice == current_page_size+1 ){ - if(page + 1 < total_pages){ - page++; - }else{ - printf("Already at last page\n"); - valid = false; - } - }else if(choice == current_page_size+2){ - if(page > 0){ - page--; - }else{ - printf("Already at first page\n"); - valid = false; - } - }else if(choice == current_page_size+3){ - printf("Enter page size: "); - fflush_stdin(); - scanf("%d", &page_size); - total_pages = ceil((double)row / page_size); - }else if(choice >= 9 && choice <= current_page_size){ - if(map == NULL){ - db = showTran(db,choice - 9 + page_size*page); - }else{ - db = showTran(db,map[choice - 9 + page_size*page].key); - } - }else if(choice != 0){ - printf("Invalid choice\n"); - valid = false; - } - - }while(!valid); - }while(choice != 0); - return; -} - - -void print_tran(struct transaction db, int cur, int end,struct Map* map){ +void print_tran(void * ddb, int cur, int end,struct Map* map){ + struct transaction db = *((struct transaction*)ddb); printf("%-5s%-15s%-10s%-10s%-10s%-10s%-10s%-10s\n","No.","Date","Time","Barcode","ID","Price","Quantity","Total"); for (int i = cur; i < end; i++) { @@ -419,13 +337,13 @@ void print_tran(struct transaction db, int cur, int end,struct Map* map){ } } } - //show trans struct transaction update_tran(struct transaction db,int index); struct transaction remove_tran(struct transaction db,int index); -struct transaction showTran(struct transaction db,int index){ +void * showTran(void * ddb,int index){ + struct transaction db = *((struct transaction*)ddb); int choice; do{ printf("Transaction detail\n"); @@ -464,7 +382,9 @@ struct transaction showTran(struct transaction db,int index){ } }while(!valid); }while(choice != 3); - return db; + void * re = malloc(sizeof(db)); + memcpy(re,&db,sizeof(db)); + return re; } //tran controls @@ -561,7 +481,8 @@ struct transaction remove_tran(struct transaction db,int index){ //sort transaction -struct Map* sortTrans(struct transaction db,int choice){ +struct Map* sortTrans(void * ddb,int choice){ + struct transaction db = *((struct transaction*)ddb); struct Map *map = malloc(sizeof(struct Map) * db.db.row_count); for (int i = 0; i < db.db.row_count; i++){ @@ -615,7 +536,9 @@ struct Map* sortTrans(struct transaction db,int choice){ //user control struct user add_user(struct user db); struct user list_user(struct user db); - +void * showUser(void * ddb,int index); +struct Map* SortUser(void * ddb,int choice); +void print_user(void * ddb, int cur, int end,struct Map* map); void user_control(){ struct user db = read_db_user(); int choice = 0; @@ -633,7 +556,14 @@ void user_control(){ db = add_user(db); break; case 2: - db = list_user(db); + char * SortItems[] = { + "Name", + "ID", + "Role", + }; + lister(&db,NULL,db.db.row_count,"User list",SortItems,3,print_user,SortUser,showUser); + db = read_db_user(); + // db = list_user(db); break; case 3: break; @@ -707,9 +637,7 @@ struct user delete_user(struct user db,int index){ return read_db_user(); } -void print_user(struct user db, int cur, int end,struct Map* map); struct Map* sortUser(struct user db,int choice); -struct user showUser(struct user db,int index); struct user list_user(struct user db){ int choice = -1; int page = 0; @@ -729,9 +657,9 @@ struct user list_user(struct user db){ printf("5 sort ID decending\n"); printf("6 sort ID ascending\n"); if(page+1==total_pages){ - print_user(db,page*page_size,db.db.row_count,map); + print_user(&db,page*page_size,db.db.row_count,map); }else{ - print_user(db,page*page_size,(page+1)*page_size,map); + print_user(&db,page*page_size,(page+1)*page_size,map); } //page control int current_page_size = page_size+8; @@ -771,9 +699,9 @@ struct user list_user(struct user db){ total_pages = ceil((double)row / page_size); }else if(choice >= 7 && choice <= current_page_size){ if(map == NULL){ - db = showUser(db,choice - 7 + page_size*page); + db = *((struct user *)showUser(&db,choice - 7 + page_size*page)); }else{ - db = showUser(db,map[choice - 7 + page_size*page].key); + db = *((struct user *)showUser(&db,map[choice - 7 + page_size*page].key)); } }else if(choice != 0){ printf("Invalid choice\n"); @@ -784,7 +712,8 @@ struct user list_user(struct user db){ return db; } -void print_user(struct user db, int cur, int end,struct Map* map){ +void print_user(void * ddb, int cur, int end,struct Map* map){ + struct user db = *((struct user*)ddb); printf("%-5s%-20s%-10s%-10s%-10s\n","No.","Username","ID","Role","IsAdmin"); for(int i = cur; i < end; i++){ if(map == NULL){ @@ -846,7 +775,57 @@ struct Map* sortUser(struct user db,int choice){ return map; } -struct user showUser(struct user db,int index){ +struct Map* SortUser(void * ddb,int choice){ + struct user db = *((struct user*)ddb); + struct Map *map = malloc(sizeof(struct Map) * db.db.row_count); + for (int i = 0; i < db.db.row_count; i++){ + map[i].key = i; + switch(choice){ + case 1: + case 2:; + char* temp = malloc(sizeof(char) * 100); + strcpy(temp,db.row[i].name); + map[i].value = (void*)temp; + break; + + case 3: + case 4:; + char* temp2 = malloc(sizeof(char) * 100); + strcpy(temp2,db.row[i].role); + map[i].value = (void*)temp2; + break; + case 5: + case 6:; + long* tempstar3 = malloc(sizeof(long)); + *tempstar3 = db.row[i].id; + map[i].value = (void*)tempstar3; + break; + + } + } + switch (choice){ + case 1: + case 3: + qsort(map, db.db.row_count, sizeof(struct Map), compare_decending_str); + break; + case 2: + case 4: + qsort(map, db.db.row_count, sizeof(struct Map), compare_ascending_str); + break; + case 5: + qsort(map, db.db.row_count, sizeof(struct Map), compare_decending); + break; + case 6: + qsort(map, db.db.row_count, sizeof(struct Map), compare_ascending); + break; + + } + return map; +} + + +void * showUser(void * ddb,int index){ + struct user db = *((struct user*)ddb); int choice = -1; do{ Cls(); @@ -877,7 +856,9 @@ struct user showUser(struct user db,int index){ break; } }while(choice != 0); - return db; + void * re = malloc(sizeof(db)); + memcpy(re,&db,sizeof(db)); + return re; } //role control