307 lines
8.3 KiB
C
307 lines
8.3 KiB
C
//include std lib if they havent been included
|
|
#ifndef STD_LIB_H
|
|
#define STD_LIB_H
|
|
#include<stdio.h>
|
|
#include<stdlib.h>
|
|
#include<string.h>
|
|
#include<stdbool.h>
|
|
#include<math.h>
|
|
#include<time.h>
|
|
#include<ctype.h>
|
|
#endif
|
|
|
|
#ifndef MENU_H
|
|
#define MENU_H
|
|
#include "menu.h"
|
|
#endif //MENU_H
|
|
|
|
//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;
|
|
}
|
|
|
|
//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();
|
|
}
|
|
|