Compare commits

..

No commits in common. "9792126a5358b017b750b642a82ccc70fe39f0f4" and "1d69fa070c2235f4ce32b558c8d56a927e6a551d" have entirely different histories.

5 changed files with 176 additions and 219 deletions

View File

@ -120,85 +120,89 @@ int admin_menu_user_choices(char* name,char* role){
//invetory control //invetory control
void add_item(); void add_item();
void remove_item(struct inventory db,int index); void remove_item();
void update_item(struct inventory db,int index); void update_item();
void view_item();
void admin_list_pages(struct inventory db); void admin_list_pages(struct inventory db);
struct inventory item_control(struct inventory db,int index);
int prompt_inv_control(){
int choice = 0;
do{
system("cls");
printf("Please select an option:\n");
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 inv_control(){ void inv_control(){
int choice = 0;
do{
choice = prompt_inv_control();
switch (choice){
case 1://list
//add a new element with product name new item
//for item control
struct inventory db = read_db_invt();
db.row = (struct inventory_row *)realloc(db.row, sizeof(struct inventory_row) * (db.db.row_count + 1));
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
list_page(db,NULL,db.db.row_count,item_control);
break;
case 2://add item
add_item();
break;
case 3://exit
break;
default://should not happen
printf("Invalid choice\n");
break;
}
}while(choice != 3);
}
void show_item_admin(struct inventory db,int index){
system("cls"); system("cls");
printf("Product: %s\n", db.row[index].product); struct inventory db = read_db_invt();
printf("Catergory: %s\n", db.row[index].category); admin_list_pages(db,NULL,db.row);
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;
} }
struct inventory item_control(struct inventory db,int index){ void item_control(struct inventory db,int index);
int choice = 0; void admin_list_pages(struct inventory db,struct Map* map,int row){//user for showing list result and search result
if(db.row[index].barcode == -1024){//new item int choice = -1;
add_item(); int page = 0;
struct inventory temp = read_db_invt();//reappend new item at back int page_size = PAGE_SIZE;
temp.row = (struct inventory_row *)realloc(temp.row, sizeof(struct inventory_row) * (temp.db.row_count + 1)); int total_pages = ceil((double)row / page_size);
temp.db.row_count += 1; do{
strcpy(temp.row[temp.db.row_count - 1].product,"CREATE NEW ITEM");
temp.row[temp.db.row_count - 1].barcode = -1024;//IMPORTANT: -1024 is reserved for new item //options
return temp; 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 do
{ {
system("cls"); system("cls");
show_item_admin(db,index); show_item(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");
@ -217,74 +221,28 @@ struct inventory item_control(struct inventory db,int index){
break; break;
} }
} while (choice != 0); } while (choice != 0);
struct inventory temp = read_db_invt();
temp.row = (struct inventory_row *)realloc(temp.row, sizeof(struct inventory_row) * (temp.db.row_count + 1));
temp.db.row_count += 1;
strcpy(temp.row[temp.db.row_count - 1].product,"CREATE NEW ITEM");
temp.row[temp.db.row_count - 1].barcode = -1024;//IMPORTANT: -1024 is reserved for new item
return temp;
}
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");
char temp[100];
fflush(stdin);
scanf("%[^\n]",temp);
if(strcmp(temp,"n") == 0){
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");
struct inventory_row *item = (struct inventory_row *)malloc(sizeof(struct inventory_row)); printf("Please input the item name\n>");
temp = prompt_item("Please input the name: \n>"); struct inventory_row *item;
if(temp == NULL){ fflush(stdin);
free(item); scanf("%[^\n]", item->product);
return; printf("Please input the item brand\n>");
} fflush(stdin);
strcpy(item->product,temp); scanf("%[^\n]", item->brand);
temp = prompt_item("Please input the brand: \n>"); printf("Please input the item price\n>");
if(temp == NULL){ scanf("%lf", &item->price);
free(item); printf("Please input the item stock\n>");
return; scanf("%d", &item->stock);
} printf("Please input the item category\n>");
strcpy(item->brand,temp); fflush(stdin);
temp = prompt_item("Please input the category: \n>"); scanf("%[^\n]", item->category);
if(temp == NULL){ printf("Please input the item barcode\n>");
free(item); fflush(stdin);
return; scanf("%[^\n]", item->barcode);
}
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{
@ -296,52 +254,24 @@ void add_item(){
} }
void update_item(struct inventory db,int index){ void update_item(struct inventory db,int index){
char temp[100]; system("cls");
printf("Update item(empty value to not change the value)\n"); printf("Update item\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]", temp);//input until /n scanf("%[^\n]", db.row[index].product);
if(strcmp(temp,"") != 0){//check if temp is not empty printf("Please input the item brand\n>");
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]", temp); scanf("%[^\n]", db.row[index].brand);
if(strcmp(temp,"") != 0){ printf("Please input the item price\n>");
strcpy(db.row[index].brand,temp); scanf("%lf", &db.row[index].price);
} printf("Please input the item stock\n>");
scanf("%d", &db.row[index].stock);
printf("Please input the category(orginal:%s)\n>",db.row[index].category); printf("Please input the item category\n>");
fflush(stdin); fflush(stdin);
scanf("%[^\n]", temp); scanf("%[^\n]", db.row[index].category);
if(strcmp(temp,"") != 0){ printf("Please input the item barcode\n>");
strcpy(db.row[index].category,temp);
}
printf("Please input the price(orginal:$%.1lf)\n>",db.row[index].price);
fflush(stdin); fflush(stdin);
scanf("%[^\n]", temp); scanf("%[^\n]", db.row[index].barcode);
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{
@ -353,6 +283,7 @@ 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;//not really used can be useful bool init_status;
//array of struct of row //array of struct of row
}basic_db; }basic_db;
@ -298,8 +298,6 @@ 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;
} }
@ -453,12 +451,29 @@ bool append_inventory(struct inventory_row* row){
printf("Error in opening file\n"); printf("Error in opening file\n");
return false; return false;
} }
fprintf(fp,"%s\n",row->category); for(INVENTORY i=category;i<=ENDOFINVENTORY;i++){
fprintf(fp,"%s\n",row->brand); switch(i){
fprintf(fp,"%s\n",row->product); case category:
fprintf(fp,"%.1f\n",row->price); fprintf(fp,"%s",row.category);
fprintf(fp,"%d\n",row->stock); break;
fprintf(fp,"%ld\n",row->barcode); 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); fclose(fp);
return true; return true;
} }
@ -469,12 +484,29 @@ bool append_transaction_db(struct transaction_row* row){
printf("Error in opening file\n"); printf("Error in opening file\n");
return false; return false;
} }
fprintf(fp,"%d-%02d-%02d\n",row->date.year,row->date.month,row->date.day); for(TRANSACTION i=date;i<=ENDOFTRANSACTION;i++){
fprintf(fp,"%02d:%02d:%02d\n",row->time.hour,row->time.minute,row->time.second); switch(i){
fprintf(fp,"%ld\n",row->id); case date:
fprintf(fp,"%.1f\n",row->price); fprintf(fp,"%d-%02d-%02d",row.date.year,row.date.month,row.date.day);
fprintf(fp,"%d\n",row->quantity); break;
fprintf(fp,"%ld\n",row->barcode); 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); fclose(fp);
return true; return true;
} }
@ -485,9 +517,20 @@ bool append_user(struct user_row* row){
printf("Error in opening file\n"); printf("Error in opening file\n");
return false; return false;
} }
fprintf(fp,"%s\n",row->name); for(USER i=name;i<=ENDOFUSER;i++){
fprintf(fp,"%s\n",row->role); switch(i){
fprintf(fp,"%ld\n",row->id); 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); fclose(fp);
return true; return true;
} }

