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

I Am writting a groceryBill class for a project but my program crashes dont know

ID: 3870732 • Letter: I

Question

I Am writting a groceryBill class for a project but my program crashes dont know why. The program should run and pass all the tests it gives in the main.cpp here is what i have.

// priceList.h

#pragma once

#include

#include

#include "PriceListItem.h"

using namespace std;

class PriceList {

public:

PriceList();

PriceList(const PriceList &list);

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
PriceList& operator = (const PriceList &list2); //overloaded assignment operator


private:

PriceListItem* arr;
int numItems;

};

========================================================

// priceListItem.h

#pragma once

#include

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:

string code;

double price;

string itemName;

bool taxable;

};

===============================================================

//groceryBill.h

#pragma once

#include "PriceList.h"


class GroceryBill {

public:

GroceryBill(const PriceList *priceList, double taxRate);

void scanItem(string 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.

private:

int billItemCount;
double billAmount;
string *codes;
double *qty;
PriceList price_list;
PriceListItem *itemList;
double tax_rate;


};

=============================================================

groceryBill.cpp

#include "GroceryBill.h"

#include
#include


using namespace std;

GroceryBill::GroceryBill(const PriceList *priceList, double taxRate) {


billAmount = 0;
billItemCount = 0;
codes = new string[100];
qty = new double[100];

price_list = *priceList;
tax_rate = taxRate;


}

void GroceryBill::scanItem(string scanCode, double quantity) {


if (price_list.isValid(scanCode))
{
  PriceListItem newItem = price_list.getItem(scanCode);

  if (newItem.isTaxable())
  {
   billAmount += (quantity * newItem.getPrice()) + ((quantity * newItem.getPrice()) * tax_rate) / 100;
  }
  else
  {
   billAmount += (quantity * newItem.getPrice());
  }
  price_list.isValid(scanCode);
  PriceListItem *temp = itemList;

}
else
  throw invalid_argument("Item not found " + scanCode);


  


}


// 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) {


// To be completed

// HINT: Look at code in PriceList::createPriceListFromDatafile(string filename)

ifstream myfile(filename.c_str());
if (myfile.is_open()) {
  cout << "Successfully opened file " << filename << endl;

  string code;
  double quantity;

  while (myfile >> code >> quantity) {
   // cout << code << " " << taxable << endl;
   scanItem(code, quantity);
  }
  myfile.close();
}
else
  throw invalid_argument("Could not open file " + filename);


}

// return the total cost of all items scanned

double GroceryBill::getTotal() {

return billAmount;

}

// 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() {

for (int i = 0; i {
  cout << price_list.getItem(codes[i]).getItemName() << " " << qty[i] << " ";
  if (price_list.getItem(codes[i]).isTaxable())
   cout << "T";
  cout << endl;
}

}

==========================================================================================

//main.cpp

#include

#include

#include

#include


#include "priceListItem.h"
#include "PriceList.h"

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

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

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 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");

}

=================================================================

// priceList

#include <string>

#include <iostream>

#include <fstream>

#include <stdexcept>

#include "PriceList.h"

#include "PriceListItem.h"

using namespace std;

PriceList::PriceList()
{
numItems = 0;

arr = new PriceListItem[1000000];
}

PriceList::PriceList(const PriceList &list)
{
arr = new PriceListItem[1000000]; //allocate space and then copy all elements
*this = list; //use overloaded assigment operator to copy over the input parameter

}

// 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) {

for (int i = 0; i < numItems; i++)
{
  
  if (arr[i].getCode()== code)
  {
   return true;

  }
  
}
return false;


}

// return price, item name, taxable? as an ItemPrice object; throw exception if code is not found

PriceListItem PriceList::getItem(string code) {
//const

for (int i = 0; i < numItems; i++)
{
  if (arr[i].getCode() == code)
  {
   return arr[i];

  }

}
   throw invalid_argument("Invalid code:"+code);

}

// add to the price list information about a new item

void PriceList::addEntry(string itemName, string code, double price, bool taxable) {

// create item object
PriceListItem a(itemName, code, price, taxable);

// store item object in array
arr[numItems] = a;
numItems++;
}

PriceList& PriceList::operator = (const PriceList &list2)
{
if (&list2 != this) //nothing to do self assignment
{
  numItems = list2.numItems;
  for (int i = 0; i <list2.numItems; i++)
   arr[i] = list2.arr[i];
}
return *this;
}

=======================================================================

// priceListItem.cpp

#include "PriceListItem.h"


//Constructor with all details
PriceListItem::PriceListItem(const string&itemName, const string&code, double price, bool taxable) {

this->code = code;
this->price = price;
this->itemName = itemName;
this->taxable = taxable;
//this->nextItem = NULL;
}
PriceListItem::PriceListItem() {
this->itemName = "";
this->code = "";
this->price = 0;
this->taxable = false;
}

//Public methods to access its properties
string PriceListItem::getCode() {
return this->code;
}


double PriceListItem::getPrice() {
return this->price;
}


string PriceListItem::getItemName() {
return this->itemName;
}


bool PriceListItem::isTaxable() {
return this->taxable;
}

Explanation / Answer

-------------------PriceListItem.h-------------------

#ifndef MY_PRICE_LIST_ITEM

#define MY_PRICE_LIST_ITEM

#include<string>

#include<iostream>

#include<fstream>

#include<stdexcept>

#include<cstring>

using namespace std;

namespace myPriceListItem

{

    class PriceListItem

    {

        public:

            PriceListItem();

            PriceListItem(const string &itemName, const string &code, double price, bool taxable);

            string getItemName();

            string getCode();

            double getPrice();

            bool isTaxable();

            private:

            string code;

            double price;

            string itemName;

            bool taxable;

    };

}

#endif

------------------------PriceListItem.cpp--------------------------------

#include "PriceListItem.h"

using namespace myPriceListItem;

//Constructor with all details

PriceListItem::PriceListItem(const string&itemName, const string&code, double price, bool taxable) {

this->code = code;

this->price = price;

this->itemName = itemName;

this->taxable = taxable;

//this->nextItem = NULL;

}

PriceListItem::PriceListItem() {

this->itemName = "";

this->code = "";

this->price = 0;

this->taxable = false;

}

//Public methods to access its properties

string PriceListItem::getCode() {

return this->code;

}

double PriceListItem::getPrice() {

return this->price;

}

string PriceListItem::getItemName() {

return this->itemName;

}

bool PriceListItem::isTaxable() {

return this->taxable;

}

-------------------PriceList.h----------------------

#ifndef MY_PRICE_LIST

#define MY_PRICE_LIST

#include "PriceListItem.cpp"

namespace myPriceList

{

    class PriceList

    {

    public:

       

        PriceList();

        PriceList(const PriceList &list);

        void createPriceListFromDatafile(); // 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

        PriceList& operator = (const PriceList &list2); //overloaded assignment operator

       

        private:

        PriceListItem* arr;

        int numItems;

    };

}

#endif

-------------------PriceList.cpp----------------------------------

#include "PriceList.h"

using namespace myPriceList;

PriceList::PriceList()

{

numItems = 0;

arr = new PriceListItem[1000000];

}

PriceList::PriceList(const PriceList &list)

{

arr = new PriceListItem[1000000]; //allocate space and then copy all elements

*this = list; //use overloaded assigment operator to copy over the input parameter

}

// Load information from a text file with the given filename.

void PriceList::createPriceListFromDatafile()

