here is the header file and cpp file for priceList just need to fill the mising
ID: 3888742 • Letter: H
Question
here is the header file and cpp file for priceList just need to fill the mising things
// PriceList.h
#pragma once
#include <string>
#include <stdexcept>
#include "PriceListItem.h"
using namespace std;
class PriceList {
public:
void createPriceListFromDatafile(string filename); // Load information from a text file with the given filename (Completed)
void addEntry(string itemName, string code, double price, bool taxable); // add to the price list information about a new item. A max of 1,000,000 entries can be added
bool isValid(string code) const; // return true only if the code is valid
PriceListItem getItem(string code) const; // return price, item name, taxable? as an PriceListItem object; throw exception if code is not found
private:
// Add private member variables for your class along with any
// other variables required to implement the public member functions
// TO BE COMPLETED
===================================================================================================================================
Pricelist cpp.
#include <string>
#include <iostream>
#include <fstream>
#include <stdexcept>
#include "PriceList.h"
#include "PriceListItem.h"
using namespace std;
// Load information from a text file with the given filename.
void PriceList::createPriceListFromDatafile(string filename) {
ifstream myfile(filename);
if (myfile.is_open()) {
cout << "Successfully opened file " << filename << endl;
string name;
string code;
double price;
bool taxable;
while (myfile >> name >> code >> price >> taxable) {
// cout << code << " " << taxable << endl;
addEntry(name, code, price, taxable);
}
myfile.close();
}
else
throw invalid_argument("Could not open file " + filename);
}
// return true only if the code is valid
bool PriceList::isValid(string code) const {
// TO BE COMPLETED
}
// return price, item name, taxable? as an ItemPrice object; throw exception if code is not found
PriceListItem PriceList::getItem(string code) const {
// TO BE COMPLETED
}
// add to the price list information about a new item
void PriceList::addEntry(string itemName, string code, double price, bool taxable) {
// TO BE COMPLETED
}
Explanation / Answer
// PriceList.h
#pragma once
#include <string>
#include <stdexcept>
#include "PriceListItem.h"
using namespace std;
class PriceList
{
public:
void createPriceListFromDatafile(string filename); // Load information from a text file with the given filename (Completed)
void addEntry(string itemName, string code, double price, bool taxable); // add to the price list information about a new item. A max of 1,000,000 entries can be added
bool isValid(string code) const; // return true only if the code is valid
PriceListItem getItem(string code) const; // return price, item name, taxable? as an PriceListItem object; throw exception if code is not found
private:
// Add private member variables for your class along with any
// other variables required to implement the public member functions
// TO BE COMPLETED
//first item pointer in the list
PriceListItem *priceItemListHead;
const int MAX_NUMBER_OF_ITEMS=1000000;
//to keep track of number of items added to the list
int item_added;
//public:
//will return priceItemListHead if required
//PriceListItem* getPriceList();
};
=====================================================================
//PriceList.cpp
#include <string>
#include <iostream>
#include <fstream>
#include <stdexcept>
#include "PriceList.h"
#include "PriceListItem.h"
using namespace std;
// Load information from a text file with the given filename.
void PriceList::createPriceListFromDatafile(string filename)
{
item_added=0;
ifstream myfile(filename);
if (myfile.is_open()) {
cout << "Successfully opened file " << filename << endl;
string name;
string code;
double price;
bool taxable;
while (myfile >> name >> code >> price >> taxable) {
// cout << code << " " << taxable << endl;
addEntry(name, code, price, taxable);
}
myfile.close();
}
else
throw invalid_argument("Could not open file " + filename);
}
// return true only if the code is valid
bool PriceList::isValid(string code) const
{
// TO BE COMPLETED
PriceListItem *temp = priceItemListHead;
if(temp != NULL)
{
//compare the code with first item code, if found return true
if(temp->getCode().compare(code)==0)
{
//code found
return true;
}
//check if it has more items in there
while(temp->nextItem != NULL)
{
//compare the code with each item code, if found return true
if(temp->getCode().compare(code)==0)
{
//item found
return true;
}
//go to the next item
temp = temp->nextItem;
}
}
//item with this code not found, so return false
//item not valid
return false;
}
// return price, item name, taxable? as an ItemPrice object; throw exception if code is not found
PriceListItem PriceList::getItem(string code) const {
// TO BE COMPLETED
PriceListItem *temp = priceItemListHead;
//check if there is item in the list
if(temp != NULL)
{
//compare the code with the first item
if(temp->getCode().compare(code)==0)
{
//item found, return item
return *temp;
}
//check if there are more items, if not found previously
while(temp->nextItem != NULL)
{
//compare code with each item's code
if(temp->getCode().compare(code)==0)
{
//code matches, return item
return *temp;
}
//go to the next item
temp = temp->nextItem;
}
}
//item not found in the item list
//return nothing
return nullptr;
}
// add to the price list information about a new item
void PriceList::addEntry(string itemName, string code, double price, bool taxable) {
// TO BE COMPLETED
//check if list is not exceeding the limit
if(item_added >= MAX_NUMBER_OF_ITEMS)
return;
//create a new item
PriceListItem item(itemName,code,price,taxable);
//make item next to NULL, if you have declared a nextItem pointer in PriceListItem
item.nextItem =NULL;
//copy the list in temp pointers
PriceListItem *temp = priceItemListHead;
//if the list is not empty
if(temp != NULL)
{
while(temp->nextItem != NULL)
{
//go to next item, since we want to add this item in the last
temp = temp->nextItem;
}
//insert item at the end.
temp->nextItem = &newItem;
}
//if the list is empty
else
{
//make it the first item of the list
priceItemListHead = &newItem;
}
//increase number of items added
item_added++;
}
/* //will return the item list, if required
PriceListItem* PriceList::getPriceList()
{
return itemList;
} */
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.