diff --git a/admin_user.h b/admin_user.h index 4964ebc..92716f0 100644 --- a/admin_user.h +++ b/admin_user.h @@ -42,7 +42,7 @@ void admin_menu(){ inv_control(); break; case 2://action with transction - // tran_control(); + tran_control(); break; case 3://action with user // user_control(); @@ -344,6 +344,7 @@ void update_item(struct inventory db,int index){ if(!update_db_invt(db)){ printf("Failed to update item\n"); + exit(1); }else{ printf("Item updated\n"); } @@ -373,3 +374,320 @@ void remove_item(struct inventory db,int index){ getchar(); } +//tran control +void list_tran(struct transaction db); +void add_tran(); +void tran_control(){ + struct transaction tran = read_db_tran(); + int choice; + do{ + system("cls"); + welcome_message(); + printf("Transaction control\n"); + printf("1. List transaction\n"); + printf("2. new transaction\n"); + printf("3. Exit\n"); + printf("Please input your choice: "); + fflush(stdin); + scanf("%d",&choice); + switch(choice){ + case 1: + list_tran(tran); + break; + case 2: + add_tran(tran); + break; + case 3: + break; + default: + printf("Invalid input, try again\n"); + printf("press any key to continue\n"); + fflush(stdin); + getchar(); + break; + } + }while(choice != 5); +} + + +void print_tran(struct transaction db, int cur, int end,struct Map* map); +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{ + system("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 price decending\n"); + printf("4 sort price 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"); + printf("No. Date Time Price Quantity Total\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 + printf("%d next page\n", page_size+3); + printf("%d previous page\n", page_size+4); + printf("%d set page size\n", page_size+5); + 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: "); + fflush(stdin); + scanf("%d",&choice); + if(choice <=8 && choice > 0){ + printf("sorting...\n"); + map = sortTrans(db,choice); + }else if(choice == page_size+3 && page + 1 < total_pages){ + page++; + }else if(choice == page_size+4 && page > 0){ + page--; + }else if(choice == page_size+5){ + printf("Enter page size: "); + fflush(stdin); + scanf("%d", &page_size); + total_pages = ceil(row / page_size); + }else if(choice >= 9 && choice < row+9){ + 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){ + for (int i = cur; i < end; i++) + { + if(map != NULL){ + double total = db.row[map[i].key].price * db.row[map[i].key].quantity; + printf("%-4d%-12s%-12s$%-12.1lf%-12d$%-12.1lf\n",i+1,db.row[map[i].key].date,db.row[map[i].key].time,db.row[map[i].key].price,db.row[map[i].key].quantity,total); + }else{ + double total = db.row[i].price * db.row[i].quantity; + printf("%-4d%-12s%-12s$%-12.1lf%-12d$%-12.1lf\n",i+1,db.row[i].date,db.row[i].time,db.row[i].price,db.row[i].quantity,total); + } + } +} + +struct transaction update_tran(struct transaction db,int index); +struct transaction remove_tran(struct transaction db,int index); + +struct transaction showTran(struct transaction db,int index){ + printf("Transaction detail\n"); + printf("Date:day %s\n",db.row[index].date.day); + printf("Date:month %s\n",db.row[index].date.month); + printf("Date:year %s\n",db.row[index].date.year); + printf("Time:hour %s\n",db.row[index].time.hour); + printf("Time:minute %s\n",db.row[index].time.minute); + printf("Time:second %s\n",db.row[index].time.second); + printf("Price: $%.1lf\n",db.row[index].price); + printf("Quantity: %d\n",db.row[index].quantity); + printf("ID: %d\n",db.row[index].id); + printf("Barcode: %s\n",db.row[index].barcode); + printf("Total: $%.1lf\n",db.row[index].price * db.row[index].quantity); + printf("1 edit transaction\n"); + printf("2 delete transaction\n"); + printf("3 exit\n"); + int choice; + do{ + printf("Please input your choice: "); + fflush(stdin); + scanf("%d",&choice); + switch(choice){ + case 1: + db = update_tran(db,index); + break; + case 2: + db = remove_tran(db,index); + break; + case 3: + break; + default: + printf("Invalid input, try again\n"); + break; + } + }while(choice != 3); +} + +//tran controls +void add_tran(){ + char* temp; + struct transaction_row* row = (struct transaction_row*)malloc(sizeof(struct transaction_row)); + temp = prompt_item("Please input date:day \n>"); + if(temp == NULL){ + free(row); + return; + } + strcpy(row->date.day,temp); + temp = prompt_item("Please input date:month \n>"); + if(temp == NULL){ + free(row); + return; + } + strcpy(row->date.month,temp); + temp = prompt_item("Please input date:year \n>"); + if(temp == NULL){ + free(row); + return; + } + strcpy(row->date.year,temp); + temp = prompt_item("Please input time:hour \n>"); + if(temp == NULL){ + free(row); + return; + } + strcpy(row->time.hour,temp); + temp = prompt_item("Please input time:minute \n>"); + if(temp == NULL){ + free(row); + return; + } + strcpy(row->time.minute,temp); + temp = prompt_item("Please input price \n>"); + if(temp == NULL){ + free(row); + return; + } + row->price = atof(temp); + temp = prompt_item("Please input quantity \n>"); + if(temp == NULL){ + free(row); + return; + } + row->quantity = atoi(temp); + temp = prompt_item("Please input ID \n>"); + if(temp == NULL){ + free(row); + return; + } + row->id = atol(temp); + temp = prompt_item("Please input barcode \n>"); + if(temp == NULL){ + free(row); + return; + } + row->barcode = atol(temp); + if(row == NULL || !append_tran(row)){ + printf("Failed to add item\n"); + }else{ + printf("Item added\n"); + } + printf("press any key to continue\n"); + fflush(stdin); + getchar(); + + +} + +struct transaction update_tran(struct transaction db,int index){ + char temp[100]; + printf("Update transaction(empty value to not change the value)\n"); + printf("Please input the Date:day(original:%s)\n>",db.row[index].date.day); + fflush(stdin); + scanf("%[^\n]", temp);//input until /n + if(strcmp(temp,"") != 0){//check if temp is not empty + db.row[index].date.day = atoi(temp); + }//else preserve the original value + printf("Please input the Date:month(original:%s)\n>",db.row[index].date.month); + fflush(stdin); + scanf("%[^\n]", temp); + if(strcmp(temp,"") != 0){ + db.row[index].date.month = atoi(temp); + } + printf("Please input the Date:year(original:%s)\n>",db.row[index].date.year); + fflush(stdin); + scanf("%[^\n]", temp); + if(strcmp(temp,"") != 0){ + db.row[index].date.year = atoi(temp); + } + printf("Please input the Time:hour(original:%s)\n>",db.row[index].time.hour); + fflush(stdin); + scanf("%[^\n]", temp); + if(strcmp(temp,"") != 0){ + db.row[index].time.hour = atoi(temp); + } + printf("Please input the Time:minute(original:%s)\n>",db.row[index].time.minute); + fflush(stdin); + scanf("%[^\n]", temp); + if(strcmp(temp,"") != 0){ + db.row[index].time.minute = atoi(temp); + } + printf("Please input the Time:second(original:%s)\n>",db.row[index].time.second); + fflush(stdin); + scanf("%[^\n]", temp); + if(strcmp(temp,"") != 0){ + db.row[index].time.second = atoi(temp); + } + printf("Please input the Price(original:%.1lf)\n>",db.row[index].price); + fflush(stdin); + scanf("%[^\n]", temp); + if(strcmp(temp,"") != 0){ + db.row[index].price = atof(temp); + } + printf("Please input the Quantity(original:%d)\n>",db.row[index].quantity); + fflush(stdin); + scanf("%[^\n]", temp); + if(strcmp(temp,"") != 0){ + db.row[index].quantity = atoi(temp); + } + printf("Please input the ID(original:%d)\n>",db.row[index].id); + fflush(stdin); + scanf("%[^\n]", temp); + if(strcmp(temp,"") != 0){ + db.row[index].id = atol(temp); + } + printf("Please input the Barcode(original:%d)\n>",db.row[index].barcode); + fflush(stdin); + scanf("%[^\n]", temp); + if(strcmp(temp,"") != 0){ + db.row[index].barcode = atol(temp); + } + if(!update_db_tran(db)){ + printf("Failed to update transaction\n"); + exit(1);//exit program to prevent errors + }else{ + printf("Transaction updated\n"); + } + printf("press any key to continue\n"); + fflush(stdin); + getchar(); + + return db; +} + +struct transaction delete_tran(struct transaction db,int index){ + db.row[index].isdeleted = true; + if(!update_db_tran(db)){ + printf("Failed to delete transaction\n"); + exit(1);//exit program to prevent errors + }else{ + printf("Transaction deleted\n"); + } + printf("press any key to continue\n"); + fflush(stdin); + getchar(); + + return read_db_tran(); +} \ No newline at end of file diff --git a/filecontrol.h b/filecontrol.h deleted file mode 100644 index 186bd61..0000000 --- a/filecontrol.h +++ /dev/null @@ -1,11 +0,0 @@ -//include std lib if they havent been included -#ifndef STD_LIB_H -#define STD_LIB_H -#include -#include -#include -#include -#include -#include -#include -#endif diff --git a/normal_user.h b/normal_user.h index a2b9b08..fab2511 100644 --- a/normal_user.h +++ b/normal_user.h @@ -140,7 +140,11 @@ void list_page(struct inventory db,struct Map* map,int row,struct inventory (*sh scanf("%d", &page_size); total_pages = ceil(row / page_size); }else if(choice >= 9 && choice < row+9){ - db = (*showitem)(db,choice - 9 + page_size*page); + if(map == NULL){ + db = (*showitem)(db,choice - 9 + page_size*page); + }else{ + db = (*showitem)(db,map[choice - 9 + page_size*page].key); + } }else if(choice != 0){ printf("Invalid choice\n"); valid = false;