HI, I have a C++ program below, and for a project have to do some editing to I w
ID: 3717983 • Letter: H
Question
HI, I have a C++ program below, and for a project have to do some editing to I was wondering if anyone could help, I would greatly appriciate it thanks.
The editing must include.......
. Modify the main file as follows :
a. Remove the declarations of product1, product2, product3, product4.
b. Declare an array of 50 elements of the productType.
c. Load the array with data form the attached file.
d. Display the following menu for the user :
Welcome to Jenna & Eddy's Appliance Store
To make a selection enter the number and press enter
1: Display the information for a specific product
2: Display the information for all the products
99: Exit the program.
The data for the attached file is
--------------------------------------------------------------------------------------
#include<iostream>
using namespace std;
class productType
{
public:
productType();
productType(int, double, double);
productType(string, int,double, double);
productType(string,string,string,int,double,double);
void set(string, string, string, int, double, double);
void print() const;
void setQuantitiesInStock(int x);
void updateQuantitiesInStock(int x);
int getQuantitiesInStock() const;
void setPrice(double x);
double getPrice() const;
void setDiscount(double d);
double getDiscount() const;
private:
string productName;
string id;
string manufacturer;
int quantitiesInStock;
double price;
double discount;
};
productType::productType()
{
productName=" ";
id=" ";
manufacturer=" ";
quantitiesInStock=0;
price=0.0;
discount=0.0;
}
productType::productType(int qis, double p, double d)
{
if(qis>=0&&p>=0&&d>=0)
{
productName=" ";
id= " ";
manufacturer=" ";
quantitiesInStock=qis;
price=p;
discount=d;
}
}
productType::productType(string i, int qis, double p ,double d)
{
if(qis>=0&&p>=0&&d>=0)
{
productName=" ";
id=i;
manufacturer=" ";
quantitiesInStock= qis;
price=p;
discount=d;
}
}
productType::productType( string pn, string i, string mf, int qis, double p, double d)
{
if(qis>=0 && p>=0 && d>=0)
{
productName=pn;
id=i;
manufacturer=mf;
quantitiesInStock=qis;
price=p;
discount=d;
}
}
void productType::set(string pn, string idNum, string mf, int qis,double p, double d)
{
if(qis>=0&&p>0&& d>=0)
{
productName=pn;
id= idNum;
manufacturer = mf;
quantitiesInStock = qis;
price = p;
discount = d;
}
}
void productType::print()const
{
cout<<endl;
cout<<productName<<endl;
cout<<id<< endl;
cout<<manufacturer<<endl;
cout<<quantitiesInStock<<endl;
cout<<price<<endl;
cout<<discount<<endl;
cout<<endl;
}
void productType::setQuantitiesInStock(int x)
{
quantitiesInStock =x;
}
void productType::updateQuantitiesInStock(int x)
{
quantitiesInStock=quantitiesInStock+x;
}
int productType::getQuantitiesInStock()const
{
return quantitiesInStock;
}
void productType::setPrice(double x)
{
price =x;
}
double productType::getPrice()const
{
return price;
}
void productType::setDiscount(double d)
{
discount = d;
}
double productType::getDiscount()const
{
return discount;
}
int main()
{
productType product1;
productType product2("Microwave", "M3562","GeneralPool", 35, 175.00, 0.1);
productType product3("D1290", 25,375.00, 0.05);
productType product4(10,8.50,0.2);
product1.set("Oven", "M3862", "GeneralPool", 35, 175.00, 5.5);
cout<<endl<<"After calling the set function ";
cout<<"values of member variables are as follows ";
product1.print();
product1.setQuantitiesInStock(10);
int q=product1.getQuantitiesInStock();
product1.updateQuantitiesInStock(5);
int q1= product1.getQuantitiesInStock();
product1.setDiscount(20);
double d = product1.getDiscount();
product1.setPrice(100);
double p = product1.getPrice();
cout<<endl<<"Quantity after calling ";
cout<<"setQuantityInStock ";
cout<<"function is"<<q<<endl;
cout<<"Quantity after calling ";
cout<<"updateQuantityInStock ";
cout<<"function and getQuantityInStock is "<<q1;
cout<< endl;
cout<<"and getPrice() function is"<<p<< endl;
cout<<endl<<"Discount after calling setDiscount ";
cout<<"function";
cout<<"and getDiscount() function is "<<d;
cout<< endl;
cout<< endl<< "After executing all functions ";
cout<<"the";
cout<<"values of member variables are as follows:";
product1.print();
}
Explanation / Answer
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
class productType {
public:
productType();
productType(int, double, double);
productType(string, int,double, double);
productType(string,string,string,int,double,double);
void set(string, string, string, int, double, double);
void print() const;
void setQuantitiesInStock(int x);
void updateQuantitiesInStock(int x);
int getQuantitiesInStock() const;
void setPrice(double x);
double getPrice() const;
void setDiscount(double d);
double getDiscount() const;
string getId();
private:
string productName;
string id;
string manufacturer;
int quantitiesInStock;
double price;
double discount;
};
productType::productType()
{
productName=" ";
id=" ";
manufacturer=" ";
quantitiesInStock=0;
price=0.0;
discount=0.0;
}
productType::productType(int qis, double p, double d)
{
if(qis>=0&&p>=0&&d>=0)
{
productName=" ";
id= " ";
manufacturer=" ";
quantitiesInStock=qis;
price=p;
discount=d;
}
}
productType::productType(string i, int qis, double p ,double d)
{
if(qis>=0&&p>=0&&d>=0){
productName=" ";
id=i;
manufacturer=" ";
quantitiesInStock= qis;
price=p;
discount=d;
}
}
productType::productType( string pn, string i, string mf, int qis, double p, double d)
{
if(qis>=0 && p>=0 && d>=0)
{
productName=pn;
id=i;
manufacturer=mf;
quantitiesInStock=qis;
price=p;
discount=d;
}
}
void productType::set(string pn, string idNum, string mf, int qis,double p, double d)
{
if(qis>=0&&p>0&& d>=0)
{
productName=pn;
id= idNum;
manufacturer = mf;
quantitiesInStock = qis;
price = p;
discount = d;
}
}
void productType::print()const
{
cout<<endl;
cout<<productName<<endl;
cout<<id<< endl;
cout<<manufacturer<<endl;
cout<<quantitiesInStock<<endl;
cout<<price<<endl;
cout<<discount<<endl;
cout<<endl;
}
void productType::setQuantitiesInStock(int x)
{
quantitiesInStock =x;
}
void productType::updateQuantitiesInStock(int x)
{
quantitiesInStock=quantitiesInStock+x;
}
int productType::getQuantitiesInStock()const
{
return quantitiesInStock;
}
void productType::setPrice(double x)
{
price =x;
}
double productType::getPrice()const
{
return price;
}
void productType::setDiscount(double d)
{
discount = d;
}
double productType::getDiscount()const
{
return discount;
}
string productType::getId() {
return id;
}
int main()
{
productType products[50];
string name;
string ID;
string manu;
int quan;
double p;
double d;
ifstream inFile;
inFile.open("input.txt");
int count = 0;
if (!inFile) {
cout << "Unable to open file";
exit(1); // terminate with error
}
while (inFile >> name >> ID >> manu >> quan >> p >> d) {
products[count].set(name, ID, manu, quan, p, d);
}
cout << "Welcome to Jenna & Eddy's Appliance Store" << endl;
cout << " To make a selection enter the number and press enter : " << endl;
cout << "1: Display the information for a specific product "
<< "2: Display the information for all the products "
<< "99: Exit the program. ";
int opt;
cin >> opt;
switch(opt) {
case 1:
{string iden;
cout << "Enter the product ID to be searched: ";
cin >> iden;
for(int i=0; i<count; i++) {
if(iden.compare(products[i].getId()) == 0) {
products[i].print();
break;
}
}
break;
}
case 2:
for(int i=0; i<count; i++) {
products[i].print();
cout << endl;
}
break;
case 99:
cout << "Exiting....";
return 0;
default:
cout << "Wrong choice....";
break;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.