289 lines
7.4 KiB
C
289 lines
7.4 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>
|
|
#endif
|
|
|
|
#ifndef MENU_H
|
|
#define MENU_H
|
|
#include "menu.h"
|
|
#endif //MENU_H
|
|
|
|
#ifndef SORT_H
|
|
#define SORT_H
|
|
#include "sorting.h"
|
|
#endif // !SORT_H
|
|
#define PAGE_SIZE 20
|
|
|
|
//fucntions for menu
|
|
void list_items();
|
|
void search_item();
|
|
void print_page(struct inventory db, int cur, int end,struct Map* map);
|
|
int normal_menu_user_choices();
|
|
|
|
//main of normal user
|
|
bool normal_menu(){
|
|
system("cls");
|
|
welcome_message();
|
|
//the selection menu
|
|
int choice = normal_menu_user_choices();
|
|
|
|
switch (choice)
|
|
{
|
|
case 1://List items
|
|
list_items();
|
|
break;
|
|
case 2://Search item
|
|
search_item();
|
|
break;
|
|
case 3://scan barcode
|
|
// scan_barcode();
|
|
break;
|
|
case 4://Exit
|
|
return false;
|
|
break;
|
|
default://invalid input
|
|
printf("Invalid choice\n");
|
|
return false;
|
|
break;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
//universal functions for normal user
|
|
struct Map* sortItems(struct inventory db, int sort);
|
|
void show_item(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: %d\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);
|
|
printf("Press any key to return to the list\n");
|
|
fflush(stdin);
|
|
getchar();
|
|
return;
|
|
}
|
|
int normal_menu_user_choices(){
|
|
int choice;
|
|
printf("You can buy items inside the 2 option below\n");
|
|
printf("1. List items\n");
|
|
printf("2. Search item\n");
|
|
printf("3. Scan Barcodes\n");
|
|
printf("4. Exit\n");
|
|
printf("Enter your choice: ");
|
|
scanf("%d", &choice);
|
|
return choice;
|
|
}
|
|
void list_page(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 band decending\n");
|
|
printf("6 sort band 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);
|
|
}
|
|
|
|
//page control
|
|
printf("%d next page\n", page_size+3);
|
|
printf("%d previous page\n", page_size+4);
|
|
printf("%d set page size\n", page_size+5);
|
|
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{
|
|
printf("Enter your choice: ");
|
|
scanf("%d", &choice);
|
|
if(choice <=8 && choice > 0){
|
|
printf("sorting...\n");
|
|
map = sortItems(db,choice);
|
|
}else if(choice == page_size+3){
|
|
page++;
|
|
}else if(choice == page_size+4 && page > 0){
|
|
page--;
|
|
}else if(choice == page_size+5){
|
|
printf("Enter page size: ");
|
|
scanf("%d", &page_size);
|
|
total_pages = ceil(row / page_size);
|
|
}else if(choice >= 9 && choice < row+9){
|
|
show_item(db,choice - 9 + page_size*page);
|
|
}else if(choice != 0){
|
|
printf("Invalid choice\n");
|
|
valid = false;
|
|
}
|
|
}while(!valid);
|
|
|
|
}while(choice != 0);
|
|
}
|
|
//print the list for the page
|
|
void print_page(struct inventory db, int cur, int end,struct Map* map){
|
|
for (int i = cur; i < end; i++)
|
|
{
|
|
if(map != NULL){
|
|
printf("%d item: %s\n", i + 9, db.row[map[i].key].product);
|
|
}
|
|
else{
|
|
printf("%d item: %s\n", i + 9, db.row[i].product);
|
|
}
|
|
}
|
|
}
|
|
//list items
|
|
|
|
|
|
void list_items(){
|
|
system("cls");
|
|
welcome_message();
|
|
//get the list of items from the database
|
|
//print the list of items
|
|
//prompt user to select an item
|
|
//if user selects an item, display the item's details
|
|
|
|
struct inventory db = read_db_invt();
|
|
struct Map* map = NULL;
|
|
list_page(db,map,db.db.row_count);
|
|
}
|
|
|
|
//for sorting
|
|
struct Map* sortItems(struct inventory db, int sort){
|
|
struct Map *map = malloc(sizeof(struct Map) * db.db.row_count);
|
|
for (int i = 0; i < db.db.row_count; i++){
|
|
map[i].key = i;
|
|
switch(sort){
|
|
case 1:
|
|
case 2:
|
|
map[i].value = (int)db.row[i].product;
|
|
break;
|
|
|
|
case 3:
|
|
case 4:
|
|
map[i].value = (int)db.row[i].price*100;//presume there is no price contain 0.001
|
|
break;
|
|
case 5:
|
|
case 6:
|
|
map[i].value = (int)db.row[i].brand;
|
|
break;
|
|
case 7:
|
|
case 8:
|
|
map[i].value = (int)db.row[i].category;
|
|
break;
|
|
}
|
|
}
|
|
switch (sort){
|
|
case 1:
|
|
case 3:
|
|
case 5:
|
|
case 7:
|
|
qsort(map, db.db.row_count, sizeof(struct Map), compare_decending);
|
|
break;
|
|
case 2:
|
|
case 4:
|
|
case 6:
|
|
case 8:
|
|
qsort(map, db.db.row_count, sizeof(struct Map), compare_ascending);
|
|
break;
|
|
}
|
|
return map;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//search items
|
|
char* prompt_search();
|
|
char* searchItems(struct inventory db, char* searchstr);
|
|
void search_item(){
|
|
system("cls");
|
|
welcome_message();
|
|
//search for item
|
|
//prompt user to select an item
|
|
//if user selects an item, display the item's details
|
|
struct inventory db = read_db_invt();
|
|
int total_pages = 0;
|
|
struct Map* map = NULL;
|
|
do{
|
|
|
|
//options
|
|
int breakout = 0;
|
|
bool valid = false;
|
|
do{
|
|
system("cls");
|
|
welcome_message();
|
|
printf("0 exit\n");
|
|
printf("1 search\n");
|
|
|
|
scanf("%d", &breakout);
|
|
if(breakout == 1 || breakout == 0){
|
|
valid = true;
|
|
}else{
|
|
printf("Invalid choice\n");
|
|
}
|
|
}while(!valid);
|
|
if(breakout == 0){
|
|
break;
|
|
}
|
|
char* searchstr = prompt_search();
|
|
printf("searching...\n");
|
|
map = searchItems(db,searchstr);
|
|
if(map[0].value == 0){
|
|
list_page(db,map+1,map[0].value);//ofset map, as it is use to store the size
|
|
}else{//empty search
|
|
printf("No result found\n");
|
|
printf("Press any key to continue\n");
|
|
fflush(stdin);
|
|
getchar();
|
|
|
|
}
|
|
}while(true);//break on top
|
|
|
|
}
|
|
|
|
|
|
char* prompt_search(){
|
|
printf("Enter search string:(largest length 100) ");
|
|
char* searchstr = malloc(sizeof(char) * 100);
|
|
scanf("%s", searchstr);
|
|
return searchstr;
|
|
}
|
|
|
|
char* searchItems(struct inventory db, char* searchstr){
|
|
struct Map* map = malloc(sizeof(struct Map) * (db.db.row_count+1));
|
|
int k = 1;
|
|
for (int i = 0; i < db.db.row_count; i++){
|
|
map[k].key = i;
|
|
if(strstr(db.row[i].product,searchstr) != NULL){
|
|
map[k].value = db.row[i].product;
|
|
k++;
|
|
}
|
|
}
|
|
map[0].value = k-1;
|
|
map[0].key = -1;
|
|
return map;
|
|
}
|
|
|