From 73672b721135da8c259ce0704c05ca32fe3035fa Mon Sep 17 00:00:00 2001 From: stmctommyau Date: Mon, 5 Sep 2022 12:05:19 +0800 Subject: [PATCH] storage control on going; inv control near fin; add new db functions need clean up --- .gitignore | 1 - .vscode/settings.json | 1 + admin_user.h | 312 ++++++++++++++++++++++++++++++++++++++---- database.h | 91 +++++++++++- main.c | 2 +- normal_user.h | 1 - 6 files changed, 379 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index 3fcd254..8808f8b 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,3 @@ main.exe .vscode/* .vscode/settings.json data_file_transaction.txt - diff --git a/.vscode/settings.json b/.vscode/settings.json index 0f06797..eef5657 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,4 +2,5 @@ "files.associations": { "stdio.h": "c" } + "git.enableCommitSigning": false } \ No newline at end of file diff --git a/admin_user.h b/admin_user.h index 6f2f496..8c45f21 100644 --- a/admin_user.h +++ b/admin_user.h @@ -15,30 +15,292 @@ #include "menu.h" #endif //MENU_H -// void admin_menu(){ -// system("cls"); -// welcome_message(); -// //the selection menu -// // if(login valid) -// int choice = admin_menu_user_choices(); -// switch (choice) -// { -// case 1://action with inventory -// inv_control(); -// break; -// case 2://action with transction -// tran_control(); -// break; -// case 3://action with user -// user_control(); -// break; -// case 4://Exit -// break; -// default://invalid input ,should not happen,as it is already catch in above function -// printf("Invalid choice\n"); -// break; -// } +//list of functions +void inv_control(); +void tran_control(); +void user_control(); +void admin_control(); +struct user_row *prompt_user(); +int admin_menu_user_choices(char* name,char* role); + +void admin_menu(){ + system("cls"); + welcome_message(); + //the selection menu + // if(login valid) + struct user_row *user = prompt_user(); + if(user == NULL){//invalid user + return; + } + int choice = 0; + do + { + choice = admin_menu_user_choices(user->name,user->role); + switch (choice) + { + case 1://action with inventory + inv_control(); + break; + case 2://action with transction + // tran_control(); + break; + case 3://action with user + // user_control(); + break; + case 4://action with admin user register + // admin_control(); + break; + case 5://Exit + break; + default://invalid input ,should not happen,as it is already catch in above function + printf("Invalid choice\n"); + break; + } + }while(choice != 5); -// return true; -// } \ No newline at end of file + return; +} + +//func for main for admin +struct user_row *prompt_user(){ + long user_id = 0; + printf("Please tap your card(student card, staff card,etc)(or input the id)(0 to cancel)\n>"); + scanf("%ld", &user_id); + if(user_id == 0){ + printf("cancelled\n"); + printf("press any key to continue\n"); + fflush(stdin); + getchar(); + return NULL; + } + struct user_row *user = get_user(user_id); + if(user == NULL){ + printf("User not found\n"); + printf("press any key to continue\n"); + fflush(stdin); + getchar(); + return NULL; + }else if(!is_admin(user->role)){ + printf("You aren't an admin\n"); + printf("press any key to continue\n"); + fflush(stdin); + getchar(); + return NULL; + }else{ + printf("Welcome %s(%s)\n", user->name,user->role); + printf("press any key to continue\n"); + fflush(stdin); + getchar(); + } + return user; +} + +int admin_menu_user_choices(char* name,char* role){ + int choice = 0; + do{ + system("cls"); + printf("Welcome %s(%s)\n", name,role); + printf("Please select an option:\n"); + printf("1. Inventory control\n"); + printf("2. Transaction control\n"); + printf("3. User control\n"); + printf("4. Admin user register\n"); + printf("5. Exit and Logout\n"); + printf(">"); + scanf("%d", &choice); + if(choice < 1 || choice > 5){ + printf("Invalid choice...press any key to retry....\n"); + fflush(stdin); + getchar(); + } + }while(choice < 1 || choice > 5); + return choice; +} + +//invetory control +void add_item(); +void remove_item(); +void update_item(); +void view_item(); +void admin_list_pages(struct inventory db); + +void inv_control(){ + system("cls"); + struct inventory db = read_db_invt(); + admin_list_pages(db,NULL,db.row); +} + +void item_control(struct inventory db,int index); +void admin_list_pages(struct inventory db,struct Map* map,int row){//user for showing list result and search result + int choice = -1; + int page = 0; + int page_size = PAGE_SIZE; + int total_pages = ceil((double)row / page_size); + do{ + + //options + system("cls"); + printf("0 exit\n"); + printf("1 sort name decending\n"); + printf("2 sort name ascending\n"); + printf("3 sort price decending\n"); + printf("4 sort price ascending\n"); + printf("5 sort brand decending\n"); + printf("6 sort brand ascending\n"); + printf("7 sort category decending\n"); + printf("8 sort category ascending\n"); + printf("List of items:\n"); + + //print items + if(page + 1 >= total_pages){ + print_page(db, page * page_size, row,map); + }else{//sorted) + print_page(db, page * page_size, (page + 1) * page_size,map); + } + printf("%d new item",page_size+4); + //page control + printf("%d next page\n", page_size+5); + printf("%d previous page\n", page_size+6); + printf("%d set page size\n", page_size+7); + printf("%d/%d/%d(page size/page number/total)\n",page_size, page+1,total_pages); + + //prompt user to select an item + bool valid = true; + do{ + valid = true; + printf("Enter your choice: "); + fflush(stdin); + scanf("%d", &choice); + if(choice <=8 && choice > 0){ + printf("sorting...\n"); + map = sortItems(db,choice); + }else if(choice == page_size+3){ + add_item(); + }else if(choice == page_size+4 && page + 1 < total_pages){ + page++; + }else if(choice == page_size+5 && page > 0){ + page--; + }else if(choice == page_size+6){ + printf("Enter page size: "); + fflush(stdin); + scanf("%d", &page_size); + total_pages = ceil(row / page_size); + }else if(choice >= 9 && choice < row+9){ + item_control(db,choice - 9 + page_size*page); + }else if(choice != 0){ + printf("Invalid choice\n"); + valid = false; + } + }while(!valid); + + }while(choice != 0); +} + +void item_control(struct inventory db,int index){ + int choice = 0; + do + { + system("cls"); + show_item(db,index); + printf("0 exit\n"); + printf("1 update item\n"); + printf("2 remove item\n"); + printf("Enter your choice: "); + fflush(stdin); + scanf("%d", &choice); + switch (choice) + { + case 1: + update_item(db,index); + break; + case 2: + remove_item(db,index); + break; + default: + break; + } + } while (choice != 0); +} + +void add_item(){ + system("cls"); + printf("Add new item\n"); + printf("Please input the item name\n>"); + struct inventory_row *item; + fflush(stdin); + scanf("%[^\n]", item->product); + printf("Please input the item brand\n>"); + fflush(stdin); + scanf("%[^\n]", item->brand); + printf("Please input the item price\n>"); + scanf("%lf", &item->price); + printf("Please input the item stock\n>"); + scanf("%d", &item->stock); + printf("Please input the item category\n>"); + fflush(stdin); + scanf("%[^\n]", item->category); + printf("Please input the item barcode\n>"); + fflush(stdin); + scanf("%[^\n]", item->barcode); + if(item == NULL || !append_inventory(item)){ + printf("Failed to add item\n"); + }else{ + printf("Item added\n"); + } + printf("press any key to continue\n"); + fflush(stdin); + getchar(); +} + +void update_item(struct inventory db,int index){ + system("cls"); + printf("Update item\n"); + printf("Please input the item name\n>"); + fflush(stdin); + scanf("%[^\n]", db.row[index].product); + printf("Please input the item brand\n>"); + fflush(stdin); + scanf("%[^\n]", db.row[index].brand); + printf("Please input the item price\n>"); + scanf("%lf", &db.row[index].price); + printf("Please input the item stock\n>"); + scanf("%d", &db.row[index].stock); + printf("Please input the item category\n>"); + fflush(stdin); + scanf("%[^\n]", db.row[index].category); + printf("Please input the item barcode\n>"); + fflush(stdin); + scanf("%[^\n]", db.row[index].barcode); + if(!update_db_invt(db)){ + printf("Failed to update item\n"); + }else{ + printf("Item updated\n"); + } + printf("press any key to continue\n"); + fflush(stdin); + getchar(); +} + +void remove_item(struct inventory db,int index){ + system("cls"); + printf("Remove item\n"); + printf("Are you sure you want to remove this item? (y/n)\n"); + char choice; + fflush(stdin); + scanf("%c", &choice); + db.row[index].isdeleted = true; + if(choice == 'y'){ + if(!update_db_invt(db)){ + printf("Failed to remove item\n"); + }else{ + printf("Item removed\n"); + } + }else{ + printf("Item not removed\n"); + } + printf("press any key to continue\n"); + fflush(stdin); + getchar(); +} + diff --git a/database.h b/database.h index 294c5cc..b2bb15b 100644 --- a/database.h +++ b/database.h @@ -69,7 +69,6 @@ typedef struct user_row{ char name[100]; char role[100]; long id; - bool isAdmin; bool isdeleted;//common for all rows,default is false }user_row; @@ -446,6 +445,96 @@ bool update_db_user(struct user user){ return true; } +bool append_inventory(struct inventory_row* row){ + FILE* fp = fopen(INVENTORY_DB,"a"); + if(fp == NULL){ + printf("Error in opening file\n"); + return false; + } + for(INVENTORY i=category;i<=ENDOFINVENTORY;i++){ + switch(i){ + case category: + fprintf(fp,"%s",row.category); + break; + case brand: + fprintf(fp,"%s",row.brand); + break; + case product: + fprintf(fp,"%s",row.product); + break; + case price_inv: + fprintf(fp,"%.1f",row.price); + break; + case stock: + fprintf(fp,"%d",row.stock); + break; + case barcode_inv: + fprintf(fp,"%ld",row.barcode); + break; + } + fprintf(fp,"\n"); + } + fclose(fp); + return true; +} + +bool append_transaction_db(struct transaction_row* row){ + FILE* fp = fopen(TRANSACTION_DB,"a"); + if(fp == NULL){ + printf("Error in opening file\n"); + return false; + } + for(TRANSACTION i=date;i<=ENDOFTRANSACTION;i++){ + switch(i){ + case date: + fprintf(fp,"%d-%02d-%02d",row.date.year,row.date.month,row.date.day); + break; + case TIME: + fprintf(fp,"%02d:%02d:%02d",row.time.hour,row.time.minute,row.time.second); + break; + case id_tran: + fprintf(fp,"%ld",row.id); + break; + case price_tran: + fprintf(fp,"%.1f",row.price); + break; + case quantity: + fprintf(fp,"%d",row.quantity); + break; + case barcode_tran: + fprintf(fp,"%ld",row.barcode); + break; + } + fprintf(fp,"\n"); + } + fclose(fp); + return true; +} + +bool append_user(struct user_row* row){ + FILE* fp = fopen(USER_DB,"a"); + if(fp == NULL){ + printf("Error in opening file\n"); + return false; + } + for(USER i=name;i<=ENDOFUSER;i++){ + switch(i){ + case name: + fprintf(fp,"%s",row.name); + break; + case role: + fprintf(fp,"%s",row.role); + break; + case id_user: + fprintf(fp,"%ld",row.id); + break; + } + fprintf(fp,"\n"); + } + fclose(fp); + return true; +} + //checkout db support, typedef struct cart{//linked list diff --git a/main.c b/main.c index 27ddceb..1a673d7 100644 --- a/main.c +++ b/main.c @@ -37,7 +37,7 @@ int main(){ switch (choice) { case 1://admin - //admin_menu();//refers to admin_user.h + admin_menu();//refers to admin_user.h check = true; break; case 2://normal user diff --git a/normal_user.h b/normal_user.h index fc15157..f9af3fc 100644 --- a/normal_user.h +++ b/normal_user.h @@ -651,7 +651,6 @@ struct cart* update_cart(struct cart* cart,int index){ //Checkout -//TODO checkout struct cart* checkout(struct cart* cart){ system("cls");