Compare commits

..

2 Commits

Author SHA1 Message Date
b15ed51c8d
lister fix, list user fin 2023-09-08 00:48:29 +08:00
b6eb8d8756
list tran fin testing list user 2023-09-08 00:14:06 +08:00
3 changed files with 102 additions and 119 deletions

View File

@ -280,8 +280,11 @@ void remove_item(struct inventory db,int index){
} }
//tran control //tran control
void list_tran(struct transaction db);
void add_tran(); 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(){ void tran_control(){
struct transaction tran = read_db_tran(); struct transaction tran = read_db_tran();
int choice; int choice;
@ -299,8 +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);
list_tran(tran);
break; break;
case 2: case 2:
add_tran(tran); add_tran(tran);
@ -311,103 +313,20 @@ void tran_control(){
}while(choice != 3); }while(choice != 3);
} }
void print_tran(void * ddb, int cur, int end,struct Map* map){
void print_tran(struct transaction db, int cur, int end,struct Map* map); struct transaction db = *((struct transaction*)ddb);
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){
printf("%-5s%-15s%-10s%-10s%-10s%-10s%-10s%-10s\n","No.","Date","Time","Barcode","ID","Price","Quantity","Total"); 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++) 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
@ -419,13 +338,13 @@ void print_tran(struct transaction db, int cur, int end,struct Map* map){
} }
} }
} }
//show trans //show trans
struct transaction update_tran(struct transaction db,int index); struct transaction update_tran(struct transaction db,int index);
struct transaction remove_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; int choice;
do{ do{
printf("Transaction detail\n"); printf("Transaction detail\n");
@ -464,7 +383,9 @@ struct transaction showTran(struct transaction db,int index){
} }
}while(!valid); }while(!valid);
}while(choice != 3); }while(choice != 3);
return db; void * re = malloc(sizeof(db));
memcpy(re,&db,sizeof(db));
return re;
} }
//tran controls //tran controls
@ -561,10 +482,12 @@ struct transaction remove_tran(struct transaction db,int index){
//sort transaction //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); 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:;
@ -615,7 +538,9 @@ struct Map* sortTrans(struct transaction db,int choice){
//user control //user control
struct user add_user(struct user db); struct user add_user(struct user db);
struct user list_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(){ void user_control(){
struct user db = read_db_user(); struct user db = read_db_user();
int choice = 0; int choice = 0;
@ -633,6 +558,13 @@ void user_control(){
db = add_user(db); db = add_user(db);
break; break;
case 2: case 2:
char * SortItems[] = {
"Name",
"ID",
"Role",
};
lister(&db,NULL,db.db.row_count,"User",SortItems,3,print_user,SortUser,showUser);
db = read_db_user();
db = list_user(db); db = list_user(db);
break; break;
case 3: case 3:
@ -707,9 +639,7 @@ struct user delete_user(struct user db,int index){
return read_db_user(); 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 Map* sortUser(struct user db,int choice);
struct user showUser(struct user db,int index);
struct user list_user(struct user db){ struct user list_user(struct user db){
int choice = -1; int choice = -1;
int page = 0; int page = 0;
@ -729,9 +659,9 @@ struct user list_user(struct user db){
printf("5 sort ID decending\n"); printf("5 sort ID decending\n");
printf("6 sort ID ascending\n"); printf("6 sort ID ascending\n");
if(page+1==total_pages){ 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{ }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 //page control
int current_page_size = page_size+8; int current_page_size = page_size+8;
@ -771,9 +701,9 @@ struct user list_user(struct user db){
total_pages = ceil((double)row / page_size); total_pages = ceil((double)row / page_size);
}else if(choice >= 7 && choice <= current_page_size){ }else if(choice >= 7 && choice <= current_page_size){
if(map == NULL){ if(map == NULL){
db = showUser(db,choice - 7 + page_size*page); db = *((struct user *)showUser(&db,choice - 7 + page_size*page));
}else{ }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){ }else if(choice != 0){
printf("Invalid choice\n"); printf("Invalid choice\n");
@ -784,7 +714,8 @@ struct user list_user(struct user db){
return 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"); printf("%-5s%-20s%-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 == NULL){ if(map == NULL){
@ -846,7 +777,57 @@ struct Map* sortUser(struct user db,int choice){
return map; 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; int choice = -1;
do{ do{
Cls(); Cls();
@ -876,8 +857,10 @@ struct user showUser(struct user db,int index){
getchar(); getchar();
break; break;
} }
}while(choice != 0); }while(choice != 0&&choice!=2);
return db; void * re = malloc(sizeof(db));
memcpy(re,&db,sizeof(db));
return re;
} }
//role control //role control

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");