Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

//Inventory Class: //Inventory.h #pragma once #include \"VideoItem.h\" #include

ID: 3631806 • Letter: #

Question

//Inventory Class:
//Inventory.h
#pragma once
#include "VideoItem.h"
#include <vector>
using namespace std;

enum SortStatus {unsorted,sortedByName,sortedByCode};


class Inventory
{
public:
Inventory(){sortStatus=unsorted;}
void addNewItem(const VideoItem & vi);
//void ADD_RENTER(const Renter & ri);
unsigned int size() { return vectorOfVideoItem.size();}
//unsigned int code() { return vectorOfVideoItem.code();}
VideoItem getItem(int i);
VideoItem & getItemByCode(int c);
void sortByCode();
void sortByName();
void clear(){sortStatus=unsorted;vectorOfVideoItem.clear();}
SortStatus getSortStatus(){return sortStatus;}
void setSortStatus(SortStatus ss){sortStatus=ss;}
bool isUsedCode(int c);
private:
VideoItem & Inventory::getItemByCodeInRange(int c,int startIndex, int endIndex);
SortStatus sortStatus;
vector<VideoItem> vectorOfVideoItem;
};

//Inventory.cpp
#include "Inventory.h"
#include <algorithm>

void Inventory::addNewItem(const VideoItem & vi)
{
vectorOfVideoItem.push_back(vi);
sortStatus=unsorted;
}

VideoItem Inventory::getItem(int i)
{
return vectorOfVideoItem[i];
}


bool Inventory::isUsedCode(int c)
{
VideoItem & refGi = getItemByCode(c);
if(refGi.getName()=="")
{
return false;
}
else
{
return true;
}
}



VideoItem & Inventory::getItemByCode(int c)
{
sortByCode();
return getItemByCodeInRange(c,0,vectorOfVideoItem.size()-1);
}


VideoItem & Inventory::getItemByCodeInRange(int c,int startIndex, int endIndex)
{
int middle=(startIndex+endIndex)/2;
if(endIndex==-1)
{
VideoItem * pNullItem = new VideoItem;
return *pNullItem;
}
if(vectorOfVideoItem[middle].getCode()==c)
{
return vectorOfVideoItem[middle];
}
else if(startIndex==endIndex)
{
VideoItem * pNullItem = new VideoItem;
return *pNullItem;
}
else if(startIndex==middle)
{
return getItemByCodeInRange(c,endIndex,endIndex);
}
else if(c<vectorOfVideoItem[middle].getCode())
{
return getItemByCodeInRange(c,startIndex,middle);
}
else
{
return getItemByCodeInRange(c,middle,endIndex);
}
}


void Inventory::sortByCode()
{
if(sortStatus!=sortedByCode)
{
sort(vectorOfVideoItem.begin(),vectorOfVideoItem.end());
sortStatus=sortedByCode;
}
}


void Inventory::sortByName()
{
if(sortStatus!=sortedByName)
{
sort(vectorOfVideoItem.begin(),vectorOfVideoItem.end(),compareVideoItemsByName);
sortStatus=sortedByName;
}
}


//*****************************************************//
//VideoItem class
//VideoItem.h
#pragma once
#include <string>
using namespace std;

class VideoItem
{
public:
VideoItem();
VideoItem(const VideoItem & vi);
VideoItem(string n,int c,int q);
~VideoItem();
string ToString();
void FromString(string s);
static string HeaderString();
void randomize();
string getName() {return name;}
void setName(string s){name=s;}
int getCode(){return code;}
int getQuantityStoreOwn() {return quantityStoreOwn;}
void setQuantityStoreOwn(int qso) {quantityStoreOwn=qso;}

int getQuantitySold() { return quantitySold;}
void setQuantitySold(int qs) {quantitySold = qs;}
int getQuantityRented() {return quantityRented;}
void setQuantityRented(int qr) {quantityRented = qr;}


bool operator < (const VideoItem & rhs);


bool isLessThan(const VideoItem & rhs);


private:
string name;
int code;
int quantityStoreOwn;
int quantitySold;
int quantityRented;

};


