diff --git a/.gitignore b/.gitignore index 8808f8b..ee26c71 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ main.exe .vscode/* .vscode/settings.json data_file_transaction.txt +*.gch +*.o diff --git a/admin_user.h b/admin_user.h index 845d470..46ad4ca 100644 --- a/admin_user.h +++ b/admin_user.h @@ -44,7 +44,9 @@ void admin_menu(){ "User control", "Role control", }; - switch (choices_selecter(Items,4,strcat("Welcome ")"""%s(%s)\n", user->name,user->role)) + char welcome[256]; + sprintf(welcome,"welcome %s(%s)",user->name,user->role); + switch (choices_selecter(Items,4,welcome)) { case 1://action with inventory inv_control(); @@ -111,29 +113,15 @@ void update_item(struct inventory db,int index); void admin_list_pages(struct inventory db); struct inventory item_control(struct inventory db,int index); -int prompt_inv_control(){ - int choice = 0; - do{ - system("cls"); - printf("Please select an option:\n"); - printf("1. List(allow all operation include add)\n"); - printf("2. Add item(shortcut to add item)\n"); - printf("3. Exit\n"); - printf(">"); - scanf("%d", &choice); - if(choice < 1 || choice > 3){ - printf("Invalid choice...press any key to retry....\n"); - fflush(stdin); - getchar(); - } - }while(choice < 1 || choice > 3); - return choice; -} void inv_control(){ int choice = 0; do{ - choice = prompt_inv_control(); + char * items[] = { + "List(allow all operation include add)", + "Add item(shortcut to add item)" + }; + choice = choices_selecter(items,2,NULL); switch (choice){ case 1:;//list //add a new element with product name new item @@ -157,16 +145,15 @@ void inv_control(){ }while(choice != 3); } -void show_item_admin(struct inventory db,int index){ - system("cls"); - printf("Product: %s\n", db.row[index].product); - printf("Catergory: %s\n", db.row[index].category); - printf("Brand: %s\n", db.row[index].brand); - printf("Price: $%lf\n", db.row[index].price); - printf("Stock: %d\n", db.row[index].stock); - printf("Barcode: %ld\n",db.row[index].barcode); - getchar(); - return; +char * show_item_admin(struct inventory db,int index){ + char * output = malloc(sizeof(char) * 1024); + sprintf(output,"Product: %s\n", db.row[index].product); + sprintf(output,"Catergory: %s\n", db.row[index].category); + sprintf(output,"Brand: %s\n", db.row[index].brand); + sprintf(output,"Price: $%lf\n", db.row[index].price); + sprintf(output,"Stock: %d\n", db.row[index].stock); + sprintf(output,"Barcode: %ld\n",db.row[index].barcode); + return output; } struct inventory item_control(struct inventory db,int index){ @@ -180,17 +167,17 @@ struct inventory item_control(struct inventory db,int index){ temp.row[temp.db.row_count - 1].barcode = -1024;//IMPORTANT: -1024 is reserved for new item return temp; } + char * item = show_item_admin(db,index); + char welcome[1035]; + strcpy(welcome,item); + strcat(welcome,"Operations"); do { - system("cls"); - show_item_admin(db,index); - printf("Operations\n"); - printf("0 exit\n"); - printf("1 update item\n"); - printf("2 remove item\n"); - printf("Enter your choice: "); - fflush(stdin); - scanf("%d", &choice); + char * items[] = { + "update item", + "remove item" + }; + choice = choices_selecter(item,2,welcome); switch (choice) { case 1: @@ -202,7 +189,7 @@ struct inventory item_control(struct inventory db,int index){ default: break; } - } while (choice != 0&&choice != 2); + } while (choice != 3&&choice != 2); struct inventory temp = read_db_invt(); temp.row = (struct inventory_row *)realloc(temp.row, sizeof(struct inventory_row) * (temp.db.row_count + 1)); temp.db.row_count += 1; @@ -367,15 +354,11 @@ 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); + char * items[] = { + "List transaction", + "new transaction" + }; + choice = choices_selecter(items,2,"><\nTransaction control"); switch(choice){ case 1: list_tran(tran); @@ -383,13 +366,7 @@ void tran_control(){ 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 != 3); @@ -1063,15 +1040,11 @@ void role_control(){ int choice = -1; struct user db = read_db_user(); do{ - system("cls"); - welcome_message(); - printf("Role control\n"); - printf("0 exit\n"); - printf("1 add admin role\n"); - printf("2 list role\n"); - printf("Enter choice: "); - fflush(stdin); - scanf("%d", &choice); + char * items[] = { + "add admin role", + "list role" + }; + choice = choices_selecter(items,2,"><\nRole control"); switch (choice){ case 0: break; @@ -1082,10 +1055,6 @@ void role_control(){ list_role(db); break; default: - printf("Invalid choice\n"); - printf("press any key to continue\n"); - fflush(stdin); - getchar(); break; } }while(choice != 0); diff --git a/main.c b/main.c index 207532c..1612504 100644 --- a/main.c +++ b/main.c @@ -15,26 +15,27 @@ #include "database.h" #endif // !CUSTOM_H -#ifndef MENU_H -#define MENU_H -#include "menu.h" -#endif //MENU_H #include "normal_user.h" #include "admin_user.h" - +#ifndef Utils +#define Utils +#include "utils.h" +#endif // !Utils int main(){ bool check; - do - { + do{ system("cls"); //print welcome message //prompt weather user is admin or normal user //then redirect them to the corisponding menu - welcome_message();//refer to menu.h, keep things easier for repeat use on future - int choice = user_choices();//same as above + char * Items[] = { + "Admin", + "Normal User" + }; + int choice = choices_selecter(Items,2,"><\n"); switch (choice) { case 1://admin @@ -45,15 +46,8 @@ int main(){ normal_menu();//refers to normal_user.h check = true; break; - case 3://exit + default://Exit check = false; - break; - default://invalid input - printf("Invalid choice...press any key to retry....\n"); - fflush(stdin); - getchar(); - check = true; - break; } diff --git a/menu.h b/menu.h deleted file mode 100644 index 2424ba4..0000000 --- a/menu.h +++ /dev/null @@ -1,30 +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 - - -void welcome_message(){ - printf("><\n");//welcome messgae -} - -int user_choices(){ - int choice; - - printf("Are you an admin or a normal user?\n"); - printf("1. Admin\n"); - printf("2. Normal User\n"); - printf("3. Exit\n"); - printf("Enter your choice: "); - fflush(stdin); - scanf("%d", &choice); - - return choice; -} \ No newline at end of file diff --git a/normal_user.h b/normal_user.h index 020d60d..ef4e824 100644 --- a/normal_user.h +++ b/normal_user.h @@ -20,24 +20,26 @@ #include "sorting.h" #endif // !SORT_H #define PAGE_SIZE 20 - +#ifndef Utils +#define Utils +#include "utils.h" +#endif // !Utils //fucntions for menu void list_items(); void search_item(); void self_help_sale_system(); void print_page(struct inventory db, int cur, int end,struct Map* map); -int normal_menu_user_choices(); //main of normal user void normal_menu(){ int choice = 0; do{ - system("cls"); - welcome_message(); - //the selection menu - - choice = normal_menu_user_choices(); - + char * items[] = { + "List items", + "Search item", + "Self help sale system" + }; + choice = choices_selecter(items,3,"><\nNormal User Menu"); switch (choice) { case 1://List items @@ -49,10 +51,7 @@ void normal_menu(){ case 3://self help sale system self_help_sale_system(); break; - case 4://Exit - break; default://invalid input ,should not happen,as it is already catch in above function - printf("Invalid choice\n"); break; } }while(choice != 4); @@ -73,18 +72,6 @@ struct inventory show_item(struct inventory db,int index){ getchar(); return db; } -int normal_menu_user_choices(){ - int choice; - printf("Normal User Menu\n"); - printf("1. List items\n"); - printf("2. Search item\n"); - printf("3. Self help sale system\n"); - printf("4. Exit\n"); - printf("Enter your choice: "); - fflush(stdin); - scanf("%d", &choice); - return choice; -} //user for showing list result and search result void list_page(struct inventory db,struct Map* map,int row,struct inventory (*showitem)(struct inventory,int)){//showitem is a function pointer for showing item,allow customization for better flexibilty @@ -253,8 +240,7 @@ struct Map* sortItems(struct inventory db, int sort){ char* prompt_search(); struct Map* searchItems(struct inventory db, char* searchstr); void search_item(){ - system("cls"); - welcome_message(); + //search for item //prompt user to select an item //if user selects an item, display the item's details @@ -264,24 +250,11 @@ void search_item(){ //options int breakout = 0; - bool valid = false; - do{ - system("cls"); - welcome_message(); - printf("0 exit\n"); - printf("1 search\n"); - printf("select your choice: "); - fflush(stdin); - scanf("%d", &breakout); - fflush(stdin); - if(breakout == 1 || breakout == 0){ - valid = true; - }else{ - valid = false; - printf("Invalid choice\n"); - } - }while(!valid); - if(breakout == 0){ + char * items[] = { + "search" + }; + breakout = choices_selecter(items,1,"><"); + if(breakout == 2){ break; } char* searchstr = prompt_search(); @@ -309,28 +282,7 @@ char* prompt_search(){ scanf("%s", searchstr); return searchstr; } -//check if strcasestr is define in current environment -//if not, define it -#ifndef HAVE_STRCASESTR -char *strcasestr(const char *s, const char *find) -{ -char c, sc; -size_t len; -if ((c = *find++) != 0) { -c = tolower((unsigned char)c); -len = strlen(find); -do { -do { -if ((sc = *s++) == 0) -return (NULL); -} while ((char)tolower((unsigned char)sc) != c); -} while (strncasecmp(s, find, len) != 0); -s--; -} -return ((char *)s); -} -#endif struct Map* searchItems(struct inventory db, char* searchstr){ @@ -353,59 +305,37 @@ struct Map* searchItems(struct inventory db, char* searchstr){ //self help - -int self_choice(); struct cart* scan_barcode(struct cart* cart,struct inventory db); struct cart* list_cart(struct cart* cart); void self_help_sale_system(){ - system("cls"); - welcome_message(); + //scan barcode struct inventory db = read_db_invt(); struct cart* cart = NULL; - bool check = false; + int choice = 0; do{ //options int breakout = 0; - int choice = self_choice(); - check = false; + char * items[] = { + "scan barcode", + "cart/checkout" + }; + choice = choices_selecter(items,2,"><"); switch (choice) { case 1: cart = scan_barcode(cart,db); - check = true; break; case 2: cart = list_cart(cart); - check = true; break; default: break; } - }while(check); + }while(choice!=3); } -int self_choice(){ - int choice = 0; - bool valid = false; - do{ - system("cls"); - welcome_message(); - printf("0 exit\n"); - printf("1 scan barcode\n"); - printf("2 cart/checkout\n"); - printf("select an option: "); - fflush(stdin); - scanf("%d", &choice); - if(choice <= 2 || choice >= 0){ - valid = true; - }else{ - printf("Invalid choice\n"); - } - }while(!valid); - return choice; -} //support for the linked list struct cart* Cartappend(struct cart* cart,struct inventory_row* row,int quantity){ if(cart == NULL){ @@ -621,20 +551,17 @@ struct cart* cart_control(struct cart* cart,int index){ struct inventory_row* row = Cartget(cart,index-1); int quantity = Cartqty(cart,index-1); do{ - system("cls"); - welcome_message(); - printf("0 exit\n"); - printf("1 remove\n"); - printf("2 edit quantity\n"); - printf("product: %s\n",row->product); - printf("price: %.1f\n",row->price); - printf("quantity: %d\n",quantity); - bool check; - do{ - check = true; - printf("select an option: "); - fflush(stdin); - scanf("%d", &choice); + char welcome[256]; + sprintf(welcome,"><\n"); + sprintf(welcome,"product: %s\n",row->product); + sprintf(welcome,"price: %.1f\n",row->price); + sprintf(welcome,"quantity: %d\n",quantity); + char * items[] = { + "remove", + "edit quantity" + }; + choice = choices_selecter(items,2,welcome); + if(choice == 1){ cart = Cartremove(cart,index-1); printf("Remove successful\n"); @@ -647,12 +574,8 @@ struct cart* cart_control(struct cart* cart,int index){ printf("press any key to continue"); fflush(stdin); getchar(); - }else if(choice != 0){ - printf("Invalid choice\n"); - check = false; } - }while(!check); - }while(choice < 0 || choice > 2); + }while(choice != 1 || choice != 2 || choice !=3 ); return cart; } diff --git a/utils.h b/utils.h index 79438eb..576f5a8 100644 --- a/utils.h +++ b/utils.h @@ -1,9 +1,22 @@ -int choices_selecter(char * Items[],int items,char * welcome_message ){ +#ifndef STD_LIB_H +#define STD_LIB_H +#include +#include +#include +#include +#include +#include +#include +#include +#endif + +int choices_selecter(char * Items[],int items,char * welcome_messages ){ int choice = 0; do{ system("cls"); - if ((*welcome_message) !=NULL){ - printf("%s",welcome_message) + + if (welcome_messages !=NULL){ + printf("%s",welcome_messages); } printf("Select An Option:\n"); int i; @@ -12,6 +25,8 @@ int choices_selecter(char * Items[],int items,char * welcome_message ){ } printf("%d. Exit\n",i+1); printf(">"); + fflush(stdin); + scanf("%d",&choice); if(choice < 1 || choice > items+1){ printf("Invalid choice...press any key to retry....\n"); fflush(stdin); @@ -21,3 +36,25 @@ int choices_selecter(char * Items[],int items,char * welcome_message ){ return choice; } +//check if strcasestr is define in current environment +//if not, define it +#ifndef HAVE_STRCASESTR +char *strcasestr(const char *s, const char *find) +{ +char c, sc; +size_t len; + +if ((c = *find++) != 0) { +c = tolower((unsigned char)c); +len = strlen(find); +do { +do { +if ((sc = *s++) == 0) +return (NULL); +} while ((char)tolower((unsigned char)sc) != c); +} while (strncasecmp(s, find, len) != 0); +s--; +} +return ((char *)s); +} +#endif