Problem 2. Movie Database System Write a C++ program that uses the following str
ID: 661586 • Letter: P
Question
Problem 2. Movie Database System
Write a C++ program that uses the following structures to store the following data about a Customer:
Name
Address
City, State, and ZIP
Telephone Number
Array of rented movies (Nested Structures)!!!
And the following data about Movie
Title
Year of issue
Duration
The structures should be used to access customer records in a file. The program should have a menu that lets the user perform the following operations:
Create new Customer record
Enter the new record into the file.
Search for a particular customer
Explanation / Answer
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
/////Declarations////////////////////////////////////////////////
const int Max_Movie = 500;
const int Movie = 500;
const char FILE_PATH[] = "C:\entry_file.txt";
typedef int INDEX;
typedef int TELEPHONE;
typedef string NAME;
typedef array MOVIELIST[];
typedef string ADDRESS;
typedef string ADDRESS2; //CITY,PIN, ZIP
typedef string TITLE;
typedef string YEAR;
typedef DOUBLE DURATION;
int entryCount = -1; //-1 means the list is empty
fstream entryFile;
class CustomerEntry
{
public:
CustomerEntry (NAME name, TELEPHONE telephone, ADDRESS address, ADDRESS2 address2), MOVIELIST[] movielist: name(name), telephone (telephone), ADDRESS (address), ADDRESS2 (address2), MOVIELIST[] (movielist) {}
void operator = (CustomerEntry *entry)
{
name = entry->name;
telephone = entry-> telephone;
address = entry-> address;
address2 = entry-> address2;
movielist = entry-> movielist;
}
NAME name;
TELEPHONE telephone;
Address address;
Address2 address2;
}*entryList[MAX_Movie];
class MovieEntry
{
public:
MovieEntry (TITLE title, DURATION duration, YEAR year: title (title), duration (duration), year (year) {}
void operator = (MovieEntry *entry2)
{
title = entry2->title;
duration = entry2-> duration;
year = entry2-> year;
}
TITLE title;
DURATION duration;
YEAR year;
}*entry2List[Movie];
/////Function prototypes////////////////////////////////////////
INDEX GetCustomerIdx(); //Gets number of index from user
TELEPHONE GetCustomertelephone();
Address GetCustomerAddress();
Address2 GetCustomerAddress2();
NAME GetCustomerName(); //Gets string of name from user
MOVIELIST GetCustomerMovieList();
DURATION GetMovieDuration();
TITLE GetMovieTitle();
YEAR GetMovieYear();
void DeleteFromTheList(INDEX idx); //Deletes an item with index of idx
void Delete(); //Called by DeleteFromTheList(INDEX)
void ChangeTelephone(); //Changes the mark field of a record
void SetNewMovieList(INDEX idx, movielist[],);//ChangeTelephone()
void SetNewAddress(INDEX idx, address);
void AddCustomerEntry(); //Adds new item to list
void AddMovieEntry();
void AddToList(NAME name, MARK mark); //Called by AddEntry()
void SortList(); //Does a buble sort on list
void ShowName(); //Shows all names with the same mark
void ShowList(); //Shows all the items in the list
int GetUserChoice(); //Gets numbet of option from user
void LoadDataFromFile(); //Loads data from a file
void SaveDataToFile(); //Saves data to a file
/////////////////////////////////////////////////////////////
INDEX GetCustomerIdx()
{
cout << "Enter index: ";
INDEX idx;
cin >> idx;
return idx;
}
NAME GetCustomerName()
{
cout << "Enter name: ";
NAME name;
cin >> name;
return name;
}
TELEPHONE GetCustomerTelephone()
{
cout << "Enter telephone: ";
TELEPHONE telephone;
cin >> telephone;
return telephone;
}
ADDRESS GetCustomerAddress()
{
cout << "Enter Address: ";
ADDRESS address;
cin >> address;
return address;
}
ADDRESS2 GetCustomerAddress2()
{
cout << "Enter City, pin and zip: ";
ADDRESS address2;
cin >> address2;
return address2;
}
MOVIELIST GetCustomerMovieList()
{
cout << "Enter Movielist: ";
MOVIELIST movielist;
cin >>movielist;
return movielist;
}
DURATION GetMovieDuration()
{ cout << "Enter MovieDuration: ";
DURATION duration;
cin >> duration;
return duration;
}
TITLE GetMovieTitle()
{
cout << "Enter Movie Tilte: ";
TITLE title;
cin >> title;
return title;
}
YEAR GetMovieYear()
{
cout << "Enter Movie Year: ";
YEAR title;
cin >> year;
return year;
}
void DeleteFromTheList(INDEX idx)
{
if(entryCount != idx)
for(int i = idx; i < entryCount; i++) {
entryList[i]->name = entryList[i+1]->name;
entryList[i]->telephone = entryList[i+1]-> telephone;
entryList[i]->address = entryList[i+1]-> address;
entryList[i]->address2 = entryList[i+1]-> address2;
entryList[i]->movielist = entryList[i+1]-> movielist;
}
delete entryList[entryCount];
}
void Delete()
{
if(entryCount != -1){
DeleteFromTheList(GetCustomerIdx());
entryCount--;
}
}
void SetNewTelephone (INDEX idx, TELEPHONE telephone)
{
entryList[idx]->telephone = telephone;
}
void ChangeTelephone ()
{
SetNewMark(GetCustomerIdx(), GetCustomerTelephone());
}
void AddToCustomerList(NAME name, TELEPHONE telephone)
{
entryList[entryCount] = new CustomerEntry(name, telephone, address,address2,movielist[]);
}
void AddToMovieList(TITLE title, DURATION duration,YEAR year)
{
entry2List[entryCount2] = new MovieEntry(title,duration, year);
}
void SortList()
{
for(int i = 0 ; i < entryCount;i++){
for(int j = 0 ; j < entryCount;j++)
{
if(entryList[j]->name.compare(entryList[j+1]->name ) == 1)
{
CustomerEntry temp = *entryList[j+1];
*entryList[j+1] = entryList[j];
*entryList[j] = temp;
}
}
}
}
void AddEntry()
{
entryCount++;
NAME name = GetCustomerName();
AddToList(name , GetCustomerTelephone(),GetCustomerAddress(),GetCustomerAddress2(),GetCustomerMovieList());
SortList();
}
void ShowTelephone()
{
NAME name = GetCustomerName();
for(int i = 0; i <= entryCount; ++i ) if(entryList[i]->name == name) cout << i << " " << entryList[i]->telephone << endl;
}
void ShowName()
{
MARK mark = GetCustomerMark();
for(int i = 0; i <= entryCount; ++i ) if(entryList[i]->mark == mark) cout << i << " " << entryList[i]->name <<endl;
}
void ShowList()
{
for(int i = 0; i <= entryCount; ++i ) cout << i << " " << entryList[i]->name << " " << entryList[i]->telephone << " " << entryList[i]->address<< " " << entryList[i]->address2<<" " << entryList[i]->movielist[] << endl;
}
void ShowMovieList()
{
for(int i = 0; i <= entry2Count; ++i ) cout << i << " " << entry2List[i]->title << " " << entry2List[i]->duration << " " << entry2List[i]->year<< endl;
}
int GetUserChoice()
{
int choice;
cout << "Enter the option's number and press enter: ";
cin >> choice;
return choice;
}
void LoadDataFromFile()
{
entryFile.open(FILE_PATH,ios_base::in);
if(entryFile.is_open()){
cout << "File opened." << endl;
char temp[100];
for(entryCount = 0; entryFile >> temp; entryCount++)
{
entryList[entryCount] = new CustomerEntry(temp, 0);
entryFile >> entryList[entryCount]->telephone;
entryFile >> entryList[entryCount]->address;
entryFile >> entryList[entryCount]-> address2;
entryFile >> entryList[entryCount]->movielist[];
}
entryCount--;
entryFile.close();
entryFile.clear();
}
else{
entryFile.clear();
cout << "File not found in " << FILE_PATH << endl;
}
}
void SaveDataToFile()
{
entryFile.open(FILE_PATH,ios_base::out);
if(entryFile.is_open()){
if(!entryFile.good())
{
cout << "Error writing file." << endl;
}
else
for(int i =0 ;i<=entryCount;i++)
{
entryFile << entryList[i]->name<<endl;
entryFile << entryList[i]->telephone<< endl;
entryFile << entryList[i]->address<< endl;
entryFile << entryList[i]->address2<< endl;
entryFile << entryList[i]->movielist<< endl;
}
entryFile.close();
}
}
int main()
{
LoadDataFromFile();
int userChoice;
do{
cout <<"1: Show List 2: Add Entry 3: Change Telephone 4: Delete 5: Search Name 6: Search Address 7: Save and Exit ";
cout << "Current number of records: " << entryCount + 1<< endl;
userChoice = GetUserChoice();
switch (userChoice) {
case 1: ShowCustomerList(); break;
case 2: AddEntry(); break;
case 3: ChangeAddress(); break;
case 4: Delete(); break;
case 5: ShowTelephone(); break;
case 6: ShowName(); break;
case 7:SortList(); break;
case 8: ShowMovieList(); break;
case 9: break;
}
}while(userChoice != 9);
SaveDataToFile();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.