bool compareVideoItemsByName(VideoItem & gi1,VideoItem & gi2);

//VideoItem.cpp
#include "VideoItem.h"
#include "misc.h"

VideoItem::VideoItem()
{
name="";
code=0;
quantityStoreOwn=0;
quantitySold=0;
quantityRented=0;


}



VideoItem::VideoItem(const VideoItem & vi)
{
name=vi.name;
code=vi.code;
quantityStoreOwn=vi.quantityStoreOwn;
quantitySold=vi.quantitySold;
quantityRented=vi.quantityRented;


}



VideoItem::VideoItem(string n,int c,int q)
{
name=n;
code=c;
quantityStoreOwn=q;
quantitySold=0;
quantityRented=0;



}


bool VideoItem::operator < (const VideoItem & rhs)
{
bool b =(code<rhs.code);
return b;
}

bool VideoItem::isLessThan(const VideoItem & rhs)
{
bool b =(code<rhs.code);
return b;
}



void VideoItem::randomize()
{
name=randString(3);
code=randInt(1,99999);
quantityStoreOwn=randInt(10,1000);
quantitySold=randInt(0,1000);
quantityRented=randInt(0,quantityStoreOwn + 1);


}

void VideoItem::FromString(string s)
{
int startAt=0;
int fieldWidth;

fieldWidth=15;
name=s.substr(startAt,fieldWidth);
startAt+=fieldWidth;

fieldWidth=10;
code = stringToInt(s.substr(startAt,fieldWidth));
startAt+=fieldWidth;

fieldWidth=8;
quantityStoreOwn = stringToInt(s.substr(startAt,fieldWidth));
startAt+=fieldWidth;

fieldWidth=8;
quantitySold = stringToInt(s.substr(startAt,fieldWidth));
startAt+=fieldWidth;

fieldWidth=6;
quantityRented = stringToInt(s.substr(startAt,fieldWidth));
startAt+=fieldWidth;



}



string VideoItem::ToString()
{
string result="";
result+=padRight(name,' ',15);
result+=" " + padLeft(intToString(code),'0',12);
result+=padLeft(intToString(quantityStoreOwn),' ',10);
result+=padLeft(intToString(quantitySold),' ',10);
result+=padLeft(intToString(quantityRented),' ',10);
int avail = quantityStoreOwn - quantityRented;
result+=padLeft(intToString(avail),' ',15);


return result;
}


string VideoItem::HeaderString()
{
char fill='-';
string result="";
result+=padRight("Item Name",fill,15);
result+=padLeft("Code",fill,12);
result+=padLeft("In Store",fill,10);
result+=padLeft("Sold",fill,10);
result+=padLeft("Rented",fill,10);
result+=padLeft("ForRent/Sale",fill,15);


return result;
}


VideoItem::~VideoItem()
{
}


bool compareVideoItemsByName(VideoItem & gi1, VideoItem & gi2)
{
return gi1.getName()<gi2.getName();
}
**********************************************************************//
//VideoStore class
//VideoStore.h
#pragma once
#include "Inventory.h"
using namespace std;

class VideoStore
{

public:
VideoStore(){load("VideoStore.txt");}
~VideoStore(){save("VideoStore.txt");}
void run();
void rebuyVideoItem();
void saleOfVideoItem();
void addNewVideoItem();
void rentalOfVideoItem();
void ADD_RENTER();
void inventoryReport();
void inventoryReportByCode();
void inventoryReportByName();
void inventoryReportByRenterID();
void generateRandItems(int num);
void help();
void save();
void load();
void save(string fileName);
void load(string fileName);


private:
Inventory inventory;
};

//VideoStore.cpp
#include "VideoStore.h"
#include "Renter.h"
#include "misc.h"
#include <iostream>
#include <fstream>
using namespace std;


void VideoStore::run()
{
help();
string command="";
while((command!="QUIT")&&(command!="99"))
{
cout<<"Enter a command >";
cin>>command;
stringToUpper(command);
if((command=="BUY")||(command=="1"))//buy new videeo
{
addNewVideoItem();
}
else if((command=="INVENTORYREPORTBYNAME")||(command=="31"))//report by name
{
inventoryReportByName();
}
else if((command=="INVENTORYREPORTBYCODE")||(command=="32"))//report by code
{
inventoryReportByCode();
}
else if((command=="REBUY")||(command=="2"))//rebuy an existing item
{
rebuyVideoItem();
}
else if((command=="RENT")||(command=="3"))//rental of video
{
rentalOfVideoItem();
}
else if((command=="SELL")||(command=="4"))//sale of video
{
saleOfVideoItem();
}
else if((command== "NEW RENTER")||(command=="20"))//add new renter
{
ADD_RENTER();
}

else if((command=="TEST")||(command=="71"))//random test
{
generateRandItems(10);
inventoryReportByCode();
}
//else if ((command=="REPORT2")||(command=="25"))//print REPORT2
//{
//inventoryReportByRenterID();
//}

else if((command=="QUIT")||(command=="99"))
{
cout <<"Goodbye"<<endl;
}
else if ((command=="HELP")||(command=="0"))
{
help();
}
else if ((command=="SAVE")||(command=="41"))
{
save();
}
else if ((command=="LOAD")||(command=="42"))
{
load();
}
else
{
cout<<"Unrecognized command."<<endl;
}
}
}

void VideoStore::help()
{
cout<<"Welcome to C++ VideoStore"<<endl;
cout<<"*---------------------------------------*"<<endl;
cout<<"*---------------------------------------*"<<endl;
cout<<"COMMANDS ARE "<<endl;
cout<<"----------------------"<<endl;
cout<<" 0) HELP"<<endl;

cout<<endl;
cout<<" 1) BUY"<<endl;
cout<<" 2) REBUY"<<endl;
cout<<" 3) RENT"<<endl;
cout<<" 4) SELL"<<endl;

cout<<endl;
cout<<"20) NEW RENTER"<<endl;

cout<<endl;
cout<<"25) INVENTORYREPORTBYRENTERID"<<endl;


cout<<endl;
cout<<"31) INVENTORYREPORTBYNAME"<<endl;
cout<<"32) INVENTORYREPORTBYCODE"<<endl;

cout<<endl;
cout<<"41) SAVE"<<endl;
cout<<"42) LOAD"<<endl;

cout<<endl;
cout<<"71) TEST"<<endl;


cout<<endl;
cout<<"99) QUIT"<<endl;
cout<<endl;
}


void VideoStore::save()
{
string fileName;
cout<<"File name >";
cin >> fileName;
save(fileName);
}


void VideoStore::save(string fileName)
{
inventory.sortByCode();
ofstream fout;
fout.open(fileName.c_str());
for(unsigned int i=0;i<inventory.size();i++)
{
fout<< inventory.getItem(i).ToString()<<endl;
}
cout<<"File saved to "<<fileName<<endl;
}

void VideoStore::load()
{
string fileName;
cout<<"File name >";
cin >> fileName;
load(fileName);
}

void VideoStore::load(string fileName)
{
ifstream fin;
fin.open(fileName.c_str());
if(!fin.fail())
{
string inputLine;
VideoItem gi;
inventory.clear();
do
{
getline(fin,inputLine);
if(!fin.eof())
{
gi.FromString(inputLine);
inventory.addNewItem(gi);
}
}
while(!fin.eof());
inventory.setSortStatus(sortedByCode);
}
}

void VideoStore::rebuyVideoItem()//rebuy video
{
int c;
int q;

cout<<"Enter Code >";
cin >> c;

VideoItem & refVi = inventory.getItemByCode(c);
if(refVi.getName()=="")
{
cout<<"Error: "<<c<<" is an invalid Video Item Code."<<endl;
}
else
{
cout<<"Quantity being rebought >";
cin >> q;
refVi.setQuantityStoreOwn(refVi.getQuantityStoreOwn() + q);
}
}

