C++ Programming Project Instructions Complete the following programming project
ID: 3917168 • Letter: C
Question
C++
Programming Project Instructions Complete the following programming project according to the given specifications. You may need to review concepts from several units of the course to implement all functionality of this project. Grocery Store Application You are to create a grocery store application that uses Object Oriented Programming techniques. All classes should contain (at a minimum) private data members, public accessor and mutator methods for all the private data members (use enumerated types where appropriate), public constructors (default, parameter, and copy). Only class accessor methods for the private data members may be declared inline. Implement friend functions and overloaded operators as needed. In addition to general C++ programming constructs, you will be graded on the application of Object Oriented Programming constructs to add any class methods that are appropriate and reasonable for your problem implementation. Create a GrItem class that contains only the following private data members: Data Member Data Type Description name C++ string name of product quantity integer number of items purchased regular price floating point suggested retail price sale price floating point product sale price on sale Boolean flag indicating whether product is on sale or not tax category enumerated type category used to determine the item’s tax rate (alcohol, food, general merchandise, or medicine) Tax rates for the enumerated tax category type are (these should be static GrItem constants):
Tax Category
alcohol 8%
food 5%
general merchandise 7%
medicine 4%
The program should perform the following listed activities for full possible credit: • create an array of objects to contain all the grocery items read from a file (maximum 20). Alternatively, you may use the STL vector container instead of a fixed size array.
Explanation / Answer
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
// Programming Project Instructions Complete the following programming project according to the given specifications.
// You may need to review concepts from several units of the course to implement all functionality of this project.
// Grocery Store Application You are to create a grocery store application that uses Object Oriented Programming techniques.
// All classes should contain (at a minimum) private data members,
// public accessor and mutator methods for all the private data members
// (use enumerated types where appropriate),
// public constructors (default, parameter, and copy).
// Only class accessor methods for the private data members may be declared inline.
// Implement friend functions and overloaded operators as needed.
// In addition to general C++ programming constructs,
// you will be graded on the application of Object Oriented Programming constructs
// to add any class methods that are appropriate and reasonable for your problem implementation.
// Create a GrItem class that contains only the following private data members:
// Data Member Data Type Description name C++ string name of product quantity integer number
// of items purchased regular price floating point suggested retail price sale price floating
// point product sale price on sale Boolean flag indicating whether product is on sale or not tax
// category enumerated type category used to determine the item’s tax rate
// (alcohol, food, general merchandise, or medicine) Tax rates for the enumerated tax category type are
// (these should be static GrItem constants):
// Tax Category
// alcohol 8%
// food 5%
// general merchandise 7%
// medicine 4%
// The program should perform the following listed activities for full possible credit:
// create an array of objects to contain all the grocery items read from a file (maximum 20).
// Alternatively, you may use the STL vector container instead of a fixed size array.
//Grocery Store Application
//catergories for Taxation
enum categoryType { alcohol=8, food=5 , generalMerchandise=7, medicine=4, None=0};
//class Defintion
class GrItem{
string productName;
int quantity;
double retailPrice;
bool saleStatus;
categoryType category;
public:
GrItem();
GrItem( string productName,int quantity, double retailPrice,bool saleStatus, categoryType category ); // simple constructor
GrItem( const GrItem &obj);
string getProductName()const ;
int getQuantity()const ;
double getRetailPrice()const ;
bool getSaleStatus()const ;
categoryType getCategory()const ;
void setProductName(string productName);
void setQuantity(int quantity);
void setRetailPrice(double retailPrice);
void setSaleStatus(bool saleStatus);
void setCategory(categoryType category);
friend ostream & operator<< (ostream &out, const GrItem &obj);
friend void displayDetails(vector<GrItem>& items);
friend void displayCalculateTotalPrice(vector<GrItem>& items);
};
//default constructor
GrItem::GrItem() {
productName="";
quantity=0;
retailPrice=0.0;
saleStatus=false;
category=None;
}
//parmaeterized constructor
GrItem::GrItem(string productName,int quantity, double retailPrice,bool saleStatus, categoryType category ) {
this->productName=productName;
this->quantity=quantity;
this->retailPrice=retailPrice;
this->saleStatus=saleStatus;
this->category=category;
}
// copy consttructor
GrItem::GrItem(const GrItem &obj) {
this->productName=obj.productName;
this->quantity=obj.quantity;
this->retailPrice=obj.retailPrice;
this->saleStatus=obj.saleStatus;
this->category=obj.category;
}
//accessor methods
string GrItem::getProductName() const {
return productName;
}
int GrItem::getQuantity() const {
return quantity;
}
double GrItem::getRetailPrice()const {
return retailPrice;
}
bool GrItem::getSaleStatus()const {
return saleStatus;
}
categoryType GrItem::getCategory()const {
return category;
}
//setter methods
void GrItem::setProductName(string productName){
this->productName = productName ;
}
void GrItem::setQuantity(int quantity){
this->quantity = quantity;
}
void GrItem::setRetailPrice(double retailPrice){
this->retailPrice = retailPrice;
}
void GrItem::setSaleStatus(bool saleStatus){
this->saleStatus = saleStatus;
}
void GrItem::setCategory(categoryType category){
this->category = category;
}
//overloaded << operator
ostream & operator<< (ostream &out, const GrItem &obj)
{
out << "GrItem(" << "ProductName= "<< obj.getProductName() << ", "
<< "quantity= "<< obj.getQuantity() << ", "
<< "retailPrice= "<< obj.getRetailPrice() << ", "
<< "saleStatus= "<< obj.getSaleStatus() << ", "
<< "category= "<< obj.getCategory() << ")"<<endl;
return out;
}
//friend display the GrItem items
void displayDetails(vector<GrItem>& items){
vector<GrItem>::iterator ptr;
for (ptr = items.begin(); ptr < items.end(); ptr++)
cout << "ProductName= "<< (*ptr).getProductName() << ", "
<< "quantity= "<< (*ptr).getQuantity() << ", "
<< "retailPrice= "<< (*ptr).getRetailPrice() << ", "
<< "saleStatus= "<< (*ptr).getSaleStatus() << ", "
<< "category= "<< (*ptr).getCategory() << ", "
<< "TotaPrice = "<< (*ptr).getQuantity() * (*ptr).getRetailPrice() << ", "
<< "TotaPrice with Tax = "<< (*ptr).getQuantity() * (*ptr).getRetailPrice() +
(*ptr).getQuantity() * (*ptr).getRetailPrice() * ((*ptr).getCategory()/double(100)) <<endl;
}
//friend calculated the grandTotal and grandTotalwithTax
void displayCalculateTotalPrice(vector<GrItem>& items){
vector<GrItem>::iterator ptr;
double grandTotal = 0.0;
double grandTotalWithTax = 0.0;
// grand Total with tax is quantity*price + quantity*price*category_percent
for (ptr = items.begin(); ptr < items.end(); ptr++){
grandTotal += (*ptr).getQuantity() * (*ptr).getRetailPrice();
grandTotalWithTax += (*ptr).getQuantity() * (*ptr).getRetailPrice()
+ (*ptr).getQuantity() * (*ptr).getRetailPrice() * ((*ptr).getCategory()/double(100));
}
cout << "The Grand Total of items is : " <<grandTotal << endl;
cout << "The Grand Total with Tax of items is : " <<grandTotalWithTax << endl;
}
int main(){
//vector of objects
vector<GrItem> items;
vector<GrItem>::iterator ptr;
ifstream myfile("input_grocery_list.txt");
//file check
if (!myfile)
{
cout << ("Error: input_grocery_list.txt not found");
exit (1);
}
string productName;
int quantity;
double retailPrice;
bool saleStatus;
int category_int;
//read from input file
while(myfile >> productName >> quantity >> retailPrice >> saleStatus>> category_int)
{
//add to vector of objects
items.push_back(GrItem(productName, quantity, retailPrice, saleStatus, (categoryType)(category_int)));
}
// cout << "The vector elements are : ";
// for (ptr = items.begin(); ptr < items.end(); ptr++)
// cout << *ptr << " ";
// dispaly grocrey details and Grand total
displayDetails(items);
displayCalculateTotalPrice(items);
return 0;
}
input file
watch 10 23.50 1 7
Bacardi 7 10.50 1 8
Sushi 5 89.50 1 5
Paracetomol 15 34.50 1 4
output displayed:
ProductName= watch, quantity= 10, retailPrice= 23.5, saleStatus= 1, category= 7, TotaPrice = 235, TotaPrice with Tax = 251.45
ProductName= Bacardi, quantity= 7, retailPrice= 10.5, saleStatus= 1, category= 8, TotaPrice = 73.5, TotaPrice with Tax = 79.38
ProductName= Sushi, quantity= 5, retailPrice= 89.5, saleStatus= 1, category= 5, TotaPrice = 447.5, TotaPrice with Tax = 469.875
ProductName= Paracetomol, quantity= 15, retailPrice= 34.5, saleStatus= 1, category= 4, TotaPrice = 517.5, TotaPrice with Tax = 538.2
The Grand Total of items is : 1273.5
The Grand Total with Tax of items is : 1338.9
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.