error fixed,compiled, not yet test
This commit is contained in:
parent
58b918c9bb
commit
0d05bb4aa4
118
admin_user.h
118
admin_user.h
@ -451,7 +451,7 @@ void list_tran(struct transaction db){
|
|||||||
bool valid = true;
|
bool valid = true;
|
||||||
do{
|
do{
|
||||||
valid = true;
|
valid = true;
|
||||||
printf("Please input your choice: ");
|
printf("Please input your choice\n>");
|
||||||
fflush(stdin);
|
fflush(stdin);
|
||||||
scanf("%d",&choice);
|
scanf("%d",&choice);
|
||||||
if(choice <=8 && choice > 0){
|
if(choice <=8 && choice > 0){
|
||||||
@ -793,6 +793,9 @@ struct Map* sortTrans(struct transaction db,int choice){
|
|||||||
}
|
}
|
||||||
|
|
||||||
//user control
|
//user control
|
||||||
|
struct user add_user(struct user db);
|
||||||
|
struct user list_user(struct user db);
|
||||||
|
|
||||||
void user_control(){
|
void user_control(){
|
||||||
struct user db = read_db_user();
|
struct user db = read_db_user();
|
||||||
int choice = 0;
|
int choice = 0;
|
||||||
@ -825,15 +828,15 @@ void user_control(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct user add_user(struct user db){
|
struct user add_user(struct user db){
|
||||||
char temp[100];
|
char* temp;
|
||||||
int index = db.db.row_count;
|
int index = db.db.row_count;
|
||||||
struct user_row *temprow = realloc(db.row, sizeof(struct user_row));
|
struct user_row *temprow = realloc(db.row, sizeof(struct user_row));
|
||||||
temp = prompt_item("Please input the username\n>",temp);
|
temp = prompt_item("Please input the username\n>");
|
||||||
strcpy(temprow.username,temp);
|
strcpy(temprow->name,temp);
|
||||||
temp = prompt_item("Please input the id\n>",temp);
|
temp = prompt_item("Please input the id\n>");
|
||||||
temprow.id = atol(temp);
|
temprow->id = atol(temp);
|
||||||
temp = prompt_item("Please input the role\n>",temp);
|
temp = prompt_item("Please input the role\n>");
|
||||||
strcpy(temprow.role,temp);
|
strcpy(temprow->role,temp);
|
||||||
|
|
||||||
if(!append_user(temprow)){
|
if(!append_user(temprow)){
|
||||||
printf("Failed to add user\n");
|
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){
|
struct user edit_user(struct user db,int index){
|
||||||
char temp[100];
|
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);
|
fflush(stdin);
|
||||||
scanf("%[^\n]", temp);
|
scanf("%[^\n]", temp);
|
||||||
if(strcmp(temp,"") != 0){
|
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);
|
printf("Please input the id(original:%d)\n>",db.row[index].id);
|
||||||
fflush(stdin);
|
fflush(stdin);
|
||||||
@ -927,50 +930,58 @@ struct user list_user(struct user db){
|
|||||||
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);
|
||||||
printf("%d set page size\n",current_page_size+3);
|
printf("%d set page size\n",current_page_size+3);
|
||||||
if(choice <=6 && choice > 0){
|
bool valid;
|
||||||
printf("sorting...\n");
|
do{
|
||||||
map = sortUser(db,choice);
|
valid = true;
|
||||||
}else if(choice == current_page_size+1 ){
|
printf("Please input your choice\n>");
|
||||||
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);
|
fflush(stdin);
|
||||||
scanf("%d", &page_size);
|
scanf("%d",&choice);
|
||||||
total_pages = ceil(row / page_size);
|
if(choice <=6 && choice > 0){
|
||||||
}else if(choice >= 7 && choice <= current_page_size){
|
printf("sorting...\n");
|
||||||
if(map == NULL){
|
map = sortUser(db,choice);
|
||||||
db = showUser(db,choice - 7 + page_size*page);
|
}else if(choice == current_page_size+1 ){
|
||||||
}else{
|
if(page + 1 < total_pages){
|
||||||
db = showUser(db,map[choice - 7 + page_size*page].key);
|
page++;
|
||||||
}
|
}else{
|
||||||
}else if(choice != 0){
|
printf("Already at last page\n");
|
||||||
printf("Invalid choice\n");
|
valid = false;
|
||||||
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){
|
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++){
|
for(int i = cur; i < end; i++){
|
||||||
if(map[i].value != NULL){
|
if(map[i].value != NULL){
|
||||||
int index = (int)map[i].value;
|
|
||||||
bool isadmin = is_admin(db.row[i].role);
|
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{
|
}else{
|
||||||
bool isadmin = is_admin(db.row[(int)map[i].value].role);
|
bool isadmin = is_admin(db.row[map[i].key].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');
|
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 1:
|
||||||
case 2:
|
case 2:
|
||||||
char* temp = malloc(sizeof(char) * 100);
|
char* temp = malloc(sizeof(char) * 100);
|
||||||
strcpy(temp,db.row[i].username);
|
strcpy(temp,db.row[i].name);
|
||||||
map[i].value = (void*)temp;
|
map[i].value = (void*)temp;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -993,18 +1004,27 @@ struct Map* sortUser(struct user db,int choice){
|
|||||||
strcpy(temp2,db.row[i].role);
|
strcpy(temp2,db.row[i].role);
|
||||||
map[i].value = (void*)temp2;
|
map[i].value = (void*)temp2;
|
||||||
break;
|
break;
|
||||||
|
case 5:
|
||||||
|
case 6:
|
||||||
|
long* tempstar3 = malloc(sizeof(long));
|
||||||
|
*tempstar3 = db.row[i].id;
|
||||||
|
map[i].value = (void*)tempstar3;
|
||||||
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
switch (choice){
|
switch (choice){
|
||||||
case 1:
|
case 1:
|
||||||
case 3:
|
case 3:
|
||||||
|
case 5:
|
||||||
qsort(map, db.db.row_count, sizeof(struct Map), compare_decending);
|
qsort(map, db.db.row_count, sizeof(struct Map), compare_decending);
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
case 4:
|
case 4:
|
||||||
|
case 6:
|
||||||
qsort(map, db.db.row_count, sizeof(struct Map), compare_ascending);
|
qsort(map, db.db.row_count, sizeof(struct Map), compare_ascending);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
@ -1015,8 +1035,8 @@ struct user showUser(struct user db,int index){
|
|||||||
system("cls");
|
system("cls");
|
||||||
welcome_message();
|
welcome_message();
|
||||||
printf("User detail\n");
|
printf("User detail\n");
|
||||||
printf("%-10s%-10s%-10s\n","Username","Role","ID");
|
printf("%-10s%-10s%-10s\n","Username","ID","Role");
|
||||||
printf("%-10s%-10s%-10ld\n",db.row[index].username,db.row[index].role,db.row[index].id);
|
printf("%-10s%-10ld%-10s\n",db.row[index].name,db.row[index].id,db.row[index].role);
|
||||||
printf("0 exit\n");
|
printf("0 exit\n");
|
||||||
printf("1 edit user\n");
|
printf("1 edit user\n");
|
||||||
printf("2 delete user\n");
|
printf("2 delete user\n");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user