Fixing small bugs

This commit is contained in:
Denis Nuțiu 2016-04-15 10:54:44 +03:00
parent bd14a90d55
commit 94ab0d2e10
2 changed files with 26 additions and 23 deletions

View file

@ -3,12 +3,11 @@
#include <stdlib.h> #include <stdlib.h>
#define ROW_LEN 50 #define ROW_LEN 50
#define IMEI_LEN 10
typedef struct Db_tag { typedef struct Db_tag {
int blacklisted; // 1 yes, 0 no int blacklisted : 1; // 1 yes, 0 no
char repair_type[ROW_LEN]; char repair_type[ROW_LEN];
char imei[IMEI_LEN]; long imei;
int price; int price;
int investment; int investment;
int profit; int profit;
@ -64,17 +63,17 @@ void modify_entry(Database * entry, int exists) {
if (exists) printf("Current: %s\n> ", entry->repair_type); if (exists) printf("Current: %s\n> ", entry->repair_type);
validate_input(0); scanf("%50[a-zA-Z -_'\"!~?]", entry->repair_type); validate_input(0); scanf("%50[a-zA-Z -_'\"!~?]", entry->repair_type);
printf("Enter new IMEI:\n"); printf("Enter new IMEI:\n");
if (exists) printf("Current: %s\n> ", entry->imei); if (exists) printf("Current: %ld\n> ", entry->imei);
validate_input(0); scanf("%10[a-zA-Z ]", entry->imei); validate_input(0); scanf("%10ld", &entry->imei);
printf("Enter new price:\n"); printf("Enter new price:\n");
if (exists) printf("Current: %d\n> ", entry->price); if (exists) printf("Current: %10d\n> ", entry->price);
validate_input(0); scanf("%d", &entry->price); validate_input(0); scanf("%10d", &entry->price);
printf("Enter new investment:\n"); printf("Enter new investment:\n");
if (exists) printf("Current: %d\n> ", entry->investment); if (exists) printf("Current: %d\n> ", entry->investment);
validate_input(0); scanf("%d", &entry->investment); validate_input(0); scanf("%10d", &entry->investment);
printf("Enter new profit:\n"); entry->profit = entry->price - entry->investment;
if (exists) printf("Current: %d\n> ", entry->profit); entry->blacklisted = 0; // in case there is an 1 that's not touched.
validate_input(0); scanf("%d", &entry->profit); validate_input(0); // clear remaining trash, if any.
} }
void update_entry(FILE * file, unsigned entry_no) { void update_entry(FILE * file, unsigned entry_no) {
@ -86,11 +85,13 @@ void update_entry(FILE * file, unsigned entry_no) {
fseek(file, -sizeof(Database), SEEK_CUR); fseek(file, -sizeof(Database), SEEK_CUR);
modify_entry(&entry, 1); modify_entry(&entry, 1);
fwrite(&entry, sizeof(Database), 1, file); fwrite(&entry, sizeof(Database), 1, file);
return;
} }
} }
printf("Entry not found!\n");
} }
// Blacklists or Unblaclists an entry. // Blacklists or Unblacklists an entry.
void blacklist_entry(FILE * file, unsigned entry_no) { void blacklist_entry(FILE * file, unsigned entry_no) {
fseek(file, 0, SEEK_SET); fseek(file, 0, SEEK_SET);
Database entry; Database entry;
@ -110,18 +111,21 @@ void blacklist_entry(FILE * file, unsigned entry_no) {
} }
} }
void search_entry(FILE * file, char * imei) { void search_entry(FILE * file, long imei) {
fseek(file, 0, SEEK_SET); fseek(file, 0, SEEK_SET);
Database entry; Database entry;
for( int i = 0; fread(&entry, sizeof(Database), 1, file); i++ ) { for( int i = 0; fread(&entry, sizeof(Database), 1, file); i++ ) {
if ( !strcmp(entry.imei, imei) ) { if ( entry.imei == imei ) {
display_entry(&entry, 0); display_entry(&entry, 0);
return;
} }
} }
printf("Entry not found!\n");
} }
void insert_entry(FILE * file, Database * entry) { void insert_entry(FILE * file, Database * entry) {
fseek(file, 0, SEEK_END); fseek(file, 0, SEEK_END);
entry->profit = entry->price - entry->investment;
fwrite(entry, sizeof(Database), 1, file); fwrite(entry, sizeof(Database), 1, file);
} }
@ -130,7 +134,7 @@ void read_entry(FILE * file, Database * entry) {
} }
void display_entry(Database * entry, unsigned index) { void display_entry(Database * entry, unsigned index) {
printf("|%3d|%50s|%10s|%6d| %10d| %10d| %c\n", printf("|%3d|%50s|%10ld|%10d| %10d| %10d| %c\n",
index, index,
entry->repair_type, entry->repair_type,
entry->imei, entry->imei,
@ -142,7 +146,7 @@ void display_entry(Database * entry, unsigned index) {
} }
void display_header() { void display_header() {
printf("|%3s|%50s|%10s|%6s| %10s| %10s|\n", printf("|%3s|%50s|%10s|%10s| %10s| %10s|\n",
"i", "Repair Type", "IMEI", "Price", "Investment", "i", "Repair Type", "IMEI", "Price", "Investment",
"Profit"); "Profit");
} }
@ -196,7 +200,6 @@ void process_choice(FILE * file) {
int choice = -1; int choice = -1;
int entry_no = 0; int entry_no = 0;
char buffer[50];
printf("Enter your choice\n>> "); printf("Enter your choice\n>> ");
validate_input(scanf("%d", &choice)); validate_input(scanf("%d", &choice));
switch (choice) { switch (choice) {
@ -217,18 +220,18 @@ void process_choice(FILE * file) {
update_entry(file, entry_no); update_entry(file, entry_no);
break; break;
case delete_entry_c: case delete_entry_c:
validate_input(printf("Enter the entry number: ")); printf("Enter the entry number: ");
scanf("%d", &entry_no); validate_input(scanf("%d", &entry_no));
delete_entry(file, entry_no); delete_entry(file, entry_no);
break; break;
case search_by_imei_c: case search_by_imei_c:
printf("Enter the IMEI: "); printf("Enter the IMEI: ");
scanf("%s", buffer); validate_input(scanf("%ld", &e.imei));
search_entry(file, buffer); search_entry(file, e.imei);
break; break;
case blacklist_c: case blacklist_c:
validate_input(printf("Enter the entry number: ")); printf("Enter the entry number: ");
scanf("%d", &entry_no); validate_input(scanf("%d", &entry_no));
blacklist_entry(file, entry_no); blacklist_entry(file, entry_no);
break; break;
default: default:

Binary file not shown.