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
#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>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
#include <time.h>
#include <ctype.h>
#endif
#ifndef Utils
@ -16,9 +16,6 @@
#include "utils.h"
#endif // !Utils
//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 "tran_control.h"
#include "user_control.h"
@ -28,9 +25,8 @@ int admin_menu_user_choices(char* name,char* role);
void admin_menu(){
Cls();
welcome_message();
welcome_message(stdout);
//the selection menu
// if(login valid)
struct user_row *user = prompt_user();
if(user == NULL){//invalid user
return;

View File

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

21
main.c
View File

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

View File

@ -16,8 +16,7 @@
#include "utils.h"
#endif // !Utils
//fucntions for menu
void list_items();
void search_item();
void search_item(void * (*showitem)(void *,int));
void self_help_sale_system();
void print_page(void * ddb, int cur, int end,struct Map* map);
struct Map* sortItems(void * ddb, int sort);
@ -26,19 +25,21 @@ struct Map* sortItems(void * ddb, int sort);
void normal_menu(){
int choice = 0;
do{
char * items[] = {
"List items",
char buf[1024];
welcome_message(buf);
sprintf(buf+strlen(buf),"Normal User Menu\n");
choice = choices_selecter((char*[]){
"List items",
"Search item",
"Self help sale system"
};
choice = choices_selecter(items,3,"><Welcome to the Student Union POS system><\nNormal User Menu\n");
},3,buf);
switch (choice)
{
case 1://List items
list_items();
break;
case 2://Search item
search_item();
search_item(show_item);
break;
case 3://self help sale system
self_help_sale_system();
@ -49,30 +50,10 @@ void normal_menu(){
}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
char* prompt_search();
struct Map* searchItems(struct inventory db, char* searchstr);
void search_item(){
void search_item(void * (*showitem)(void *,int)){
//search for item
//prompt user to select an item
@ -83,10 +64,7 @@ void search_item(){
//options
int breakout = 0;
char * items[] = {
"search"
};
breakout = choices_selecter(items,1,"><Welcome to the Student Union POS system><\n");
breakout = choices_selecter((char*[]){"Search"},1,welcome_message(NULL));
if(breakout == 2){
break;
}
@ -101,7 +79,7 @@ void search_item(){
"Brand",
"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
printf("No result found\n");
printf("Press any key to continue\n");
@ -115,7 +93,7 @@ void search_item(){
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);
fflush_stdin();
scanf("%s", searchstr);
@ -156,11 +134,10 @@ void self_help_sale_system(){
//options
int breakout = 0;
char * items[] = {
"scan barcode",
choice = choices_selecter((char*[]){
"scan barcode",
"cart/checkout"
};
choice = choices_selecter(items,2,"><Welcome to the Student Union POS system><\n");
},2,welcome_message(NULL));
switch (choice)
{
case 1:
@ -344,7 +321,7 @@ struct cart* list_cart(struct cart* cart){
int choice = 0;
do{
Cls();
welcome_message();
welcome_message(stdout);
double total_price = 0;
int i=1;
printf("0 exit\n");
@ -394,15 +371,14 @@ struct cart* cart_control(struct cart* cart,int index){
int quantity = Cartqty(cart,index-1);
do{
char welcome[256];
sprintf(welcome,"><Welcome to the Student Union POS system><\n");
sprintf(welcome,"product: %s\n",row->product);
sprintf(welcome,"price: %.1f\n",row->price);
sprintf(welcome,"quantity: %d\n",quantity);
char * items[] = {
"remove",
welcome_message(welcome);
sprintf(welcome+strlen(welcome),"product: %s\n",row->product);
sprintf(welcome+strlen(welcome),"price: %.1f\n",row->price);
sprintf(welcome+strlen(welcome),"quantity: %d\n",quantity);
choice = choices_selecter((char*[]){
"remove",
"edit quantity"
};
choice = choices_selecter(items,2,welcome);
},2,welcome);
if(choice == 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){
Cls();
welcome_message();
welcome_message(stdout);
char choice;
do{
int quantity = 0;
@ -451,7 +427,7 @@ struct cart* update_cart(struct cart* cart,int index){
struct cart* checkout(struct cart* cart){
Cls();
welcome_message();
welcome_message(stdout);
long user_id = 0;
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
void add_role();
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);
void role_control(){
int choice = -1;
struct user db = read_db_user();
do{
char * items[] = {
"add admin role",
char buf[1024];
welcome_message(buf);
sprintf(buf+strlen(buf),"Role control\n");
choice = choices_selecter((char*[]){
"add admin role",
"list role"
};
choice = choices_selecter(items,2,"><Welcome to the Student Union POS system><\nRole control\n");
},2,buf);
switch (choice){
case 3:
break;
@ -55,7 +57,6 @@ void add_role(){
}
return;
}
void print_role(struct linkedlist* list,int cur,int end,struct Map* map){
printf("%-10s%-10s%-10s\n","No.","Role","Admin?");
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;
list = getLinkedList(list,index);
do{
char* name = list->data;
Cls();
printf("%-10s%-10s\n","Role","Admin?");
printf("%-10s%-10s\n",name,is_admin(name)?"T":"F");
printf("1 toogle admin\n");
printf("2 exit\n");
bool valid = true;
do{
valid = true;
printf("Please input your choice\n>");
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;
choice = choices_selecter((char*[]){"Toggle admin"},1,"Role control\n");
if(choice == 1){
if(is_admin(name)){
remove_admin(name);
}else{
add_admin(name);
}
}while(!valid);
}while(choice != 2);
return;
printf("Admin role toggled\n");
printf("Press any key to continue\n");
fflush_stdin();
getchar();
}
return list;
}

View File

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

View File

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

View File

@ -31,8 +31,11 @@ void fflush_stdin(){
#endif
}
void welcome_message(){
printf("><Welcome to the Student Union POS system><\n");//welcome messgae
char * welcome_message(FILE * fp){ //cross compatible
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){
@ -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);
do{
Cls();
welcome_message();
welcome_message(stdout);
printf("%s list\n",lister_name);
printf("0 exit\n");
for(int i=0;i<NoSortItems*2;i+=2){