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
.VSCodeCounter/*
.vscode/*
.vscode/settings.json

3
menu.h
View File

@ -20,7 +20,8 @@ int user_choices(){
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

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