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);
void admin_menu(){
system("cls");
Cls();
welcome_message();
//the selection menu
// if(login valid)
@ -41,28 +41,32 @@ void admin_menu(){
"Role control",
};
char welcome[256];
sprintf(welcome,"welcome %s(%s)",user->name,user->role);
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
break;
default://invalid input ,should not happen,as it is already catch in above function
printf("Invalid choice\n");
break;
}
bool exit = false;
do{
sprintf(welcome,"welcome %s(%s)",user->name,user->role);
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;
}
@ -107,7 +111,7 @@ void add_item();
void remove_item(struct inventory db,int index);
void update_item(struct inventory db,int index);
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(){
@ -127,14 +131,14 @@ void inv_control(){
db.db.row_count += 1;
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
// char * SortItems[] = {
// "Name",
// "Price",
// "Brand",
// "Category",
// };
// lister(&db,NULL,db.db.row_count,"Inventory",SortItems,4,print_page,sortItems,item_control);
list_page(db,NULL,db.db.row_count,item_control);
char * SortItemss[] = {
"Name",
"Price",
"Brand",
"Category",
};
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);
break;
case 2://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);
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;
if(db.row[index].barcode == -1024){//new item
add_item();
@ -167,7 +172,9 @@ struct inventory item_control(struct inventory db,int index){
temp.db.row_count += 1;
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
return temp;
void * re = malloc(sizeof(temp));
memcpy(re,&temp,sizeof(temp));
return re;
}
char * item = show_item_admin(db,index);
char welcome[4096];
@ -197,12 +204,13 @@ struct inventory item_control(struct inventory db,int index){
temp.db.row_count += 1;
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
return temp;
void * re = malloc(sizeof(temp));
memcpy(re,&temp,sizeof(temp));
return re;
}
void add_item(){
system("cls");
Cls();
printf("Add new item\n");
struct inventory_row *item = (struct inventory_row *)malloc(sizeof(struct inventory_row));
if(!item_inputer("name",STRING,&item->product,false) ||
@ -309,7 +317,7 @@ void list_tran(struct transaction db){
int row = db.db.row_count;
struct Map* map = NULL;
do{
system("cls");
Cls();
welcome_message();
printf("Transaction list\n");
printf("0 exit\n");
@ -363,7 +371,7 @@ void list_tran(struct transaction db){
printf("Enter page size: ");
fflush(stdin);
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){
if(map == NULL){
db = showTran(db,choice - 9 + page_size*page);
@ -499,21 +507,22 @@ void add_tran(){
struct transaction update_tran(struct transaction db,int index){
char temp[100];
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(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%-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%-10s%-10s%-10s%-10s\n","Date","Time","Barcode","ID","Price","Quantity");
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:month",INT,&db.row[index].date.month);
item_inputer("Date:year",INT,&db.row[index].date.year);
item_inputer("Time:hour",INT,&db.row[index].time.hour);
item_inputer("Time:minute",INT,&db.row[index].time.minute);
item_inputer("Time:second",INT,&db.row[index].time.second);
item_inputer("Price",DOUBLE,&db.row[index].price);
item_inputer("Quantity",INT,&db.row[index].quantity);
item_inputer("ID",LONG,&db.row[index].id);
item_inputer("Barcode",LONG,&db.row[index].barcode);
item_inputer("Date:day",INT,&db.row[index].date.day,true);
item_inputer("Date:month",INT,&db.row[index].date.month,true);
item_inputer("Date:year",INT,&db.row[index].date.year,true);
item_inputer("Time:hour",INT,&db.row[index].time.hour,true);
item_inputer("Time:minute",INT,&db.row[index].time.minute,true);
item_inputer("Time:second",INT,&db.row[index].time.second,true);
item_inputer("Price",DOUBLE,&db.row[index].price,true);
item_inputer("Quantity",INT,&db.row[index].quantity,true);
item_inputer("ID",LONG,&db.row[index].id,true);
item_inputer("Barcode",LONG,&db.row[index].barcode,true);
if(!update_db_tran(db)){
printf("Failed to update transaction\n");
@ -604,7 +613,7 @@ void user_control(){
struct user db = read_db_user();
int choice = 0;
while(choice != 3){
system("cls");
Cls();
printf("User Control\n");
printf("1. Add User(shortcut)\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("%-20s%-10s%-10s\n","Username","ID","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("id",LONG,db.row[index].id);
item_inputer("role",STRING,db.row[index].role);
item_inputer("username",STRING,&db.row[index].name,true);
item_inputer("id",LONG,&db.row[index].id,true);
item_inputer("role",STRING,&db.row[index].role,true);
if(!update_db_user(db)){
printf("Failed to update user\n");
@ -702,7 +711,7 @@ struct user list_user(struct user db){
int row = db.db.row_count;
struct Map* map = NULL;
do{
system("cls");
Cls();
welcome_message();
printf("User list\n");
printf("0 exit\n");
@ -752,7 +761,7 @@ struct user list_user(struct user db){
printf("Enter page size: ");
fflush(stdin);
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){
if(map == NULL){
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){
int choice = -1;
do{
system("cls");
Cls();
welcome_message();
printf("User detail\n");
printf("%-20s%-10s%-10s\n","Username","ID","Role");
@ -926,7 +935,7 @@ void list_role(){
struct Map* map = NULL;
//list role
do{
system("cls");
Cls();
welcome_message();
printf("Role list\n");
printf("0 exit\n");

2
main.c
View File

@ -27,7 +27,7 @@
int main(){
bool check;
do{
system("cls");
Cls();
//print welcome message
//prompt weather user is admin or normal user
//then redirect them to the corisponding menu

View File

@ -57,7 +57,7 @@ void normal_menu(){
//universal functions for normal user
struct Map* sortItems(struct inventory db, int sort);
struct inventory show_item(struct inventory db,int index){
system("cls");
Cls();
printf("Product: %s\n", db.row[index].product);
printf("Catergory: %s\n", db.row[index].category);
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{
//options
system("cls");
Cls();
printf("0 exit\n");
printf("1 sort name decending\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: ");
fflush(stdin);
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){
if(map == NULL){
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
void list_items(){
system("cls");
Cls();
welcome_message();
//get the list of items from the database
//print the list of items
@ -176,12 +188,12 @@ void list_items(){
struct inventory db = read_db_invt();
struct Map* map = NULL;
// char * SortItems[] = {
// "Name",
// "Price",
// "Brand",
// "Category",
// };
char * SortItems[] = {
"Name",
"Price",
"Brand",
"Category",
};
// FIXME:function for normal list page
// 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);
@ -241,6 +253,57 @@ struct Map* sortItems(struct inventory db, int sort){
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
@ -454,7 +517,7 @@ struct cart* scan_barcode(struct cart* cart,struct inventory db){
int quantity = 0;
char choice = 'n';
do{
system("cls");
Cls();
printf("product: %s\n",row->product);
printf("price: %.1f\n",row->price);
printf("Enter quantity(0 to cancel)\n>");\
@ -519,7 +582,7 @@ struct cart* checkout(struct cart* cart);
struct cart* list_cart(struct cart* cart){
int choice = 0;
do{
system("cls");
Cls();
welcome_message();
double total_price = 0;
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){
system("cls");
Cls();
welcome_message();
char choice;
do{
@ -623,7 +686,7 @@ struct cart* update_cart(struct cart* cart,int index){
//Checkout
struct cart* checkout(struct cart* cart){
system("cls");
Cls();
welcome_message();
long user_id = 0;

20
utils.h
View File

@ -9,6 +9,15 @@
#include<ctype.h>
#include <stddef.h>
#endif
void Cls(){
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
void welcome_message(){
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 choice = 0;
do{
system("cls");
Cls();
if (welcome_messages !=NULL){
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 total_pages = ceil((double)row / page_size);
do{
system("cls");
Cls();
welcome_message();
printf("%s list\n",lister_name);
printf("0 exit\n");
for(int i=1;i<=NoSortItems*2;i+=2){
printf("%d sort %s decending\n",i,SortItems[i/2-1]);
printf("%d sort %s ascending\n",i+1,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]);
}
if(page+1 == total_pages){
(*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: ");
fflush(stdin);
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){
if(map == NULL){
db = (*showitem)(db,choice - 9 + page_size*page);
@ -186,3 +195,4 @@ s--;
return ((char *)s);
}
#endif