stock fix,typo fix;

reserve code for admin;
self salse system (in work);
This commit is contained in:
stmctommyau 2022-09-01 23:46:16 +08:00
parent 6e95628734
commit 1e5999a4e5
7 changed files with 322 additions and 55 deletions

2
.gitignore vendored
View File

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

View File

@ -13,10 +13,30 @@
#include "menu.h" #include "menu.h"
#endif //MENU_H #endif //MENU_H
bool admin_menu(){ // void admin_menu(){
system("cls"); // system("cls");
welcome_message(); // 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;
// }
return true; // return true;
} // }

View File

@ -1,31 +1,37 @@
#category, brand, product, price`vc , stock, barcode #category, brand, product, price , stock, barcode
Test Test
Testing Co Ltd Testing Co Ltd
Testing Product Testing Product
15.0 15.0
0
1122 1122
Juice Juice
Mr Juicy Mr Juicy
Orange Juice Drink 360mL (Bottle) Orange Juice Drink 360mL (Bottle)
9.9 9.9
0
9665 9665
Juice Juice
F&N Fruit Tree F&N Fruit Tree
Apple & Aloe Vera Juice Drink 500mL Apple & Aloe Vera Juice Drink 500mL
16.0 16.0
0
6479 6479
Juice Juice
F&N Fruit Tree F&N Fruit Tree
Apple & Aloe Vera Juice Drink 1L Apple & Aloe Vera Juice Drink 1L
22.9 22.9
0
5155 5155
Juice Juice
Homegrown Juice Company Homegrown Juice Company
Cold Pressed NZ Apple Juice 1L Cold Pressed NZ Apple Juice 1L
35.5 35.5
0
6470 6470
T2 T2
T2 Brand T2 Brand
T2 Product T2 Product
20.0 20.0
0
2983 2983

View File

