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

You are given a partial implementation of two classes. GroceryItem is a class th

ID: 3751892 • Letter: Y

Question

You are given a partial implementation of two classes. GroceryItem is a class that holds the information for each grocery item. GroceryInventory is a class that holds an internal listing of GroceryItems as well as the tax rate for the store. Your inventory could be 100 items it could be 9000 items. You won’t know until your program gets the shipment at runtime. For this you should use the vector class from the C++ standard library.

I've completed the GroceryItem.h list but don't know how to implent the things on GroceryInventory.h list. The main.cpp was already provided and should not be touched.  

//GROCERYITEM.H

#pragma once

#ifndef GROCERYITEM_H

#define GROCERYITEM_H

#include <iostream>

#include <vector>

#include <string>

using namespace std;

class GroceryItem {

private:

string _name;

int _quantity;

float _unitPrice;

bool _taxable;

public:

GroceryItem();

GroceryItem(const string&, const int&, const float&, const bool&);

string getName() const;

void setName(const string&);

int getQuantity() const;

void setQuantity(const int&);

float getUnitPrice() const;

void setUnitPrice(const float&);

bool isTaxable() const;

void setTaxable(const bool&);

};

GroceryItem::GroceryItem() {

_name = " ";

_quantity = 0;

_unitPrice = 0.0;

_taxable = true;

}

GroceryItem::GroceryItem(const string& itemName, const int& itemQuan, const float& price, const bool& b) {

setName(itemName);

setQuantity(itemQuan);

setUnitPrice(price);

setTaxable(b);

};

void GroceryItem::setName(const string &itemName) {

_name = itemName;

}

string GroceryItem::getName() const {

return _name;

}

void GroceryItem::setQuantity(const int &itemQuan) {

_quantity = itemQuan;

}

int GroceryItem::getQuantity() const {

return _quantity;

}

void GroceryItem::setUnitPrice(const float &price) {

_unitPrice = price;

}

float GroceryItem::getUnitPrice() const {

return _unitPrice;

}

void GroceryItem::setTaxable(const bool &b) {

_taxable = b;

}

bool GroceryItem::isTaxable() const {

return _taxable;

}

#endif // !GROCERYITEM_H

// GROCERYINVENTORY.H!

#pragma once

#ifndef GROCERYINVENTORY.H

#define GROCERYINVENTORY.H

#include <vector>

#include <iostream>

#include <fstream>

#include <stdexcept>

#include "GroceryItem.h"

using namespace std;

class GroceryInventory {

private:

vector<GroceryItem> _inventory;

float _taxRate;

public:

GroceryInventory();

GroceryItem& getEntry(const string&);

void addEntry(const string&, const int&, const float&, const bool&);

float getTaxRate() const;

void setTaxRate(const float&);

void createListFromFile(const string&);

float calculateUnitRevenue() const;

float calculateTaxRevenue() const;

float calculateTotalRevenue() const;

GroceryItem& operator[](const int&);

};

GroceryInventory::GroceryInventory(){

_taxRate = 0.0;

}

GroceryItem& getEntry(const string& item){

}

void GroceryInventory::createListFromFile(const string& filename) {

ifstream input_file(filename);

if (input_file.is_open()) {

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

string name;

int quantity;

float unit_price;

bool taxable;

while (input_file >> name >> quantity >> unit_price >> taxable) {

addEntry(name, quantity, unit_price, taxable);

}

input_file.close();

}

else {

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

}

}

#endif // !GROCERYINVENTORY.H

// MAIN.CPP

#include <string>

#include "GroceryItem.h"

#include "GroceryInventory.h"

using namespace std;

template <typename T>

bool testAnswer(const string&, const T&, const T&);

template <typename T>

bool testAnswerEpsilon(const string&, const T&, const T&);

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

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

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

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

int main() {

// test only the GroceryItem class

GroceryItem item("Apples", 1000, 1.29, true);

testAnswer("GroceryItem.getName() test", item.getName(), string("Apples"));

testAnswer("GroceryItem.getQuantity() test", item.getQuantity(), 1000);

testAnswerEpsilon("GroceryItem.getPrice test", item.getUnitPrice(), 1.29f);

testAnswer("GroceryItem.isTaxable test", item.isTaxable(), true);

// test only the GroceryInventory class

GroceryInventory inventory;

inventory.addEntry("Apples", 1000, 1.99, false);

inventory.addEntry("Bananas", 2000, 0.99, false);

testAnswerEpsilon("GroceryInventory.getEntry() 1", inventory.getEntry("Apples").getUnitPrice(), 1.99f);

testAnswerEpsilon("GroceryInventory.getEntry() 2", inventory.getEntry("Bananas").getUnitPrice(), 0.99f);

// test copy constructor

GroceryInventory inventory2 = inventory;

testAnswer("GroceryInventory copy constructor 1", inventory2.getEntry("Apples").getUnitPrice(), 1.99f);

inventory.addEntry("Milk", 3000, 3.49, false);

inventory2.addEntry("Eggs", 4000, 4.99, false);

// Expect the following to fail

string nameOfTest = "GroceryInventory copy constructor 2";

try {

testAnswerEpsilon(nameOfTest, inventory.getEntry("Eggs").getUnitPrice(), 3.49f);

cout << "FAILED " << nameOfTest << ": expected to recieve an error but didn't" << endl;

}

catch (const exception& e) {

cout << "PASSED " << nameOfTest << ": expected and received error " << e.what() << endl;

}

// test assignment operator

GroceryInventory inventory3;

inventory3 = inventory;

testAnswerEpsilon("GroceryInventory assignment operator 1", inventory3.getEntry("Apples").getUnitPrice(), 1.99f);

inventory.addEntry("Orange Juice", 4500, 6.49, false);

inventory3.addEntry("Diapers", 5000, 19.99, false);

// Expect the following to fail

nameOfTest = "GroceryInventory assignment operator 2";

try {

testAnswerEpsilon(nameOfTest, inventory.getEntry("Diapers").getUnitPrice(), 19.99f);

cout << "FAILED " << nameOfTest << ": expected to recieve an error but didn't" << endl;

}

catch (const exception& e) {

cout << "PASSED " << nameOfTest << ": expected and received error " << e.what() << endl;

}

// test the GroceryInventory class

GroceryInventory inventory4;

inventory4.createListFromFile("shipment.txt");

inventory4.setTaxRate(7.75);

testAnswer("GroceryInventory initialization", inventory4.getEntry("Lettuce_iceberg").getQuantity(), 9541);

testAnswerEpsilon("GroceryInventory.calculateUnitRevenue()", inventory4.calculateUnitRevenue(), 1835852.375f);

testAnswerEpsilon("GroceryInventory.calculateTaxRevenue()", inventory4.calculateTaxRevenue(), 11549.519531f);

testAnswerEpsilon("GroceryInventory.calculateTotalRevenue()", inventory4.calculateTotalRevenue(), 1847401.875f);

return 0;

}

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 << fixed << "PASSED " << nameOfTest << ": expected and received " << received << endl;

return true;

}

cout << "FAILED " << nameOfTest << ": expected " << expected << " but received " << received << endl;

return false;

}

Explanation / Answer

In GroceryInventory use the List to store 'GroceryItem'. You can declare a private or public member of GroceryItem some thing like this in GroceryInventory.h:

Private List<GroceryItem> listOfAllGroceryItems;

and create some function like AddToListOfItems() //Which will add GroceryItem in the List named listOfAllGroceryItems.

Method would be something like:

Public void AddToList( GroceryItem gItem ) { listOfAllGroceryItems.pushback(gItem); }

This concet of having GroceryItem class in GroceryInventory is called Assosiation.

//Feel free to contact me back if you need any further assistence

//You can use this

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote