selector migration finish, code strinked

This commit is contained in:
AU KA LONG 2023-07-18 12:37:06 +08:00
parent ae074c42e6
commit ee7aa0dffa
6 changed files with 126 additions and 231 deletions

2
.gitignore vendored
View File

@ -3,3 +3,5 @@ main.exe
.vscode/* .vscode/*
.vscode/settings.json .vscode/settings.json
data_file_transaction.txt data_file_transaction.txt
*.gch
*.o

View File

@ -44,7 +44,9 @@ void admin_menu(){
"User control", "User control",
"Role 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 case 1://action with inventory
inv_control(); inv_control();
@ -111,29 +113,15 @@ void update_item(struct inventory db,int index);
void admin_list_pages(struct inventory db); void admin_list_pages(struct inventory db);
struct inventory item_control(struct inventory db,int index); 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(){ void inv_control(){
int choice = 0; int choice = 0;
do{ 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){ switch (choice){
case 1:;//list case 1:;//list
//add a new element with product name new item //add a new element with product name new item
@ -157,16 +145,15 @@ void inv_control(){
}while(choice != 3); }while(choice != 3);
} }
void show_item_admin(struct inventory db,int index){ char * show_item_admin(struct inventory db,int index){
system("cls"); char * output = malloc(sizeof(char) * 1024);
printf("Product: %s\n", db.row[index].product); sprintf(output,"Product: %s\n", db.row[index].product);
printf("Catergory: %s\n", db.row[index].category); sprintf(output,"Catergory: %s\n", db.row[index].category);
printf("Brand: %s\n", db.row[index].brand); sprintf(output,"Brand: %s\n", db.row[index].brand);
printf("Price: $%lf\n", db.row[index].price); sprintf(output,"Price: $%lf\n", db.row[index].price);
printf("Stock: %d\n", db.row[index].stock); sprintf(output,"Stock: %d\n", db.row[index].stock);
printf("Barcode: %ld\n",db.row[index].barcode); sprintf(output,"Barcode: %ld\n",db.row[index].barcode);
getchar(); return output;
return;
} }
struct inventory item_control(struct inventory db,int index){ 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 temp.row[temp.db.row_count - 1].barcode = -1024;//IMPORTANT: -1024 is reserved for new item
return temp; return temp;
} }
char * item = show_item_admin(db,index);
char welcome[1035];
strcpy(welcome,item);
strcat(welcome,"Operations");
do do
{ {
system("cls"); char * items[] = {
show_item_admin(db,index); "update item",
printf("Operations\n"); "remove item"
printf("0 exit\n"); };
printf("1 update item\n"); choice = choices_selecter(item,2,welcome);
printf("2 remove item\n");
printf("Enter your choice: ");
fflush(stdin);
scanf("%d", &choice);
switch (choice) switch (choice)
{ {
case 1: case 1:
@ -202,7 +189,7 @@ struct inventory item_control(struct inventory db,int index){
default: default:
break; break;
} }
} while (choice != 0&&choice != 2); } while (choice != 3&&choice != 2);
struct inventory temp = read_db_invt(); struct inventory temp = read_db_invt();
temp.row = (struct inventory_row *)realloc(temp.row, sizeof(struct inventory_row) * (temp.db.row_count + 1)); temp.row = (struct inventory_row *)realloc(temp.row, sizeof(struct inventory_row) * (temp.db.row_count + 1));
temp.db.row_count += 1; temp.db.row_count += 1;
@ -367,15 +354,11 @@ void tran_control(){
struct transaction tran = read_db_tran(); struct transaction tran = read_db_tran();
int choice; int choice;
do{ do{
system("cls"); char * items[] = {
welcome_message(); "List transaction",
printf("Transaction control\n"); "new transaction"
printf("1. List transaction\n"); };
printf("2. new transaction\n"); choice = choices_selecter(items,2,"><Welcome to the Student Union POS system><\nTransaction control");
printf("3. Exit\n");
printf("Please input your choice: ");
fflush(stdin);
scanf("%d",&choice);
switch(choice){ switch(choice){
case 1: case 1:
list_tran(tran); list_tran(tran);
@ -383,13 +366,7 @@ void tran_control(){
case 2: case 2:
add_tran(tran); add_tran(tran);
break; break;
case 3:
break;
default: default:
printf("Invalid input, try again\n");
printf("press any key to continue\n");
fflush(stdin);
getchar();
break; break;
} }
}while(choice != 3); }while(choice != 3);
@ -1063,15 +1040,11 @@ void role_control(){
int choice = -1; int choice = -1;
struct user db = read_db_user(); struct user db = read_db_user();
do{ do{
system("cls"); char * items[] = {
welcome_message(); "add admin role",
printf("Role control\n"); "list role"
printf("0 exit\n"); };
printf("1 add admin role\n"); choice = choices_selecter(items,2,"><Welcome to the Student Union POS system><\nRole control");
printf("2 list role\n");
printf("Enter choice: ");
fflush(stdin);
scanf("%d", &choice);
switch (choice){ switch (choice){
case 0: case 0:
break; break;
@ -1082,10 +1055,6 @@ void role_control(){
list_role(db); list_role(db);
break; break;
default: default:
printf("Invalid choice\n");
printf("press any key to continue\n");
fflush(stdin);
getchar();
break; break;
} }
}while(choice != 0); }while(choice != 0);

28
main.c
View File

@ -15,26 +15,27 @@
#include "database.h" #include "database.h"
#endif // !CUSTOM_H #endif // !CUSTOM_H
#ifndef MENU_H
#define MENU_H
#include "menu.h"
#endif //MENU_H
#include "normal_user.h" #include "normal_user.h"
#include "admin_user.h" #include "admin_user.h"
#ifndef Utils
#define Utils
#include "utils.h"
#endif // !Utils
int main(){ int main(){
bool check; bool check;
do do{
{
system("cls"); system("cls");
//print welcome message //print welcome message
//prompt weather user is admin or normal user //prompt weather user is admin or normal user
//then redirect them to the corisponding menu //then redirect them to the corisponding menu
welcome_message();//refer to menu.h, keep things easier for repeat use on future char * Items[] = {
int choice = user_choices();//same as above "Admin",
"Normal User"
};
int choice = choices_selecter(Items,2,"><Welcome to the Student Union POS system><\n");
switch (choice) switch (choice)
{ {
case 1://admin case 1://admin
@ -45,15 +46,8 @@ int main(){
normal_menu();//refers to normal_user.h normal_menu();//refers to normal_user.h
check = true; check = true;
break; break;
case 3://exit default://Exit
check = false; check = false;
break;
default://invalid input
printf("Invalid choice...press any key to retry....\n");
fflush(stdin);
getchar();
check = true;
break; break;
} }

30
menu.h
View File

@ -1,30 +0,0 @@
//include std lib if they havent been included
#ifndef STD_LIB_H
#define STD_LIB_H
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
#include<math.h>
#include<time.h>
#include<ctype.h>
#endif
void welcome_message(){
printf("><Welcome to the Student Union POS system><\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;
}

View File

@ -20,24 +20,26 @@
#include "sorting.h" #include "sorting.h"
#endif // !SORT_H #endif // !SORT_H
#define PAGE_SIZE 20 #define PAGE_SIZE 20
#ifndef Utils
#define Utils
#include "utils.h"
#endif // !Utils
//fucntions for menu //fucntions for menu
void list_items(); void list_items();
void search_item(); void search_item();
void self_help_sale_system(); void self_help_sale_system();
void print_page(struct inventory db, int cur, int end,struct Map* map); void print_page(struct inventory db, int cur, int end,struct Map* map);
int normal_menu_user_choices();
//main of normal user //main of normal user
void normal_menu(){ void normal_menu(){
int choice = 0; int choice = 0;
do{ do{
system("cls"); char * items[] = {
welcome_message(); "List items",
//the selection menu "Search item",
"Self help sale system"
choice = normal_menu_user_choices(); };
choice = choices_selecter(items,3,"><Welcome to the Student Union POS system><\nNormal User Menu");
switch (choice) switch (choice)
{ {
case 1://List items case 1://List items
@ -49,10 +51,7 @@ void normal_menu(){
case 3://self help sale system case 3://self help sale system
self_help_sale_system(); self_help_sale_system();
break; break;
case 4://Exit
break;
default://invalid input ,should not happen,as it is already catch in above function default://invalid input ,should not happen,as it is already catch in above function
printf("Invalid choice\n");
break; break;
} }
}while(choice != 4); }while(choice != 4);
@ -73,18 +72,6 @@ struct inventory show_item(struct inventory db,int index){
getchar(); getchar();
return db; 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 //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 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(); char* prompt_search();
struct Map* searchItems(struct inventory db, char* searchstr); struct Map* searchItems(struct inventory db, char* searchstr);
void search_item(){ void search_item(){
system("cls");
welcome_message();
//search for item //search for item
//prompt user to select an item //prompt user to select an item
//if user selects an item, display the item's details //if user selects an item, display the item's details
@ -264,24 +250,11 @@ void search_item(){
//options //options
int breakout = 0; int breakout = 0;
bool valid = false; char * items[] = {
do{ "search"
system("cls"); };
welcome_message(); breakout = choices_selecter(items,1,"><Welcome to the Student Union POS system><");
printf("0 exit\n"); if(breakout == 2){
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){
break; break;
} }
char* searchstr = prompt_search(); char* searchstr = prompt_search();
@ -309,28 +282,7 @@ char* prompt_search(){
scanf("%s", searchstr); scanf("%s", searchstr);
return 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){ struct Map* searchItems(struct inventory db, char* searchstr){
@ -353,59 +305,37 @@ struct Map* searchItems(struct inventory db, char* searchstr){
//self help //self help
int self_choice();
struct cart* scan_barcode(struct cart* cart,struct inventory db); struct cart* scan_barcode(struct cart* cart,struct inventory db);
struct cart* list_cart(struct cart* cart); struct cart* list_cart(struct cart* cart);
void self_help_sale_system(){ void self_help_sale_system(){
system("cls");
welcome_message();
//scan barcode //scan barcode
struct inventory db = read_db_invt(); struct inventory db = read_db_invt();
struct cart* cart = NULL; struct cart* cart = NULL;
bool check = false; int choice = 0;
do{ do{
//options //options
int breakout = 0; int breakout = 0;
int choice = self_choice(); char * items[] = {
check = false; "scan barcode",
"cart/checkout"
};
choice = choices_selecter(items,2,"><Welcome to the Student Union POS system><");
switch (choice) switch (choice)
{ {
case 1: case 1:
cart = scan_barcode(cart,db); cart = scan_barcode(cart,db);
check = true;
break; break;
case 2: case 2:
cart = list_cart(cart); cart = list_cart(cart);
check = true;
break; break;
default: default:
break; 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 //support for the linked list
struct cart* Cartappend(struct cart* cart,struct inventory_row* row,int quantity){ struct cart* Cartappend(struct cart* cart,struct inventory_row* row,int quantity){
if(cart == NULL){ if(cart == NULL){
@ -621,20 +551,17 @@ struct cart* cart_control(struct cart* cart,int index){
struct inventory_row* row = Cartget(cart,index-1); struct inventory_row* row = Cartget(cart,index-1);
int quantity = Cartqty(cart,index-1); int quantity = Cartqty(cart,index-1);
do{ do{
system("cls"); char welcome[256];
welcome_message(); sprintf(welcome,"><Welcome to the Student Union POS system><\n");
printf("0 exit\n"); sprintf(welcome,"product: %s\n",row->product);
printf("1 remove\n"); sprintf(welcome,"price: %.1f\n",row->price);
printf("2 edit quantity\n"); sprintf(welcome,"quantity: %d\n",quantity);
printf("product: %s\n",row->product); char * items[] = {
printf("price: %.1f\n",row->price); "remove",
printf("quantity: %d\n",quantity); "edit quantity"
bool check; };
do{ choice = choices_selecter(items,2,welcome);
check = true;
printf("select an option: ");
fflush(stdin);
scanf("%d", &choice);
if(choice == 1){ if(choice == 1){
cart = Cartremove(cart,index-1); cart = Cartremove(cart,index-1);
printf("Remove successful\n"); printf("Remove successful\n");
@ -647,12 +574,8 @@ struct cart* cart_control(struct cart* cart,int index){
printf("press any key to continue"); printf("press any key to continue");
fflush(stdin); fflush(stdin);
getchar(); getchar();
}else if(choice != 0){
printf("Invalid choice\n");
check = false;
} }
}while(!check); }while(choice != 1 || choice != 2 || choice !=3 );
}while(choice < 0 || choice > 2);
return cart; return cart;
} }

43
utils.h
View File

@ -1,9 +1,22 @@
int choices_selecter(char * Items[],int items,char * welcome_message ){ #ifndef STD_LIB_H
#define STD_LIB_H
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
#include<math.h>
#include<time.h>
#include<ctype.h>
#include <stddef.h>
#endif
int choices_selecter(char * Items[],int items,char * welcome_messages ){
int choice = 0; int choice = 0;
do{ do{
system("cls"); system("cls");
if ((*welcome_message) !=NULL){
printf("%s",welcome_message) if (welcome_messages !=NULL){
printf("%s",welcome_messages);
} }
printf("Select An Option:\n"); printf("Select An Option:\n");
int i; int i;
@ -12,6 +25,8 @@ int choices_selecter(char * Items[],int items,char * welcome_message ){
} }
printf("%d. Exit\n",i+1); printf("%d. Exit\n",i+1);
printf(">"); printf(">");
fflush(stdin);
scanf("%d",&choice);
if(choice < 1 || choice > items+1){ if(choice < 1 || choice > items+1){
printf("Invalid choice...press any key to retry....\n"); printf("Invalid choice...press any key to retry....\n");
fflush(stdin); fflush(stdin);
@ -21,3 +36,25 @@ int choices_selecter(char * Items[],int items,char * welcome_message ){
return choice; 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