admin ongoing;

inventory control basically finish;
debug needed
This commit is contained in:
stmctommyau 2022-09-06 23:25:14 +08:00
parent 1d69fa070c
commit 771b36b4a6
5 changed files with 203 additions and 172 deletions

View File

@ -120,89 +120,80 @@ int admin_menu_user_choices(char* name,char* role){
//invetory control //invetory control
void add_item(); void add_item();
void remove_item(); void remove_item(struct inventory db,int index);
void update_item(); void update_item(struct inventory db,int index);
void view_item();
void admin_list_pages(struct inventory db); void admin_list_pages(struct inventory db);
void item_control(struct inventory db,int index);
void inv_control(){ int prompt_inv_control(){
int choice = 0;
do{
system("cls"); system("cls");
struct inventory db = read_db_invt(); printf("Please select an option:\n");
admin_list_pages(db,NULL,db.row); printf("1. List(allow all operation include add)\n");
printf("2. Add item(shortcut to add item)\n");
printf("3. Exit\n");
printf(">");
scanf("%d", &choice);
if(choice < 1 || choice > 3){
printf("Invalid choice...press any key to retry....\n");
fflush(stdin);
getchar();
}
}while(choice < 1 || choice > 3);
return choice;
} }
void item_control(struct inventory db,int index); void inv_control(){
void admin_list_pages(struct inventory db,struct Map* map,int row){//user for showing list result and search result struct inventory db = read_db_invt();
int choice = -1; int choice = 0;
int page = 0;
int page_size = PAGE_SIZE;
int total_pages = ceil((double)row / page_size);
do{ do{
choice = prompt_inv_control();
//options switch (choice){
system("cls"); case 1://list
printf("0 exit\n"); //add a new element with product name new item
printf("1 sort name decending\n"); //for item control
printf("2 sort name ascending\n"); db.row = (struct inventory_row *)realloc(db.row, sizeof(struct inventory_row) * (db.db.row_count + 1));
printf("3 sort price decending\n"); db.db.row_count += 1;
printf("4 sort price ascending\n"); strcpy(db.row[db.db.row_count - 1].product,"CREATE NEW ITEM");
printf("5 sort brand decending\n"); db.row[db.db.row_count - 1].barcode = -1024;//IMPORTANT: -1024 is reserved for new item
printf("6 sort brand ascending\n"); list_page(db,NULL,db.db.row_count,item_control);
printf("7 sort category decending\n"); break;
printf("8 sort category ascending\n"); case 2://add item
printf("List of items:\n");
//print items
if(page + 1 >= total_pages){
print_page(db, page * page_size, row,map);
}else{//sorted)
print_page(db, page * page_size, (page + 1) * page_size,map);
}
printf("%d new item",page_size+4);
//page control
printf("%d next page\n", page_size+5);
printf("%d previous page\n", page_size+6);
printf("%d set page size\n", page_size+7);
printf("%d/%d/%d(page size/page number/total)\n",page_size, page+1,total_pages);
//prompt user to select an item
bool valid = true;
do{
valid = true;
printf("Enter your choice: ");
fflush(stdin);
scanf("%d", &choice);
if(choice <=8 && choice > 0){
printf("sorting...\n");
map = sortItems(db,choice);
}else if(choice == page_size+3){
add_item(); add_item();
}else if(choice == page_size+4 && page + 1 < total_pages){ break;
page++; case 3://exit
}else if(choice == page_size+5 && page > 0){ break;
page--; default://should not happen
}else if(choice == page_size+6){
printf("Enter page size: ");
fflush(stdin);
scanf("%d", &page_size);
total_pages = ceil(row / page_size);
}else if(choice >= 9 && choice < row+9){
item_control(db,choice - 9 + page_size*page);
}else if(choice != 0){
printf("Invalid choice\n"); printf("Invalid choice\n");
valid = false; break;
} }
}while(!valid); }while(choice != 3);
}
}while(choice != 0); void show_item_admin(struct inventory db,int index){
system("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);
printf("Price: $%lf\n", db.row[index].price);
printf("Stock: %d\n", db.row[index].stock);
printf("Barcode: %ld\n",db.row[index].barcode);
getchar();
return;
} }
void item_control(struct inventory db,int index){ void item_control(struct inventory db,int index){
int choice = 0; int choice = 0;
if(db.row[index].barcode == -1024){//new item
add_item();
return;
}
do do
{ {
system("cls"); system("cls");
show_item(db,index); show_item_admin(db,index);
printf("Operations\n");
printf("0 exit\n"); printf("0 exit\n");
printf("1 update item\n"); printf("1 update item\n");
printf("2 remove item\n"); printf("2 remove item\n");
@ -223,26 +214,65 @@ void item_control(struct inventory db,int index){
} while (choice != 0); } while (choice != 0);
} }
char* prompt_item(char* prompt){
char* item = malloc(sizeof(char) * 100);
do{
printf("%s",prompt);
fflush(stdin);
scanf("%[^\n]",item);
if(strcmp(item,"") == 0){
printf("Invalid input, try again?(y/n)\n");
fflush(stdin);
char c = getchar();
if(c == 'n'){
return NULL;
}
}
}while(strcmp(item,"") == 0);
return item;
}
void add_item(){ void add_item(){
char* temp;
system("cls"); system("cls");
printf("Add new item\n"); printf("Add new item\n");
printf("Please input the item name\n>"); struct inventory_row *item = (struct inventory_row *)malloc(sizeof(struct inventory_row));
struct inventory_row *item; temp = prompt_item("Please input the name: \n>");
fflush(stdin); if(temp == NULL){
scanf("%[^\n]", item->product); free(item);
printf("Please input the item brand\n>"); return;
fflush(stdin); }
scanf("%[^\n]", item->brand); strcpy(item->product,temp);
printf("Please input the item price\n>"); temp = prompt_item("Please input the brand: \n>");
scanf("%lf", &item->price); if(temp == NULL){
printf("Please input the item stock\n>"); free(item);
scanf("%d", &item->stock); return;
printf("Please input the item category\n>"); }
fflush(stdin); strcpy(item->brand,temp);
scanf("%[^\n]", item->category); temp = prompt_item("Please input the category: \n>");
printf("Please input the item barcode\n>"); if(temp == NULL){
fflush(stdin); free(item);
scanf("%[^\n]", item->barcode); return;
}
strcpy(item->category,temp);
temp = prompt_item("Please input the price: \n>");
if(temp == NULL){
free(item);
return;
}
item->price = atof(temp);
temp = prompt_item("Please input the stock: \n>");
if(temp == NULL){
free(item);
return;
}
item->stock = atoi(temp);
temp = prompt_item("Please input the barcode: \n>");
if(temp == NULL){
free(item);
return;
}
item->barcode = atol(temp);
if(item == NULL || !append_inventory(item)){ if(item == NULL || !append_inventory(item)){
printf("Failed to add item\n"); printf("Failed to add item\n");
}else{ }else{
@ -254,24 +284,52 @@ void add_item(){
} }
void update_item(struct inventory db,int index){ void update_item(struct inventory db,int index){
system("cls"); char temp[100];
printf("Update item\n"); printf("Update item(empty value to not change the value)\n");
printf("Please input the item name\n>");
printf("Please input the name(original:%s)\n>",db.row[index].product);
fflush(stdin); fflush(stdin);
scanf("%[^\n]", db.row[index].product); scanf("%[^\n]", temp);//input until /n
printf("Please input the item brand\n>"); if(strcmp(temp,"") != 0){//check if temp is not empty
strcpy(db.row[index].product,temp);//if yes, copy temp to product
}//else preserve the original value
printf("Please input the brand(orginal:%s)\n>",db.row[index].brand);
fflush(stdin); fflush(stdin);
scanf("%[^\n]", db.row[index].brand); scanf("%[^\n]", temp);
printf("Please input the item price\n>"); if(strcmp(temp,"") != 0){
scanf("%lf", &db.row[index].price); strcpy(db.row[index].brand,temp);
printf("Please input the item stock\n>"); }
scanf("%d", &db.row[index].stock);
printf("Please input the item category\n>"); printf("Please input the category(orginal:%s)\n>",db.row[index].category);
fflush(stdin); fflush(stdin);
scanf("%[^\n]", db.row[index].category); scanf("%[^\n]", temp);
printf("Please input the item barcode\n>"); if(strcmp(temp,"") != 0){
strcpy(db.row[index].category,temp);
}
printf("Please input the price(orginal:$%.1lf)\n>",db.row[index].price);
fflush(stdin); fflush(stdin);
scanf("%[^\n]", db.row[index].barcode); scanf("%[^\n]", temp);
if(strcmp(temp,"") != 0){
db.row[index].price = atof(temp);
}
printf("Please input the stock(orginal:%d)\n>",db.row[index].stock);
fflush(stdin);
scanf("%[^\n]", temp);
if(strcmp(temp,"") != 0){
db.row[index].stock = atoi(temp);
}
printf("Please input the barcode(original:%ld)\n>",db.row[index].barcode);
fflush(stdin);
scanf("%[^\n]", temp);
if(strcmp(temp,"") != 0){
db.row[index].barcode = atol(temp);
}
if(!update_db_invt(db)){ if(!update_db_invt(db)){
printf("Failed to update item\n"); printf("Failed to update item\n");
}else{ }else{
@ -283,7 +341,6 @@ void update_item(struct inventory db,int index){
} }
void remove_item(struct inventory db,int index){ void remove_item(struct inventory db,int index){
system("cls");
printf("Remove item\n"); printf("Remove item\n");
printf("Are you sure you want to remove this item? (y/n)\n"); printf("Are you sure you want to remove this item? (y/n)\n");
char choice; char choice;

View File

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

View File

@ -15,7 +15,7 @@
#endif // !DNT_H #endif // !DNT_H
typedef struct basic_db{ typedef struct basic_db{
int row_count; int row_count;
bool init_status; bool init_status;//not really used can be useful
//array of struct of row //array of struct of row
}basic_db; }basic_db;
@ -298,6 +298,8 @@ bool update_db_invt(struct inventory invt){
return false; return false;
} }
for(int i=0;i<invt.db.row_count;i++){ for(int i=0;i<invt.db.row_count;i++){
if(invt.row[i].barcode == -1024)//skip create new row
continue;
if(invt.row[i].isdeleted == true){ if(invt.row[i].isdeleted == true){
continue; continue;
} }
@ -451,29 +453,12 @@ bool append_inventory(struct inventory_row* row){
printf("Error in opening file\n"); printf("Error in opening file\n");
return false; return false;
} }
for(INVENTORY i=category;i<=ENDOFINVENTORY;i++){ fprintf(fp,"%s\n",row->category);
switch(i){ fprintf(fp,"%s\n",row->brand);
case category: fprintf(fp,"%s\n",row->product);
fprintf(fp,"%s",row.category); fprintf(fp,"%.1f\n",row->price);
break; fprintf(fp,"%d\n",row->stock);
case brand: fprintf(fp,"%ld\n",row->barcode);
fprintf(fp,"%s",row.brand);
break;
case product:
fprintf(fp,"%s",row.product);
break;
case price_inv:
fprintf(fp,"%.1f",row.price);
break;
case stock:
fprintf(fp,"%d",row.stock);
break;
case barcode_inv:
fprintf(fp,"%ld",row.barcode);
break;
}
fprintf(fp,"\n");
}
fclose(fp); fclose(fp);
return true; return true;
} }
@ -484,29 +469,12 @@ bool append_transaction_db(struct transaction_row* row){
printf("Error in opening file\n"); printf("Error in opening file\n");
return false; return false;
} }
for(TRANSACTION i=date;i<=ENDOFTRANSACTION;i++){ fprintf(fp,"%d-%02d-%02d\n",row->date.year,row->date.month,row->date.day);
switch(i){ fprintf(fp,"%02d:%02d:%02d\n",row->time.hour,row->time.minute,row->time.second);
case date: fprintf(fp,"%ld\n",row->id);
fprintf(fp,"%d-%02d-%02d",row.date.year,row.date.month,row.date.day); fprintf(fp,"%.1f\n",row->price);
break; fprintf(fp,"%d\n",row->quantity);
case TIME: fprintf(fp,"%ld\n",row->barcode);
fprintf(fp,"%02d:%02d:%02d",row.time.hour,row.time.minute,row.time.second);
break;
case id_tran:
fprintf(fp,"%ld",row.id);
break;
case price_tran:
fprintf(fp,"%.1f",row.price);
break;
case quantity:
fprintf(fp,"%d",row.quantity);
break;
case barcode_tran:
fprintf(fp,"%ld",row.barcode);
break;
}
fprintf(fp,"\n");
}
fclose(fp); fclose(fp);
return true; return true;
} }
@ -517,20 +485,9 @@ bool append_user(struct user_row* row){
printf("Error in opening file\n"); printf("Error in opening file\n");
return false; return false;
} }
for(USER i=name;i<=ENDOFUSER;i++){ fprintf(fp,"%s\n",row->name);
switch(i){ fprintf(fp,"%s\n",row->role);
case name: fprintf(fp,"%ld\n",row->id);
fprintf(fp,"%s",row.name);
break;
case role:
fprintf(fp,"%s",row.role);
break;
case id_user:
fprintf(fp,"%ld",row.id);
break;
}
fprintf(fp,"\n");
}
fclose(fp); fclose(fp);
return true; return true;
} }

View File

@ -85,7 +85,9 @@ int normal_menu_user_choices(){
scanf("%d", &choice); scanf("%d", &choice);
return choice; return choice;
} }
void list_page(struct inventory db,struct Map* map,int row){//user for showing list result and search result
//user for showing list result and search result
void list_page(struct inventory db,struct Map* map,int row,void (*showitem)(struct inventory,int)){//showitem is a function pointer for showing item,allow customization for better flexibilty
int choice = -1; int choice = -1;
int page = 0; int page = 0;
int page_size = PAGE_SIZE; int page_size = PAGE_SIZE;
@ -138,7 +140,7 @@ void list_page(struct inventory db,struct Map* map,int row){//user for showing l
scanf("%d", &page_size); scanf("%d", &page_size);
total_pages = ceil(row / page_size); total_pages = ceil(row / page_size);
}else if(choice >= 9 && choice < row+9){ }else if(choice >= 9 && choice < row+9){
show_item(db,choice - 9 + page_size*page); (*showitem)(db,choice - 9 + page_size*page);
}else if(choice != 0){ }else if(choice != 0){
printf("Invalid choice\n"); printf("Invalid choice\n");
valid = false; valid = false;
@ -172,7 +174,7 @@ void list_items(){
struct inventory db = read_db_invt(); struct inventory db = read_db_invt();
struct Map* map = NULL; struct Map* map = NULL;
list_page(db,map,db.db.row_count); list_page(db,map,db.db.row_count,show_item);
} }
//for sorting //for sorting
@ -180,6 +182,9 @@ struct Map* sortItems(struct inventory db, int sort){
struct Map *map = malloc(sizeof(struct Map) * db.db.row_count); struct Map *map = malloc(sizeof(struct Map) * db.db.row_count);
for (int i = 0; i < db.db.row_count; i++){ for (int i = 0; i < db.db.row_count; i++){
map[i].key = i; map[i].key = i;
if(db.row[i].barcode == -1024)//catch new item
map[i].value = NULL;
switch(sort){ switch(sort){
case 1: case 1:
case 2: case 2:
@ -259,7 +264,7 @@ void search_item(){
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,(int)map[0].value);//ofset map, as it is use to store the size list_page(db,map+1,(int)map[0].value,show_item);//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");
printf("Press any key to continue\n"); printf("Press any key to continue\n");
@ -479,7 +484,7 @@ struct cart* scan_barcode(struct cart* cart,struct inventory db){
do{ do{
system("cls"); system("cls");
printf("product: %s\n",row->product); printf("product: %s\n",row->product);
printf("price: %.2f\n",row->price); printf("price: %.1f\n",row->price);
printf("Enter quantity(0 to cancel)\n>");\ printf("Enter quantity(0 to cancel)\n>");\
fflush(stdin); fflush(stdin);
scanf("%d", &quantity); scanf("%d", &quantity);
@ -556,15 +561,15 @@ struct cart* list_cart(struct cart* cart){
double price = temp->row->price * qty; double price = temp->row->price * qty;
total_price += price; total_price += price;
printf("%d product:%s\n",i,temp->row->product); printf("%d product:%s\n",i,temp->row->product);
printf(" price(per qty): %.2f\n",temp->row->price);//space to align printf(" price(per qty): %.1f\n",temp->row->price);//space to align
printf(" price(total): %.2f\n",price); printf(" price(total): %.1f\n",price);
printf(" quantity: %d\n",qty); printf(" quantity: %d\n",qty);
printf("<------------------------>\n"); printf("<------------------------>\n");
temp = temp->next; temp = temp->next;
i++; i++;
} }
printf("\n<------------------------>\n"); printf("\n<------------------------>\n");
printf("Total price: $%.2f\n",total_price); printf("Total price: $%.1f\n",total_price);
printf("<------------------------>\n"); printf("<------------------------>\n");
printf("\n%d CHECKOUT\n",i); printf("\n%d CHECKOUT\n",i);
@ -595,7 +600,7 @@ struct cart* cart_control(struct cart* cart,int index){
printf("1 remove\n"); printf("1 remove\n");
printf("2 edit quantity\n"); printf("2 edit quantity\n");
printf("product: %s\n",row->product); printf("product: %s\n",row->product);
printf("price: %.2f\n",row->price); printf("price: %.1f\n",row->price);
printf("quantity: %d\n",quantity); printf("quantity: %d\n",quantity);
bool check; bool check;
do{ do{

View File

@ -4,9 +4,21 @@ typedef struct Map{
}Map; }Map;
int compare_decending(const void *a, const void *b){ int compare_decending(const void *a, const void *b){
if((*(struct Map *)b).value == NULL){//for dont sort new item
return -1;
}else if((*(struct Map *)a).value == NULL){
return 1;
}
return (*(struct Map *)b).value - (*(struct Map *)a).value; return (*(struct Map *)b).value - (*(struct Map *)a).value;
} }
int compare_ascending(const void *a, const void *b){ int compare_ascending(const void *a, const void *b){
if((*(struct Map *)b).value == NULL){
return -1;
}else if((*(struct Map *)a).value == NULL){
return 1;
}
return (*(struct Map *)a).value - (*(struct Map *)b).value; return (*(struct Map *)a).value - (*(struct Map *)b).value;
} }