Finishing problem 2

This commit is contained in:
Denis Nuțiu 2016-04-14 22:40:40 +03:00
parent 5732564492
commit ec4b139724
2 changed files with 115 additions and 38 deletions

153
lab8/2.c
View file

@ -1,4 +1,5 @@
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <stdlib.h> #include <stdlib.h>
#define ROW_LEN 50 #define ROW_LEN 50
@ -16,6 +17,7 @@ typedef struct Db_tag {
void display_entry(Database * entry, unsigned index); void display_entry(Database * entry, unsigned index);
void insert_entry(FILE * file, Database * entry); void insert_entry(FILE * file, Database * entry);
void read_entry(FILE * file, Database * entry); void read_entry(FILE * file, Database * entry);
void validate_input(int i);
void delete_entry(FILE * file, unsigned entry_no) { void delete_entry(FILE * file, unsigned entry_no) {
fseek(file, 0, SEEK_SET); fseek(file, 0, SEEK_SET);
@ -45,19 +47,27 @@ void delete_entry(FILE * file, unsigned entry_no) {
} }
} }
void modify_entry(Database * entry) { void modify_entry(Database * entry, int exists) {
if (entry->blacklisted && exists) {
printf("Entry is blacklisted!\n Access Denied\n");
return;
}
printf("Enter new repair type:\n"); printf("Enter new repair type:\n");
printf("Current: %s\n> ", entry->repair_type); if (exists) printf("Current: %s\n> ", entry->repair_type);
scanf("%50[a-zA-Z ]", entry->repair_type); validate_input(0); scanf("%50[a-zA-Z ]", entry->repair_type);
printf("Enter new IMEI:\n");
if (exists) printf("Current: %s\n> ", entry->imei);
validate_input(0); scanf("%50[a-zA-Z ]", entry->imei);
printf("Enter new price:\n"); printf("Enter new price:\n");
printf("Current: %d\n> ", entry->price); if (exists) printf("Current: %d\n> ", entry->price);
scanf("%d", &entry->price); validate_input(0); scanf("%d", &entry->price);
printf("Enter new investment:\n"); printf("Enter new investment:\n");
printf("Current: %d\n> ", entry->investment); if (exists) printf("Current: %d\n> ", entry->investment);
scanf("%d", &entry->investment); validate_input(0); scanf("%d", &entry->investment);
printf("Enter new profit:\n"); printf("Enter new profit:\n");
printf("Current: %d\n> ", entry->profit); if (exists) printf("Current: %d\n> ", entry->profit);
scanf("%d", &entry->profit); validate_input(0); scanf("%d", &entry->profit);
} }
void update_entry(FILE * file, unsigned entry_no) { void update_entry(FILE * file, unsigned entry_no) {
@ -67,9 +77,37 @@ void update_entry(FILE * file, unsigned entry_no) {
if ( i == entry_no ) { if ( i == entry_no ) {
printf("Entry found!\n"); printf("Entry found!\n");
fseek(file, -sizeof(Database), SEEK_CUR); fseek(file, -sizeof(Database), SEEK_CUR);
modify_entry(&entry); modify_entry(&entry, 1);
fwrite(&entry, sizeof(Database), 1, file); fwrite(&entry, sizeof(Database), 1, file);
printf("RETSwrote %s\n", entry.repair_type); }
}
}
void blacklist_entry(FILE * file, unsigned entry_no) {
fseek(file, 0, SEEK_SET);
Database entry;
for( int i = 0; fread(&entry, sizeof(Database), 1, file); i++ ) {
if ( i == entry_no ) {
printf("Entry found!\n");
fseek(file, -sizeof(Database), SEEK_CUR);
if (entry.blacklisted) {
printf("Entry has been Unblacklisted!\n");
entry.blacklisted = 0;
} else {
printf("Entry has been Blacklisted!\n");
entry.blacklisted = 1;
}
fwrite(&entry, sizeof(Database), 1, file);
}
}
}
void search_entry(FILE * file, char * imei) {
fseek(file, 0, SEEK_SET);
Database entry;
for( int i = 0; fread(&entry, sizeof(Database), 1, file); i++ ) {
if ( !strcmp(entry.imei, imei) ) {
display_entry(&entry, 0);
} }
} }
} }
@ -84,13 +122,14 @@ 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|\n", printf("|%3d|%50s|%10s|%6d| %10d| %10d| %c\n",
index, index,
entry->repair_type, entry->repair_type,
entry->imei, entry->imei,
entry->price, entry->price,
entry->investment, entry->investment,
entry->profit entry->profit,
(entry->blacklisted ? 'B' : '_')
); );
} }
@ -125,34 +164,72 @@ void display_menu() {
printf("3. Update an entry.\n"); printf("3. Update an entry.\n");
printf("4. Delete an entry.\n"); printf("4. Delete an entry.\n");
printf("5. Search by IMEI\n"); printf("5. Search by IMEI\n");
printf("6. Blacklist/UnBlacklist\n");
printf("\n"); printf("\n");
} }
int main(void) { void validate_input(int i) {
FILE * fp = fopen("data.bdb", "r+b"); if (!i) {
// for (int i = 0; i < 10; i++) { int c;
// Database nokia = { while( (c = getchar()) != '\n' );
// .blacklisted = 6, }
// .repair_type = "BATERIE3", }
// .imei = "nuavema",
// .price = i, void process_choice(FILE * file) {
// .investment = 2, display_menu();
// .profit = 3,
// };
// insert_entry(fp, &nokia);
// }
Database e; Database e;
// read_entry(fp, &e);
display_database_entries(fp); enum choices { exit_program_c, display_the_database_c, add_entry_c, update_entry_c,
update_entry(fp, 2); delete_entry_c, search_by_imei_c, blacklist_c };
display_database_entries(fp);
// delete_entry(fp, 1); int choice = -1;
// display_database_entries(fp); int entry_no = 0;
// printf("after ins %ld\n", ftell(fp)); char buffer[50];
// fseek(fp, -sizeof(Database), SEEK_CUR); printf("Enter your choice\n>> ");
// printf("before read %ld\n", ftell(fp)); validate_input(scanf("%d", &choice));
// read_entry(fp, &e); switch (choice) {
// printf("after read %ld\n", ftell(fp)); case exit_program_c:
// printf("%s\n", e.repair_type); printf("Exiting...\n");
exit(0);
break;
case display_the_database_c:
display_database_entries(file);
break;
case add_entry_c:
modify_entry(&e, 0);
insert_entry(file, &e);
break;
case update_entry_c:
printf("Enter the entry number: ");
validate_input(scanf("%d", &entry_no));
update_entry(file, entry_no);
break;
case delete_entry_c:
validate_input(printf("Enter the entry number: "));
scanf("%d", &entry_no);
delete_entry(file, entry_no);
break;
case search_by_imei_c:
printf("Enter the IMEI: ");
scanf("%s", buffer);
search_entry(file, buffer);
break;
case blacklist_c:
validate_input(printf("Enter the entry number: "));
scanf("%d", &entry_no);
blacklist_entry(file, entry_no);
break;
default:
printf("Invalid choice!\n");
}
process_choice(file); // redo the choice, since the user hasn't exited;
}
int main(void) {
FILE * fp;
if ( !(fp = fopen("data.bdb", "r+b")) ) {
perror("Error opening file");
}
process_choice(fp);
return 0; return 0;
} }

Binary file not shown.