@ -78,7 +78,6 @@ typedef enum {
//TODOLwrite function for these functions
//list of db func //list of db func
int basic_get_row_count(int end, FILE *fp){ int basic_get_row_count(int end, FILE *fp){
fseek(fp, 0, SEEK_SET);//prevent pointer on wrong position fseek(fp, 0, SEEK_SET);//prevent pointer on wrong position
@ -92,7 +91,6 @@ int basic_get_row_count(int end, FILE *fp){
continue; continue;
} }
colmun_count++; colmun_count++;
puts(buffer);
if(colmun_count == end){ if(colmun_count == end){
row_count++; row_count++;
colmun_count = 0; colmun_count = 0;

6
main.c
View File

@ -35,10 +35,12 @@ int main(){
switch (choice) switch (choice)
{ {
case 1://admin case 1://admin
check = admin_menu();//refers to admin_user.h //admin_menu();//refers to admin_user.h
check = true;
break; break;
case 2://normal user case 2://normal user
check = normal_menu();//refers to normal_user.h normal_menu();//refers to normal_user.h
check = true;
break; break;
case 3://exit case 3://exit
check = false; check = false;

View File

@ -22,15 +22,19 @@
//fucntions for menu //fucntions for menu
void list_items(); void list_items();
void search_item(); void search_item();
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(); int normal_menu_user_choices();
//main of normal user //main of normal user
bool normal_menu(){ void normal_menu(){
int choice = 0;
do{
system("cls"); system("cls");
welcome_message(); welcome_message();
//the selection menu //the selection menu
int choice = normal_menu_user_choices();
choice = normal_menu_user_choices();
switch (choice) switch (choice)
{ {
@ -40,19 +44,16 @@ bool normal_menu(){
case 2://Search item case 2://Search item
search_item(); search_item();
break; break;
case 3://scan barcode case 3://self help sale system
// scan_barcode(); self_help_sale_system();
break; break;
case 4://Exit case 4://Exit
return false;
break; break;
default://invalid input default://invalid input ,should not happen,as it is already catch in above function
printf("Invalid choice\n"); printf("Invalid choice\n");
return false;
break; break;
} }
}while(choice != 4);
return true;
} }
//universal functions for normal user //universal functions for normal user
@ -72,10 +73,10 @@ void show_item(struct inventory db,int index){
} }
int normal_menu_user_choices(){ int normal_menu_user_choices(){
int choice; int choice;
printf("You can buy items inside the 2 option below\n"); printf("Normal User Menu\n");
printf("1. List items\n"); printf("1. List items\n");
printf("2. Search item\n"); printf("2. Search item\n");
printf("3. Scan Barcodes\n"); printf("3. Self help sale system\n");
printf("4. Exit\n"); printf("4. Exit\n");
printf("Enter your choice: "); printf("Enter your choice: ");
scanf("%d", &choice); scanf("%d", &choice);
@ -122,7 +123,7 @@ void list_page(struct inventory db,struct Map* map,int row){//user for showing l
if(choice <=8 && choice > 0){ if(choice <=8 && choice > 0){
printf("sorting...\n"); printf("sorting...\n");
map = sortItems(db,choice); map = sortItems(db,choice);
}else if(choice == page_size+3){ }else if(choice == page_size+3 && page + 1 < total_pages){
page++; page++;
}else if(choice == page_size+4 && page > 0){ }else if(choice == page_size+4 && page > 0){
page--; page--;
@ -145,10 +146,10 @@ void print_page(struct inventory db, int cur, int end,struct Map* map){
for (int i = cur; i < end; i++) for (int i = cur; i < end; i++)
{ {
if(map != NULL){ if(map != NULL){
printf("%d item: %s\n", i + 9, db.row[map[i].key].product); printf("%d item %d: %s\n", i + 9,i, db.row[map[i].key].product);
} }
else{ else{
printf("%d item: %s\n", i + 9, db.row[i].product); printf("%d item %d: %s\n", i + 9,i, db.row[i].product);
} }
} }
} }
@ -176,20 +177,21 @@ struct Map* sortItems(struct inventory db, int sort){
switch(sort){ switch(sort){
case 1: case 1:
case 2: case 2:
map[i].value = (int)db.row[i].product; map[i].value = (void*)db.row[i].product;
break; break;
case 3: case 3:
case 4: case 4:
map[i].value = (int)db.row[i].price*100;//presume there is no price contain 0.001 long price = db.row[i].price * 100;
map[i].value = (void*)price;//presume there is no price contain 0.001
break; break;
case 5: case 5:
case 6: case 6:
map[i].value = (int)db.row[i].brand; map[i].value = (void*)db.row[i].brand;
break; break;
case 7: case 7:
case 8: case 8:
map[i].value = (int)db.row[i].category; map[i].value = (void*)db.row[i].category;
break; break;
} }
} }
@ -212,11 +214,9 @@ struct Map* sortItems(struct inventory db, int sort){
//search items //search items
char* prompt_search(); char* prompt_search();
char* searchItems(struct inventory db, char* searchstr); struct Map* searchItems(struct inventory db, char* searchstr);
void search_item(){ void search_item(){
system("cls"); system("cls");
welcome_message(); welcome_message();
@ -224,7 +224,6 @@ void search_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
struct inventory db = read_db_invt(); struct inventory db = read_db_invt();
int total_pages = 0;
struct Map* map = NULL; struct Map* map = NULL;
do{ do{
@ -236,8 +235,9 @@ void search_item(){
welcome_message(); welcome_message();
printf("0 exit\n"); printf("0 exit\n");
printf("1 search\n"); printf("1 search\n");
printf("select your choice: ");
scanf("%d", &breakout); scanf("%d", &breakout);
fflush(stdin);
if(breakout == 1 || breakout == 0){ if(breakout == 1 || breakout == 0){
valid = true; valid = true;
}else{ }else{
@ -250,7 +250,7 @@ void search_item(){
char* searchstr = prompt_search(); char* searchstr = prompt_search();
printf("searching...\n"); printf("searching...\n");
map = searchItems(db,searchstr); map = searchItems(db,searchstr);
if(map[0].value == 0){ if(map[0].value > 0){
list_page(db,map+1,map[0].value);//ofset map, as it is use to store the size list_page(db,map+1,map[0].value);//ofset map, as it is use to store the size
}else{//empty search }else{//empty search
printf("No result found\n"); printf("No result found\n");
@ -271,13 +271,14 @@ char* prompt_search(){
return searchstr; return searchstr;
} }
char* 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(strstr(db.row[i].product,searchstr) != NULL){
map[k].value = db.row[i].product;
map[k].value = (void*)db.row[i].product;
k++; k++;
} }
} }
@ -286,3 +287,241 @@ char* searchItems(struct inventory db, char* searchstr){
return map; return map;
} }
//self help
typedef struct cart{//linked list
struct inventory_row* row;//pointer to the row
int quantity;//quantity of the item
struct cart* next;
};
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;
do{
//options
int breakout = 0;
int choice = self_choice();
switch (choice)
{
case 1:
cart = scan_barcode(cart,db);
check = true;
break;
case 2:
cart = list_cart(cart);
check = true;
default:
break;
}
}while(check);
}
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\n");
printf("3 checkout\n");
printf("select an option: ");
scanf("%d", &choice);
if(choice <= 3 || 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){
cart = malloc(sizeof(struct cart));
cart->row = row;
cart->quantity = quantity;
cart->next = NULL;
}else{
struct cart* temp = cart;
while(temp->next != NULL){
temp = temp->next;
}
temp->next = malloc(sizeof(struct cart));
temp->next->row = row;
temp->next->quantity = quantity;
temp->next->next = NULL;
}
return cart;
}
struct cart* Cartremove(struct cart* cart,int index){
if (cart == NULL){
return cart;
}else{
struct cart* temp = cart;
if (index == 0){
cart = cart->next;
free(temp);
return cart;
}else{
for (int i = 0; i < index-1; i++){
temp = temp->next;
}
struct cart* new = temp->next->next;
free(temp->next);
temp->next = new;
return cart;
}
}
}
struct cart* Cartupdate(struct cart* cart,int index,int quantity){
if (cart == NULL){
return cart;
}else{
struct cart* temp = cart;
if (index == 0){
cart->quantity = quantity;
return cart;
}else{
for (int i = 0; i < index-1; i++){
temp = temp->next;
}
temp->next->quantity = quantity;
return cart;
}
}
}
//scan barcode
struct inventory_row* find_barcode(struct inventory db,long barcode);
long prompt_barcode();
struct cart* scan_barcode(struct cart* cart,struct inventory db){
long barcode = prompt_barcode();
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){
cart = Cartappend(cart,row,quantity);
}else{//empty search
printf("Unable to match or invalid input\n");
}
}while(true);//break on top
return cart;
}
long prompt_barcode(){
printf("Please scan the qr code(or input the barcode ) ");
long barcode;
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){
return &db.row[i];
}
}
return (struct inventory_row*)NULL;
}
//list cart
struct cart* cart_control(struct cart* cart,int index);
struct cart* list_cart(struct cart* cart){
int choice = 0;
do{
system("cls");
welcome_message();
double total_price = 0;
int i=1;
printf("0 exit\n");
if(cart == NULL){
printf("Cart is empty\n");
}else{
struct cart* temp = cart;
while(temp != NULL){
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);
temp = temp->next;
i++;
}
printf("Total price: $%.2f\n",total_price);
}
do{
printf("input the corrisponding value for more action\n");
scanf("%d", &choice);
if(choice >0 && choice <= i){
cart = cart_control(cart,choice);
}
}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;
do{
system("cls");
welcome_message();
printf("0 exit\n");
printf("1 remove\n");
printf("2 edit quantity\n");
bool check = false;
do{
scanf("%d", &choice);
if(choice == 1){
cart = Cartremove(cart,index-1);
check = true;
}else if(choice == 2){
cart = update_cart(cart,index);
check = true;
}else if(choice != 0){
printf("Invalid choice\n");
check = false;
}
}while(!check);
}while(choice != 0);
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");
}
return cart;
}

View File

@ -1,6 +1,6 @@
typedef struct Map{ typedef struct Map{
int key; int key;
int value; void* value;
}; };
int compare_decending(const void *a, const void *b){ int compare_decending(const void *a, const void *b){