finish most user menu;

only checkout need to be done;
clean up, better interface;
improve user experience.
This commit is contained in:
stmctommyau 2022-09-03 00:07:16 +08:00
parent 1e5999a4e5
commit baeef3f7c8
3 changed files with 163 additions and 36 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
main.exe main.exe
.VSCodeCounter/* .VSCodeCounter/*
.vscode/* .vscode/*
.vscode/settings.json

3
menu.h
View File

@ -20,7 +20,8 @@ int user_choices(){
printf("1. Admin\n"); printf("1. Admin\n");
printf("2. Normal User\n"); printf("2. Normal User\n");
printf("3. Exit\n"); printf("3. Exit\n");
printf("Enter your choice: ");
fflush(stdin);
scanf("%d", &choice); scanf("%d", &choice);
return choice; return choice;

View File

@ -79,6 +79,7 @@ int normal_menu_user_choices(){
printf("3. Self help sale system\n"); printf("3. Self help sale system\n");
printf("4. Exit\n"); printf("4. Exit\n");
printf("Enter your choice: "); printf("Enter your choice: ");
fflush(stdin);
scanf("%d", &choice); scanf("%d", &choice);
return choice; return choice;
} }
@ -119,6 +120,7 @@ void list_page(struct inventory db,struct Map* map,int row){//user for showing l
bool valid = true; bool valid = true;
do{ do{
printf("Enter your choice: "); printf("Enter your choice: ");
fflush(stdin);
scanf("%d", &choice); scanf("%d", &choice);
if(choice <=8 && choice > 0){ if(choice <=8 && choice > 0){
printf("sorting...\n"); printf("sorting...\n");
@ -129,6 +131,7 @@ void list_page(struct inventory db,struct Map* map,int row){//user for showing l
page--; page--;
}else if(choice == page_size+5){ }else if(choice == page_size+5){
printf("Enter page size: "); printf("Enter page size: ");
fflush(stdin);
scanf("%d", &page_size); scanf("%d", &page_size);
total_pages = ceil(row / page_size); total_pages = ceil(row / page_size);
}else if(choice >= 9 && choice < row+9){ }else if(choice >= 9 && choice < row+9){
@ -236,6 +239,7 @@ void search_item(){
printf("0 exit\n"); printf("0 exit\n");
printf("1 search\n"); printf("1 search\n");
printf("select your choice: "); printf("select your choice: ");
fflush(stdin);
scanf("%d", &breakout); scanf("%d", &breakout);
fflush(stdin); fflush(stdin);
if(breakout == 1 || breakout == 0){ if(breakout == 1 || breakout == 0){
@ -265,18 +269,42 @@ void search_item(){
char* prompt_search(){ char* prompt_search(){
printf("Enter search string:(largest length 100) "); printf("Enter search string:(largest length 100)(case insesitive)\n");
char* searchstr = malloc(sizeof(char) * 100); char* searchstr = malloc(sizeof(char) * 100);
fflush(stdin);
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){
struct Map* map = malloc(sizeof(struct Map) * (db.db.row_count+1)); struct Map* map = malloc(sizeof(struct Map) * (db.db.row_count+1));
int k = 1; int k = 1;
for (int i = 0; i < db.db.row_count; i++){ for (int i = 0; i < db.db.row_count; i++){
map[k].key = i; map[k].key = i;
if(strstr(db.row[i].product,searchstr) != NULL){ if(strcasestr(db.row[i].product,searchstr) != NULL){
map[k].value = (void*)db.row[i].product; map[k].value = (void*)db.row[i].product;
k++; k++;
@ -309,6 +337,7 @@ void self_help_sale_system(){
//options //options
int breakout = 0; int breakout = 0;
int choice = self_choice(); int choice = self_choice();
check = false;
switch (choice) switch (choice)
{ {
case 1: case 1:
@ -318,6 +347,7 @@ void self_help_sale_system(){
case 2: case 2:
cart = list_cart(cart); cart = list_cart(cart);
check = true; check = true;
break;
default: default:
break; break;
} }
@ -332,11 +362,11 @@ int self_choice(){
welcome_message(); welcome_message();
printf("0 exit\n"); printf("0 exit\n");
printf("1 scan barcode\n"); printf("1 scan barcode\n");
printf("2 cart\n"); printf("2 cart/checkout\n");
printf("3 checkout\n");
printf("select an option: "); printf("select an option: ");
fflush(stdin);
scanf("%d", &choice); scanf("%d", &choice);
if(choice <= 3 || choice >= 0){ if(choice <= 2 || choice >= 0){
valid = true; valid = true;
}else{ }else{
printf("Invalid choice\n"); printf("Invalid choice\n");
@ -361,6 +391,7 @@ struct cart* Cartappend(struct cart* cart,struct inventory_row* row,int quantity
temp->next->quantity = quantity; temp->next->quantity = quantity;
temp->next->next = NULL; temp->next->next = NULL;
} }
return cart; return cart;
} }
@ -402,6 +433,38 @@ struct cart* Cartupdate(struct cart* cart,int index,int quantity){
} }
} }
struct inventory_row* Cartget(struct cart* cart,int index){
if (cart == NULL){
return NULL;
}else{
struct cart* temp = cart;
if (index == 0){
return cart->row;
}else{
for (int i = 0; i < index-1; i++){
temp = temp->next;
}
return temp->next->row;
}
}
}
int Cartqty(struct cart* cart,int index){
if (cart == NULL){
return 0;
}else{
struct cart* temp = cart;
if (index == 0){
return cart->quantity;
}else{
for (int i = 0; i < index-1; i++){
temp = temp->next;
}
return temp->next->quantity;
}
}
}
//scan barcode //scan barcode
struct inventory_row* find_barcode(struct inventory db,long barcode); struct inventory_row* find_barcode(struct inventory db,long barcode);
long prompt_barcode(); long prompt_barcode();
@ -411,18 +474,37 @@ struct cart* scan_barcode(struct cart* cart,struct inventory db){
printf("matching...\n"); printf("matching...\n");
struct inventory_row* row = find_barcode(db,barcode); struct inventory_row* row = find_barcode(db,barcode);
if(row != NULL){ if(row != NULL){
printf("product: %s\n",row->product); int quantity = 0;
printf("price: %.2f\n",row->price); char choice = 'n';
printf("Enter quantity: ");\ do{
int quantity = 0; system("cls");
scanf("%d", &quantity); printf("product: %s\n",row->product);
if(quantity > 0){ printf("price: %.2f\n",row->price);
printf("Enter quantity(0 to cancel)\n>");\
fflush(stdin);
scanf("%d", &quantity);
fflush(stdin);
if(quantity == 0){
printf("cancelled\n");
printf("press any key to continue\n");
fflush(stdin);
getchar();
return cart;
}
printf("Are you sure you want to add this item to cart?(y/n)");
scanf("%c", &choice);
}while(choice != 'y');
if(quantity != 0){
cart = Cartappend(cart,row,quantity); cart = Cartappend(cart,row,quantity);
printf("added to cart\n");
}
}else{//empty search }else{//empty search
printf("Unable to match or invalid input\n"); printf("Unable to match or invalid input\n");
} }
}while(true);//break on top printf("press any key to continue\n");
fflush(stdin);
getchar();
return cart; return cart;
} }
@ -430,15 +512,14 @@ struct cart* scan_barcode(struct cart* cart,struct inventory db){
long prompt_barcode(){ long prompt_barcode(){
printf("Please scan the qr code(or input the barcode ) "); printf("Please scan the qr code(or input the barcode ) ");
long barcode; long barcode;
scanf("%ld", barcode); fflush(stdin);
scanf("%ld", &barcode);
return barcode; return barcode;
} }
struct inventory_row* find_barcode(struct inventory db,long barcode){ struct inventory_row* find_barcode(struct inventory db,long barcode){
struct inventory_row row; struct inventory_row row;
printf("s");
for (int i = 0; i < db.db.row_count; i++){ for (int i = 0; i < db.db.row_count; i++){
printf("s");
printf("%ld\n",db.row[i].barcode); printf("%ld\n",db.row[i].barcode);
if(db.row[i].barcode == barcode){ if(db.row[i].barcode == barcode){
@ -451,6 +532,7 @@ struct inventory_row* find_barcode(struct inventory db,long barcode){
//list cart //list cart
struct cart* cart_control(struct cart* cart,int index); struct cart* cart_control(struct cart* cart,int index);
struct cart* checkout(struct cart* cart);
struct cart* list_cart(struct cart* cart){ struct cart* list_cart(struct cart* cart){
int choice = 0; int choice = 0;
do{ do{
@ -463,65 +545,108 @@ struct cart* list_cart(struct cart* cart){
printf("Cart is empty\n"); printf("Cart is empty\n");
}else{ }else{
struct cart* temp = cart; struct cart* temp = cart;
while(temp != NULL){ while(temp != NULL){//show cart
int qty = temp->quantity; int qty = temp->quantity;
double price = temp->row->price * qty; double price = temp->row->price * qty;
total_price += price; total_price += price;
printf("%d product:%s price:$%lf qty:%d\n",i,temp->row->product,price,qty); printf("%d product:%s price:$%lf\n quantity:%d\n",i,temp->row->product,price,qty);
printf("<------------------------>\n");
temp = temp->next; temp = temp->next;
i++; i++;
} }
printf("Total price: $%.2f\n",total_price); printf("Total price: $%.2f\n",total_price);
printf("%d checkout\n",i);
} }
do{ do{
printf("input the corrisponding value for more action\n"); printf("input the corrisponding value for more action\n>");
fflush(stdin);
scanf("%d", &choice); scanf("%d", &choice);
if(choice >0 && choice <= i){ if(choice >0 && choice < i){
cart = cart_control(cart,choice); cart = cart_control(cart,choice);
} }else if(choice == i && cart != NULL){
}while(choice >0 && choice < i); cart = checkout(cart);
}
}while(choice <0 || choice > i);
}while(choice != 0); }while(choice != 0);
return cart; return cart;
} }
struct cart* update_cart(struct cart* cart,int index); struct cart* update_cart(struct cart* cart,int index);
struct cart* cart_control(struct cart* cart,int index){ struct cart* cart_control(struct cart* cart,int index){
int choice = 0; int choice = 0;
struct inventory_row* row = Cartget(cart,index-1);
int quantity = Cartqty(cart,index-1);
do{ do{
system("cls"); system("cls");
welcome_message(); welcome_message();
printf("0 exit\n"); printf("0 exit\n");
printf("1 remove\n"); printf("1 remove\n");
printf("2 edit quantity\n"); printf("2 edit quantity\n");
bool check = false; printf("product: %s\n",row->product);
printf("price: %.2f\n",row->price);
printf("quantity: %d\n",quantity);
bool check;
do{ do{
check = true;
printf("select an option: ");
fflush(stdin);
scanf("%d", &choice); scanf("%d", &choice);
if(choice == 1){ if(choice == 1){
cart = Cartremove(cart,index-1); cart = Cartremove(cart,index-1);
check = true; printf("Remove successful\n");
printf("press any key to continue\n");
fflush(stdin);
getchar();
}else if(choice == 2){ }else if(choice == 2){
cart = update_cart(cart,index); cart = update_cart(cart,index);
check = true; printf("Update successful\n");
printf("press any key to continue");
fflush(stdin);
getchar();
}else if(choice != 0){ }else if(choice != 0){
printf("Invalid choice\n"); printf("Invalid choice\n");
check = false; check = false;
} }
}while(!check); }while(!check);
}while(choice != 0); }while(choice < 0 || choice > 2);
return cart; return cart;
} }
struct cart* update_cart(struct cart* cart,int index){ struct cart* update_cart(struct cart* cart,int index){
system("cls"); system("cls");
welcome_message(); welcome_message();
printf("enter the new quantity: "); char choice;
int quantity = 0; do{
scanf("%d", &quantity); int quantity = 0;
if(quantity > 0){ printf("enter the new quantity: ");
cart = Cartupdate(cart,index-1,quantity); fflush(stdin);
}else if (quantity == 0){ scanf("%d", &quantity);
cart = Cartremove(cart,index-1); printf("Are you sure you want to update this item?(y/n/e to exit)");
}else{ fflush(stdin);
printf("Invalid quantity\n"); scanf("%c", &choice);
} if(choice == 'y'){
if(quantity != 0){
cart = Cartupdate(cart,index-1,quantity);
}else if (quantity == 0){
cart = Cartremove(cart,index-1);
}
}
}while(choice != 'y' && choice != 'e');
return cart;
}
//Checkout
//TODO checkout
struct cart* checkout(struct cart* cart){
//TODO:write db func for transaction
//TODO:write db func for user
//TODO:update stock
system("cls");
welcome_message();
return cart; return cart;
} }