cleanup, fix code style, some bug fixes

This commit is contained in:
stmctommyau 2023-09-16 17:36:36 +08:00
parent d2bc1575e6
commit 6a95897df7
No known key found for this signature in database
GPG Key ID: 87B1991E1277F054
8 changed files with 122 additions and 160 deletions

View File

@ -1,13 +1,13 @@
//include std lib if they havent been included //include std lib if they havent been included
#ifndef STD_LIB_H #ifndef STD_LIB_H
#define STD_LIB_H #define STD_LIB_H
#include<stdio.h> #include <stdio.h>
#include<stdlib.h> #include <stdlib.h>
#include<string.h> #include <string.h>
#include<stdbool.h> #include <stdbool.h>
#include<math.h> #include <math.h>
#include<time.h> #include <time.h>
#include<ctype.h> #include <ctype.h>
#endif #endif
#ifndef Utils #ifndef Utils
@ -16,9 +16,6 @@
#include "utils.h" #include "utils.h"
#endif // !Utils #endif // !Utils
//list of functions //list of functions
//TODO: split all controller functions into their own files
//TODO: reformat all output to be formatted and unifed
//TODO: create a list base function to unified all variants of list functions
#include "inv_control.h" #include "inv_control.h"
#include "tran_control.h" #include "tran_control.h"
#include "user_control.h" #include "user_control.h"
@ -28,9 +25,8 @@ int admin_menu_user_choices(char* name,char* role);
void admin_menu(){ void admin_menu(){
Cls(); Cls();
welcome_message(); welcome_message(stdout);
//the selection menu //the selection menu
// if(login valid)
struct user_row *user = prompt_user(); struct user_row *user = prompt_user();
if(user == NULL){//invalid user if(user == NULL){//invalid user
return; return;

View File

@ -11,11 +11,14 @@ struct Map* sortItems(void * ddb, int sort);
void inv_control(){ void inv_control(){
int choice = 0; int choice = 0;
do{ do{
char * items[] = { char buf[1024];
"List(allow all operation include add)", welcome_message(buf);
"Add item(shortcut to add item)" sprintf(buf+strlen(buf),"Inventory control\n");
}; choice = choices_selecter((char*[]){
choice = choices_selecter(items,2,"><Welcome to the Student Union POS system><\nInventory control\n"); "List(allow all operation include add)",
"Add item(shortcut to add item)",
"Search item"
},3,buf);
switch (choice){ switch (choice){
case 1:;//list case 1:;//list
//add a new element with product name new item //add a new element with product name new item
@ -36,13 +39,16 @@ void inv_control(){
case 2://add item case 2://add item
add_item(); add_item();
break; break;
case 3://exit case 3:
search_item(item_control);
break;
case 4://exit
break; break;
default://should not happen default://should not happen
printf("Invalid choice\n"); printf("Invalid choice\n");
break; break;
} }
}while(choice != 3); }while(choice != 4);
} }
char * show_item_admin(struct inventory db,int index){ char * show_item_admin(struct inventory db,int index){
@ -106,11 +112,10 @@ void * item_control(void * ddb,int index){
strcat(welcome,"Operations\n"); strcat(welcome,"Operations\n");
do do
{ {
char * items[] = { choice = choices_selecter((char*[]){
"update item", "update item",
"remove item" "remove item"
}; },2,welcome);
choice = choices_selecter(items,2,welcome);
switch (choice) switch (choice)
{ {
case 1: case 1:

21
main.c
View File

@ -1,13 +1,13 @@
//include std lib if they havent been included //include std lib if they havent been included
#ifndef STD_LIB_H #ifndef STD_LIB_H
#define STD_LIB_H #define STD_LIB_H
#include<stdio.h> #include <stdio.h>
#include<stdlib.h> #include <stdlib.h>
#include<string.h> #include <string.h>
#include<stdbool.h> #include <stdbool.h>
#include<math.h> #include <math.h>
#include<time.h> #include <time.h>
#include<ctype.h> #include <ctype.h>
#include <stddef.h> #include <stddef.h>
#endif #endif
#ifndef CUSTOM_H #ifndef CUSTOM_H
@ -32,11 +32,10 @@ int main(){
//print welcome message //print welcome message
//prompt weather user is admin or normal user //prompt weather user is admin or normal user
//then redirect them to the corisponding menu //then redirect them to the corisponding menu
char * Items[] = { int choice = choices_selecter((char*[]){
"Admin", "Admin",
"Normal User" "Normal User"
}; },2,welcome_message(NULL));
int choice = choices_selecter(Items,2,"><Welcome to the Student Union POS system><\n");
switch (choice) switch (choice)
{ {
case 1://admin case 1://admin

View File

@ -16,8 +16,7 @@
#include "utils.h" #include "utils.h"
#endif // !Utils #endif // !Utils
//fucntions for menu //fucntions for menu
void list_items(); void search_item(void * (*showitem)(void *,int));
void search_item();
void self_help_sale_system(); void self_help_sale_system();
void print_page(void * ddb, int cur, int end,struct Map* map); void print_page(void * ddb, int cur, int end,struct Map* map);
struct Map* sortItems(void * ddb, int sort); struct Map* sortItems(void * ddb, int sort);
@ -26,19 +25,21 @@ struct Map* sortItems(void * ddb, int sort);
void normal_menu(){ void normal_menu(){
int choice = 0; int choice = 0;
do{ do{
char * items[] = { char buf[1024];
"List items", welcome_message(buf);
sprintf(buf+strlen(buf),"Normal User Menu\n");
choice = choices_selecter((char*[]){
"List items",
"Search item", "Search item",
"Self help sale system" "Self help sale system"
}; },3,buf);
choice = choices_selecter(items,3,"><Welcome to the Student Union POS system><\nNormal User Menu\n");
switch (choice) switch (choice)
{ {
case 1://List items case 1://List items
list_items(); list_items();
break; break;
case 2://Search item case 2://Search item
search_item(); search_item(show_item);
break; break;
case 3://self help sale system case 3://self help sale system
self_help_sale_system(); self_help_sale_system();
@ -49,30 +50,10 @@ void normal_menu(){
}while(choice != 4); }while(choice != 4);
} }
//list items
void list_items(){
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;
char * SortItems[] = {
"Name",
"Price",
"Brand",
"Category",
};
lister(&db,map,db.db.row_count,"List of items",SortItems,4,print_page,sortItems,show_item);
}
//search items //search items
char* prompt_search(); char* prompt_search();
struct Map* searchItems(struct inventory db, char* searchstr); struct Map* searchItems(struct inventory db, char* searchstr);
void search_item(){ void search_item(void * (*showitem)(void *,int)){
//search for item //search for item
//prompt user to select an item //prompt user to select an item
@ -83,10 +64,7 @@ void search_item(){
//options //options
int breakout = 0; int breakout = 0;
char * items[] = { breakout = choices_selecter((char*[]){"Search"},1,welcome_message(NULL));
"search"
};
breakout = choices_selecter(items,1,"><Welcome to the Student Union POS system><\n");
if(breakout == 2){ if(breakout == 2){
break; break;
} }
@ -101,7 +79,7 @@ void search_item(){
"Brand", "Brand",
"Category", "Category",
}; };
lister(&db,map+1,temp_row,"List of items",SortItems,4,print_page,sortItems,show_item);//ofset map, as it is use to store the size lister(&db,map+1,temp_row,"List of items",SortItems,4,print_page,sortItems,showitem);//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");
@ -115,7 +93,7 @@ void search_item(){
char* prompt_search(){ char* prompt_search(){
printf("Enter search string:(largest length 100)(case insesitive)\n"); printf("Enter search string:(largest length 100)(case insesitive)\n>");
char* searchstr = malloc(sizeof(char) * 100); char* searchstr = malloc(sizeof(char) * 100);
fflush_stdin(); fflush_stdin();
scanf("%s", searchstr); scanf("%s", searchstr);
@ -156,11 +134,10 @@ void self_help_sale_system(){
//options //options
int breakout = 0; int breakout = 0;
char * items[] = { choice = choices_selecter((char*[]){
"scan barcode", "scan barcode",
"cart/checkout" "cart/checkout"
}; },2,welcome_message(NULL));
choice = choices_selecter(items,2,"><Welcome to the Student Union POS system><\n");
switch (choice) switch (choice)
{ {
case 1: case 1:
@ -344,7 +321,7 @@ struct cart* list_cart(struct cart* cart){
int choice = 0; int choice = 0;
do{ do{
Cls(); Cls();
welcome_message(); welcome_message(stdout);
double total_price = 0; double total_price = 0;
int i=1; int i=1;
printf("0 exit\n"); printf("0 exit\n");
@ -394,15 +371,14 @@ struct cart* cart_control(struct cart* cart,int index){
int quantity = Cartqty(cart,index-1); int quantity = Cartqty(cart,index-1);
do{ do{
char welcome[256]; char welcome[256];
sprintf(welcome,"><Welcome to the Student Union POS system><\n"); welcome_message(welcome);
sprintf(welcome,"product: %s\n",row->product); sprintf(welcome+strlen(welcome),"product: %s\n",row->product);
sprintf(welcome,"price: %.1f\n",row->price); sprintf(welcome+strlen(welcome),"price: %.1f\n",row->price);
sprintf(welcome,"quantity: %d\n",quantity); sprintf(welcome+strlen(welcome),"quantity: %d\n",quantity);
char * items[] = { choice = choices_selecter((char*[]){
"remove", "remove",
"edit quantity" "edit quantity"
}; },2,welcome);
choice = choices_selecter(items,2,welcome);
if(choice == 1){ if(choice == 1){
cart = Cartremove(cart,index-1); cart = Cartremove(cart,index-1);
@ -423,7 +399,7 @@ struct cart* cart_control(struct cart* cart,int index){
struct cart* update_cart(struct cart* cart,int index){ struct cart* update_cart(struct cart* cart,int index){
Cls(); Cls();
welcome_message(); welcome_message(stdout);
char choice; char choice;
do{ do{
int quantity = 0; int quantity = 0;
@ -451,7 +427,7 @@ struct cart* update_cart(struct cart* cart,int index){
struct cart* checkout(struct cart* cart){ struct cart* checkout(struct cart* cart){
Cls(); Cls();
welcome_message(); welcome_message(stdout);
long user_id = 0; long user_id = 0;
printf("Please tap your card(student card, staff card,etc)(or input the id)(0 to cancel)\n>"); printf("Please tap your card(student card, staff card,etc)(or input the id)(0 to cancel)\n>");

View File

@ -2,18 +2,20 @@
//role control //role control
void add_role(); void add_role();
void print_role(struct linkedlist* list,int cur,int end,struct Map* map); void print_role(struct linkedlist* list,int cur,int end,struct Map* map);
void showRole(struct linkedlist* list,int index); void * showRole(struct linkedlist* list,int index);
struct Map* sortRole(struct linkedlist* list,int choice); struct Map* sortRole(struct linkedlist* list,int choice);
void role_control(){ void role_control(){
int choice = -1; int choice = -1;
struct user db = read_db_user(); struct user db = read_db_user();
do{ do{
char * items[] = { char buf[1024];
"add admin role", welcome_message(buf);
sprintf(buf+strlen(buf),"Role control\n");
choice = choices_selecter((char*[]){
"add admin role",
"list role" "list role"
}; },2,buf);
choice = choices_selecter(items,2,"><Welcome to the Student Union POS system><\nRole control\n");
switch (choice){ switch (choice){
case 3: case 3:
break; break;
@ -55,7 +57,6 @@ void add_role(){
} }
return; return;
} }
void print_role(struct linkedlist* list,int cur,int end,struct Map* map){ void print_role(struct linkedlist* list,int cur,int end,struct Map* map){
printf("%-10s%-10s%-10s\n","No.","Role","Admin?"); printf("%-10s%-10s%-10s\n","No.","Role","Admin?");
if(map == NULL){ if(map == NULL){
@ -73,38 +74,26 @@ void print_role(struct linkedlist* list,int cur,int end,struct Map* map){
} }
} }
void showRole(struct linkedlist* list,int index){ void * showRole(struct linkedlist* list,int index){
int choice; int choice;
list = getLinkedList(list,index); list = getLinkedList(list,index);
do{
char* name = list->data; char* name = list->data;
Cls();
printf("%-10s%-10s\n","Role","Admin?"); printf("%-10s%-10s\n","Role","Admin?");
printf("%-10s%-10s\n",name,is_admin(name)?"T":"F"); printf("%-10s%-10s\n",name,is_admin(name)?"T":"F");
printf("1 toogle admin\n"); choice = choices_selecter((char*[]){"Toggle admin"},1,"Role control\n");
printf("2 exit\n"); if(choice == 1){
bool valid = true; if(is_admin(name)){
do{ remove_admin(name);
valid = true; }else{
printf("Please input your choice\n>"); add_admin(name);
fflush_stdin();
scanf("%d", &choice);
if(choice == 1){
if(is_admin(name)){
remove_admin(name);
}else{
add_admin(name);
}
printf("Admin role toggled\n");
printf("Press any key to continue\n");
fflush_stdin();
getchar();
}else if(choice != 2){
printf("Invalid choice\n");
valid = false;
} }
}while(!valid); printf("Admin role toggled\n");
}while(choice != 2); printf("Press any key to continue\n");
return; fflush_stdin();
getchar();
}
return list;
} }

View File

@ -8,11 +8,13 @@ void tran_control(){
struct transaction tran = read_db_tran(); struct transaction tran = read_db_tran();
int choice; int choice;
do{ do{
char * items[] = { char buf[1024];
"List transaction", welcome_message(buf);
"new transaction" sprintf(buf+strlen(buf),"Transaction control\n");
}; choice = choices_selecter((char*[]){
choice = choices_selecter(items,2,"><Welcome to the Student Union POS system><\nTransaction control\n"); "List transaction",
"new transaction"
},2,buf);
switch(choice){ switch(choice){
case 1: case 1:
char * SortItems[] = { char * SortItems[] = {
@ -66,41 +68,33 @@ void * showTran(void * ddb,int index){
struct transaction db = *((struct transaction*)ddb); struct transaction db = *((struct transaction*)ddb);
int choice; int choice;
do{ do{
printf("Transaction detail\n"); char buff[1024];
double total = db.row[index].price * db.row[index].quantity; sprintf(buff,"Transaction detail\n");
//reconstuct date and time and print all transaction info out double total = db.row[index].price * db.row[index].quantity;
char date[11]; //reconstuct date and time and print all transaction info out
char time[9]; char date[11];
sprintf(date,"%02d-%02d-%04d",db.row[index].date.day,db.row[index].date.month,db.row[index].date.year); char time[9];
sprintf(time,"%02d:%02d:%02d",db.row[index].time.hour,db.row[index].time.minute,db.row[index].time.second); sprintf(date,"%02d-%02d-%04d",db.row[index].date.day,db.row[index].date.month,db.row[index].date.year);
printf("%-15s%-10s%-10s%-10s%-10s%-10s%-10s\n","Date","Time","Barcode","ID","Price","Quantity","Total"); sprintf(time,"%02d:%02d:%02d",db.row[index].time.hour,db.row[index].time.minute,db.row[index].time.second);
printf("%-15s%-10s%-10d%-10d%-10.2lf%-10d%-10.2lf\n",date,time,db.row[index].barcode,db.row[index].id,db.row[index].price,db.row[index].quantity,total); sprintf(buff+strlen(buff),"%-15s%-10s%-10s%-10s%-10s%-10s%-10s\n","Date","Time","Barcode","ID","Price","Quantity","Total");
printf("1 edit transaction\n"); sprintf(buff+strlen(buff),"%-15s%-10s%-10d%-10d%-10.2lf%-10d%-10.2lf\n",date,time,db.row[index].barcode,db.row[index].id,db.row[index].price,db.row[index].quantity,total);
printf("2 delete transaction\n"); choice = choices_selecter((char*[]){
printf("3 exit\n"); "Edit transaction",
bool valid = true; "Delete transaction",
do{ },2,buff);
printf("Please input your choice: ");
fflush_stdin();
scanf("%d",&choice);
switch(choice){ switch(choice){
case 1: case 1:
valid = true;
db = update_tran(db,index); db = update_tran(db,index);
break; break;
case 2: case 2:
valid = true;
db = remove_tran(db,index); db = remove_tran(db,index);
break; break;
case 3: case 3:
valid = true;
break; break;
default: default:
printf("Invalid input, try again\n"); printf("Invalid Choice\n");
valid = false;
break; break;
} }
}while(!valid);
}while(choice != 3); }while(choice != 3);
void * re = malloc(sizeof(db)); void * re = malloc(sizeof(db));
memcpy(re,&db,sizeof(db)); memcpy(re,&db,sizeof(db));

View File

@ -10,10 +10,13 @@ void user_control(){
int choice = 0; int choice = 0;
while(choice != 3){ while(choice != 3){
Cls(); Cls();
char buf[1024];
welcome_message(buf);
sprintf(buf+strlen(buf),"User control\n");
choice = choices_selecter((char*[]){ choice = choices_selecter((char*[]){
"Add user", "Add user",
"List user", "List user",
},3,"><Welcome to the Student Union POS system><\nUser control\n"); },2,buf);
switch(choice){ switch(choice){
case 1: case 1:
db = add_user(db); db = add_user(db);
@ -44,19 +47,16 @@ void * showUser(void * ddb,int index){
int choice = -1; int choice = -1;
do{ do{
Cls(); Cls();
welcome_message(); char buff[1024];
printf("User detail\n"); welcome_message(buff);
printf("%-20s%-10s%-10s\n","Username","ID","Role"); sprintf(buff+strlen(buff),"User detail\n");
printf("%-20s%-10ld%-10s\n",db.row[index].name,db.row[index].id,db.row[index].role); sprintf(buff+strlen(buff),"%-20s%-10s%-10s\n","Username","ID","Role");
printf("0 exit\n"); sprintf(buff+strlen(buff),"%-20s%-10ld%-10s\n",db.row[index].name,db.row[index].id,db.row[index].role);
printf("1 edit user\n"); choice = choices_selecter((char*[]){
printf("2 delete user\n"); "Edit user",
printf("Enter choice: "); "Delete user",
fflush_stdin(); },2,buff);
scanf("%d", &choice);
switch (choice){ switch (choice){
case 0:
break;
case 1: case 1:
db = edit_user(db,index); db = edit_user(db,index);
break; break;
@ -70,7 +70,7 @@ void * showUser(void * ddb,int index){
getchar(); getchar();
break; break;
} }
}while(choice != 0&&choice!=2); }while(choice!=3);
void * re = malloc(sizeof(db)); void * re = malloc(sizeof(db));
memcpy(re,&db,sizeof(db)); memcpy(re,&db,sizeof(db));
return re; return re;

View File

@ -31,8 +31,11 @@ void fflush_stdin(){
#endif #endif
} }
void welcome_message(){ char * welcome_message(FILE * fp){ //cross compatible
printf("><Welcome to the Student Union POS system><\n");//welcome messgae if(!fp){//if fp is NULL
return "><Welcome to the Student Union POS system><\n";
}
fprintf(fp,"><Welcome to the Student Union POS system><\n");//welcome messgae
} }
int choices_selecter(char * Items[],int items,char * welcome_messages){ int choices_selecter(char * Items[],int items,char * welcome_messages){
@ -117,7 +120,7 @@ void lister(void * db,struct Map* map, int row,char * lister_name,char * SortIte
int total_pages = ceil((double)row / page_size); int total_pages = ceil((double)row / page_size);
do{ do{
Cls(); Cls();
welcome_message(); welcome_message(stdout);
printf("%s list\n",lister_name); printf("%s list\n",lister_name);
printf("0 exit\n"); printf("0 exit\n");
for(int i=0;i<NoSortItems*2;i+=2){ for(int i=0;i<NoSortItems*2;i+=2){