Finish the given code. DOWNLOAD THIS SOURCE FILE THIS HAS EVERYTHING ON IT. Sour
ID: 3888525 • Letter: F
Question
Finish the given code.
DOWNLOAD THIS SOURCE FILE THIS HAS EVERYTHING ON IT.
Source Files: https://www.mediafire.com/file/96vt5cubhk0c5kt/sourcefiles.zip
This file contain all the classes, header, and text file of the grocery items.
COMPLETE THE UNFINISHED CODE OF ALL Six.
To compute a grocery bill, a checkout stand typically has a scanner to identify product codes of items. This code is used to get the price of the item from the store’s price list, along with the item name and whether it is taxable. This information is then used to compute the price of the item and the total of the bill (see figure below). In this project, you will write C++ code to represent the workings of such a cash register.
Objective
You are given partial implementations of three classes. PriceList is a class to hold information of all items in the grocery store. There could be up to 1,000,000 items. The information of each item is its price, barcode, item name, and whether it is taxable; these are kept together in Class PriceListItem. The items in PriceList are loaded all together from a text file, or inserted one at a time. The complete grocery bill is computed in Class GroceryBill. To do so, it is given (a const pointer to) a previously created PriceList to get the prices, and the tax rate percentage for taxable items when constructed. It is then given barcodes of items and their quantity. The barcodes can be given one at a time, or they can be collected together in a text file.
You are to complete the implementations of these classes, adding public/private member variables and functions as needed. You should not use any of the C++ Standard Library containers (such as std::array, std::vector, std::list) for this project. Your code is tested in the provided main.cpp.
Explanation / Answer
//PriceListItem.h
#pragma once
#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:
// any private member variables and methods go here
// TO BE COMPLETED
string itemName;
string itemCode;
double itemPrice;
bool isItemTaxable;
public:
PriceListItem *nextItem;
};
//PriceListItem
#include "PriceListItem.h"
PriceListItem::PriceListItem(const string &name, const string &code, double price, bool taxable) {
// TO BE COMPLETED
itemName = name;
itemCode = code;
itemPrice = price;
isItemTaxable = taxable;
}
PriceListItem::PriceListItem() {
// TO BE COMPLETED
itemName = "";
itemCode = "";
itemPrice = 0.0;
isItemTaxable = false;
}
string PriceListItem::getItemName() {
// TO BE COMPLETED
return itemName;
}
string PriceListItem::getCode() {
// TO BE COMPLETED
return itemCode;
}
double PriceListItem::getPrice() {
// TO BE COMPLETED
return itemPrice;
}
bool PriceListItem::isTaxable() {
// TO BE COMPLETED
return isItemTaxable;
}
//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:
// Add private member variables for your class along with any
// other variables required to implement the public member functions
// TO BE COMPLETED
PriceListItem *itemList;
public:
PriceListItem* getPriceList();
};
//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.c_str());
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
PriceListItem *temp = itemList;
if(temp != NULL)
{
if(temp->getCode().compare(code)==0)
{
return true;
}
while(temp->nextItem != NULL)
{
if(temp->getCode().compare(code)==0)
{
return true;
}
temp = temp->nextItem;
}
}
}
// 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
if(isValid(code))
{
PriceListItem *temp = itemList;
if(temp != NULL)
{
if(temp->getCode().compare(code)==0)
{
return *temp;
}
while(temp->nextItem != NULL)
{
if(temp->getCode().compare(code)==0)
{
return *temp;
}
temp = temp->nextItem;
}
}
}
}
// add to the price list information about a new item
void PriceList::addEntry(string itemName, string code, double price, bool taxable) {
// TO BE COMPLETED
//create an item
PriceListItem newItem(itemName,code,price,taxable);
//make item next to NULL
newItem.nextItem =NULL;
PriceListItem *temp = itemList;
if(temp != NULL)
{
while(temp->nextItem != NULL)
{
temp = temp->nextItem;
}
temp->nextItem = &newItem;
}
else
{
itemList = &newItem;
}
}
PriceListItem* PriceList::getPriceList()
{
return itemList;
}
//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:
// any private member variables and methods go here
// TO BE COMPLETED
PriceList price_list;
PriceListItem *itemList;
double total;
double tax_rate;
};
//GroceryBill.cpp
#include "GroceryBill.h"
#include <iostream>
#include <fstream>
#include<stdexcept>
using namespace std;
GroceryBill::GroceryBill(const PriceList *priceList, double taxRate) {
// To be completed
price_list = *priceList;
tax_rate = taxRate;
}
void GroceryBill::scanItem(string scanCode, double quantity) {
// To be completed
if(price_list.isValid(scanCode))
{
PriceListItem newItem = price_list.getItem(scanCode);
if(newItem.isTaxable())
{
total += (quantity * newItem.getPrice() ) + ( (quantity * newItem.getPrice() ) * tax_rate)/100;
}
else
{
total += (quantity * newItem.getPrice() );
}
price_list.isValid(scanCode);
PriceListItem *temp = itemList;
if(temp != NULL)
{
while(temp->nextItem != NULL)
{
temp = temp->nextItem;
}
temp->nextItem = &newItem;
}
else
{
itemList = &newItem;
}
}
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)
// Load information from a text file with the given 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() {
// To be completed
return total;
}
// 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() {
// To be completed
PriceListItem *temp = itemList;
while(itemList.nextItem != NULL)
{
if(temp->isTaxable())
{
cout<< temp->getItemName() <<" "<<temp->getPrice()<<" "<<temp->isTaxable()<<"T"<<endl;
}
else
{
cout<< temp->getItemName() <<" "<<temp->getPrice()<<" "<<temp->isTaxable()<<endl;
}
temp = temp->nextItem;
}
}
//main.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <stdexcept>
#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 <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");
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.