Question

You are given a partial implementation of two classes. GroceryItem is a class that holds the...

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;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

Add a comment
Know the answer?
Add Answer to:
You are given a partial implementation of two classes. GroceryItem is a class that holds the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h"...

    - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h" template <typename DataType> StackArray<DataType>::StackArray(int maxNumber) { } template <typename DataType> StackArray<DataType>::StackArray(const StackArray& other) { } template <typename DataType> StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other) { } template <typename DataType> StackArray<DataType>::~StackArray() { } template <typename DataType> void StackArray<DataType>::push(const DataType& newDataItem) throw (logic_error) { } template <typename DataType> DataType StackArray<DataType>::pop() throw (logic_error) { } template <typename DataType> void StackArray<DataType>::clear() { } template <typename DataType> bool StackArray<DataType>::isEmpty() const {...

  • CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer;...

    CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer; cout << "MENU" << endl; cout << "a - Add item to cart" << endl; cout << "d - Remove item from cart" << endl; cout << "c - Change item quantity" << endl; cout << "i - Output items' descriptions" << endl; cout << "o - Output shopping cart" << endl; cout << "q - Quit" << endl << endl; while (true) {...

  • 10.18 LAB: Plant information (vector) Given a base Plant class and a derived Flower class, complete...

    10.18 LAB: Plant information (vector) Given a base Plant class and a derived Flower class, complete main() to create a vector called myGarden. The vector should be able to store objects that belong to the Plant class or the Flower class. Create a function called PrintVector(), that uses the PrintInfo() functions defined in the respective classes and prints each element in myGarden. The program should read plants or flowers from input (ending with -1), adding each Plant or Flower to...

  • Add a recursive Boolean function called checkRecurse to class IntegerLinkedList to check if any two consecutive...

    Add a recursive Boolean function called checkRecurse to class IntegerLinkedList to check if any two consecutive integers in the linked list are equal (return true in this case, false otherwise). You may assume that the list is not empty. A recursion “helper” function is already included in class IntegerLinkedList. You only need to write the recursive function. For example, checkRecurseHelper() should return true for the linked list shown below. A main function (prob3.cpp) is given to you to add data...

  • Design a class named Triangle that extends GeometricObject class. The class contains: Three double data fields...

    Design a class named Triangle that extends GeometricObject class. The class contains: Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle. A no-arg constructor that creates a default triangle with color = "blue", filled = true. A constructor that creates a triangle with the specified side1, side2, side3 and color = "blue", filled = true. The accessor functions for all three data fields, named getSide1(), getSide2(), getSide3(). A function...

  • A library maintains a collection of books. Books can be added to and deleted from and...

    A library maintains a collection of books. Books can be added to and deleted from and checked out and checked in to this collection. Title and author name identify a book. Each book object maintains a count of the number of copies available and the number of copies checked out. The number of copies must always be greater than or equal to zero. If the number of copies for a book goes to zero, it must be deleted from the...

  • In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the...

    In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the following sort member function on a singly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); Implement the following sort member function on a doubly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); The sort(…) methods take as a parameter a comparator function, having a default assignment of defaultCompare, a static function defined as follows: template <typename T> static bool defaultCompare(const...

  • Hi guys! I need help for the Data Structure class i need to provide implementation of...

    Hi guys! I need help for the Data Structure class i need to provide implementation of the following methods: Destructor Add Subtract Multiply Derive (extra credit ) Evaluate (extra credit ) ------------------------------------------------------- This is Main file cpp file #include "polynomial.h" #include <iostream> #include <sstream> using std::cout; using std::cin; using std::endl; using std::stringstream; int main(int argc, char* argv[]){    stringstream buffer1;    buffer1.str(        "3 -1 2 0 -2.5"    );    Polynomial p(3);    p.Read(buffer1);    cout << p.ToString()...

  • Requirements I have already build a hpp file for the class architecture, and your job is to imple...

    Requirements I have already build a hpp file for the class architecture, and your job is to implement the specific functions. In order to prevent name conflicts, I have already declared a namespace for payment system. There will be some more details: // username should be a combination of letters and numbers and the length should be in [6,20] // correct: ["Alice1995", "Heart2you", "love2you", "5201314"] // incorrect: ["222@_@222", "12306", "abc12?"] std::string username; // password should be a combination of letters...

  • Please zoom in so the pictures become high resolution. I need three files, FlashDrive.cpp, FlashDrive.h and...

    Please zoom in so the pictures become high resolution. I need three files, FlashDrive.cpp, FlashDrive.h and user_main.cpp. The programming language is C++. I will provide the Sample Test Driver Code as well as the codes given so far in text below. Sample Testing Driver Code: cs52::FlashDrive empty; cs52::FlashDrive drive1(10, 0, false); cs52::FlashDrive drive2(20, 0, false); drive1.plugIn(); drive1.formatDrive(); drive1.writeData(5); drive1.pullOut(); drive2.plugIn(); drive2.formatDrive(); drive2.writeData(2); drive2.pullOut(); cs52::FlashDrive combined = drive1 + drive2; // std::cout << "this drive's filled to " << combined.getUsed( )...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT