I finished the project but when i debug I get errors here is what i have //price
ID: 3889273 • Letter: I
Question
I finished the project but when i debug I get errors here is what i have
//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:
//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();
};
===============================================================
//priceListItem.h
#ifndef PRICELIST_ITEM
#define PRICELIST_ITEM
#include<iostream>
#include <string>
using namespace std;
class PriceListItem {
public:
PriceListItem();
PriceListItem(const string &itemName, const string &code, double price, bool taxable);
string getItemName();
string getCode();
double getPrice();
bool isTaxable();
private:
// private member variables
string itemName;
string code;
double price;
bool taxable;
};
#endif PRICELIST_ITEM
======================================================================
//groceryBill.h
#include "PriceList.h"
#include <string>
using namespace std;
//Structure for Grocery items purchased, mainatined in Linked list
struct GroceryItem {
int code;
double quantity;
//Linked list next node
GroceryItem * next;
};
class GroceryBill {
private:
//Grocery items are stored in linked list
GroceryItem * head = NULL;
PriceList *list;
double taxRate;
public:
GroceryBill(PriceList *priceList, double taxRate);
void scanItem(int scanCode, double quantity); // add item and quantity to bill; throw exception if item's code is not found in the pricelist
void scanItemsFromFile(string filename); // Scan multiple codes and quantities from the given text file
double getTotal(); // return the total cost of all items scanned
void printBill(); // Print the bill to cout. Each line contains the name of an item, total price, and the letter "T" if tax was addded.
};
===========================================================================================
//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
{
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 {
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) {
//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;
} */
===========================================================================
//priceListItem.cpp
#include<iostream>
#include<string>
#include "PriceListItem.h"
using namespace std;
//paramter constructor
PriceListItem::PriceListItem(const string &itemName, const string &code, double price, bool taxable) {
this->itemName = itemName;
this->code = code;
this->price = price;
this->taxable = taxable;
}
//default constructor
PriceListItem::PriceListItem() {
this->itemName = "";
this->code = "";
this->price = 0;
this->taxable = false;
}
//Returns item name
string PriceListItem::getItemName() {
return itemName;
}
//Returns item code
string PriceListItem::getCode() {
return code;
}
//Returns item price
double PriceListItem::getPrice() {
return price;
}
//Returns boolean value
bool PriceListItem::isTaxable() {
return taxable;
}
===========================================================================
//groceryBill.cpp
#include "GroceryBill.h"
#include <iostream>
#include <fstream>
using namespace std;
GroceryBill::GroceryBill(PriceList *priceList, double taxRate) {
this->list = priceList;
this->taxRate = taxRate;
}
void GroceryBill::scanItem(int scanCode, double quantity) {
GroceryItem * item = (GroceryItem *)malloc(sizeof(GroceryItem));
item->code = scanCode;
item->quantity = quantity;
item->next = NULL;
if (head == NULL) {
head = item;
}
else {
GroceryItem *cur = head;
while (cur->next != NULL)
{
cur = cur->next;
}
cur->next = item;
}
}
// Scan multiple codes and quantities from the given text file
// Each line contains two numbers separated by space: the first is the code (an integer), the second the quantity (a float/double)
// Example line from text file:
// 15000000 1.5
void GroceryBill::scanItemsFromFile(string filename) {
int code;
double quantity;
ifstream file;
//Repalce your file path
file.open(filename);
string line;
while (!(file.eof())) {
if (!(file >> code))
break;
file >> quantity;
scanItem(code, quantity);
getline(file, line);
}
file.close();
}
// return the total cost of all items scanned
double GroceryBill::getTotal() {
GroceryItem *cur = head;
double total = 0;
while (cur != NULL) {
PriceListItem * item = this->list->getItem(cur->code);
if (item != NULL)
total += (cur->quantity * item->getPrice());
cur = cur->next;
}
}
// Print the bill to cout. Each line contains the name of an item, total price, and the letter "T" if tax was addded.
// The last line shows the total.
// An example:
//Plastic_Wrap 1.60547 T
//Sugar_white 5.475
//Waffles_frozen 5.16
//Oil_Canola_100%_pure 2.69
//Potatoes_red 13.446
//TOTAL 28.3765
void GroceryBill::printBill() {
GroceryItem *cur = head;
while (cur != NULL) {
PriceListItem *item = this->list->getItem(cur ->code);
cout << item->getName() << " " << (cur->quantity * item->getPrice()) << (item->isTaxable() ? "T" : "") << endl;
cur = cur->next;
}
}
=================================================================================
// main.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <stdexcept>
#include "PriceList.h"
#include "GroceryBill.h"
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 PriceListItem class
PriceListItem item("Apples", "1000", 1.29, true);
testAnswer("PriceListItem.getCode test", item.getCode(), string("1000"));
testAnswer("PriceListItem.getItemName test", item.getItemName(), string("Apples"));
testAnswerEpsilon("PriceListItem.getPrice test", item.getPrice(), 1.29);
testAnswer("PriceListItem.isTaxable test", item.isTaxable(), true);
}
{
// 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);
}
{
// test the GroceryBill class
PriceList priceList;
priceList.createPriceListFromDatafile("pricelist.txt");
GroceryBill mybill(&priceList, 7.75);
testAnswer("GroceryBill initialization", mybill.getTotal(), 0.0);
// test GroceryBill::scanItem
mybill.scanItem( "9752347409", 1);
testAnswerEpsilon("GroceryBill.getTotal", mybill.getTotal(), 1.49*1.0775);
// test if error checking is done in GroceryBill::scanItem
try {
mybill.scanItem("1000", 1);
cout << "FAILED GroceryBill::scanItem did not throw exception for invalid input" << endl;
}
catch (exception &e) {
cout << "Caught exception: " << e.what() << endl;
cout << "PASSED GroceryBill::scanItem threw exception for invalid input" << endl;
}
// test quantity calculation in total
mybill.scanItem("9129592337", 2.5);
testAnswerEpsilon("GroceryBill.getTotal", mybill.getTotal(), 1.49*1.0775 + 2.5*2.19);
mybill.printBill();
// test reading items from file
mybill.scanItemsFromFile("billitems1.txt");
testAnswerEpsilon("GroceryBill.getTotal", mybill.getTotal(), 28.3765);
mybill.printBill();
}
// system("pause");
}
Explanation / Answer
#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:
//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();
};
===============================================================
//priceListItem.h
#ifndef PRICELIST_ITEM
#define PRICELIST_ITEM
#include<iostream>
#include <string>
using namespace std;
class PriceListItem {
public:
PriceListItem();
PriceListItem(const string &itemName, const string &code, double price, bool taxable);
string getItemName();
string getCode();
double getPrice();
bool isTaxable();
private:
// private member variables
string itemName;
string code;
double price;
bool taxable;
};
#endif PRICELIST_ITEM
======================================================================
//groceryBill.h
#include "PriceList.h"
#include <string>
using namespace std;
//Structure for Grocery items purchased, mainatined in Linked list
struct GroceryItem {
int code;
double quantity;
//Linked list next node
GroceryItem * next;
};
class GroceryBill {
private:
//Grocery items are stored in linked list
GroceryItem * head = NULL;
PriceList *list;
double taxRate;
public:
GroceryBill(PriceList *priceList, double taxRate);
void scanItem(int scanCode, double quantity); // add item and quantity to bill; throw exception if item's code is not found in the pricelist
void scanItemsFromFile(string filename); // Scan multiple codes and quantities from the given text file
double getTotal(); // return the total cost of all items scanned
void printBill(); // Print the bill to cout. Each line contains the name of an item, total price, and the letter "T" if tax was addded.
};
===========================================================================================
//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
{
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 {
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) {
//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.