Reveiwing the following code please answer the following questions 1. Discuss th
ID: 3797342 • Letter: R
Question
Reveiwing the following code please answer the following questions
1. Discuss the data structures used
2. Discuss the algorithm used to search the list for a particular video and the time complexity of the algorithm
#include <string>
#include <algorithm>
#include <iostream>
class VideoType
{
private:
std::wstring movieTitle;
std::wstring producer;
std::wstring director;
std::wstring company;
int noOfCopies = 0;
//
public:
VideoType(const std::wstring &movieTitle, const std::wstring &producer, const std::wstring &director, const std::wstring &company, int noOfCopies);
//
virtual std::wstring getMovieTitle();
virtual void setMovieTitle(const std::wstring &movieTitle);
virtual std::wstring getProducer();
virtual void setProducer(const std::wstring &producer);
virtual std::wstring getDirector();
virtual void setDirector(const std::wstring &director);
virtual std::wstring getCompany();
virtual void setCompany(const std::wstring &company);
virtual int getNoOfCopies();
virtual void setNoOfCopies(int noOfCopies);
//
/*
* Rent video to customer
*/
virtual bool rentVideo(CustomerType *customer);
/*
* If customer has video, return it back to stock
*/
virtual bool returnVideo(CustomerType *cusomer);
//
/*
* If video is available in stock
*/
virtual bool isAvailableInStore();
//
virtual std::wstring toString() override;
};
VideoType::VideoType(const std::wstring &movieTitle, const std::wstring &producer, const std::wstring &director, const std::wstring &company, int noOfCopies)
{ this->movieTitle = movieTitle;
this->producer = producer;
this->director = director;
this->company = company;
this->noOfCopies = noOfCopies;
}
std::wstring VideoType::getMovieTitle()
{
return movieTitle;
}
void VideoType::setMovieTitle(const std::wstring &movieTitle)
{
this->movieTitle = movieTitle;
}
std::wstring VideoType::getProducer()
{
return producer;
}
void VideoType::setProducer(const std::wstring &producer)
{
this->producer = producer;
}
std::wstring VideoType::getDirector()
{
return director;
}
void VideoType::setDirector(const std::wstring &director)
{
this->director = director;
}
std::wstring VideoType::getCompany()
{
return company;
}
void VideoType::setCompany(const std::wstring &company)
{
this->company = company;
}
int VideoType::getNoOfCopies()
{
return noOfCopies;
}
void VideoType::setNoOfCopies(int noOfCopies)
{
this->noOfCopies = noOfCopies;
}
bool VideoType::rentVideo(CustomerType *customer)
{
if (noOfCopies > 0)
{
customer->addVideo(this);
noOfCopies--;
return true;
}
return false;
}
bool VideoType::returnVideo(CustomerType *cusomer)
{
if (cusomer->hasVideo(this))
{
cusomer->returnVideo(this);
noOfCopies++;
return true;
}
return false;
}
bool VideoType::isAvailableInStore()
{
return noOfCopies > 0? true: false;
}
std::wstring VideoType::toString()
{
return std::wstring(L"Title: ") + movieTitle + std::wstring(L" Producer: ") + producer + std::wstring(L" Director: ") + director + std::wstring(L" Company: ") + company + std::wstring(L" Stock: ") + std::to_wstring(noOfCopies);
}
#include <string>
#include <vector>
#include "stringbuilder.h"
public:
CustomerType::java *import;
class CustomerType
{
private:
std::wstring firstName;
std::wstring lastName;
std::wstring accountNumber;
std::vector<VideoType*> rentedVideos;
//
public:
CustomerType(const std::wstring &firstName, const std::wstring &lastName, const std::wstring &accountNumber);
virtual std::wstring getFirstName();
virtual void setFirstName(const std::wstring &firstName);
virtual std::wstring getLastName();
virtual void setLastName(const std::wstring &lastName);
virtual std::wstring getAccountNumber();
virtual void setAccountNumber(const std::wstring &accountNumber);
//
virtual void addVideo(VideoType *v);
virtual std::wstring toString() override;
virtual void returnVideo(VideoType *video);
/*
* Check if customer has a video
*/
virtual bool hasVideo(VideoType *video);
};
#include <string>
#include <sstream>
class StringBuilder
{
private:
std::wstring privateString;
public:
StringBuilder()
{
}
StringBuilder(const std::wstring &initialString)
{
privateString = initialString;
}
StringBuilder(std::size_t capacity)
{
ensureCapacity(capacity);
}
wchar_t charAt(std::size_t index)
{
return privateString[index];
}
StringBuilder *append(const std::wstring &toAppend)
{
privateString += toAppend;
return this;
}
template<typename T>
StringBuilder *append(const T &toAppend)
{
privateString += toString(toAppend);
return this;
}
StringBuilder *insert(std::size_t position, const std::wstring &toInsert)
{
privateString.insert(position, toInsert);
return this;
}
template<typename T>
StringBuilder *insert(std::size_t position, const T &toInsert)
{
privateString.insert(position, toString(toInsert));
return this;
}
std::wstring toString()
{
return privateString;
}
std::size_t length()
{
return privateString.length();
}
void setLength(std::size_t newLength)
{
privateString.resize(newLength);
}
std::size_t capacity()
{
return privateString.capacity();
}
void ensureCapacity(std::size_t minimumCapacity)
{
privateString.reserve(minimumCapacity);
}
StringBuilder *remove(std::size_t start, std::size_t end)
{
privateString.erase(start, end - start);
return this;
}
StringBuilder *replace(std::size_t start, std::size_t end, const std::wstring &newString)
{
privateString.replace(start, end - start, newString);
return this;
}
private:
template<typename T>
static std::wstring toString(const T &subject)
{
std::wostringstream ss;
ss << subject;
return ss.str();
}
};
CustomerType::CustomerType(const std::wstring &firstName, const std::wstring &lastName, const std::wstring &accountNumber)
{
this->firstName = firstName;
this->lastName = lastName;
this->accountNumber = accountNumber;
}
std::wstring CustomerType::getFirstName()
{
return firstName;
}
void CustomerType::setFirstName(const std::wstring &firstName)
{
this->firstName = firstName;
}
std::wstring CustomerType::getLastName()
{
return lastName;
}
void CustomerType::setLastName(const std::wstring &lastName)
{
this->lastName = lastName;
}
std::wstring CustomerType::getAccountNumber()
{
return accountNumber;
}
void CustomerType::setAccountNumber(const std::wstring &accountNumber)
{
this->accountNumber = accountNumber;
}
void CustomerType::addVideo(VideoType *v)
{
rentedVideos.push_back(v);
}
std::wstring CustomerType::toString()
{
StringBuilder *s = new StringBuilder();
s->append(std::wstring(L"Name: ") + firstName + std::wstring(L" ") + lastName + std::wstring(L" Account Number: ") + accountNumber + std::wstring(L" Rented Videos : "));
std::vector<VideoType*>::const_iterator i = rentedVideos.begin();
while (i != rentedVideos.end())
{
s->append(*i.getMovieTitle());
s->append(L" ");
i++;
}
//
return s->toString();
//
}
void CustomerType::returnVideo(VideoType *video)
{
rentedVideos.remove(video);
}
bool CustomerType::hasVideo(VideoType *video)
{
if (rentedVideos.find(video) != -1)
{
return true;
}
else
{
return false;
}
}
//.h file code:
#include <string>
#include <vector>
#include <iostream>
public:
VideoStore::java *import;
class VideoStore
{
public:
static std::vector<VideoType*> listOfVideos;
static std::vector<CustomerType*> listOfCustomers;
static Scanner *s;
/*
* Read from file videos.txt and create video list
*/
static void createVideoList();
/*
* Read from file customers.txt and create Customer list
*/
static void createCustomerList();
/*
* Prompt user to select a customer
*/
static CustomerType *getCustomer();
/*
* Prompt user to select a video
*/
static VideoType *getVideo();
//
std::vector<VideoType*> VideoStore::listOfVideos;
std::vector<CustomerType*> VideoStore::listOfCustomers;
java::util::Scanner *VideoStore::s = new java::util::Scanner(System::in);
void VideoStore::createVideoList()
{
try
{
BufferedReader *videoFile = nullptr;
FileReader tempVar(L"videos.txt");
videoFile = new BufferedReader(&tempVar);
videoFile->readLine(); // to flush comment line
for (std::wstring line = videoFile->readLine(); (line != L""); line = videoFile->readLine())
{
std::vector<std::wstring> parts = line.split(L" ");
VideoType tempVar2(parts[0], parts[1], parts[2], parts[3], std::stoi(parts[4]));
listOfVideos.push_back(&tempVar2);
}
delete videoFile;
}
catch (const IOException &e)
{
System::err::print(e);
exit(-1);
}
}
void VideoStore::createCustomerList()
{
try
{
BufferedReader *customerFile = nullptr;
FileReader tempVar(L"customers.txt");
customerFile = new BufferedReader(&tempVar);
customerFile->readLine(); // to flush comment line
for (std::wstring line = customerFile->readLine(); (line != L""); line = customerFile->readLine())
{
std::vector<std::wstring> parts = line.split(L" ");
CustomerType tempVar2(parts[0], parts[1], parts[2]);
listOfCustomers.push_back(&tempVar2);
}
delete customerFile;
}
catch (const IOException &e)
{
System::err::print(e);
exit(-1);
}
}
CustomerType *VideoStore::getCustomer()
{
std::wcout << std::wstring(L"Enter Customer Id") << std::endl;
for (int i = 0; i < listOfCustomers.size(); i++)
{
std::wcout << std::wstring(L"Id:") << (i << 1) << std::wstring(L" ") << listOfCustomers[i]->getFirstName() << std::endl;
}
int custId = s->nextInt();
if (custId > 0 && custId <= listOfCustomers.size())
{
return listOfCustomers[custId - 1];
}
else
{
std::wcout << std::wstring(L"Invalid Customer Id") << std::endl;
return nullptr;
}
}
VideoType *VideoStore::getVideo()
{
std::wcout << std::wstring(L"Enter Video Id") << std::endl;
for (int i = 0; i < listOfVideos.size(); i++)
{
std::wcout << std::wstring(L"Id:") << (i << 1) << std::wstring(L" ") << listOfVideos[i]->getMovieTitle() << std::endl;
}
int videoId = s->nextInt();
if (videoId > 0 && videoId <= listOfVideos.size())
{
return listOfVideos[videoId - 1];
}
else
{
std::wcout << std::wstring(L"Invalid Video Id") << std::endl;
return nullptr;
}
}
#include <string>
#include <vector>
#include <iostream>
/*
* Driver function
*/
static void main(std::vector<std::wstring> &a);
/*
* Show menu and ask for user choice
*/
public:
static int displyMenu();
//
//.cpp file code:
void <missing_class_definition>::main(std::vector<std::wstring> &a)
{
createVideoList();
createCustomerList();
//
int choice = displyMenu();
while (choice != 9)
{
if (choice == 1)
{
std::wcout << std::wstring(L"Please type the exact movie Title") << std::endl;
std::wstring title = s::nextLine();
Iterator<VideoType*> iterator = listOfVideos::begin();
bool found = false;
while (iterator->hasNext())
{
VideoType *v = iterator->next();
if (v->getMovieTitle().equals(title))
{
found = true;
std::wcout << std::wstring(L"Yes, we have this video in our store.") << std::endl;
break;
}
iterator++;
}
if (!found)
{
std::wcout << std::wstring(L"Sorry!! we dont have this video in our store") << std::endl;
}
}
else if (choice == 2)
{
CustomerType *cust = getCustomer();
VideoType *video = getVideo();
//
if (cust != nullptr && video != nullptr)
{
if (video->rentVideo(cust))
{
std::wcout << std::wstring(L"Rented successfully") << std::endl;
}
else
{
std::wcout << std::wstring(L"Can not rent. Stock not available") << std::endl;
}
}
}
else if (choice == 3)
{
CustomerType *cust = getCustomer();
VideoType *video = getVideo();
//
if (cust != nullptr && video != nullptr)
{
if (video->returnVideo(cust))
{
std::wcout << std::wstring(L"Returned Successfully") << std::endl;
}
else
{
std::wcout << std::wstring(L"Customer don't have this video") << std::endl;
}
}
}
else if (choice == 4)
{
VideoType *video = getVideo();
if (video != nullptr)
{
if (video->isAvailableInStore())
{
std::wcout << std::wstring(L"Stock available") << std::endl;
}
else
{
std::wcout << std::wstring(L"Stock Unavaiable.") << std::endl;
}
}
}
else if (choice == 5)
{
std::wcout << std::wstring(L"Title of all the videos:") << std::endl;
for (int i = 0; i < listOfVideos->size(); i++)
{
std::wcout << listOfVideos->get(i).getMovieTitle() << std::endl;
std::wcout << std::endl;
}
}
else if (choice == 6)
{
std::wcout << std::wstring(L"Title of all the videos:") << std::endl;
for (int i = 0; i < listOfVideos->size(); i++)
{
std::wcout << listOfVideos->get(i) << std::endl;
std::wcout << std::endl;
}
}
choice = displyMenu();
}
delete s;
}
int <missing_class_definition>::displyMenu()
{
std::wcout << std::wstring(L"Select one of following:") << std::endl;
std::wcout << std::wstring(L"1: To check whether store carries a particular video") << std::endl;
std::wcout << std::wstring(L"2: To Check out a video") << std::endl;
std::wcout << std::wstring(L"3: To Check in a video") << std::endl;
std::wcout << std::wstring(L"4: To Check whether a particular video is in stock.") << std::endl;
std::wcout << std::wstring(L"5: To Print only the titles of all the videos") << std::endl;
std::wcout << std::wstring(L"6: To print a list of all the videos") << std::endl;
std::wcout << std::wstring(L"9: to exit") << std::endl;
int result = s::nextInt();
return result;
}
Explanation / Answer
Stack :-
void VideoStore::createVideoList()
{
try
{
BufferedReader *videoFile = nullptr;
FileReader tempVar(L"videos.txt");
videoFile = new BufferedReader(&tempVar);
videoFile->readLine(); // to flush comment line
for (std::wstring line = videoFile->readLine(); (line != L""); line = videoFile->readLine())
{
std::vector<std::wstring> parts = line.split(L" ");
VideoType tempVar2(parts[0], parts[1], parts[2], parts[3], std::stoi(parts[4]));
listOfVideos.push_back(&tempVar2);
}
delete videoFile;
}
above code declare a object for BufferReader and points to null
created one temp obj which points to video file and then it is assigned to videotype object of BufferedReader
it reads the video or we can say it stores the video in parts an then it uses the push function of stack
Pointer : - everywhere in program it is easily seen pointer and their reference type are used
Searching: - linear search is used to find particular video present in store and same for customer
object oriented programming approach used by defining getter and setter for all declared variable
2. algorithm used to search the list for a particular video and the time complexity of the algorithm
VideoType *VideoStore::getVideo()
{
std::wcout << std::wstring(L"Enter Video Id") << std::endl;
for (int i = 0; i < listOfVideos.size(); i++)
{
std::wcout << std::wstring(L"Id:") << (i << 1) << std::wstring(L" ") << listOfVideos[i]->getMovieTitle() << std::endl;
}
int videoId = s->nextInt();
if (videoId > 0 && videoId <= listOfVideos.size())
{
return listOfVideos[videoId - 1];
}
else
{
std::wcout << std::wstring(L"Invalid Video Id") << std::endl;
return nullptr;
}
}
it shows the list of videos and ask the id of video
it checks if the id persents in the list or not by comparing it videoId > 0 && videoId <= listOfVideos.size()
if id presents between 0 to size of list then it will return the list of video
Complexity :-
for (int i = 0; i < listOfVideos.size(); i++)
{
std::wcout << std::wstring(L"Id:") << (i << 1) << std::wstring(L" ") << listOfVideos[i]->getMovieTitle() << std::endl;
}
takes --- > O(N)
if (videoId > 0 && videoId <= listOfVideos.size())
{
return listOfVideos[videoId - 1];
}
else
{
std::wcout << std::wstring(L"Invalid Video Id") << std::endl;
return nullptr;
}
second step takes O(1) time
total Complexity will be O(N)+O(1) = O(N)
algorithm has O(N) Complexity
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.