fix user and buffer \n bugs,

fflush multi OS compat
This commit is contained in:
stmctommyau 2023-09-07 23:37:19 +08:00
parent 11249fc563
commit 70e46087c2
No known key found for this signature in database
GPG Key ID: 87B1991E1277F054
5 changed files with 200 additions and 229 deletions

View File

@ -43,7 +43,7 @@ void admin_menu(){
char welcome[256]; char welcome[256];
bool exit = false; bool exit = false;
do{ do{
sprintf(welcome,"welcome %s(%s)",user->name,user->role); sprintf(welcome,"welcome %s(%s)\n",user->name,user->role);
switch (choices_selecter(Items,4,welcome)) switch (choices_selecter(Items,4,welcome))
{ {
@ -79,27 +79,28 @@ struct user_row *prompt_user(){
if(user_id == 0){ if(user_id == 0){
printf("cancelled\n"); printf("cancelled\n");
printf("press any key to continue\n"); printf("press any key to continue\n");
fflush(stdin); fflush_stdin();
getchar(); getchar();
return NULL; return NULL;
} }
struct user_row *user = get_user(user_id); struct user_row *user = get_user(user_id);
Cls();
if(user == NULL){ if(user == NULL){
printf("User not found\n"); printf("User not found\n");
printf("press any key to continue\n"); printf("press any key to continue\n");
fflush(stdin); fflush_stdin();
getchar(); getchar();
return NULL; return NULL;
}else if(!is_admin(user->role)){ }else if(!is_admin(user->role)){
printf("You aren't an admin\n"); printf("You aren't an admin\n");
printf("press any key to continue\n"); printf("press any key to continue\n");
fflush(stdin); fflush_stdin();
getchar(); getchar();
return NULL; return NULL;
}else{ }else{
printf("Welcome %s(%s)\n", user->name,user->role); printf("Welcome %s %s\n", user->name , user->role);
printf("press any key to continue\n"); printf("press any key to continue\n");
fflush(stdin); fflush_stdin();
getchar(); getchar();
} }
return user; return user;
@ -131,14 +132,13 @@ void inv_control(){
db.db.row_count += 1; db.db.row_count += 1;
strcpy(db.row[db.db.row_count - 1].product,"CREATE NEW ITEM"); 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 db.row[db.db.row_count - 1].barcode = -1024;//IMPORTANT: -1024 is reserved for new item
char * SortItemss[] = { char * SortItems[] = {
"Name", "Name",
"Price", "Price",
"Brand", "Brand",
"Category", "Category",
}; };
lister(&db,NULL,db.db.row_count,"Inventory",SortItemss,4,Print_page,SortItems,item_control); lister(&db,NULL,db.db.row_count,"Inventory",SortItems,4,print_page,sortItems,item_control);
// list_page(db,NULL,db.db.row_count,item_control);
break; break;
case 2://add item case 2://add item
add_item(); add_item();
@ -229,7 +229,7 @@ void add_item(){
printf("Item added\n"); printf("Item added\n");
} }
printf("press any key to continue\n"); printf("press any key to continue\n");
fflush(stdin); fflush_stdin();
getchar(); getchar();
} }
@ -254,7 +254,7 @@ void update_item(struct inventory db,int index){
printf("Item updated\n"); printf("Item updated\n");
} }
printf("press any key to continue\n"); printf("press any key to continue\n");
fflush(stdin); fflush_stdin();
getchar(); getchar();
} }
@ -262,7 +262,7 @@ void remove_item(struct inventory db,int index){
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;
fflush(stdin); fflush_stdin();
scanf("%c", &choice); scanf("%c", &choice);
db.row[index].isdeleted = true; db.row[index].isdeleted = true;
if(choice == 'y'){ if(choice == 'y'){
@ -275,7 +275,7 @@ void remove_item(struct inventory db,int index){
printf("Item not removed\n"); printf("Item not removed\n");
} }
printf("press any key to continue\n"); printf("press any key to continue\n");
fflush(stdin); fflush_stdin();
getchar(); getchar();
} }
@ -293,6 +293,13 @@ void tran_control(){
choice = choices_selecter(items,2,"><Welcome to the Student Union POS system><\nTransaction control"); choice = choices_selecter(items,2,"><Welcome to the Student Union POS system><\nTransaction control");
switch(choice){ switch(choice){
case 1: case 1:
char * SortItems[] = {
"Date&Time",
"product(barcode)",
"quantity",
"total",
};
lister(&tran,NULL,tran.db.row_count,"Transaction list",SortItems,4,print_tran,sortTrans,showTran);)
list_tran(tran); list_tran(tran);
break; break;
case 2: case 2:
@ -348,7 +355,7 @@ void list_tran(struct transaction db){
do{ do{
valid = true; valid = true;
printf("Please input your choice\n>"); printf("Please input your choice\n>");
fflush(stdin); fflush_stdin();
scanf("%d",&choice); scanf("%d",&choice);
if(choice <=8 && choice > 0){ if(choice <=8 && choice > 0){
printf("sorting...\n"); printf("sorting...\n");
@ -369,7 +376,7 @@ void list_tran(struct transaction db){
} }
}else if(choice == current_page_size+3){ }else if(choice == current_page_size+3){
printf("Enter page size: "); printf("Enter page size: ");
fflush(stdin); fflush_stdin();
scanf("%d", &page_size); scanf("%d", &page_size);
total_pages = ceil((double)row / page_size); total_pages = ceil((double)row / page_size);
}else if(choice >= 9 && choice <= current_page_size){ }else if(choice >= 9 && choice <= current_page_size){
@ -436,7 +443,7 @@ struct transaction showTran(struct transaction db,int index){
bool valid = true; bool valid = true;
do{ do{
printf("Please input your choice: "); printf("Please input your choice: ");
fflush(stdin); fflush_stdin();
scanf("%d",&choice); scanf("%d",&choice);
switch(choice){ switch(choice){
case 1: case 1:
@ -466,7 +473,7 @@ void add_tran(){
struct transaction_row* row = (struct transaction_row*)malloc(sizeof(struct transaction_row)); struct transaction_row* row = (struct transaction_row*)malloc(sizeof(struct transaction_row));
char check; char check;
printf("Use current date time? (y/n): "); printf("Use current date time? (y/n): ");
fflush(stdin); fflush_stdin();
scanf("%c",&check); scanf("%c",&check);
if(check == 'y'){ if(check == 'y'){
row->date = get_date(); row->date = get_date();
@ -498,7 +505,7 @@ void add_tran(){
printf("Item added\n"); printf("Item added\n");
} }
printf("press any key to continue\n"); printf("press any key to continue\n");
fflush(stdin); fflush_stdin();
getchar(); getchar();
@ -531,7 +538,7 @@ struct transaction update_tran(struct transaction db,int index){
printf("Transaction updated\n"); printf("Transaction updated\n");
} }
printf("press any key to continue\n"); printf("press any key to continue\n");
fflush(stdin); fflush_stdin();
getchar(); getchar();
return db; return db;
@ -546,7 +553,7 @@ struct transaction remove_tran(struct transaction db,int index){
printf("Transaction deleted\n"); printf("Transaction deleted\n");
} }
printf("press any key to continue\n"); printf("press any key to continue\n");
fflush(stdin); fflush_stdin();
getchar(); getchar();
return read_db_tran(); return read_db_tran();
@ -619,7 +626,7 @@ void user_control(){
printf("2. List User\n"); printf("2. List User\n");
printf("3. Exit\n"); printf("3. Exit\n");
printf("Please input your choice\n>"); printf("Please input your choice\n>");
fflush(stdin); fflush_stdin();
scanf("%d",&choice); scanf("%d",&choice);
switch(choice){ switch(choice){
case 1: case 1:
@ -633,7 +640,7 @@ void user_control(){
default: default:
printf("Invalid choice\n"); printf("Invalid choice\n");
printf("press any key to continue\n"); printf("press any key to continue\n");
fflush(stdin); fflush_stdin();
getchar(); getchar();
break; break;
} }
@ -660,7 +667,7 @@ struct user add_user(struct user db){
printf("User added\n"); printf("User added\n");
} }
printf("press any key to continue\n"); printf("press any key to continue\n");
fflush(stdin); fflush_stdin();
getchar(); getchar();
return read_db_user(); return read_db_user();
@ -681,7 +688,7 @@ struct user edit_user(struct user db,int index){
printf("User updated\n"); printf("User updated\n");
} }
printf("press any key to continue\n"); printf("press any key to continue\n");
fflush(stdin); fflush_stdin();
getchar(); getchar();
return db; return db;
@ -695,7 +702,7 @@ struct user delete_user(struct user db,int index){
printf("User deleted\n"); printf("User deleted\n");
} }
printf("press any key to continue\n"); printf("press any key to continue\n");
fflush(stdin); fflush_stdin();
getchar(); getchar();
return read_db_user(); return read_db_user();
@ -738,7 +745,7 @@ struct user list_user(struct user db){
do{ do{
valid = true; valid = true;
printf("Please input your choice\n>"); printf("Please input your choice\n>");
fflush(stdin); fflush_stdin();
scanf("%d",&choice); scanf("%d",&choice);
if(choice <=6 && choice > 0){ if(choice <=6 && choice > 0){
printf("sorting...\n"); printf("sorting...\n");
@ -759,7 +766,7 @@ struct user list_user(struct user db){
} }
}else if(choice == current_page_size+3){ }else if(choice == current_page_size+3){
printf("Enter page size: "); printf("Enter page size: ");
fflush(stdin); fflush_stdin();
scanf("%d", &page_size); scanf("%d", &page_size);
total_pages = ceil((double)row / page_size); total_pages = ceil((double)row / page_size);
}else if(choice >= 7 && choice <= current_page_size){ }else if(choice >= 7 && choice <= current_page_size){
@ -782,6 +789,7 @@ void print_user(struct user db, int cur, int end,struct Map* map){
for(int i = cur; i < end; i++){ for(int i = cur; i < end; i++){
if(map == NULL){ if(map == NULL){
bool isadmin = is_admin(db.row[i].role); bool isadmin = is_admin(db.row[i].role);
// printf("isadmin%s\n",db.row[i].id);
printf("%-5d%-20s%-10ld%-10s%-10c\n",i+7,db.row[i].name,db.row[i].id,db.row[i].role,isadmin?'Y':'N'); printf("%-5d%-20s%-10ld%-10s%-10c\n",i+7,db.row[i].name,db.row[i].id,db.row[i].role,isadmin?'Y':'N');
}else{ }else{
int index = map[i].key; int index = map[i].key;
@ -850,7 +858,7 @@ struct user showUser(struct user db,int index){
printf("1 edit user\n"); printf("1 edit user\n");
printf("2 delete user\n"); printf("2 delete user\n");
printf("Enter choice: "); printf("Enter choice: ");
fflush(stdin); fflush_stdin();
scanf("%d", &choice); scanf("%d", &choice);
switch (choice){ switch (choice){
case 0: case 0:
@ -864,7 +872,7 @@ struct user showUser(struct user db,int index){
default: default:
printf("Invalid choice\n"); printf("Invalid choice\n");
printf("press any key to continue\n"); printf("press any key to continue\n");
fflush(stdin); fflush_stdin();
getchar(); getchar();
break; break;
} }
@ -904,18 +912,18 @@ void role_control(){
void add_role(){ void add_role(){
char role[100]; char role[100];
printf("Enter Admin role name: "); printf("Enter Admin role name: ");
fflush(stdin); fflush_stdin();
scanf("%s", role); scanf("%s", role);
if(is_admin(role)){//check if role exist in admin file if(is_admin(role)){//check if role exist in admin file
printf("Admin role already exist\n"); printf("Admin role already exist\n");
printf("press any key to continue\n"); printf("press any key to continue\n");
fflush(stdin); fflush_stdin();
getchar(); getchar();
}else{ }else{
add_admin(role); add_admin(role);
printf("role added\n"); printf("role added\n");
printf("press any key to continue\n"); printf("press any key to continue\n");
fflush(stdin); fflush_stdin();
getchar(); getchar();
} }
return; return;
@ -962,7 +970,7 @@ void list_role(){
do{ do{
valid = true; valid = true;
printf("Please input your choice\n>"); printf("Please input your choice\n>");
fflush(stdin); fflush_stdin();
scanf("%d", &choice); scanf("%d", &choice);
if(choice <=4 && choice > 0){ if(choice <=4 && choice > 0){
printf("sorting...\n"); printf("sorting...\n");
@ -983,7 +991,7 @@ void list_role(){
} }
}else if(choice == current_page_size+3){ }else if(choice == current_page_size+3){
printf("Enter page size: "); printf("Enter page size: ");
fflush(stdin); fflush_stdin();
scanf("%d", &page_size); scanf("%d", &page_size);
total_pages = ceil( (double)list_size / page_size); total_pages = ceil( (double)list_size / page_size);
page = 0; page = 0;
@ -1032,7 +1040,7 @@ void showRole(struct linkedlist* list,int index){
do{ do{
valid = true; valid = true;
printf("Please input your choice\n>"); printf("Please input your choice\n>");
fflush(stdin); fflush_stdin();
scanf("%d", &choice); scanf("%d", &choice);
if(choice == 1){ if(choice == 1){
if(is_admin(name)){ if(is_admin(name)){
@ -1042,7 +1050,7 @@ void showRole(struct linkedlist* list,int index){
} }
printf("Admin role toggled\n"); printf("Admin role toggled\n");
printf("Press any key to continue\n"); printf("Press any key to continue\n");
fflush(stdin); fflush_stdin();
getchar(); getchar();
}else if(choice != 2){ }else if(choice != 2){
printf("Invalid choice\n"); printf("Invalid choice\n");

View File

@ -127,7 +127,7 @@ bool is_admin(char* role){
while(fgets(line,100,fp) != NULL){ while(fgets(line,100,fp) != NULL){
if(line[0] == '#') continue; if(line[0] == '#') continue;
if(line[strlen(line)-1] == '\n') while(line[strlen(line)-1] == '\n' || (int)line[strlen(line)-1] == 13)
line[strlen(line)-1] = '\0'; line[strlen(line)-1] = '\0';
if(strcmp(line,role) == 0){ if(strcmp(line,role) == 0){
@ -169,7 +169,7 @@ void remove_admin(char* role){
fprintf(fp2,"%s",line); fprintf(fp2,"%s",line);
continue; continue;
} }
if(line[strlen(line)-1] == '\n')//remove newline for compare while(line[strlen(line)-1] == '\n' || (int)line[strlen(line)-1] == 13)//remove newline for compare
line[strlen(line)-1] = '\0'; line[strlen(line)-1] = '\0';
if(strcmp(line,role) != 0){ if(strcmp(line,role) != 0){
fprintf(fp2,"%s\n",line); fprintf(fp2,"%s\n",line);
@ -222,10 +222,12 @@ struct inventory read_db_invt(){//please open file in read mode
if(buffer[0] == '#'){//catch comment line and ignore if(buffer[0] == '#'){//catch comment line and ignore
j--;//decrement j to prevent skipping next column j--;//decrement j to prevent skipping next column
}else{ }else{
buffer[strlen(buffer)] = '\0';
if(buffer[strlen(buffer)-1] == '\n') while(buffer[strlen(buffer)-1] == '\n' || (int)buffer[strlen(buffer)-1] == 13){
buffer[strlen(buffer)-1] = '\0'; buffer[strlen(buffer)-1] = '\0';
}
switch(j){ switch(j){
case category: case category:
strcpy(db.row[i].category,buffer); strcpy(db.row[i].category,buffer);
@ -282,9 +284,12 @@ struct transaction read_db_tran(){
if(buffer[0] == '#'){//catch comment line and ignore if(buffer[0] == '#'){//catch comment line and ignore
j--;//decrement j to prevent skipping next column j--;//decrement j to prevent skipping next column
}else{ }else{
if(buffer[strlen(buffer)-1] == '\n') buffer[strlen(buffer)] = '\0';
while(buffer[strlen(buffer)-1] == '\n' || (int)buffer[strlen(buffer)-1] == 13){
buffer[strlen(buffer)-1] = '\0'; buffer[strlen(buffer)-1] = '\0';
}
switch(j){ switch(j){
case date: case date:
db.row[i].date = convert_to_date(buffer); db.row[i].date = convert_to_date(buffer);
@ -338,9 +343,12 @@ struct user read_db_user(){
if(buffer[0] == '#'){//catch comment line and ignore if(buffer[0] == '#'){//catch comment line and ignore
j--;//decrement j to prevent skipping next column j--;//decrement j to prevent skipping next column
}else{ }else{
if(buffer[strlen(buffer)-1] == '\n') buffer[strlen(buffer)] = '\0';//prevent any garbage value
while(buffer[strlen(buffer)-1] == '\n' || (int)buffer[strlen(buffer)-1] == 13){
buffer[strlen(buffer)-1] = '\0'; buffer[strlen(buffer)-1] = '\0';
}
switch(j){ switch(j){
case name: case name:
strcpy(db.row[i].name,buffer); strcpy(db.row[i].name,buffer);
@ -732,7 +740,8 @@ struct user_row* get_user(long userid){
if(buffer[0] == '#'){//catch comment line and ignore if(buffer[0] == '#'){//catch comment line and ignore
j--;//decrement j to prevent skipping next column j--;//decrement j to prevent skipping next column
}else{ }else{
if(buffer[strlen(buffer)-1] == '\n'){ buffer[strlen(buffer)] = '\0';
while(buffer[strlen(buffer)-1] == '\n' || (int)buffer[strlen(buffer)-1] == 13){
buffer[strlen(buffer)-1] = '\0'; buffer[strlen(buffer)-1] = '\0';
} }

2
main.c
View File

@ -51,7 +51,7 @@ int main(){
break; break;
} }
fflush(stdin); fflush_stdin();
} while (check); } while (check);
return 0; return 0;

View File

@ -25,7 +25,8 @@
void list_items(); void list_items();
void search_item(); void search_item();
void self_help_sale_system(); void self_help_sale_system();
void print_page(struct inventory db, 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);
//main of normal user //main of normal user
void normal_menu(){ void normal_menu(){
@ -55,8 +56,8 @@ void normal_menu(){
} }
//universal functions for normal user //universal functions for normal user
struct Map* sortItems(struct inventory db, int sort); void * show_item(void * ddb,int index){
struct inventory show_item(struct inventory db,int index){ struct inventory db = *((struct inventory*)ddb);
Cls(); 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);
@ -65,105 +66,105 @@ struct inventory show_item(struct inventory db,int index){
printf("Stock: %d\n", db.row[index].stock); printf("Stock: %d\n", db.row[index].stock);
printf("Barcode: %ld\n",db.row[index].barcode); printf("Barcode: %ld\n",db.row[index].barcode);
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 ddb;
} }
//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 // 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;
int total_pages = ceil((double)row / page_size); // int total_pages = ceil((double)row / page_size);
do{ // do{
//options // //options
Cls(); // Cls();
printf("0 exit\n"); // printf("0 exit\n");
printf("1 sort name decending\n"); // printf("1 sort name decending\n");
printf("2 sort name ascending\n"); // printf("2 sort name ascending\n");
printf("3 sort price decending\n"); // printf("3 sort price decending\n");
printf("4 sort price ascending\n"); // printf("4 sort price ascending\n");
printf("5 sort brand decending\n"); // printf("5 sort brand decending\n");
printf("6 sort brand ascending\n"); // printf("6 sort brand ascending\n");
printf("7 sort category decending\n"); // printf("7 sort category decending\n");
printf("8 sort category ascending\n"); // printf("8 sort category ascending\n");
printf("List of items:\n"); // printf("List of items:\n");
//print items // //print items
if(page + 1 >= total_pages){ // if(page + 1 >= total_pages){
print_page(db, page * page_size, row,map); // print_page(db, page * page_size, row,map);
}else{ // }else{
print_page(db, page * page_size, (page + 1) * page_size,map); // print_page(db, page * page_size, (page + 1) * page_size,map);
} // }
//page control // //page control
int current_page_size = 8 + page_size; // int current_page_size = 8 + page_size;
if(page + 1 >= total_pages){ // if(page + 1 >= total_pages){
current_page_size = row - page * page_size+8; // current_page_size = row - page * page_size+8;
} // }
printf("%d next page\n", current_page_size+1); // printf("%d next page\n", current_page_size+1);
printf("%d previous page\n", current_page_size+2); // printf("%d previous page\n", current_page_size+2);
printf("%d set page size\n", current_page_size+3); // printf("%d set page size\n", current_page_size+3);
printf("%d/%d/%d(page size/page number/total)\n",page_size, page+1,total_pages); // printf("%d/%d/%d(page size/page number/total)\n",page_size, page+1,total_pages);
//prompt user to select an item // //prompt user to select an item
bool valid = true; // bool valid = true;
do{ // do{
valid = true; // valid = true;
printf("Enter your choice: "); // printf("Enter your choice: ");
fflush(stdin); // fflush_stdin();
scanf("%d", &choice); // scanf("%d", &choice);
if(choice <=8 && choice > 0){ // if(choice <=8 && choice > 0){
printf("sorting...\n"); // printf("sorting...\n");
map = sortItems(db,choice); // map = sortItems(db,choice);
}else if(choice == current_page_size+1 ){ // }else if(choice == current_page_size+1 ){
if(page + 1 < total_pages){ // if(page + 1 < total_pages){
page++; // page++;
}else{ // }else{
printf("Already at last page\n"); // printf("Already at last page\n");
valid = false; // valid = false;
} // }
}else if(choice == current_page_size+2){ // }else if(choice == current_page_size+2){
if(page > 0){ // if(page > 0){
page--; // page--;
}else{ // }else{
printf("Already at first page\n"); // printf("Already at first page\n");
valid = false; // valid = false;
} // }
}else if(choice == current_page_size+3){ // }else if(choice == current_page_size+3){
printf("Enter page size: "); // printf("Enter page size: ");
fflush(stdin); // fflush_stdin();
scanf("%d", &page_size); // scanf("%d", &page_size);
total_pages = ceil((double)row / page_size); // total_pages = ceil((double)row / page_size);
}else if(choice >= 9 && choice <= current_page_size){ // }else if(choice >= 9 && choice <= current_page_size){
if(map == NULL){ // if(map == NULL){
db = (*showitem)(db,choice - 9 + page_size*page); // db = (*showitem)(db,choice - 9 + page_size*page);
}else{ // }else{
db = (*showitem)(db,map[choice - 9 + page_size*page].key); // db = (*showitem)(db,map[choice - 9 + page_size*page].key);
} // }
}else if(choice != 0){ // }else if(choice != 0){
printf("Invalid choice\n"); // printf("Invalid choice\n");
valid = false; // valid = false;
} // }
}while(!valid); // }while(!valid);
}while(choice != 0); // }while(choice != 0);
} // }
//print the list for the page //print the list for the page
void print_page(struct inventory db, int cur, int end,struct Map* map){ // void print_page(struct inventory db, int cur, int end,struct Map* map){
for (int i = cur; i < end; i++) // for (int i = cur; i < end; i++)
{ // {
if(map != NULL){ // if(map != NULL){
printf("%d item %d: %s\n", i + 9,i, db.row[map[i].key].product); // printf("%d item %d: %s\n", i + 9,i, db.row[map[i].key].product);
} // }
else{ // else{
printf("%d item %d: %s\n", i + 9,i, db.row[i].product); // printf("%d item %d: %s\n", i + 9,i, db.row[i].product);
} // }
} // }
} // }
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 inventory db = *((struct inventory*)ddb); struct inventory db = *((struct inventory*)ddb);
for (int i = cur; i < end; i++) for (int i = cur; i < end; i++)
{ {
@ -194,66 +195,12 @@ void list_items(){
"Brand", "Brand",
"Category", "Category",
}; };
// FIXME:function for normal list page lister(&db,map,db.db.row_count,"List of items",SortItems,4,print_page,sortItems,show_item);
// lister(&db,map,db.db.row_count,"List of items",SortItems,4,print_page,sortItems,item_control);
list_page(db,map,db.db.row_count,show_item);
} }
//for sorting //for sorting
struct Map* sortItems(struct inventory db, int sort){
// struct Map* sortItems(void * ddb, int sort){
// struct inventory db = *((struct inventory*)ddb);
struct Map *map = malloc(sizeof(struct Map) * db.db.row_count-1);
for (int i = 0; i < db.db.row_count-1; i++){
map[i].key = i;
switch(sort){ struct Map* sortItems(void * ddb, int sort){
case 1:
case 2:
map[i].value = (void*)db.row[i].product;
break;
case 3:
case 4:;
double price = db.row[i].price * 100;
double * price_star = malloc(sizeof(long));
*price_star = price;
map[i].value = (void*)price_star;//presume there is no price contain 0.001
break;
case 5:
case 6:
map[i].value = (void*)db.row[i].brand;
break;
case 7:
case 8:
map[i].value = (void*)db.row[i].category;
break;
}
}
switch (sort){
case 1:
case 5:
case 7:
qsort(map, db.db.row_count-1, sizeof(struct Map), compare_decending_str);
break;
case 2:
case 6:
case 8:
qsort(map, db.db.row_count-1, sizeof(struct Map), compare_ascending_str);
break;
case 3:
qsort(map, db.db.row_count-1, sizeof(struct Map), compare_decending);
break;
case 4:
qsort(map, db.db.row_count-1, sizeof(struct Map), compare_ascending);
break;
}
map = realloc(map, sizeof(struct Map) * db.db.row_count);
map[db.db.row_count-1].key = db.db.row_count-1;
return map;
}
struct Map* SortItems(void * ddb, int sort){
struct inventory db = *((struct inventory*)ddb); struct inventory db = *((struct inventory*)ddb);
struct Map *map = malloc(sizeof(struct Map) * db.db.row_count-1); struct Map *map = malloc(sizeof(struct Map) * db.db.row_count-1);
for (int i = 0; i < db.db.row_count-1; i++){ for (int i = 0; i < db.db.row_count-1; i++){
@ -332,19 +279,17 @@ void search_item(){
map = searchItems(db,searchstr); map = searchItems(db,searchstr);
if(map[0].value > 0){ if(map[0].value > 0){
int temp_row = *((int*)map[0].value); int temp_row = *((int*)map[0].value);
// char * SortItems[] = { char * SortItems[] = {
// "Name", "Name",
// "Price", "Price",
// "Brand", "Brand",
// "Category", "Category",
// }; };
//FIXME 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,item_control);//ofset map, as it is use to store the size
list_page(db,map+1,temp_row,show_item);//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");
fflush(stdin); fflush_stdin();
getchar(); getchar();
} }
@ -356,7 +301,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);
return searchstr; return searchstr;
} }
@ -521,23 +466,23 @@ struct cart* scan_barcode(struct cart* cart,struct inventory db){
printf("product: %s\n",row->product); printf("product: %s\n",row->product);
printf("price: %.1f\n",row->price); printf("price: %.1f\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);
fflush(stdin); fflush_stdin();
if(quantity == 0){ if(quantity == 0){
printf("cancelled\n"); printf("cancelled\n");
printf("press any key to continue\n"); printf("press any key to continue\n");
fflush(stdin); fflush_stdin();
getchar(); getchar();
return cart; return cart;
} }
printf("Are you sure you want to add this item to cart?(y/n)\n>"); printf("Are you sure you want to add this item to cart?(y/n)\n>");
fflush(stdin); fflush_stdin();
scanf("%c", &choice); scanf("%c", &choice);
if(row->stock - quantity < 0 && choice == 'y'){ if(row->stock - quantity < 0 && choice == 'y'){
printf("WARNING:not enough stock\n"); printf("WARNING:not enough stock\n");
printf("Are you sure the quantity is correct?\n(y to ignore and continue/n)\n>"); printf("Are you sure the quantity is correct?\n(y to ignore and continue/n)\n>");
fflush(stdin); fflush_stdin();
scanf("%c", &choice); scanf("%c", &choice);
} }
}while(choice != 'y'); }while(choice != 'y');
@ -550,7 +495,7 @@ struct cart* scan_barcode(struct cart* cart,struct inventory db){
} }
printf("press any key to continue\n"); printf("press any key to continue\n");
fflush(stdin); fflush_stdin();
getchar(); getchar();
return cart; return cart;
} }
@ -559,7 +504,7 @@ struct cart* scan_barcode(struct cart* cart,struct inventory db){
long prompt_barcode(){ long prompt_barcode(){
printf("Please scan the qr code(or input the barcode ) "); printf("Please scan the qr code(or input the barcode ) ");
long barcode; long barcode;
fflush(stdin); fflush_stdin();
scanf("%ld", &barcode); scanf("%ld", &barcode);
return barcode; return barcode;
} }
@ -611,7 +556,7 @@ struct cart* list_cart(struct cart* cart){
} }
do{ do{
printf("input the corresponding value for more action\n>"); printf("input the corresponding value for more action\n>");
fflush(stdin); fflush_stdin();
scanf("%d", &choice); scanf("%d", &choice);
if(choice >0 && choice < i){ if(choice >0 && choice < i){
cart = cart_control(cart,choice); cart = cart_control(cart,choice);
@ -644,13 +589,13 @@ struct cart* cart_control(struct cart* cart,int index){
cart = Cartremove(cart,index-1); cart = Cartremove(cart,index-1);
printf("Remove successful\n"); printf("Remove successful\n");
printf("press any key to continue\n"); printf("press any key to continue\n");
fflush(stdin); fflush_stdin();
getchar(); getchar();
}else if(choice == 2){ }else if(choice == 2){
cart = update_cart(cart,index); cart = update_cart(cart,index);
printf("Update successful\n"); printf("Update successful\n");
printf("press any key to continue"); printf("press any key to continue");
fflush(stdin); fflush_stdin();
getchar(); getchar();
} }
}while(choice != 1 || choice != 2 || choice !=3 ); }while(choice != 1 || choice != 2 || choice !=3 );
@ -664,10 +609,10 @@ struct cart* update_cart(struct cart* cart,int index){
do{ do{
int quantity = 0; int quantity = 0;
printf("enter the new quantity: "); printf("enter the new quantity: ");
fflush(stdin); fflush_stdin();
scanf("%d", &quantity); scanf("%d", &quantity);
printf("Are you sure you want to update this item?(y/n/e to exit)"); printf("Are you sure you want to update this item?(y/n/e to exit)");
fflush(stdin); fflush_stdin();
scanf("%c", &choice); scanf("%c", &choice);
if(choice == 'y'){ if(choice == 'y'){
@ -695,7 +640,7 @@ struct cart* checkout(struct cart* cart){
if(user_id == 0){ if(user_id == 0){
printf("cancelled\n"); printf("cancelled\n");
printf("press any key to continue\n"); printf("press any key to continue\n");
fflush(stdin); fflush_stdin();
getchar(); getchar();
return cart; return cart;
} }
@ -705,33 +650,33 @@ struct cart* checkout(struct cart* cart){
if(user == NULL){ if(user == NULL){
printf("Fail to indentify your card or error occurs\n"); printf("Fail to indentify your card or error occurs\n");
printf("press any key to continue\n"); printf("press any key to continue\n");
fflush(stdin); fflush_stdin();
getchar(); getchar();
return cart; return cart;
}else{ }else{
printf("Welcome %s(%s)\n",user->name,user->role); printf("Welcome %s(%s)\n",user->name,user->role);
printf("Are you sure you want to checkout?(y/n)\n>"); printf("Are you sure you want to checkout?(y/n)\n>");
char choice; char choice;
fflush(stdin); fflush_stdin();
scanf("%c", &choice); scanf("%c", &choice);
if(choice == 'y'){ if(choice == 'y'){
if(update_stock_N_checkout(cart,user)){//catch err if(update_stock_N_checkout(cart,user)){//catch err
printf("Checkout successful\n"); printf("Checkout successful\n");
printf("press any key to continue\n"); printf("press any key to continue\n");
fflush(stdin); fflush_stdin();
getchar(); getchar();
return NULL; return NULL;
}else{ }else{
printf("Checkout failed(files maybe corrputed)\n"); printf("Checkout failed(files maybe corrputed)\n");
printf("press any key to continue\n"); printf("press any key to continue\n");
fflush(stdin); fflush_stdin();
getchar(); getchar();
return cart; return cart;
} }
}else{ }else{
printf("cancelled\n"); printf("cancelled\n");
printf("press any key to continue\n"); printf("press any key to continue\n");
fflush(stdin); fflush_stdin();
getchar(); getchar();
return cart; return cart;
} }

25
utils.h
View File

@ -9,7 +9,10 @@
#include<ctype.h> #include<ctype.h>
#include <stddef.h> #include <stddef.h>
#endif #endif
#ifdef _WIN32
#else
#include <stdio_ext.h>
#endif
void Cls(){ void Cls(){
#ifdef _WIN32 #ifdef _WIN32
system("cls"); system("cls");
@ -17,6 +20,13 @@ void Cls(){
system("clear"); system("clear");
#endif #endif
} }
void fflush_stdin(){
#ifdef _WIN32
fflush(stdin);
#else
__fpurge(stdin);
#endif
}
void welcome_message(){ void welcome_message(){
printf("><Welcome to the Student Union POS system><\n");//welcome messgae printf("><Welcome to the Student Union POS system><\n");//welcome messgae
@ -37,11 +47,11 @@ int choices_selecter(char * Items[],int items,char * welcome_messages){
} }
printf("%d. Exit\n",i+1); printf("%d. Exit\n",i+1);
printf(">"); printf(">");
fflush(stdin); fflush_stdin();
scanf("%d",&choice); scanf("%d",&choice);
if(choice < 1 || choice > items+1){ if(choice < 1 || choice > items+1){
printf("Invalid choice...press any key to retry....\n"); printf("Invalid choice...press any key to retry....\n");
fflush(stdin); fflush_stdin();
getchar(); getchar();
} }
}while(choice < 1 || choice > items+1); }while(choice < 1 || choice > items+1);
@ -53,12 +63,12 @@ char* prompt_item(char* prompt,bool empty_allowed){
char* item = malloc(sizeof(char) * 100); char* item = malloc(sizeof(char) * 100);
do{ do{
printf("%s",prompt); printf("%s",prompt);
fflush(stdin); fflush_stdin();
scanf("%[^\n]",item);//input until /n scanf("%[^\n]",item);//input until /n
if(!empty_allowed&&(item,"") == 0){//check if temp is not empty if(!empty_allowed&&(item,"") == 0){//check if temp is not empty
printf("Invalid input, try again?(y/n)\n"); printf("Invalid input, try again?(y/n)\n");
char temp[100]; char temp[100];
fflush(stdin); fflush_stdin();
scanf("%[^\n]",temp); scanf("%[^\n]",temp);
if(strcmp(temp,"n") == 0){ if(strcmp(temp,"n") == 0){
return NULL; return NULL;
@ -97,7 +107,6 @@ bool item_inputer(char * varname,INPUT_TYPE type,void * item,bool empty_allowed)
return true; return true;
} }
//TODO: lister still useable
void lister(void * db,struct Map* map, int row,char * lister_name,char * SortItems[],int NoSortItems,void (*page_printer)(void*,int,int,struct Map*) , struct Map* (*sorter)(void *,int),void * (*showitem)(void *,int)){ void lister(void * db,struct Map* map, int row,char * lister_name,char * SortItems[],int NoSortItems,void (*page_printer)(void*,int,int,struct Map*) , struct Map* (*sorter)(void *,int),void * (*showitem)(void *,int)){
int choice = -1; int choice = -1;
int page = 0; int page = 0;
@ -131,7 +140,7 @@ void lister(void * db,struct Map* map, int row,char * lister_name,char * SortIte
do{ do{
valid = true; valid = true;
printf("Please input your choice\n>"); printf("Please input your choice\n>");
fflush(stdin); fflush_stdin();
scanf("%d",&choice); scanf("%d",&choice);
if(choice <=NoSortItems && choice > 0){ if(choice <=NoSortItems && choice > 0){
printf("sorting...\n"); printf("sorting...\n");
@ -152,7 +161,7 @@ void lister(void * db,struct Map* map, int row,char * lister_name,char * SortIte
} }
}else if(choice == current_page_size+3){ }else if(choice == current_page_size+3){
printf("Enter page size: "); printf("Enter page size: ");
fflush(stdin); fflush_stdin();
scanf("%d", &page_size); scanf("%d", &page_size);
total_pages = ceil((double)row / page_size); total_pages = ceil((double)row / page_size);
}else if(choice >= 9 && choice <= current_page_size){ }else if(choice >= 9 && choice <= current_page_size){