void VideoStore::saleOfVideoItem()//sell video
{
int c;
int qS;

cout<<"Please Enter Video Code >";
cin >> c;

VideoItem & refVi = inventory.getItemByCode(c);
if(refVi.getName()=="")
{
cout<<"Error: "<<c<<" is an invalid Video Item Code."<<endl;
}


else
{
cout<<"Quantity being sold >";
cin >> qS;

refVi.setQuantityStoreOwn(refVi.getQuantityStoreOwn() - qS);
refVi.setQuantitySold(refVi.getQuantitySold() + qS);

}
}

void VideoStore::rentalOfVideoItem()//rent video
{
int c;

cout<<"Please Enter Code >";
cin >> c;

VideoItem & refVi = inventory.getItemByCode(c);
if(refVi.getName()=="")
{
cout<<"Error: "<<c<<" is an invalid Video Item Code."<<endl;
}

if(refVi.getQuantityStoreOwn() - refVi.getQuantityRented() > 0)
{
refVi.setQuantityRented (refVi.getQuantityRented() + 1);
}
else
{
cout<<"The video is not available. >"<<endl;

}
}

void VideoStore::addNewVideoItem()//buy new video
{
string n;
int c;
int q;//quantity

cout<<"Enter Video Name >";
cin >> n;

cout<<"Enter the Code >";
cin >> c;

while(inventory.isUsedCode(c))
{
cout<<"Warning: "<< c <<" code is already in use."<<endl;
cout<<"Enter different Code >";
cin >> c;
}

cout<<"quantity being bought >";
cin >> q;

VideoItem vi(n,c,q);
inventory.addNewItem(vi);

}

void VideoStore::ADD_RENTER()//add new renter
{
string n; //name
string p; //phone number
string a; //address
int ID; //id number

cout<<"Please Enter Your Name> "<<endl;
cin >> n;


cout<<"Your phone number> "<<endl;
cin >> p;


cout<<"Your address> "<<endl;
cin >> a;


cout<<"Enter a six-digit number for your ID> "<<endl;
cin >> ID;

Renter ri(n,p,a,ID);
//VideoStore.ADD_RENTER(ri);


}

void VideoStore::inventoryReport()
{
cout<<VideoItem::HeaderString()<<endl;
for(unsigned int i=0;i<inventory.size();i++)
{
cout<< inventory.getItem(i).ToString()<<endl;
}
}

void VideoStore::inventoryReportByCode()
{
inventory.sortByCode();
inventoryReport();
}


void VideoStore::inventoryReportByName()
{
inventory.sortByName();
inventoryReport();
}

void VideoStore::generateRandItems(int num)
{
VideoItem vi;
for(int i=0;i<num;i++)
{
vi.randomize();
while(inventory.isUsedCode(vi.getCode()))
{
vi.randomize();
}
inventory.addNewItem(vi);
}
}
***************************************************************************
//misc class
//misc.h

#pragma once
#include <string>
using namespace std;

void stringToLower(string & s);
void stringToUpper(string & s);

string padLeft(string s, char fill, unsigned int size);
string padRight(string s,char fill, unsigned int size);

string intToString(int x);
int stringToInt(string s);

string intToDollarString(int x);
int dollarStringToInt(string s);

string randString(int numOfChars);
int randInt(int lower,int upper);

//misc.cpp

#include "misc.h"

void stringToLower(string & s)
{
for(unsigned int i=0;i<s.length();i++)
{
s[i]=tolower(s[i]);
}
}

void stringToUpper(string & s)
{
for(unsigned int i=0;i<s.length();i++)
{
s[i]=toupper(s[i]);
}
}


string padLeft(string s,char fill, unsigned int size)
{
while(s.length()<size)
{
s= fill + s;
}
return s;
}

string padRight(string s,char fill, unsigned int size)
{
while(s.length()<size)
{
s= s + fill;
}
return s;
}


string intToString(int x)
{
string result;
char temp[256];
_itoa_s(x,temp,255,10);
result=temp;
return result;
}

int stringToInt(string s)
{
return atoi(s.c_str());
}

int dollarStringToInt(string s)
{
string s1 = s.erase(s.length()-3,1);
return stringToInt(s1);

}




string intToDollarString(int x)
{
string result=intToString(x);
result=padLeft(result,'0',3);
result.insert(result.length()-2,".");
return result;
}



