224 lines
6.7 KiB
C
224 lines
6.7 KiB
C
|
|
//role control
|
|
void add_role();
|
|
void list_role();
|
|
void print_role(struct linkedlist* list,int cur,int end,struct Map* map);
|
|
void showRole(struct linkedlist* list,int index);
|
|
struct Map* sortRole(struct linkedlist* list,int choice);
|
|
|
|
void role_control(){
|
|
int choice = -1;
|
|
struct user db = read_db_user();
|
|
do{
|
|
char * items[] = {
|
|
"add admin role",
|
|
"list role"
|
|
};
|
|
choice = choices_selecter(items,2,"><Welcome to the Student Union POS system><\nRole control");
|
|
switch (choice){
|
|
case 0:
|
|
break;
|
|
case 1:
|
|
add_role();
|
|
break;
|
|
case 2:
|
|
char * SortItems[] = {
|
|
"admin",
|
|
"role",
|
|
};
|
|
void * list = getLinkedList(role_list_db(),0);
|
|
// lister(list,NULL,sizeofLinkedlist(list),"Role",SortItems,2,print_role,sortRole,showRole);
|
|
list_role(db);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}while(choice != 0);
|
|
}
|
|
|
|
|
|
void add_role(){
|
|
char role[100];
|
|
printf("Enter Admin role name: ");
|
|
fflush_stdin();
|
|
scanf("%s", role);
|
|
if(is_admin(role)){//check if role exist in admin file
|
|
printf("Admin role already exist\n");
|
|
printf("press any key to continue\n");
|
|
fflush_stdin();
|
|
getchar();
|
|
}else{
|
|
add_admin(role);
|
|
printf("role added\n");
|
|
printf("press any key to continue\n");
|
|
fflush_stdin();
|
|
getchar();
|
|
}
|
|
return;
|
|
}
|
|
|
|
|
|
|
|
void list_role(){
|
|
struct linkedlist* list = getLinkedList(role_list_db(),0);
|
|
int list_size = sizeofLinkedlist(list);
|
|
int choice = -1;
|
|
int page = 0;
|
|
int page_size = PAGE_SIZE;
|
|
int total_pages = ceil( (double)list_size / page_size);
|
|
struct Map* map = NULL;
|
|
//list role
|
|
do{
|
|
Cls();
|
|
welcome_message();
|
|
printf("Role list\n");
|
|
printf("0 exit\n");
|
|
printf("1 sort admin ascending\n");
|
|
printf("2 sort admin descending\n");
|
|
printf("3 sort role ascending\n");
|
|
printf("4 sort role descending\n");
|
|
if(page+1 == total_pages){
|
|
print_role(list,page*page_size,list_size,map);
|
|
}else{
|
|
print_role(list,page*page_size,(page+1)*page_size,map);
|
|
}
|
|
//page control
|
|
int current_page_size = page_size+2;
|
|
if(page+1 == total_pages){
|
|
current_page_size = list_size - page*page_size + 4;
|
|
}
|
|
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 <=4 && choice > 0){
|
|
printf("sorting...\n");
|
|
map = sortRole(list,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)list_size / page_size);
|
|
page = 0;
|
|
|
|
}else if(choice >= 5 && choice <= current_page_size){
|
|
if(map == NULL){
|
|
showRole(list,choice - 5 + page_size*page);
|
|
}else{
|
|
showRole(list,map[choice - 5 + page_size*page].key);
|
|
}
|
|
}else if(choice != 0){
|
|
printf("Invalid choice\n");
|
|
valid = false;
|
|
}
|
|
}while(!valid);
|
|
}while(choice != 0);
|
|
}
|
|
|
|
void print_role(struct linkedlist* list,int cur,int end,struct Map* map){
|
|
printf("%-10s%-10s%-10s\n","No.","Role","Admin?");
|
|
if(map == NULL){
|
|
for(int i = cur; i < end; i++){
|
|
list = getLinkedList(list,i);
|
|
char* name = list->data;
|
|
printf("%-10d%-10s%-10s\n",i-cur + 5,name,is_admin(name)?"T":"F");
|
|
}
|
|
}else{
|
|
for(int i = cur; i < end; i++){
|
|
list = getLinkedList(list,map[i].key);
|
|
char* name = list->data;
|
|
printf("%-10d%-10s%-10s\n",i-cur + 5,name,is_admin(name)?"T":"F");
|
|
}
|
|
}
|
|
}
|
|
|
|
void showRole(struct linkedlist* list,int index){
|
|
int choice;
|
|
list = getLinkedList(list,index);
|
|
do{
|
|
char* name = list->data;
|
|
printf("%-10s%-10s\n","Role","Admin?");
|
|
printf("%-10s%-10s\n",name,is_admin(name)?"T":"F");
|
|
printf("1 toogle admin\n");
|
|
printf("2 exit\n");
|
|
bool valid = true;
|
|
do{
|
|
valid = true;
|
|
printf("Please input your choice\n>");
|
|
fflush_stdin();
|
|
scanf("%d", &choice);
|
|
if(choice == 1){
|
|
if(is_admin(name)){
|
|
remove_admin(name);
|
|
}else{
|
|
add_admin(name);
|
|
}
|
|
printf("Admin role toggled\n");
|
|
printf("Press any key to continue\n");
|
|
fflush_stdin();
|
|
getchar();
|
|
}else if(choice != 2){
|
|
printf("Invalid choice\n");
|
|
valid = false;
|
|
}
|
|
}while(!valid);
|
|
}while(choice != 2);
|
|
return;
|
|
}
|
|
|
|
|
|
struct Map* sortRole(struct linkedlist* list,int choice){
|
|
struct Map* map = malloc(sizeof(struct Map)*sizeofLinkedlist(list));
|
|
struct linkedlist* cur = getLinkedList(list,0);
|
|
for(int i = 0; i < sizeofLinkedlist(list); i++){
|
|
switch(choice){
|
|
case 1:
|
|
case 2:
|
|
map[i].key = i;
|
|
bool temp = !is_admin(cur->data);
|
|
bool* tempstar = malloc(sizeof(bool));
|
|
*tempstar = temp;
|
|
map[i].value = tempstar;
|
|
break;
|
|
case 3:
|
|
case 4:
|
|
map[i].key = i;
|
|
map[i].value = cur->data;
|
|
break;
|
|
}
|
|
cur = cur->next;
|
|
}
|
|
switch(choice){
|
|
case 1:
|
|
case 3:
|
|
qsort(map,sizeofLinkedlist(list),sizeof(struct Map),compare_decending_str);
|
|
break;
|
|
case 2:
|
|
case 4:
|
|
qsort(map,sizeofLinkedlist(list),sizeof(struct Map),compare_ascending_str);
|
|
break;
|
|
}
|
|
return map;
|
|
}
|