View File

@ -60,7 +60,7 @@ void normal_menu(){
//universal functions for normal user //universal functions for normal user
struct Map* sortItems(struct inventory db, int sort); struct Map* sortItems(struct inventory db, int sort);
struct inventory show_item(struct inventory db,int index){ void show_item(struct inventory db,int index){
system("cls"); system("cls");
printf("Product: %s\n", db.row[index].product); printf("Product: %s\n", db.row[index].product);
printf("Catergory: %s\n", db.row[index].category); printf("Catergory: %s\n", db.row[index].category);
@ -71,7 +71,7 @@ struct inventory show_item(struct inventory db,int index){
printf("Press any key to return to the list\n"); printf("Press any key to return to the list\n");
fflush(stdin); fflush(stdin);
getchar(); getchar();
return db; return;
} }
int normal_menu_user_choices(){ int normal_menu_user_choices(){
int choice; int choice;
@ -85,9 +85,7 @@ 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,struct inventory (*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;
@ -140,7 +138,7 @@ void list_page(struct inventory db,struct Map* map,int row,struct inventory (*sh
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){
db = (*showitem)(db,choice - 9 + page_size*page); show_item(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;
@ -174,7 +172,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,show_item); list_page(db,map,db.db.row_count);
} }
//for sorting //for sorting
@ -182,9 +180,6 @@ 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:
@ -264,7 +259,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,show_item);//ofset map, as it is use to store the size list_page(db,map+1,(int)map[0].value);//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");
@ -484,7 +479,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: %.1f\n",row->price); printf("price: %.2f\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);
@ -561,15 +556,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): %.1f\n",temp->row->price);//space to align printf(" price(per qty): %.2f\n",temp->row->price);//space to align
printf(" price(total): %.1f\n",price); printf(" price(total): %.2f\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: $%.1f\n",total_price); printf("Total price: $%.2f\n",total_price);
printf("<------------------------>\n"); printf("<------------------------>\n");
printf("\n%d CHECKOUT\n",i); printf("\n%d CHECKOUT\n",i);
@ -600,7 +595,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: %.1f\n",row->price); printf("price: %.2f\n",row->price);
printf("quantity: %d\n",quantity); printf("quantity: %d\n",quantity);
bool check; bool check;
do{ do{

View File

@ -4,21 +4,9 @@ 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;
} }