lister ongoing other small bug fixed, still need to migrate

This commit is contained in:
stmctommyau 2023-09-04 16:33:23 +08:00
parent de75f1bd4f
commit d61a2abc6a
5 changed files with 81 additions and 90 deletions

View File

@ -127,6 +127,13 @@ 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);
break;
case 2://add item
@ -142,16 +149,15 @@ void inv_control(){
}
char * show_item_admin(struct inventory db,int index){
char * output = malloc(sizeof(char) * 1024);
char * output = (char*)malloc(sizeof(char) * 2048);
sprintf(output,"Product: %s\n", db.row[index].product);
sprintf(output,"Catergory: %s\n", db.row[index].category);
sprintf(output,"Brand: %s\n", db.row[index].brand);
sprintf(output,"Price: $%lf\n", db.row[index].price);
sprintf(output,"Stock: %d\n", db.row[index].stock);
sprintf(output,"Barcode: %ld\n",db.row[index].barcode);
sprintf(output+strlen(output),"Catergory: %s\n", db.row[index].category);// strlen to add offset
sprintf(output+strlen(output),"Brand: %s\n", db.row[index].brand);
sprintf(output+strlen(output),"Price: $%lf\n", db.row[index].price);
sprintf(output+strlen(output),"Stock: %d\n", db.row[index].stock);
sprintf(output+strlen(output),"Barcode: %ld\n",db.row[index].barcode);
return output;
}
struct inventory item_control(struct inventory db,int index){
int choice = 0;
if(db.row[index].barcode == -1024){//new item
@ -164,16 +170,16 @@ struct inventory item_control(struct inventory db,int index){
return temp;
}
char * item = show_item_admin(db,index);
char welcome[1035];
char welcome[4096];
strcpy(welcome,item);
strcat(welcome,"Operations");
strcat(welcome,"Operations\n");
do
{
char * items[] = {
"update item",
"remove item"
};
choice = choices_selecter(item,2,welcome);
choice = choices_selecter(items,2,welcome);
switch (choice)
{
case 1:
@ -195,16 +201,16 @@ struct inventory item_control(struct inventory db,int index){
}
void add_item(){
system("cls");
printf("Add new item\n");
struct inventory_row *item = (struct inventory_row *)malloc(sizeof(struct inventory_row));
if(!item_inputer("name",INPUT_TYPE::STRING,item->product) ||
!item_inputer("brand",INPUT_TYPE::STRING,item->brand) ||
!item_inputer("category",INPUT_TYPE::STRING,item->category) ||
!item_inputer("stock",INPUT_TYPE::FLOAT,item->price) ||
!item_inputer("barcode",INPUT_TYPE::LONG,item->barcode)
if(!item_inputer("name",STRING,&item->product,false) ||
!item_inputer("brand",STRING,&item->brand,false) ||
!item_inputer("category",STRING,&item->category,false) ||
!item_inputer("price",DOUBLE,&item->price,false) ||
!item_inputer("stock",INT,&item->stock,false) ||
!item_inputer("barcode",LONG,&item->barcode,false)
){
free(item);
return;
@ -228,11 +234,11 @@ void update_item(struct inventory db,int index){
printf("Price: $%lf\n", db.row[index].price);
printf("Stock: %d\n", db.row[index].stock);
printf("Barcode: %ld\n",db.row[index].barcode);
item_inputer("name",INPUT_TYPE::STRING,db.row[index].product,true);
item_inputer("brand",INPUT_TYPE::STRING,db.row[index].brand,true);
item_inputer("category",INPUT_TYPE::STRING,db.row[index].category,true);
item_inputer("stock",INPUT_TYPE::FLOAT,db.row[index].price,true);
item_inputer("barcode",INPUT_TYPE::LONG,db.row[index].barcode,true);
item_inputer("name",STRING,&db.row[index].product,true);
item_inputer("brand",STRING,&db.row[index].brand,true);
item_inputer("category",STRING,&db.row[index].category,true);
item_inputer("stock",DOUBLE,&db.row[index].price,true);
item_inputer("barcode",LONG,&db.row[index].barcode,true);
if(!update_db_invt(db)){
printf("Failed to update item\n");
exit(1);
@ -458,66 +464,26 @@ void add_tran(){
row->date = get_date();
row->time = get_time();
}else{
temp = prompt_item("Please input date:day \n>");
if(temp == NULL){
free(row);
return;
}
row->date.day = atoi(temp);
temp = prompt_item("Please input date:month \n>");
if(temp == NULL){
free(row);
return;
}
row->date.month = atoi(temp);
temp = prompt_item("Please input date:year \n>");
if(temp == NULL){
free(row);
return;
}
row->date.year = atoi(temp);
temp = prompt_item("Please input time:hour \n>");
if(temp == NULL){
free(row);
return;
}
row->time.hour = atoi(temp);
temp = prompt_item("Please input time:minute \n>");
if(temp == NULL){
free(row);
return;
}
row->time.minute = atoi(temp);
temp = prompt_item("Please input time:second \n>");
if(temp == NULL){
if( !item_inputer("date:day",INT,&row->date.day,false) ||
!item_inputer("date:month",INT,&row->date.month,false) ||
!item_inputer("date:year",INT,&row->date.year,false) ||
!item_inputer("time:hour",INT,&row->time.hour,false) ||
!item_inputer("time:minute",INT,&row->time.minute,false) ||
!item_inputer("time:second",INT,&row->time.second,false)
){
free(row);
return;
}
}
temp = prompt_item("Please input price \n>");
if(temp == NULL){
if (
!item_inputer("price",DOUBLE,&row->price,false) ||
!item_inputer("quantity",INT,&row->quantity,false) ||
!item_inputer("ID",LONG,&row->id,false) ||
!item_inputer("barcode",LONG,&row->barcode,false)
){
free(row);
return;
}
row->price = atof(temp);
temp = prompt_item("Please input quantity \n>");
if(temp == NULL){
free(row);
return;
}
row->quantity = atoi(temp);
temp = prompt_item("Please input ID \n>");
if(temp == NULL){
free(row);
return;
}
row->id = atol(temp);
temp = prompt_item("Please input barcode \n>");
if(temp == NULL){
free(row);
return;
}
row->barcode = atol(temp);
if(row == NULL || !append_transaction_db(row)){
printf("Failed to add item\n");
}else{
@ -713,12 +679,14 @@ struct user add_user(struct user db){
char* temp;
int index = db.db.row_count;
struct user_row *temprow = realloc(db.row, sizeof(struct user_row));
temp = prompt_item("Please input the username\n>");
strcpy(temprow->name,temp);
temp = prompt_item("Please input the id\n>");
temprow->id = atol(temp);
temp = prompt_item("Please input the role\n>");
strcpy(temprow->role,temp);
if (
!item_inputer("name",STRING,&temprow->name,false) ||
!item_inputer("id",LONG,&temprow->id,false) ||
!item_inputer("role",STRING,&temprow->role,false)
){
free(temprow);
return db;
}
if(!append_user(temprow)){
printf("Failed to add user\n");

View File

@ -1,4 +1,5 @@
#name of role that are consider as admin
teacher
Staff
Admin
Admin
teacher
admin

View File

@ -8,3 +8,6 @@ student
Chan Siu Man
student
20002
test
admin
10001

View File

@ -176,11 +176,21 @@ void list_items(){
struct inventory db = read_db_invt();
struct Map* map = NULL;
// 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);
}
//for sorting
struct Map* sortItems(struct inventory db, int sort){
// 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;
@ -259,6 +269,14 @@ void search_item(){
map = searchItems(db,searchstr);
if(map[0].value > 0){
int temp_row = *((int*)map[0].value);
// char * SortItems[] = {
// "Name",
// "Price",
// "Brand",
// "Category",
// };
//FIXME
// lister(&db,map+1,temp_row,"List of items",SortItems,4,print_page,sortItems,item_control);//ofset map, as it is use to store the size
list_page(db,map+1,temp_row,show_item);//ofset map, as it is use to store the size
}else{//empty search
printf("No result found\n");

17
utils.h
View File

@ -40,7 +40,7 @@ int choices_selecter(char * Items[],int items,char * welcome_messages){
}
char* prompt_item(char* prompt,bool empty_allowed=false){
char* prompt_item(char* prompt,bool empty_allowed){
char* item = malloc(sizeof(char) * 100);
do{
printf("%s",prompt);
@ -59,9 +59,9 @@ char* prompt_item(char* prompt,bool empty_allowed=false){
return item;
}
typedef enum{
INT=1,STRING=2,LONG=3,FLOAT=4
INT=1,STRING=2,LONG=3,DOUBLE=4
}INPUT_TYPE;
bool item_inputer(char * varname,INPUT_TYPE type,void * item,bool empty_allowed=false){
bool item_inputer(char * varname,INPUT_TYPE type,void * item,bool empty_allowed){
char output[1024] = "Please input the ";
strcat(output,varname);
strcat(output,": \n>");
@ -71,16 +71,16 @@ bool item_inputer(char * varname,INPUT_TYPE type,void * item,bool empty_allowed=
}
switch(type){
case INT:
item = atoi(temp);
*((int*)item) = atoi(temp);
break;
case STRING:
strcpy(item,temp);
break;
case LONG:
item = atol(temp);
*((long*)item) = atol(temp);
break;
case FLOAT:
item = atof(temp);
case DOUBLE:
*((double*)item) = atof(temp);
break;
default:
return false;
@ -88,7 +88,8 @@ bool item_inputer(char * varname,INPUT_TYPE type,void * item,bool empty_allowed=
return true;
}
void lister(void * db,struct Map* map, int row,char * lister_name,char * SortItems[],int NoSortItems,void (*page_printer)(void*,int,int,struct * Map) , struct * Map (*sorter)(void *,int),void * (*showitem)(void *,int)){
//TODO: lister still useable
void lister(void * db,struct Map* map, int row,char * lister_name,char * SortItems[],int NoSortItems,void (*page_printer)(void*,int,int,struct Map*) , struct Map* (*sorter)(void *,int),void * (*showitem)(void *,int)){
int choice = -1;
int page = 0;
int page_size = PAGE_SIZE;