// PriceList.h #pragma once #include <string> #include <stdexcept> #include \"Pr
ID: 3887849 • Letter: #
Question
// 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
===================================================================================================================================
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);
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
}
// 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
}
// add to the price list information about a new item
void PriceList::addEntry(string itemName, string code, double price, bool taxable) {
// TO BE COMPLETED
}
};
====================================================================================================================================
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
};
=======================================================================================================================================
PriceListItem cpp.
#include "PriceListItem.h"
PriceListItem::PriceListItem(const string &itemName, const string &code, double price, bool taxable) {
// TO BE COMPLETED
}
PriceListItem::PriceListItem() {
// TO BE COMPLETED
}
string PriceListItem::getItemName() {
// TO BE COMPLETED
}
string PriceListItem::getCode() {
// TO BE COMPLETED
}
double PriceListItem::getPrice() {
// TO BE COMPLETED
}
bool PriceListItem::isTaxable() {
// TO BE COMPLETED
}
=========================================================================================================================================
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
// testing
};
=========================================================================================================================================
GroceryBill cpp.
#include "GroceryBill.h"
#include <iostream>
using namespace std;
GroceryBill::GroceryBill(const PriceList *priceList, double taxRate) {
// To be completed
}
void GroceryBill::scanItem(string scanCode, double quantity) {
// To be completed
}
// 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)
}
// return the total cost of all items scanned
double GroceryBill::getTotal() {
// To be completed
}
// 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
}
Explanation / Answer
PROGRAM CODE:
// PriceList.h
#pragma once
#include <string>
#include <stdexcept>
#include "PriceListItem.h"
using namespace std;
class PriceList {
public:
PriceList();
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
private PriceListItem *items;
int size;
};
//===================================================================================================================================
Pricelist cpp.
#include <string>
#include <iostream>
#include <fstream>
#include <stdexcept>
#include "PriceList.h"
#include "PriceListItem.h"
using namespace std;
//constructor to initialize variables
PriceList::PriceList()
{
//this will grow dynamically
items = new PriceList[100];
size = 0;
}
// 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) const {
for(int i=0; i<size; i++)
{
if(items[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<size; i++)
{
if(items[i].getCode() == code)
return items[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) {
if(size < 1000000)
items[size++] = new PriceListItem(itemName, code, price, taxable);
throw invalid_argument("Max of 1000000 items reached");
}
};
//====================================================================================================================================
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
string itemName;
string code;
double price;
bool taxable;
};
//=======================================================================================================================================
PriceListItem cpp.
#include "PriceListItem.h"
PriceListItem::PriceListItem(const string &itemName, const string &code, double price, bool taxable) {
this.itemName = itemName;
this.code = code;
this.price = price;
this.taxable = taxable;
}
PriceListItem::PriceListItem() {
this.itemName = "";
this.code = "";
this.price = 0;
this.taxable = false;
}
string PriceListItem::getItemName() {
return this.itemName;
}
string PriceListItem::getCode() {
return this.code;
}
double PriceListItem::getPrice() {
return this.price;
}
bool PriceListItem::isTaxable() {
return this.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:
// any private member variables and methods go here
PriceList itemList;
int billItemCount;
double taxRate;
double billAmount;
string *codes;
double *qty;
// testing
};
=========================================================================================================================================
GroceryBill cpp.
#include "GroceryBill.h"
#include <iostream>
using namespace std;
GroceryBill::GroceryBill(const PriceList *priceList, double taxRate) {
this.itemList = priceList;
this.taxRate = taxRate;
billAmount = 0;
billItemCount = 0;
codes = new string[100];
qty = new double[100];
}
void GroceryBill::scanItem(string scanCode, double quantity) {
PriceListItem item = itemList.getItem(scanCode);
billAmount += item.getPrice()*quantity;
if(item.isTaxable())
billAmount += item.getPrice()*quantity*taxRate;
codes[billItemCount] = scanCode;
qty[billItemCount++] = quantity;
}
// 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);
if (myfile.is_open()) {
cout << "Successfully opened file " << filename << endl;
string code;
double qty;
while (myfile >> code >> qty) {
scanItem(code, qty);
}
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<<priceList.getItem(codes[i]).getItemName()<<" "<<qty[i]<<" ";
if(priceList.getItem(codes[i]).isTaxable())
cout<<"T";
cout<<endl;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.