diff --git a/admin_user.h b/admin_user.h index ac6e7fa..fdc77be 100644 --- a/admin_user.h +++ b/admin_user.h @@ -451,7 +451,7 @@ void list_tran(struct transaction db){ bool valid = true; do{ valid = true; - printf("Please input your choice: "); + printf("Please input your choice\n>"); fflush(stdin); scanf("%d",&choice); if(choice <=8 && choice > 0){ @@ -793,6 +793,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 user_control(){ struct user db = read_db_user(); int choice = 0; @@ -825,15 +828,15 @@ void user_control(){ } struct user add_user(struct user db){ - char temp[100]; + char* temp; int index = db.db.row_count; struct user_row *temprow = realloc(db.row, sizeof(struct user_row)); - temp = prompt_item("Please input the username\n>",temp); - strcpy(temprow.username,temp); - temp = prompt_item("Please input the id\n>",temp); - temprow.id = atol(temp); - temp = prompt_item("Please input the role\n>",temp); - strcpy(temprow.role,temp); + temp = prompt_item("Please input the username\n>"); + strcpy(temprow->name,temp); + temp = prompt_item("Please input the id\n>"); + temprow->id = atol(temp); + temp = prompt_item("Please input the role\n>"); + strcpy(temprow->role,temp); if(!append_user(temprow)){ printf("Failed to add user\n"); @@ -849,11 +852,11 @@ struct user add_user(struct user db){ } struct user edit_user(struct user db,int index){ char temp[100]; - printf("Please input the username(original:%s)\n>",db.row[index].username); + printf("Please input the username(original:%s)\n>",db.row[index].name); fflush(stdin); scanf("%[^\n]", temp); if(strcmp(temp,"") != 0){ - strcpy(db.row[index].username,temp); + strcpy(db.row[index].name,temp); } printf("Please input the id(original:%d)\n>",db.row[index].id); fflush(stdin); @@ -927,50 +930,58 @@ struct user list_user(struct user db){ 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); - if(choice <=6 && choice > 0){ - printf("sorting...\n"); - map = sortUser(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: "); + bool valid; + do{ + valid = true; + printf("Please input your choice\n>"); fflush(stdin); - scanf("%d", &page_size); - total_pages = ceil(row / page_size); - }else if(choice >= 7 && choice <= current_page_size){ - if(map == NULL){ - db = showUser(db,choice - 7 + page_size*page); - }else{ - db = showUser(db,map[choice - 7 + page_size*page].key); - } - }else if(choice != 0){ - printf("Invalid choice\n"); - valid = false; - } + scanf("%d",&choice); + if(choice <=6 && choice > 0){ + printf("sorting...\n"); + map = sortUser(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(row / page_size); + }else if(choice >= 7 && choice <= current_page_size){ + if(map == NULL){ + db = showUser(db,choice - 7 + page_size*page); + }else{ + db = showUser(db,map[choice - 7 + page_size*page].key); + } + }else if(choice != 0){ + printf("Invalid choice\n"); + valid = false; + } + }while(!valid); + }while(choice != 0); + return db; } void print_user(struct user db, int cur, int end,struct Map* map){ - printf("%-5s%-10s%-10s%-10s%-10s\n","No.","Username",,"ID""Role","IsAdmin"); + printf("%-5s%-10s%-10s%-10s%-10s\n","No.","Username","ID","Role","IsAdmin"); for(int i = cur; i < end; i++){ if(map[i].value != NULL){ - int index = (int)map[i].value; bool isadmin = is_admin(db.row[i].role); - printf("%-5d%-10s%-10ld%-10s%-10c\n",i+7,db.row[i].username,db.row[i].id,db.row[i].role,isadmin?'Y':'N'); + printf("%-5d%-10s%-10ld%-10s%-10c\n",i+7,db.row[i].name,db.row[i].id,db.row[i].role,isadmin?'Y':'N'); }else{ - bool isadmin = is_admin(db.row[(int)map[i].value].role); - printf("%-5s%-10s%-10ld%-10s%-10c\n",i+7,db.row[(int)map[i].value].username,db.row[(int)map[i].value].id,db.row[(int)map[i].value].role,isadmin?'Y':'N'); + bool isadmin = is_admin(db.row[map[i].key].role); + printf("%-5s%-10s%-10ld%-10s%-10c\n",i+7,db.row[map[i].key].name,db.row[map[i].key].id,db.row[map[i].key].role,isadmin?'Y':'N'); } } } @@ -983,7 +994,7 @@ struct Map* sortUser(struct user db,int choice){ case 1: case 2: char* temp = malloc(sizeof(char) * 100); - strcpy(temp,db.row[i].username); + strcpy(temp,db.row[i].name); map[i].value = (void*)temp; break; @@ -993,18 +1004,27 @@ struct Map* sortUser(struct user db,int choice){ 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: + case 5: qsort(map, db.db.row_count, sizeof(struct Map), compare_decending); break; case 2: case 4: + case 6: qsort(map, db.db.row_count, sizeof(struct Map), compare_ascending); break; + } return map; } @@ -1015,8 +1035,8 @@ struct user showUser(struct user db,int index){ system("cls"); welcome_message(); printf("User detail\n"); - printf("%-10s%-10s%-10s\n","Username","Role","ID"); - printf("%-10s%-10s%-10ld\n",db.row[index].username,db.row[index].role,db.row[index].id); + printf("%-10s%-10s%-10s\n","Username","ID","Role"); + printf("%-10s%-10ld%-10s\n",db.row[index].name,db.row[index].id,db.row[index].role); printf("0 exit\n"); printf("1 edit user\n"); printf("2 delete user\n");