string randString(int numOfChars)
{
string result;
for(int i=0;i<numOfChars;i++)
{
result+='A'+rand()%26;
}
return result;
}

int randInt(int lower,int upper)
{
if(upper-lower<RAND_MAX)
{
return (rand()%(upper-lower))+lower;
}
else
{
int r=rand()*RAND_MAX+rand();
return (r % (upper-lower))+lower;
}
}



//***********************************************************************************//
//Person class
//Person.h



#pragma once
#include <string>
using namespace std;

class Person
{
public:
Person();
Person(const Person & pi);
Person(string n, string p, string a);
~Person();

string getName() {return name;}
void setName (string n) {name =n;}

string getphoneNumber() {return phoneNumber;}
void setphoneNumber(string p) {phoneNumber =p;}

string getaddress() {return address;}
void setaddress (string a) {address = a;}







private:
string name;
string phoneNumber;
string address;



};
//*************************************************//
//Renter class
//Renter.h
//****************************************************//


#pragma once
#include <string>
#include <vector>
#include "Inventory.h"
#include "Person.h"
#include "VideoItem.h"
using namespace std;

class Renter : public Person
{
public:

Renter(string n, string p, string a, int ID);

int getIDNumber() {return IDNumber;}
void setIDNumber(int ID) { IDNumber = ID;}

//unsigned int size() {return VectorOfCodesOfVideosRentedOut.size();}

~Renter();


private:

int IDNumber;

//vector<int>VectorOfCodesOfVideosRentedOut; //vector of integers of videos codes checked out



};

//************************************************************************//
//main.cpp
#include "VideoStore.h"
using namespace std;


int main()
{
VideoStore myStore;
myStore.run();
return 0;
}

Explanation / Answer

#include // #include #include //Library fro File I/O using std::string; class Video { private: //Data Members string title; string barcode; string category; long runtime; short copies; public: //Member Functions void setTitle(string t); string getTitle(); void setBarcode(string b); string getBarcode(); void setCategory(string c); string getCategory(); void setRuntime(long r); long getRuntime(); void setCopies(short c); short getCopies(); void AddVideoRec(string t,string b,string cat,long r,short c); void DeleteVidoeRec(); void ModifyVideoRec(); void PrintVideoRec(); void FindVideoRec(); void IncVideoCount(); void DecVideoCount(); void RentVideo(); void RetVideo(); }; void Video::setTitle(string t) { title = t; } string Video::getTitle() { return title; } void Video::setCategory(string c) { category = c; } string Video::getCategory() { return category; } void Video::setBarcode(string b) { barcode = b; } string Video::getBarcode() { return barcode; } void Video::setRuntime(long r) { runtime = r; } long Video::getRuntime() { return runtime; } void Video::setCopies(short c) { copies = c; } short Video::getCopies() { return copies; } void Video::AddVideoRec(string t,string b, string cat,long r, short c) { ofstream myFile; myFile.open("VideoDatabase.txt",ios::app);//Opening File in append mode myFile>videoRuntime; myVideo->setRuntime(videoRuntime); cout>videoCopies; myVideo->setCopies(videoCopies); myVideo->AddVideoRec( videoTitle, videoBarcode,videoCategory, videoRuntime,videoCopies); break; case 'B': //Code for Deleting Video Record break; case 'b': //Code for Deleting Video Record break; case 'C': //Code for Modifying a Video Record break; case 'c': //Code for Modifying a Video Record break; case 'D': //Code for Printing out a Video Record break; case 'd': //Code for Printing out a Video Record break; case 'E': //Code for Printing out all Video Records break; case 'e': //Code for Printing out all Video Records break; case 'F': //Code for Finding a Video Record break; case 'f': //Code for Finding a Video Record break; case 'G': //Code for Renting a Video break; case 'g': //Code for Renting a Video break; case 'H': //Code for Returning a Video break; case 'h': //Code for Returning a Video break; case 'Q': //Code to end program system("pause"); break; case 'q': //Code to end program system("pause"); break; default: system("cls"); cout