test lister, fix bugs, clear screen os based func

This commit is contained in:
stmctommyau 2023-09-07 21:51:32 +08:00
parent 7587d48557
commit 11249fc563
No known key found for this signature in database
GPG Key ID: 87B1991E1277F054
4 changed files with 160 additions and 78 deletions

View File

@ -26,7 +26,7 @@ struct user_row *prompt_user();
int admin_menu_user_choices(char* name,char* role); int admin_menu_user_choices(char* name,char* role);
void admin_menu(){ void admin_menu(){
system("cls"); Cls();
welcome_message(); welcome_message();
//the selection menu //the selection menu
// if(login valid) // if(login valid)
@ -41,28 +41,32 @@ void admin_menu(){
"Role control", "Role control",
}; };
char welcome[256]; char welcome[256];
sprintf(welcome,"welcome %s(%s)",user->name,user->role); bool exit = false;
switch (choices_selecter(Items,4,welcome)) do{
{ sprintf(welcome,"welcome %s(%s)",user->name,user->role);
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://role management
role_control();
break;
case 5://Exit
break;
default://invalid input ,should not happen,as it is already catch in above function
printf("Invalid choice\n");
break;
}
switch (choices_selecter(Items,4,welcome))
{
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://role management
role_control();
break;
case 5://Exit
exit = true;
break;
default://invalid input ,should not happen,as it is already catch in above function
printf("Invalid choice\n");
break;
}
}while(!exit);
return; return;
} }
@ -107,7 +111,7 @@ void add_item();
void remove_item(struct inventory db,int index); void remove_item(struct inventory db,int index);
void update_item(struct inventory db,int index); 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); void * item_control(void * ddb,int index);
void inv_control(){ void inv_control(){
@ -127,14 +131,14 @@ void inv_control(){
db.db.row_count += 1; db.db.row_count += 1;
strcpy(db.row[db.db.row_count - 1].product,"CREATE NEW ITEM"); strcpy(db.row[db.db.row_count - 1].product,"CREATE NEW ITEM");
db.row[db.db.row_count - 1].barcode = -1024;//IMPORTANT: -1024 is reserved for new item db.row[db.db.row_count - 1].barcode = -1024;//IMPORTANT: -1024 is reserved for new item
// char * SortItems[] = { char * SortItemss[] = {
// "Name", "Name",
// "Price", "Price",
// "Brand", "Brand",
// "Category", "Category",
// }; };
// lister(&db,NULL,db.db.row_count,"Inventory",SortItems,4,print_page,sortItems,item_control); lister(&db,NULL,db.db.row_count,"Inventory",SortItemss,4,Print_page,SortItems,item_control);
list_page(db,NULL,db.db.row_count,item_control); // list_page(db,NULL,db.db.row_count,item_control);
break; break;
case 2://add item case 2://add item
add_item(); add_item();
@ -158,7 +162,8 @@ char * show_item_admin(struct inventory db,int index){
sprintf(output+strlen(output),"Barcode: %ld\n",db.row[index].barcode); sprintf(output+strlen(output),"Barcode: %ld\n",db.row[index].barcode);
return output; return output;
} }
struct inventory item_control(struct inventory db,int index){ void * item_control(void * ddb,int index){
struct inventory db = *((struct inventory*)ddb);
int choice = 0; int choice = 0;
if(db.row[index].barcode == -1024){//new item if(db.row[index].barcode == -1024){//new item
add_item(); add_item();
@ -167,7 +172,9 @@ struct inventory item_control(struct inventory db,int index){
temp.db.row_count += 1; temp.db.row_count += 1;
strcpy(temp.row[temp.db.row_count - 1].product,"CREATE NEW ITEM"); strcpy(temp.row[temp.db.row_count - 1].product,"CREATE NEW ITEM");
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; void * re = malloc(sizeof(temp));
memcpy(re,&temp,sizeof(temp));
return re;
} }
char * item = show_item_admin(db,index); char * item = show_item_admin(db,index);
char welcome[4096]; char welcome[4096];
@ -197,12 +204,13 @@ struct inventory item_control(struct inventory db,int index){
temp.db.row_count += 1; temp.db.row_count += 1;
strcpy(temp.row[temp.db.row_count - 1].product,"CREATE NEW ITEM"); strcpy(temp.row[temp.db.row_count - 1].product,"CREATE NEW ITEM");
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; void * re = malloc(sizeof(temp));
memcpy(re,&temp,sizeof(temp));
return re;
} }
void add_item(){ void add_item(){
system("cls"); Cls();
printf("Add new item\n"); printf("Add new item\n");
struct inventory_row *item = (struct inventory_row *)malloc(sizeof(struct inventory_row)); struct inventory_row *item = (struct inventory_row *)malloc(sizeof(struct inventory_row));
if(!item_inputer("name",STRING,&item->product,false) || if(!item_inputer("name",STRING,&item->product,false) ||
@ -309,7 +317,7 @@ void list_tran(struct transaction db){
int row = db.db.row_count; int row = db.db.row_count;
struct Map* map = NULL; struct Map* map = NULL;
do{ do{
system("cls"); Cls();
welcome_message(); welcome_message();
printf("Transaction list\n"); printf("Transaction list\n");
printf("0 exit\n"); printf("0 exit\n");
@ -363,7 +371,7 @@ void list_tran(struct transaction db){
printf("Enter page size: "); printf("Enter page size: ");
fflush(stdin); fflush(stdin);
scanf("%d", &page_size); scanf("%d", &page_size);
total_pages = ceil(row / page_size); total_pages = ceil((double)row / page_size);
}else if(choice >= 9 && choice <= current_page_size){ }else if(choice >= 9 && choice <= current_page_size){
if(map == NULL){ if(map == NULL){
db = showTran(db,choice - 9 + page_size*page); db = showTran(db,choice - 9 + page_size*page);
@ -499,21 +507,22 @@ void add_tran(){
struct transaction update_tran(struct transaction db,int index){ struct transaction update_tran(struct transaction db,int index){
char temp[100]; char temp[100];
printf("Update transaction(empty value to not change the value)\n"); printf("Update transaction(empty value to not change the value)\n");
char date[11],time[9];
sprintf(date,"%02d-%02d-%04d",db.row[index].date.day,db.row[index].date.month,db.row[index].date.year); sprintf(date,"%02d-%02d-%04d",db.row[index].date.day,db.row[index].date.month,db.row[index].date.year);
sprintf(time,"%02d:%02d:%02d",db.row[index].time.hour,db.row[index].time.minute,db.row[index].time.second); sprintf(time,"%02d:%02d:%02d",db.row[index].time.hour,db.row[index].time.minute,db.row[index].time.second);
printf("%-15s%-10s%-10s%-10s%-10s%-10s%-10s\n","Date","Time","Barcode","ID","Price","Quantity","Total"); printf("%-15s%-10s%-10s%-10s%-10s%-10s\n","Date","Time","Barcode","ID","Price","Quantity");
printf("%-15s%-10s%-10d%-10d%-10.2lf%-10d%-10.2lf\n",date,time,db.row[index].barcode,db.row[index].id,db.row[index].price,db.row[index].quantity,total); printf("%-15s%-10s%-10d%-10d%-10.2lf%-10d\n",date,time,db.row[index].barcode,db.row[index].id,db.row[index].price,db.row[index].quantity);
item_inputer("Date:day",INT,&db.row[index].date.day); item_inputer("Date:day",INT,&db.row[index].date.day,true);
item_inputer("Date:month",INT,&db.row[index].date.month); item_inputer("Date:month",INT,&db.row[index].date.month,true);
item_inputer("Date:year",INT,&db.row[index].date.year); item_inputer("Date:year",INT,&db.row[index].date.year,true);
item_inputer("Time:hour",INT,&db.row[index].time.hour); item_inputer("Time:hour",INT,&db.row[index].time.hour,true);
item_inputer("Time:minute",INT,&db.row[index].time.minute); item_inputer("Time:minute",INT,&db.row[index].time.minute,true);
item_inputer("Time:second",INT,&db.row[index].time.second); item_inputer("Time:second",INT,&db.row[index].time.second,true);
item_inputer("Price",DOUBLE,&db.row[index].price); item_inputer("Price",DOUBLE,&db.row[index].price,true);
item_inputer("Quantity",INT,&db.row[index].quantity); item_inputer("Quantity",INT,&db.row[index].quantity,true);
item_inputer("ID",LONG,&db.row[index].id); item_inputer("ID",LONG,&db.row[index].id,true);
item_inputer("Barcode",LONG,&db.row[index].barcode); item_inputer("Barcode",LONG,&db.row[index].barcode,true);
if(!update_db_tran(db)){ if(!update_db_tran(db)){
printf("Failed to update transaction\n"); printf("Failed to update transaction\n");
@ -604,7 +613,7 @@ void user_control(){
struct user db = read_db_user(); struct user db = read_db_user();
int choice = 0; int choice = 0;
while(choice != 3){ while(choice != 3){
system("cls"); Cls();
printf("User Control\n"); printf("User Control\n");
printf("1. Add User(shortcut)\n"); printf("1. Add User(shortcut)\n");
printf("2. List User\n"); printf("2. List User\n");
@ -661,9 +670,9 @@ struct user edit_user(struct user db,int index){
printf("Update transaction(empty value to not change the value)\n"); printf("Update transaction(empty value to not change the value)\n");
printf("%-20s%-10s%-10s\n","Username","ID","Role"); printf("%-20s%-10s%-10s\n","Username","ID","Role");
printf("%-20s%-10ld%-10s\n",db.row[index].name,db.row[index].id,db.row[index].role); printf("%-20s%-10ld%-10s\n",db.row[index].name,db.row[index].id,db.row[index].role);
item_inputer("username",STRING,db.row[index].name); item_inputer("username",STRING,&db.row[index].name,true);
item_inputer("id",LONG,db.row[index].id); item_inputer("id",LONG,&db.row[index].id,true);
item_inputer("role",STRING,db.row[index].role); item_inputer("role",STRING,&db.row[index].role,true);
if(!update_db_user(db)){ if(!update_db_user(db)){
printf("Failed to update user\n"); printf("Failed to update user\n");
@ -702,7 +711,7 @@ struct user list_user(struct user db){
int row = db.db.row_count; int row = db.db.row_count;
struct Map* map = NULL; struct Map* map = NULL;
do{ do{
system("cls"); Cls();
welcome_message(); welcome_message();
printf("User list\n"); printf("User list\n");
printf("0 exit\n"); printf("0 exit\n");
@ -752,7 +761,7 @@ struct user list_user(struct user db){
printf("Enter page size: "); printf("Enter page size: ");
fflush(stdin); fflush(stdin);
scanf("%d", &page_size); scanf("%d", &page_size);
total_pages = ceil(row / page_size); total_pages = ceil((double)row / page_size);
}else if(choice >= 7 && choice <= current_page_size){ }else if(choice >= 7 && choice <= current_page_size){
if(map == NULL){ if(map == NULL){
db = showUser(db,choice - 7 + page_size*page); db = showUser(db,choice - 7 + page_size*page);
@ -832,7 +841,7 @@ struct Map* sortUser(struct user db,int choice){
struct user showUser(struct user db,int index){ struct user showUser(struct user db,int index){
int choice = -1; int choice = -1;
do{ do{
system("cls"); Cls();
welcome_message(); welcome_message();
printf("User detail\n"); printf("User detail\n");
printf("%-20s%-10s%-10s\n","Username","ID","Role"); printf("%-20s%-10s%-10s\n","Username","ID","Role");
@ -926,7 +935,7 @@ void list_role(){
struct Map* map = NULL; struct Map* map = NULL;
//list role //list role
do{ do{
system("cls"); Cls();
welcome_message(); welcome_message();
printf("Role list\n"); printf("Role list\n");
printf("0 exit\n"); printf("0 exit\n");

2
main.c
View File

@ -27,7 +27,7 @@
int main(){ int main(){
bool check; bool check;
do{ do{
system("cls"); 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

View File

@ -57,7 +57,7 @@ void normal_menu(){
//universal functions for normal user //universal functions for normal user
struct Map* sortItems(struct inventory db, int sort); struct Map* sortItems(struct inventory db, int sort);
struct inventory show_item(struct inventory db,int index){ struct inventory show_item(struct inventory db,int index){
system("cls"); Cls();
printf("Product: %s\n", db.row[index].product); printf("Product: %s\n", db.row[index].product);
printf("Catergory: %s\n", db.row[index].category); printf("Catergory: %s\n", db.row[index].category);
printf("Brand: %s\n", db.row[index].brand); printf("Brand: %s\n", db.row[index].brand);
@ -79,7 +79,7 @@ void list_page(struct inventory db,struct Map* map,int row,struct inventory (*sh
do{ do{
//options //options
system("cls"); Cls();
printf("0 exit\n"); printf("0 exit\n");
printf("1 sort name decending\n"); printf("1 sort name decending\n");
printf("2 sort name ascending\n"); printf("2 sort name ascending\n");
@ -136,7 +136,7 @@ void list_page(struct inventory db,struct Map* map,int row,struct inventory (*sh
printf("Enter page size: "); printf("Enter page size: ");
fflush(stdin); fflush(stdin);
scanf("%d", &page_size); scanf("%d", &page_size);
total_pages = ceil(row / page_size); total_pages = ceil((double)row / page_size);
}else if(choice >= 9 && choice <= current_page_size){ }else if(choice >= 9 && choice <= current_page_size){
if(map == NULL){ if(map == NULL){
db = (*showitem)(db,choice - 9 + page_size*page); db = (*showitem)(db,choice - 9 + page_size*page);
@ -163,11 +163,23 @@ void print_page(struct inventory db, int cur, int end,struct Map* map){
} }
} }
} }
void Print_page(void * ddb, int cur, int end,struct Map* map){
struct inventory db = *((struct inventory*)ddb);
for (int i = cur; i < end; i++)
{
if(map != NULL){
printf("%d item %d: %s\n", i + 9,i, db.row[map[i].key].product);
}
else{
printf("%d item %d: %s\n", i + 9,i, db.row[i].product);
}
}
}
//list items //list items
void list_items(){ void list_items(){
system("cls"); Cls();
welcome_message(); welcome_message();
//get the list of items from the database //get the list of items from the database
//print the list of items //print the list of items
@ -176,12 +188,12 @@ void list_items(){
struct inventory db = read_db_invt(); struct inventory db = read_db_invt();
struct Map* map = NULL; struct Map* map = NULL;
// char * SortItems[] = { char * SortItems[] = {
// "Name", "Name",
// "Price", "Price",
// "Brand", "Brand",
// "Category", "Category",
// }; };
// FIXME:function for normal list page // FIXME:function for normal list page
// lister(&db,map,db.db.row_count,"List of items",SortItems,4,print_page,sortItems,item_control); // lister(&db,map,db.db.row_count,"List of items",SortItems,4,print_page,sortItems,item_control);
list_page(db,map,db.db.row_count,show_item); list_page(db,map,db.db.row_count,show_item);
@ -241,6 +253,57 @@ struct Map* sortItems(struct inventory db, int sort){
return map; return map;
} }
struct Map* SortItems(void * ddb, int sort){
struct inventory db = *((struct inventory*)ddb);
struct Map *map = malloc(sizeof(struct Map) * db.db.row_count-1);
for (int i = 0; i < db.db.row_count-1; i++){
map[i].key = i;
switch(sort){
case 1:
case 2:
map[i].value = (void*)db.row[i].product;
break;
case 3:
case 4:;
double price = db.row[i].price * 100;
double * price_star = malloc(sizeof(long));
*price_star = price;
map[i].value = (void*)price_star;//presume there is no price contain 0.001
break;
case 5:
case 6:
map[i].value = (void*)db.row[i].brand;
break;
case 7:
case 8:
map[i].value = (void*)db.row[i].category;
break;
}
}
switch (sort){
case 1:
case 5:
case 7:
qsort(map, db.db.row_count-1, sizeof(struct Map), compare_decending_str);
break;
case 2:
case 6:
case 8:
qsort(map, db.db.row_count-1, sizeof(struct Map), compare_ascending_str);
break;
case 3:
qsort(map, db.db.row_count-1, sizeof(struct Map), compare_decending);
break;
case 4:
qsort(map, db.db.row_count-1, sizeof(struct Map), compare_ascending);
break;
}
map = realloc(map, sizeof(struct Map) * db.db.row_count);
map[db.db.row_count-1].key = db.db.row_count-1;
return map;
}
//search items //search items
@ -454,7 +517,7 @@ struct cart* scan_barcode(struct cart* cart,struct inventory db){
int quantity = 0; int quantity = 0;
char choice = 'n'; char choice = 'n';
do{ do{
system("cls"); Cls();
printf("product: %s\n",row->product); printf("product: %s\n",row->product);
printf("price: %.1f\n",row->price); printf("price: %.1f\n",row->price);
printf("Enter quantity(0 to cancel)\n>");\ printf("Enter quantity(0 to cancel)\n>");\
@ -519,7 +582,7 @@ 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{
system("cls"); Cls();
welcome_message(); welcome_message();
double total_price = 0; double total_price = 0;
int i=1; int i=1;
@ -595,7 +658,7 @@ struct cart* cart_control(struct cart* cart,int index){
} }
struct cart* update_cart(struct cart* cart,int index){ struct cart* update_cart(struct cart* cart,int index){
system("cls"); Cls();
welcome_message(); welcome_message();
char choice; char choice;
do{ do{
@ -623,7 +686,7 @@ struct cart* update_cart(struct cart* cart,int index){
//Checkout //Checkout
struct cart* checkout(struct cart* cart){ struct cart* checkout(struct cart* cart){
system("cls"); Cls();
welcome_message(); welcome_message();
long user_id = 0; long user_id = 0;

20
utils.h
View File

@ -9,6 +9,15 @@
#include<ctype.h> #include<ctype.h>
#include <stddef.h> #include <stddef.h>
#endif #endif
void Cls(){
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
void welcome_message(){ void welcome_message(){
printf("><Welcome to the Student Union POS system><\n");//welcome messgae printf("><Welcome to the Student Union POS system><\n");//welcome messgae
} }
@ -16,7 +25,7 @@ void welcome_message(){
int choices_selecter(char * Items[],int items,char * welcome_messages){ int choices_selecter(char * Items[],int items,char * welcome_messages){
int choice = 0; int choice = 0;
do{ do{
system("cls"); Cls();
if (welcome_messages !=NULL){ if (welcome_messages !=NULL){
printf("%s",welcome_messages); printf("%s",welcome_messages);
@ -95,13 +104,13 @@ void lister(void * db,struct Map* map, int row,char * lister_name,char * SortIte
int page_size = PAGE_SIZE; int page_size = PAGE_SIZE;
int total_pages = ceil((double)row / page_size); int total_pages = ceil((double)row / page_size);
do{ do{
system("cls"); Cls();
welcome_message(); welcome_message();
printf("%s list\n",lister_name); printf("%s list\n",lister_name);
printf("0 exit\n"); printf("0 exit\n");
for(int i=1;i<=NoSortItems*2;i+=2){ for(int i=1;i<=NoSortItems*2;i+=2){
printf("%d sort %s decending\n",i,SortItems[i/2-1]); printf("%d sort %s decending\n",i,SortItems[i/2]);
printf("%d sort %s ascending\n",i+1,SortItems[i/2-1]); printf("%d sort %s ascending\n",i+1,SortItems[i/2]);
} }
if(page+1 == total_pages){ if(page+1 == total_pages){
(*page_printer)(db,page*page_size,row,map); (*page_printer)(db,page*page_size,row,map);
@ -145,7 +154,7 @@ void lister(void * db,struct Map* map, int row,char * lister_name,char * SortIte
printf("Enter page size: "); printf("Enter page size: ");
fflush(stdin); fflush(stdin);
scanf("%d", &page_size); scanf("%d", &page_size);
total_pages = ceil(row / page_size); total_pages = ceil((double)row / page_size);
}else if(choice >= 9 && choice <= current_page_size){ }else if(choice >= 9 && choice <= current_page_size){
if(map == NULL){ if(map == NULL){
db = (*showitem)(db,choice - 9 + page_size*page); db = (*showitem)(db,choice - 9 + page_size*page);
@ -186,3 +195,4 @@ s--;
return ((char *)s); return ((char *)s);
} }
#endif #endif