diff --git a/admin_user.h b/admin_user.h index 92716f0..073f126 100644 --- a/admin_user.h +++ b/admin_user.h @@ -411,6 +411,7 @@ void tran_control(){ 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){ @@ -427,8 +428,8 @@ void list_tran(struct transaction db){ printf("0 exit\n"); printf("1 sort Date&Time decending\n"); printf("2 sort Date&Time ascending\n"); - printf("3 sort price decending\n"); - printf("4 sort price 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"); @@ -478,6 +479,8 @@ void list_tran(struct transaction db){ }while(choice != 0); return; } + + void print_tran(struct transaction db, int cur, int end,struct Map* map){ for (int i = cur; i < end; i++) { @@ -491,6 +494,8 @@ void print_tran(struct transaction db, 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); @@ -540,31 +545,31 @@ void add_tran(){ free(row); return; } - strcpy(row->date.day,temp); + row->date.day = atoi(temp); temp = prompt_item("Please input date:month \n>"); if(temp == NULL){ free(row); return; } - strcpy(row->date.month,temp); + row->date.month = atoi(temp); temp = prompt_item("Please input date:year \n>"); if(temp == NULL){ free(row); return; } - strcpy(row->date.year,temp); + row->date.year = atoi(temp); temp = prompt_item("Please input time:hour \n>"); if(temp == NULL){ free(row); return; } - strcpy(row->time.hour,temp); + row->time.hour = atoi(temp); temp = prompt_item("Please input time:minute \n>"); if(temp == NULL){ free(row); return; } - strcpy(row->time.minute,temp); + row->time.minute = atoi(temp); temp = prompt_item("Please input price \n>"); if(temp == NULL){ free(row); @@ -589,7 +594,7 @@ void add_tran(){ return; } row->barcode = atol(temp); - if(row == NULL || !append_tran(row)){ + if(row == NULL || !append_transaction_db(row)){ printf("Failed to add item\n"); }else{ printf("Item added\n"); @@ -677,7 +682,7 @@ struct transaction update_tran(struct transaction db,int index){ return db; } -struct transaction delete_tran(struct transaction db,int index){ +struct transaction remove_tran(struct transaction db,int index){ db.row[index].isdeleted = true; if(!update_db_tran(db)){ printf("Failed to delete transaction\n"); @@ -690,4 +695,57 @@ struct transaction delete_tran(struct transaction db,int index){ getchar(); return read_db_tran(); -} \ No newline at end of file +} + +//sort transaction + +struct Map* sortTrans(struct transaction db,int choice){ + struct Map *map = malloc(sizeof(struct Map) * db.db.row_count); + for (int i = 0; i < db.db.row_count; i++){ + + switch(choice){ + case 1: + case 2: + long long temp = (long long)db.row[i].time.second+ db.row[i].time.minute*60 + db.row[i].time.hour*3600 + db.row[i].date.day*86400 + db.row[i].date.month*2592000 + db.row[i].date.year*31104000; + long long* tempstar = malloc(sizeof(long long)); + *tempstar = temp; + map[i].value = (void*)tempstar; + break; + + case 3: + case 4: + long* tempstar2 = malloc(sizeof(long)); + *tempstar2 = db.row[i].barcode; + map[i].value = (void*)tempstar2; + break; + case 5: + case 6: + int* tempstar3 = malloc(sizeof(int)); + *tempstar3 = db.row[i].quantity; + map[i].value = (void*)tempstar3; + break; + case 7: + case 8: + long temp4 = lround(db.row[i].price * db.row[i].quantity * 10000);//clear all decimal places + long* tempstar4 = malloc(sizeof(long)); + *tempstar4 = temp4; + map[i].value = (void*)tempstar4; + break; + } + } + switch (choice){ + case 1: + case 3: + case 5: + case 7: + qsort(map, db.db.row_count, sizeof(struct Map), compare_decending); + break; + case 2: + case 4: + case 6: + case 8: + qsort(map, db.db.row_count, sizeof(struct Map), compare_ascending); + break; + } + return map; +} \ No newline at end of file diff --git a/normal_user.h b/normal_user.h index fab2511..94b8d8b 100644 --- a/normal_user.h +++ b/normal_user.h @@ -188,6 +188,7 @@ struct Map* sortItems(struct inventory db, int sort){ map[i].key = i; if(db.row[i].barcode == -1024)//catch new item map[i].value = NULL; + continue; switch(sort){ case 1: @@ -198,7 +199,9 @@ struct Map* sortItems(struct inventory db, int sort){ case 3: case 4: long price = db.row[i].price * 100; - map[i].value = (long*)price;//presume there is no price contain 0.001 + long * price_star = malloc(sizeof(long)); + *price_star = price; + map[i].value = (void*)price_star;//presume there is no price contain 0.001 break; case 5: case 6: @@ -268,7 +271,8 @@ void search_item(){ printf("searching...\n"); map = searchItems(db,searchstr); if(map[0].value > 0){ - list_page(db,map+1,(int)map[0].value,show_item);//ofset map, as it is use to store the size + int temp_row = *((int*)map[0].value); + list_page(db,map+1,temp_row,show_item);//ofset map, as it is use to store the size }else{//empty search printf("No result found\n"); printf("Press any key to continue\n"); @@ -323,7 +327,9 @@ struct Map* searchItems(struct inventory db, char* searchstr){ k++; } } - map[0].value = (int*)(k-1); + int* k_star = malloc(sizeof(int)); + *k_star = --k; + map[0].value = (void*)k_star; map[0].key = -1; return map; }