MovieType.h
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
#ifndef __MovieType_H__
#define __MovieType_H__
const string EndOfFile = "***";
class MovieType
{
string m_title;
int m_year;
int m_gross;
string m_studio;
string m_stars;
public:
MovieType();
//Purpose:Default Constructor.
//Precondition:None
//Postcondition:None
int CompareKeys(const MovieType& movie2) const;
//Purpose:To compare two movies and determine where it should be inserted.
//Precondition:Add function must be called and executed.
//Postcondition:String compare function is called to execute the comparison, resulting in two possible outcomes:
// 1.A positive value is returned and is added into the list.
// 2.A 0 or a negative value is returned and isn't added into the list.
void Initialize(string title, int year, int gross, string studio, string stars);
//Purpose:Initializes a movie item with data, basically a constructor.
//Precondition:None
//Postcondition:Populated with initial data.
bool ReadOneMovieFromFile(istream& is = cin);
//Purpose:To read one movie from a file at a time.
//Precondition:ReadAllMovies() must be called.
//Postcondition:1.True is returned if one movie was taken from the data file and entered into the list.
// 2.False is returned if the EndOfFile flag was reached.
void Display(ostream&os = cout) const;
//Purpose:Displays one movie from the list.
//Precondition:List array must contain data.
//Postcondition:Data listed to the console screen.
};
int MovieType::CompareKeys(const MovieType& movie2) const
{
return (m_title.compare(movie2.m_title));
}
void MovieType::Initialize(string title, int year, int gross, string studio, string stars)
{
m_title = title;
m_year = year;
m_gross = gross;
m_studio = studio;
m_stars = stars;
}
void MovieType::Display(ostream& os) const
{
if(m_title == EndOfFile)
{
os<<"";
}
else
{
os<<"Title:\t"<<m_title<<endl;
os<<"Year:\t"<<m_year<<endl;
os<<"Gross:\t$ "<<m_gross<<endl;
os<<"Studio:\t"<<m_studio<<endl;
os<<"Stars:\t"<<m_stars<<endl;
os<<endl;
}
}
bool MovieType::ReadOneMovieFromFile(istream& is)
{
int index;
string str = "";
getline(is,m_title);
if(m_title == EndOfFile)
{
return false;
}
else
{
is>>m_year;
is>>m_gross;
is>>m_studio;
getline(is,str);
index = (int)str.find_first_of(",",0);
if(index > 0)
m_stars = str.substr(index+2,(str.length() - (index+1)));
else
m_stars = "None";
return true;
}
}
MovieType::MovieType()
:m_title(""), m_year(-1), m_gross(-1), m_studio(""), m_stars("")
{}
#endif
SortedMovieList.h
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include "MovieType.h"
using namespace std;
typedef MovieType MovieListItemType;
const int MAX = 20;
int counter = 0;
class SortedMovieList
{
public:
SortedMovieList();
//Purpose:Default Constructor.
//Precondition:None
//Postcondition:None
void listAdd(const MovieListItemType& newItem, bool& success);
//Purpose:To insert a movie in the array.
//Precondition:ReadAllMovies function executes.
//Postcondition:listInsert function is called to find the correct position for the movie to be inserted.
void listInsert(int index, MovieListItemType newItem, bool& success);
//Purpose:To insert a movie in the correct position in the array.
//Precondition:listAdd function is called.
//Postcondition:Item is inserted into the correct place in the list.
void listSubtract(const MovieListItemType& itemToRemove, bool& success);
//Purpose:To remove a movie from the array.
//Precondition:Remove choice is called from the menu.
//Postcondition:The movie is removed from the list
void listRemove(int index, bool& success);
//Purpose:To remove a movie from the array.
//Precondition:Remove choice is called from the menu.
//Postcondition:The movie is removed from the list.
void ReadAllMovies();
//Purpose:Reads all of the movies in an input file.
//Precondition:ReadOneMovieFromFile executes correctly.
//Postcondition:The list is populated with all of the movies from the file.
void DisplayList() const;
//Purpose:Displays the entire list of movies.
//Precondition:Array is populated from ReadAllMovies().
//Postcondition:The list is displayed to the console.
void Search(const MovieListItemType& itemToSearch, bool& success);
//Purpose:To search through the list for the correct movie
//Precondition:Search called from the menu.
//Postcondition:1.If the movie is found the index where it was found is returned.
// 2.If the movie is not found the user will be notified.
private:
MovieListItemType items[MAX];
int size;
};
SortedMovieList::SortedMovieList()
:size(0)
{}
void SortedMovieList::listAdd(const MovieListItemType& newItem, bool& success)
{
int i = 0;
if(size == 0)
{
listInsert(i,newItem,success);
return;
}
while((i < size) && (newItem.CompareKeys(items[i]) == 1))
{
i++;
}
listInsert(i,newItem,success);
}
void SortedMovieList::listInsert(int index, MovieListItemType newItem, bool& success)
{
success = ((index >= 0) && (index <= size) && (size < MAX));
if(success)
{
for(int pos = size; pos >= index; pos--)
{
items[pos+1] = items[pos];
}
items[index] = newItem;
size++;
counter++;
}
}
void SortedMovieList::listSubtract(const MovieListItemType& itemToRemove, bool& success)
{
int i = 0;
if(size == 0)
{
cout<<"List is empty"<<endl;
}
while((i < size) && (itemToRemove.CompareKeys(items[i]) == 1))
{
i++;
}
listRemove(i,success);
}
void SortedMovieList::listRemove(int index, bool& success)
{
success = ((index >= 0) && (index < size));
if(success)
{
for(int fromPosition = index + 1; fromPosition < size; fromPosition++)
items[fromPosition-1] = items[fromPosition];
size--;
counter--;
}
}
void SortedMovieList::ReadAllMovies()
{
MovieListItemType newItem;
ifstream Input;
int i = 0;
bool success;
string inputFile = "";
Input.clear();
cout<<"Welcome, please enter the name of your Input file: ";
cin>>inputFile;
cin.ignore(50,'\n');
Input.open(inputFile.c_str());
while(newItem.ReadOneMovieFromFile(Input))
{
this->listAdd(newItem,success);
}
Input.close();
}
void SortedMovieList::DisplayList() const
{
int i;
for(i = 0; i < size; i++)
{
items[i].Display();
if(i%3 == 0) //Every 3 movies, pause.
{
cout<<"Press 'ENTER' if you wish to display more movies";
cin.get();
cout<<endl;
}
}
}
void SortedMovieList::Search(const MovieListItemType& itemToSearch, bool& success)
{
int i;
for(i = 0; i < size; i++)
{
if(itemToSearch.CompareKeys(items[i]) == 0)
{
cout<<endl<<"The Movie has been found at index "<<i<<"."<<endl;//add ...
items[i].Display();
break; //If the movie is found, go no further.
}
if(itemToSearch.CompareKeys(items[i]) == -1)
{
cout<<endl<<"The movie you wished to search for has not been found."<<endl;
break; //If you have gone past the point
//where the movie should be stored in the list, go no further.
}
}
if(i == size)
cout<<endl<<"The movie you wished to search for has not been found."<<endl;
}
Main.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include "MovieType.h"
#include "SortedMovieList.h"
using namespace std;
void clearScreen();
//Purpose:To clear the screen.
//Precondition:None
//Postcondition:Screen is cleared.
char displayMenuAndGetSelection();
//Purpose:To display the menu and get the selection for searching.
//Precondition:None
//Postcondition:Search choice is entered and appropriate function is called.
void addPerson(SortedMovieList& movieList);
//Purpose:To add a person to the list.
//Precondition:Choice must be selected from the menu.
//Postcondition:List is searched and results are output to the console.
void removePerson(SortedMovieList& movieList);
//Purpose:To remove a person from the list.
//Precondition:Choice must be selected from the menu.
//Postcondition:List is searched and results are output to the console.
void Pause();
//Purpose:To pause the program and allow the user a break.
//Precondition:None
//Postcondition:None
void searchList(SortedMovieList& movieList);
//Purpose:To call the search function and find a movie in the list.
//Precondition:Choice must be selected from the menu.
//Postcondition:Search function is called.
void quitProgram();
//Purpose:To inform the user the program has been terminated.
//Precondition:None
//Postcondition:Program terminated.
void main()
{
char menuChoice = ' ';
bool done = false;
SortedMovieList movieList;
movieList.ReadAllMovies();
while(!done)
{
menuChoice = displayMenuAndGetSelection();
clearScreen();
switch(menuChoice)
{
case '1':
movieList.DisplayList();
Pause();
done = false;
break;
case '2':
searchList(movieList);
Pause();
done = false;
break;
case '3':
removePerson(movieList);
Pause();
done = false;
break;
case '4':
addPerson(movieList);
Pause();
done = false;
break;
case '5':
cout<<"There are currently "<<counter<<" movies in the list."<<endl;
Pause();
done = false;
break;
case '6':
quitProgram();
done = true;
break;
default:
cout<<"Incorrect choice selected"<<endl;
Pause();
done = false;
break;
}
}
}
void clearScreen()
{
system("cls");
}
char displayMenuAndGetSelection()
{
char choice;
clearScreen();
cout<<endl<<endl<<endl;
cout<<"'1' -- View the list."<<endl<<endl;
cout<<"'2' -- Search the list."<<endl<<endl;
cout<<"'3' -- Delete a movie from the list."<<endl<<endl;
cout<<"'4' -- Add a movie to the list."<<endl<<endl;
cout<<"'5' -- Count the number of movies in the list."<<endl<<endl;
cout<<"'6' -- Quit the program."<<endl<<endl;
cin>>choice;
cin.ignore(50,'\n');
return choice;
}
void addPerson(SortedMovieList& movieList)
{
MovieType movie;
string title, studio, stars;
int year, gross;
cout<<"Enter title: ";
getline(cin,title,'\n');
cout<<"Enter year: ";
cin>>year;
cout<<"Enter gross: ";
cin>>gross;
cout<<"Enter studio: ";
cin>>studio;
cout<<"Enter stars: ";
cin>>stars;
movie.Initialize(title,year,gross,studio,stars);
bool success;
movieList.listAdd(movie,success);
cout<<endl<<"The movie has been added to the list."<<endl;
}
void removePerson(SortedMovieList& movieList)
{
MovieType movie;
string title;
cout<<"Enter the title of the movie you wish to remove: ";
getline(cin,title,'\n');
movie.Initialize(title,0,0,"","");
bool success;
movieList.listSubtract(movie,success);
cout<<endl<<"The movie has been removed from the list."<<endl;
}
void searchList(SortedMovieList& movieList)
{
MovieType movie;
string title;
cout<<"Enter the title of the movie you wish to search for: ";
getline(cin,title,'\n');
bool success;
movie.Initialize(title,0,0,"","");
movieList.Search(movie,success);
cout<<endl<<"The search has been conducted."<<endl;
}
void Pause()
{
cout<<endl<<"Press 'ENTER' to continue..."<<endl;
cin.get();
}
void quitProgram()
{
cout<<"Program terminated, good bye!"<<endl;
cout<<endl;
}