Main.cpp
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#define MIN_MENU_ITEM '1'
#define MAX_MENU_ITEM '5'
FILE* db = 0;
char dbFileName[256];
/* Displays a list of program menu variants */
void print_menu()
{
system("cls"); //display the current loaded database filename
if (db)
printf("Database loaded: %s
", dbFileName);
else
puts("Database loaded: none
");
puts("Use the following commands:
");
puts(" 1 - Load database.
");
puts(" 2 - Add new record.
");
puts(" 3 - Display database.
");
puts(" 4 - Generate report.
");
puts(" 5 - Exit.
");
}
/* Inputs a number of the seleceted menu item */
char input_menu_item()
{
int item;
printf("Enter menu item number: ");
item = getch();
while (MIN_MENU_ITEM > item || item > MAX_MENU_ITEM)
{
print_menu();
printf("Wrong item number <%c>. Retype please: ", item);
item = getch();
}
return (char)item;
}
/* Creates and loads a new database */
FILE* create_db()
{
char dbFile[260];
strcpy(dbFile, "local_db");
int index = 1;
FILE* result = fopen(dbFile, "r+");
while (result) //check if a there is already a file named $dbFile
{
fclose(result);
dbFile[8] = '_';
itoa(index++, dbFile + 9, 10);
result = fopen(dbFile, "r+"); //trying create database filename "local_db_<N>"
}
result = fopen(dbFile, "w+");
strcpy(dbFileName, dbFile);
print_menu();
printf("Database "%s" sucessfully created and loaded.", dbFile);
getch();
return result;
}
/* Inputs a database filename and opens it */
FILE* load_database()
{
if (db) //check if there is already a database loaded
{
print_menu();
printf("There is already a database loaded. Would you like to close it? (y/n): ");
if (tolower(getch()) == 'y')
{
fclose(db);
dbFileName[0] = 0;
}
else
return db;
}
FILE* result;
char fileName[260];
fileName[0] = 0;
printf("Enter database filename: ");
flushall();
gets(fileName);
while (!(result = fopen(fileName, "r+"))) //getting DB filename
{
print_menu();
printf("Wrong database filename "%s".
", &fileName);
printf("Would you like to create the new database automatically? (y/n): ");
if (tolower(getch()) == 'y')
return create_db();
else
{
print_menu();
printf("Enter database filename: ");
flushall();
gets(fileName);
}
}
return result;
}
/* Inputs and saves to a database a new movie name, ticket price and sold ticket count
*/
void add_record(FILE* db)
{
if (!db)
{
printf("There is no database loaded. Do it choosing the 1 menu item.");
getch();
return;
}
char buffer[256];
buffer[0] = 0;
int ticketPrice = -1;
int ticketsSold = -1;
flushall();
printf("Enter the movie name: ");
while (!gets(buffer) && buffer[0] == 0)
{
print_menu();
printf("Enter the movie name: ");
}
printf("Enter the ticket price: ");
scanf("%d", &ticketPrice);
printf("Enter the number of tickets sold: ");
scanf("%d", &ticketsSold);
fseek(db, 0, SEEK_END);
fprintf(db, "%d %d %s
", ticketPrice, ticketsSold, buffer);
fflush(db);
printf("
Record added.");
getch();
}
void display_database(FILE* db, int minRevenue)
{
if (!db)
{
printf("There is no database loaded. Do it choosing the 1 menu item.");
getch();
return;
}
int tmp;
int index = 1;
int ticketPrice = -1;
int ticketsSold = -1;
char* line = new char[256];
rewind(db);
system("cls");
printf("
");
if (minRevenue != -1)
printf("Movies, whose revenue is bigger than %d:
", minRevenue);
printf("---------------------------------------------------------------------------
");
printf(" # | Movie name | Ticket price | Tickets sold
");
printf("---------------------------------------------------------------------------
");
while (fgets(line, 256, db))
{
sscanf(line, "%d %d", &ticketPrice, &ticketsSold);
line = strchr(strchr(line, ' ') + 1, ' ') + 1;
tmp = strlen(line) - 1;
if (line[tmp] == '
')
line[tmp] = 0;
Comments
Leave a comment