need to fill in the missing things in the code priceList.h and priceList.cpp her
ID: 3869942 • Letter: N
Question
need to fill in the missing things in the code priceList.h and priceList.cpp here is the code.
//priceList.h
};
=============================================================================
//priceList.cpp
}
===========================================================================
//PriceListItem.h
#ifndef PRICELISTITEM_H
#define PRICELISTITEM_H
#include <string>
using namespace std;
class PriceListItem
{
private:
string code;
double price;
string itemName;
bool taxable;
//Pointer to next Item, As the items are maintained as Linked List
//PriceListItem *nextItem = NULL;
public:
//Constructor with all details
PriceListItem();
PriceListItem(const string &itemName, const string &code, double price, bool taxable);
//Public methods to access its properties
string getCode();
double getPrice();
string getItemName();
bool isTaxable();
};
#endif // PRICELISTITEM_H
=========================================================
//main.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <stdexcept>
#include "PriceList.h"
#include "GroceryBill.h"
#include "priceListItem.h"
//////////////////////////////////////////////////////////////////////////////////////////////
// DO NOT EDIT THIS FILE (except for your own testing)
// CODE WILL BE GRADED USING A MAIN FUNCTION SIMILAR TO THIS
//////////////////////////////////////////////////////////////////////////////////////////////
using namespace std;
template <typename T>
bool testAnswer(const string &nameOfTest, const T &received, const T &expected) {
if (received == expected) {
cout << "PASSED " << nameOfTest << ": expected and received " << received << endl;
return true;
}
cout << "FAILED " << nameOfTest << ": expected " << expected << " but received " << received << endl;
return false;
}
template <typename T>
bool testAnswerEpsilon(const string &nameOfTest, const T &received, const T &expected) {
const double epsilon = 0.0001;
if ((received - expected < epsilon) && (expected - received < epsilon)) {
cout << "PASSED " << nameOfTest << ": expected and received " << received << endl;
return true;
}
cout << "FAILED " << nameOfTest << ": expected " << expected << " but received " << received << endl;
return false;
}
int main() {
{
//test only the PriceList class
PriceList priceList;
priceList.addEntry("Apples", "1000", 1.99, false);
priceList.addEntry("Bananas", "2000", 0.99, false);
testAnswer("PriceList isValid1", priceList.isValid("1000"), true);
testAnswer("PriceList isValid2", priceList.isValid("19"), false);
// test copy constructor
PriceList priceList2 = priceList;
testAnswer("PriceList copy constructor test1", priceList2.isValid("1000"), true);
priceList.addEntry("Milk", "3000", 3.49, false);
priceList2.addEntry("Eggs", "4000", 4.99, false);
testAnswer("PriceList copy constructor test2", priceList.isValid("4000"), false);
// test assignment operator
PriceList priceList3;
priceList3 = priceList;
testAnswer("PriceList assignment test1", priceList3.isValid("1000"), true);
priceList.addEntry("Orange_juice_1gal", "4500", 6.49, false);
priceList3.addEntry("Diapers_30ct", "5000", 19.99, false);
testAnswer("PriceList assignment test2", priceList.isValid("5000"), false);
{
// test capacity of the PriceList class
PriceList priceList;
for (int i = 1; i<100000; i++)
priceList.addEntry(string("Apples_") + to_string(i), to_string(i), 1.99, false);
testAnswer("PriceList big data1", priceList.isValid("20000"), true);
testAnswer("PriceList big data2", priceList.isValid("100000"), false);
}
system("pause");
}
};
=============================================================================
//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}
===========================================================================
//PriceListItem.h
#ifndef PRICELISTITEM_H
#define PRICELISTITEM_H
#include <string>
using namespace std;
class PriceListItem
{
private:
string code;
double price;
string itemName;
bool taxable;
//Pointer to next Item, As the items are maintained as Linked List
//PriceListItem *nextItem = NULL;
public:
//Constructor with all details
PriceListItem();
PriceListItem(const string &itemName, const string &code, double price, bool taxable);
//Public methods to access its properties
string getCode();
double getPrice();
string getItemName();
bool isTaxable();
};
#endif // PRICELISTITEM_H
=========================================================
//main.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <stdexcept>
#include "PriceList.h"
#include "GroceryBill.h"
#include "priceListItem.h"
//////////////////////////////////////////////////////////////////////////////////////////////
// DO NOT EDIT THIS FILE (except for your own testing)
// CODE WILL BE GRADED USING A MAIN FUNCTION SIMILAR TO THIS
//////////////////////////////////////////////////////////////////////////////////////////////
using namespace std;
template <typename T>
bool testAnswer(const string &nameOfTest, const T &received, const T &expected) {
if (received == expected) {
cout << "PASSED " << nameOfTest << ": expected and received " << received << endl;
return true;
}
cout << "FAILED " << nameOfTest << ": expected " << expected << " but received " << received << endl;
return false;
}
template <typename T>
bool testAnswerEpsilon(const string &nameOfTest, const T &received, const T &expected) {
const double epsilon = 0.0001;
if ((received - expected < epsilon) && (expected - received < epsilon)) {
cout << "PASSED " << nameOfTest << ": expected and received " << received << endl;
return true;
}
cout << "FAILED " << nameOfTest << ": expected " << expected << " but received " << received << endl;
return false;
}
int main() {
{
//test only the PriceList class
PriceList priceList;
priceList.addEntry("Apples", "1000", 1.99, false);
priceList.addEntry("Bananas", "2000", 0.99, false);
testAnswer("PriceList isValid1", priceList.isValid("1000"), true);
testAnswer("PriceList isValid2", priceList.isValid("19"), false);
// test copy constructor
PriceList priceList2 = priceList;
testAnswer("PriceList copy constructor test1", priceList2.isValid("1000"), true);
priceList.addEntry("Milk", "3000", 3.49, false);
priceList2.addEntry("Eggs", "4000", 4.99, false);
testAnswer("PriceList copy constructor test2", priceList.isValid("4000"), false);
// test assignment operator
PriceList priceList3;
priceList3 = priceList;
testAnswer("PriceList assignment test1", priceList3.isValid("1000"), true);
priceList.addEntry("Orange_juice_1gal", "4500", 6.49, false);
priceList3.addEntry("Diapers_30ct", "5000", 19.99, false);
testAnswer("PriceList assignment test2", priceList.isValid("5000"), false);
{
// test capacity of the PriceList class
PriceList priceList;
for (int i = 1; i<100000; i++)
priceList.addEntry(string("Apples_") + to_string(i), to_string(i), 1.99, false);
testAnswer("PriceList big data1", priceList.isValid("20000"), true);
testAnswer("PriceList big data2", priceList.isValid("100000"), false);
}
system("pause");
}
Explanation / Answer
#pragam once
#include<string>
#include<stdexecpt>
#include "Pricelistitem.h"
using namespace std;
class PriceList
{
public:
PriceList();
void createPriceListFromDatafile(string filename);
void addEntry(string itemName,string code,double price,bool taxable);
bool is valid(string code)const;
PriceListitem getitem(string code)const;
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 priceitemLilstHead if required
//priceListitem* getPriceList();
};
//priceList.cpp
#include<string>
#include<iostream>
#include<fstream>
#include<stdexecpt>
#include"PriceList.h"
#include"PriceListitem.h"
using namespace std;
//Load information from a text file with the given filename
void PriceList::createPriceListFormDatafile(string filename)
{
item_added = 0;
ifstream myfile(filename);
if(mylife is_open())
{
cout <<"Successfully opened file"<<filename<<endl;
string name;
string code;
double price;
bool taxable;
while(mylife>>name >>code>>price>>taxable)
{
//cout<<code<<""<<taxable<<endl;
addEntry(name,code,price,taxable);
}
mylife.close();
}
else
throw invalid_argument("could not open file"+filename);
}
//return true only if the code is valid
boolPriceList::is Valid(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)
{
//item found
return true;
}
//go to the next item
temp = temp->nextitem;
}
}
//item with this code not found ,so retrun false
//item not valid
return false;
}
//return price,item name,taxable? as an itemPrice object;throw execption 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 previsouly
while(temp->nextitem !=NULL)
{
//compare the code with the first item
if(item->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 nulptr;
}
//add to the price list information about a new itam
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 decleared a nextitem pointer in PriceListitem
item.nextitem =NULL;
//copy the list in temp pointers
PriceListitem*temp pointers
//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.