{

ifstream myfile("pricelist.txt");

if (myfile.is_open()) {

cout << "Successfully opened file pricelist.txt" << 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 pricelist.txt");

}

// return true only if the code is valid

bool PriceList::isValid(string code) {

for (int i = 0; i < numItems; i++)

{

if (arr[i].getCode()== code)

{

   return true;

}

}

return false;

}

// return price, item name, taxable? as an ItemPrice object; throw exception if code is not found

PriceListItem PriceList::getItem(string code) {

//const

for (int i = 0; i < numItems; i++)

{

if (arr[i].getCode() == code)

{

   return arr[i];

}

}

   throw invalid_argument("Invalid code:"+code);

}

// add to the price list information about a new item

void PriceList::addEntry(string itemName, string code, double price, bool taxable) {

// create item object

PriceListItem a(itemName, code, price, taxable);

// store item object in array

arr[numItems] = a;

numItems++;

}

PriceList& PriceList::operator = (const PriceList &list2)

{

if (&list2 != this) //nothing to do self assignment

{

numItems = list2.numItems;

for (int i = 0; i <list2.numItems; i++)

   arr[i] = list2.arr[i];

}

return *this;

}

-------------------------GroceryBill.h---------------------------

#ifndef MY_GROCERY_BILL

#define MY_GROCERY_BILL

#include "PriceList.cpp"

namespace myGroceryBill

{

    class GroceryBill

    {

        public:

            GroceryBill(const PriceList *priceList, double taxRate);

            void scanItem(string 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.

            private:

           

            int billItemCount;

            double billAmount;

            string *codes;

            double *qty;

            PriceList price_list;

            PriceListItem *itemList;

            double tax_rate;

   

    };

}

#endif

--------------------GroceryBill.cpp-----------------------

#include "GroceryBill.h"

using namespace myGroceryBill;

GroceryBill::GroceryBill(const PriceList *priceList, double taxRate) {

billAmount = 0;

billItemCount = 0;

codes = new string[100];

qty = new double[100];

price_list = *priceList;

tax_rate = taxRate;

}

void GroceryBill::scanItem(string scanCode, double quantity) {

if (price_list.isValid(scanCode))

{

PriceListItem newItem = price_list.getItem(scanCode);

if (newItem.isTaxable())

{

   billAmount += (quantity * newItem.getPrice()) + ((quantity * newItem.getPrice()) * tax_rate) / 100;

}

else

{

   billAmount += (quantity * newItem.getPrice());

}

price_list.isValid(scanCode);

PriceListItem *temp = itemList;

}

else

throw invalid_argument("Item not found " + scanCode);

}

// 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) {

// To be completed

// HINT: Look at code in PriceList::createPriceListFromDatafile(string filename)

ifstream myfile(filename.c_str());

if (myfile.is_open()) {

cout << "Successfully opened file " << filename << endl;

string code;

double quantity;

while (myfile >> code >> quantity) {

   // cout << code << " " << taxable << endl;

   scanItem(code, quantity);

}

myfile.close();

}

else

throw invalid_argument("Could not open file " + filename);

}

// return the total cost of all items scanned

double GroceryBill::getTotal() {

return billAmount;

}

// 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() {

for (int i = 0; i<billItemCount;i++) {

cout << price_list.getItem(codes[i]).getItemName() << " " << qty[i] << " ";

if (price_list.getItem(codes[i]).isTaxable())

   cout << "T";

cout << endl;

}

}

------------------main.cpp--------------------------

#include "GroceryBill.cpp"

//////////////////////////////////////////////////////////////////////////////////////////////

// DO NOT EDIT THIS FILE (except for your own testing)

// CODE WILL BE GRADED USING A MAIN FUNCTION SIMILAR TO THIS

//////////////////////////////////////////////////////////////////////////////////////////////

using namespace std;

bool testAnswer(const string &nameOfTest, const double &received, const double &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;

}

bool testAnswerEpsilon(const string &nameOfTest, const double &received, const double &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 the GroceryBill class

PriceList priceList;

priceList.createPriceListFromDatafile();

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");

}

// i don't know how you are creating list from file, so I wasn't able to perform a test case. But the errors are

//removed.