storage control on going;

inv control near fin;
add new db functions
need clean up
This commit is contained in:
stmctommyau 2022-09-05 12:05:19 +08:00
parent e8fb9616f3
commit 73672b7211
6 changed files with 379 additions and 29 deletions

1
.gitignore vendored
View File

@ -3,4 +3,3 @@ main.exe
.vscode/*
.vscode/settings.json
data_file_transaction.txt

View File

@ -2,4 +2,5 @@
"files.associations": {
"stdio.h": "c"
}
"git.enableCommitSigning": false
}

View File

@ -15,30 +15,292 @@
#include "menu.h"
#endif //MENU_H
// void admin_menu(){
// system("cls");
// 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;
// }
//list of functions
void inv_control();
void tran_control();
void user_control();
void admin_control();
struct user_row *prompt_user();
int admin_menu_user_choices(char* name,char* role);
void admin_menu(){
system("cls");
welcome_message();
//the selection menu
// if(login valid)
struct user_row *user = prompt_user();
if(user == NULL){//invalid user
return;
}
int choice = 0;
do
{
choice = admin_menu_user_choices(user->name,user->role);
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://action with admin user register
// admin_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;
}
}while(choice != 5);
// return true;
// }
return;
}
//func for main for admin
struct user_row *prompt_user(){
long user_id = 0;
printf("Please tap your card(student card, staff card,etc)(or input the id)(0 to cancel)\n>");
scanf("%ld", &user_id);
if(user_id == 0){
printf("cancelled\n");
printf("press any key to continue\n");
fflush(stdin);
getchar();
return NULL;
}
struct user_row *user = get_user(user_id);
if(user == NULL){
printf("User not found\n");
printf("press any key to continue\n");
fflush(stdin);
getchar();
return NULL;
}else if(!is_admin(user->role)){
printf("You aren't an admin\n");
printf("press any key to continue\n");
fflush(stdin);
getchar();
return NULL;
}else{
printf("Welcome %s(%s)\n", user->name,user->role);
printf("press any key to continue\n");
fflush(stdin);
getchar();
}
return user;
}
int admin_menu_user_choices(char* name,char* role){
int choice = 0;
do{
system("cls");
printf("Welcome %s(%s)\n", name,role);
printf("Please select an option:\n");
printf("1. Inventory control\n");
printf("2. Transaction control\n");
printf("3. User control\n");
printf("4. Admin user register\n");
printf("5. Exit and Logout\n");
printf(">");
scanf("%d", &choice);
if(choice < 1 || choice > 5){
printf("Invalid choice...press any key to retry....\n");
fflush(stdin);
getchar();
}
}while(choice < 1 || choice > 5);
return choice;
}
//invetory control
void add_item();
void remove_item();
void update_item();
void view_item();
void admin_list_pages(struct inventory db);
void inv_control(){
system("cls");
struct inventory db = read_db_invt();
admin_list_pages(db,NULL,db.row);
}
void item_control(struct inventory db,int index);
void admin_list_pages(struct inventory db,struct Map* map,int row){//user for showing list result and search result
int choice = -1;
int page = 0;
int page_size = PAGE_SIZE;
int total_pages = ceil((double)row / page_size);
do{
//options
system("cls");
printf("0 exit\n");
printf("1 sort name decending\n");
printf("2 sort name ascending\n");
printf("3 sort price decending\n");
printf("4 sort price ascending\n");
printf("5 sort brand decending\n");
printf("6 sort brand ascending\n");
printf("7 sort category decending\n");
printf("8 sort category ascending\n");
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();
}else if(choice == page_size+4 && page + 1 < total_pages){
page++;
}else if(choice == page_size+5 && page > 0){
page--;
}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");
valid = false;
}
}while(!valid);
}while(choice != 0);
}
void item_control(struct inventory db,int index){
int choice = 0;
do
{
system("cls");
show_item(db,index);
printf("0 exit\n");
printf("1 update item\n");
printf("2 remove item\n");
printf("Enter your choice: ");
fflush(stdin);
scanf("%d", &choice);
switch (choice)
{
case 1:
update_item(db,index);
break;
case 2:
remove_item(db,index);
break;
default:
break;
}
} while (choice != 0);
}
void add_item(){
system("cls");
printf("Add new item\n");
printf("Please input the item name\n>");
struct inventory_row *item;
fflush(stdin);
scanf("%[^\n]", item->product);
printf("Please input the item brand\n>");
fflush(stdin);
scanf("%[^\n]", item->brand);
printf("Please input the item price\n>");
scanf("%lf", &item->price);
printf("Please input the item stock\n>");
scanf("%d", &item->stock);
printf("Please input the item category\n>");
fflush(stdin);
scanf("%[^\n]", item->category);
printf("Please input the item barcode\n>");
fflush(stdin);
scanf("%[^\n]", item->barcode);
if(item == NULL || !append_inventory(item)){
printf("Failed to add item\n");
}else{
printf("Item added\n");
}
printf("press any key to continue\n");
fflush(stdin);
getchar();
}
void update_item(struct inventory db,int index){
system("cls");
printf("Update item\n");
printf("Please input the item name\n>");
fflush(stdin);
scanf("%[^\n]", db.row[index].product);
printf("Please input the item brand\n>");
fflush(stdin);
scanf("%[^\n]", db.row[index].brand);
printf("Please input the item price\n>");
scanf("%lf", &db.row[index].price);
printf("Please input the item stock\n>");
scanf("%d", &db.row[index].stock);
printf("Please input the item category\n>");
fflush(stdin);
scanf("%[^\n]", db.row[index].category);
printf("Please input the item barcode\n>");
fflush(stdin);
scanf("%[^\n]", db.row[index].barcode);
if(!update_db_invt(db)){
printf("Failed to update item\n");
}else{
printf("Item updated\n");
}
printf("press any key to continue\n");
fflush(stdin);
getchar();
}
void remove_item(struct inventory db,int index){
system("cls");
printf("Remove item\n");
printf("Are you sure you want to remove this item? (y/n)\n");
char choice;
fflush(stdin);
scanf("%c", &choice);
db.row[index].isdeleted = true;
if(choice == 'y'){
if(!update_db_invt(db)){
printf("Failed to remove item\n");
}else{
printf("Item removed\n");
}
}else{
printf("Item not removed\n");
}
printf("press any key to continue\n");
fflush(stdin);
getchar();
}

View File

@ -69,7 +69,6 @@ typedef struct user_row{
char name[100];
char role[100];
long id;
bool isAdmin;
bool isdeleted;//common for all rows,default is false
}user_row;
@ -446,6 +445,96 @@ bool update_db_user(struct user user){
return true;
}
bool append_inventory(struct inventory_row* row){
FILE* fp = fopen(INVENTORY_DB,"a");
if(fp == NULL){
printf("Error in opening file\n");
return false;
}
for(INVENTORY i=category;i<=ENDOFINVENTORY;i++){
switch(i){
case category:
fprintf(fp,"%s",row.category);
break;
case brand:
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);
return true;
}
bool append_transaction_db(struct transaction_row* row){
FILE* fp = fopen(TRANSACTION_DB,"a");
if(fp == NULL){
printf("Error in opening file\n");
return false;
}
for(TRANSACTION i=date;i<=ENDOFTRANSACTION;i++){
switch(i){
case date:
fprintf(fp,"%d-%02d-%02d",row.date.year,row.date.month,row.date.day);
break;
case TIME:
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);
return true;
}
bool append_user(struct user_row* row){
FILE* fp = fopen(USER_DB,"a");
if(fp == NULL){
printf("Error in opening file\n");
return false;
}
for(USER i=name;i<=ENDOFUSER;i++){
switch(i){
case name:
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);
return true;
}
//checkout db support,
typedef struct cart{//linked list

2
main.c
View File

@ -37,7 +37,7 @@ int main(){
switch (choice)
{
case 1://admin
//admin_menu();//refers to admin_user.h
admin_menu();//refers to admin_user.h
check = true;
break;
case 2://normal user

View File

@ -651,7 +651,6 @@ struct cart* update_cart(struct cart* cart,int index){
//Checkout
//TODO checkout
struct cart* checkout(struct cart* cart){
system("cls");