Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 #include #include 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 #include #include "stringbuilder.h"

public: CustomerType::java *import;

class CustomerType { private: std::wstring firstName; std::wstring lastName; std::wstring accountNumber; std::vector 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 #include

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 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 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 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::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 #include #include

public: VideoStore::java *import; class VideoStore { public: static std::vector listOfVideos; static std::vector 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 VideoStore::listOfVideos; std::vector 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 parts = line.split(L"\t"); 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 parts = line.split(L"\t"); 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"\t") << 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"\t") << 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 #include #include

/* * Driver function */ static void main(std::vector &a); /* * Show menu and ask for user choice */ public: static int displyMenu(); //

//.cpp file code:

void ::main(std::vector &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 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 ::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; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Students also viewed these Databases questions

Question

Q.No.1 Explain Large scale map ? Q.No.2 Explain small scale map ?

Answered: 1 week ago