Compare commits

..

No commits in common. "b15ed51c8d848c712da0ddd4b7b9c5e5a45fb71e" and "70e46087c2c5aba591d5ab664329c71de3146a93" have entirely different histories.

3 changed files with 119 additions and 102 deletions

View File

@ -280,11 +280,8 @@ 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;
@ -302,7 +299,8 @@ void tran_control(){
"quantity",
"total",
};
lister(&tran,NULL,tran.db.row_count,"Transaction",SortItems,4,print_tran,sortTrans,showTran);
lister(&tran,NULL,tran.db.row_count,"Transaction list",SortItems,4,print_tran,sortTrans,showTran);)
list_tran(tran);
break;
case 2:
add_tran(tran);
@ -313,20 +311,103 @@ void tran_control(){
}while(choice != 3);
}
void print_tran(void * ddb, int cur, int end,struct Map* map){
struct transaction db = *((struct transaction*)ddb);
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){
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++)
{
if(map != NULL){
int index = map[i].key;
double total = db.row[index].price * db.row[index].quantity;
double total = db.row[map[i].key].price * db.row[map[i].key].quantity;
//reconstuct date and time and print all transaction info out
char date[11];
char time[9];
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[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[index].barcode,db.row[index].id,db.row[index].price,db.row[index].quantity,total);
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(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);
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);
}else{
double total = db.row[i].price * db.row[i].quantity;
//reconstuct date and time and print all transaction info out
@ -338,13 +419,13 @@ void print_tran(void * ddb, 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);
void * showTran(void * ddb,int index){
struct transaction db = *((struct transaction*)ddb);
struct transaction showTran(struct transaction db,int index){
int choice;
do{
printf("Transaction detail\n");
@ -383,9 +464,7 @@ void * showTran(void * ddb,int index){
}
}while(!valid);
}while(choice != 3);
void * re = malloc(sizeof(db));
memcpy(re,&db,sizeof(db));
return re;
return db;
}
//tran controls
@ -482,12 +561,10 @@ struct transaction remove_tran(struct transaction db,int index){
//sort transaction
struct Map* sortTrans(void * ddb,int choice){
struct transaction db = *((struct transaction*)ddb);
struct Map* sortTrans(struct transaction db,int choice){
struct Map *map = malloc(sizeof(struct Map) * db.db.row_count);
printf("l");
for (int i = 0; i < db.db.row_count; i++){
map[i].key = i;
switch(choice){
case 1:
case 2:;
@ -538,9 +615,7 @@ struct Map* sortTrans(void * ddb,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;
@ -558,13 +633,6 @@ void user_control(){
db = add_user(db);
break;
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);
break;
case 3:
@ -639,7 +707,9 @@ 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;
@ -659,9 +729,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;
@ -701,9 +771,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 = *((struct user *)showUser(&db,choice - 7 + page_size*page));
db = showUser(db,choice - 7 + page_size*page);
}else{
db = *((struct user *)showUser(&db,map[choice - 7 + page_size*page].key));
db = showUser(db,map[choice - 7 + page_size*page].key);
}
}else if(choice != 0){
printf("Invalid choice\n");
@ -714,8 +784,7 @@ struct user list_user(struct user db){
return db;
}
void print_user(void * ddb, int cur, int end,struct Map* map){
struct user db = *((struct user*)ddb);
void print_user(struct user db, int cur, int end,struct Map* map){
printf("%-5s%-20s%-10s%-10s%-10s\n","No.","Username","ID","Role","IsAdmin");
for(int i = cur; i < end; i++){
if(map == NULL){
@ -777,57 +846,7 @@ struct Map* sortUser(struct user db,int choice){
return map;
}
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);
struct user showUser(struct user db,int index){
int choice = -1;
do{
Cls();
@ -857,10 +876,8 @@ void * showUser(void * ddb,int index){
getchar();
break;
}
}while(choice != 0&&choice!=2);
void * re = malloc(sizeof(db));
memcpy(re,&db,sizeof(db));
return re;
}while(choice != 0);
return db;
}
//role control

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();
printf("%s list\n",lister_name);
printf("0 exit\n");
for(int i=0;i<NoSortItems*2;i+=2){
printf("%d sort %s decending\n",i+1,SortItems[i/2]);
printf("%d sort %s ascending\n",i+2,SortItems[i/2]);
for(int i=1;i<=NoSortItems*2;i+=2){
printf("%d sort %s decending\n",i,SortItems[i/2]);
printf("%d sort %s ascending\n",i+1,SortItems[i/2]);
}
if(page+1 == total_pages){
(*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 control
int current_page_size = page_size+NoSortItems*2;
int current_page_size = page_size+8;
if(page+1 == total_pages){
current_page_size = row - page*page_size+NoSortItems*2;
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);
@ -142,7 +142,7 @@ void lister(void * db,struct Map* map, int row,char * lister_name,char * SortIte
printf("Please input your choice\n>");
fflush_stdin();
scanf("%d",&choice);
if(choice <=NoSortItems*2 && choice > 0){
if(choice <=NoSortItems && choice > 0){
printf("sorting...\n");
map = (*sorter)(db,choice);
}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();
scanf("%d", &page_size);
total_pages = ceil((double)row / page_size);
}else if(choice > NoSortItems*2 && choice <= current_page_size){
}else if(choice >= 9 && choice <= current_page_size){
if(map == NULL){
db = (*showitem)(db,choice - NoSortItems*2-1 + page_size*page);
db = (*showitem)(db,choice - 9 + page_size*page);
}else{
db = (*showitem)(db,map[choice - NoSortItems*2-1 + page_size*page].key);
db = (*showitem)(db,map[choice - 9 + page_size*page].key);
}
}else if(choice != 0){
printf("Invalid choice\n");