From 7b1ccd61d2c8f145692521e4f43b091afc3d3002 Mon Sep 17 00:00:00 2001 From: smarttommyau Date: Sat, 27 Aug 2022 19:00:57 +0800 Subject: [PATCH] basically finished list item, no yet confirm stock column --- .vscode/settings.json | 5 +++++ data_file_inventory.txt | 2 +- database.h | 20 +++++++++++--------- main.c | 1 + normal_user.h | 33 +++++++++++++++++---------------- 5 files changed, 35 insertions(+), 26 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..0f06797 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "stdio.h": "c" + } +} \ No newline at end of file diff --git a/data_file_inventory.txt b/data_file_inventory.txt index b219055..eac62bf 100644 --- a/data_file_inventory.txt +++ b/data_file_inventory.txt @@ -1,4 +1,4 @@ -#category, brand, product, price, stock, barcode +#category, brand, product, price`vc , stock, barcode Test Testing Co Ltd Testing Product diff --git a/database.h b/database.h index 3deade3..9408024 100644 --- a/database.h +++ b/database.h @@ -26,7 +26,7 @@ typedef struct inventory{ }; typedef struct inventory_row{ char category[100]; - char band[100]; + char brand[100]; char product[100]; double price; int stock; @@ -54,9 +54,9 @@ typedef struct transaction_row{ bool isdeleted;//common for all rows,default is false }; typedef enum { - date = 1, time = 2, id_tran = 3, price_tran = 5, quantity = 4, barcode_tran = 7 + date = 1, time = 2, id_tran = 3, price_tran = 4, quantity = 5, barcode_tran = 6 }TRANSACTION; -#define ENDOFTRANSACTION 7 +#define ENDOFTRANSACTION 6 //user #define USER_DB "data_file_user.txt" typedef struct user{ @@ -88,15 +88,17 @@ int basic_get_row_count(int end, FILE *fp){ while(!feof(fp)&&!ferror(fp)){ char buffer[100]; fgets(buffer, sizeof(buffer),fp); - if(buffer[0] == '#'){//catch comment line and ignore + if(buffer[0] == '#' || buffer[0] == '\0'){//catch comment line and ignore continue; } colmun_count++; + puts(buffer); if(colmun_count == end){ row_count++; colmun_count = 0; } } + return row_count; } struct inventory read_db_invt(){//please open file in read mode @@ -113,7 +115,7 @@ struct inventory read_db_invt(){//please open file in read mode //read data for(int i=0;irow[i].category); break; case brand: - fprintf(fpW,"%s",invt->row[i].band); + fprintf(fpW,"%s",invt->row[i].brand); break; case product: fprintf(fpW,"%s",invt->row[i].product); diff --git a/main.c b/main.c index 268ea13..e4a562e 100644 --- a/main.c +++ b/main.c @@ -26,6 +26,7 @@ int main(){ bool check; do { + system("cls"); //print welcome message //prompt weather user is admin or normal user //then redirect them to the corisponding menu diff --git a/normal_user.h b/normal_user.h index 0771344..6393634 100644 --- a/normal_user.h +++ b/normal_user.h @@ -79,13 +79,11 @@ void list_items(){ int page = 0; int page_size = PAGE_SIZE; struct inventory db = read_db_invt(); - int total_pages = ceil(db.db.row_count / page_size); + int total_pages = (int)ceil((double)db.db.row_count / page_size); struct Map* map = NULL; - - do{ - //ooptions + //options system("cls"); printf("0 exit\n"); printf("1 sort name decending\n"); @@ -99,17 +97,17 @@ void list_items(){ printf("List of items:\n"); //print items - if(map == NULL){ - print_page(db, page * page_size, (page + 1== total_pages)?db.db.row_count:(page+1) * page_size,(struct Map*)NULL); + if(page + 1 >= total_pages){ + print_page(db, page * page_size, db.db.row_count,map); }else{//sorted) - print_page(db, page * page_size, (page + 1== total_pages)?db.db.row_count:(page+1) * page_size,map); + 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); + 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; @@ -127,9 +125,10 @@ void list_items(){ printf("Enter page size: "); scanf("%d", &page_size); total_pages = ceil(db.db.row_count / page_size); - }else if(choice >= 0 && choice < db.db.row_count){ - show_item(db,choice-9 + page_size*page); - }else{ + }else if(choice >= 9 && choice < db.db.row_count+9){ + printf("s"); + show_item(db,choice - 9 + page_size*page); + }else if(choice != 0){ printf("Invalid choice\n"); valid = false; } @@ -153,7 +152,7 @@ struct Map* sortItems(struct inventory db, int sort){ break; case 5: case 6: - map[i].value = (int)db.row[i].band; + map[i].value = (int)db.row[i].brand; break; case 7: case 8: @@ -182,10 +181,10 @@ 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. %s\n", i + 1, db.row[map[i].key].product); + printf("%d item: %s\n", i + 9, db.row[map[i].key].product); } else{ - printf("item%d. %s\n", i + 3, db.row[i].product); + printf("%d item: %s\n", i + 9, db.row[i].product); } } } @@ -194,11 +193,13 @@ 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("Band: %d\n", db.row[index].band); + 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; }