Question
C++ ; Thank you in advance! Use the code developed below 1.1. Create an abstract class Item, having the abstract function: double GetTotalPrice() 2. Update
C++ ; Thank you in advance!
Use the code developed below
1.1. Create an abstract class Item, having the abstract function: double GetTotalPrice()
2. Update the Product class to inherit Item
Driver Program:
Change the driver program from the code below to:
(1) Remove the Products and Services vectors
(2) Add a new vector called Items, which contains pointers to Item instances
(3) Add at least 3 products and 3 services to the Items collection
(4) Use a loop to calculate and display the total prices of all elements in the Items collection
The code:
#include
#include
#include
#include
using namespace std;
//Enum for discount type
enum Discount_Type {amount,percentage};
//to store all the department id to check for duplication
set
//Department class which contains id, name,room phone number and parent department
class Department{
string id;
string name;
int room;
string phoneNumber;
Department *parent;
public:
//Empty constructor
Department(){}
//parametrized constructor
Department(string id,string name,int room,string phoneNumber,Department *parent){
//check for duplication of id
if(departmentID.count(id)>0){
cout<<"Oject Not created Due to Department ID already exist ";
return;
}else{
departmentID.insert(id);
}
this->id = id;
this->name = name;
this->room = room;
this->phoneNumber = phoneNumber;
this->parent = parent;
}
//this will return all the values of the object for printing
string toString(){
string output=" ";
output += "\tId: "+ id + " ";
output += "\tName: " +name + " ";
output += "\tRoom: " + to_string(room) + " ";
output += "\tPhoneNumber: " + phoneNumber + " ";
if(parent !=NULL){
output += "Parent Department: " + parent->toString();
}
return output;
}
};
//Product class whih Contains name, description category, department, price, discount,discount type
class Product{
protected:
string name;
string description;
string category;
Department department;
double price;
double discount;
int discount_type;
public:
//empty constructor
Product(){}
//parametrized constructor
Product(string name,string description,string category,Department department,float price,float discount,Discount_Type discount_type){
//if price value is negative then it returns with a messsage;
if(price < 0){
cout<<"No object create due to negative value of price ";
return;
}
//if discount value is negative then it returns with a messsage;
if(discount < 0){
cout<<"No object create due to negative value of discount ";
return;
}
//if every thing fine then we just simply add values to the variables
this->name = name;
this->description = description;
this->category = category;
this->department = department;
this->price = price;
this->discount = discount;
this->discount_type = discount_type;
}
//this will return the total price after neglect the discount
double getTotalPrice(){
//if discount is of type amount
if(discount_type == amount){
double finalP = price-discount;
if(finalP < 0 ){
cout<<"Total Price cannot be negative";
return 0;
}else{
return finalP;
}
}else{
//if the discount is of the type percentage
double finalP = price - (price * discount/100);
if(finalP < 0 ){
cout<<" Total Price cannot be negative ";
return 0;
}else{
return finalP;
}
}
}
//return the values of each variable to print in string form
virtual string toString(){
string output="";
output += "Name: " +name + " " ;
output += "Description: " + description + " " ;
output += "Category: " + category + " ";
output += "Department: " + department.toString() + " ";
double totalPrice = this->getTotalPrice();
output += "Total Price " + to_string(totalPrice) + " ";
return output;
}
};
//Service class which contains duration and it import the class Product in public accessor mode
class Service : public Product{
double duration;
//Call the product i.e. super class constructor
public:
Service(string name, string description,string category,Department department,double duration,double rate,double rate_discount,Discount_Type discount_type):Product(name,description,category,department,rate,rate_discount,discount_type){
this->duration = duration;
}
//this will return all the variables value
virtual string toString2(){
string output ="";
output += toString();
output += "Duration: "+ to_string(duration) + " ";
return output;
}
};
int main(){
//create vector of all the class
vector
vector
vector
// add 3 objects of departments
Department d1("ABCD-EFGH-1234","Electronics",260,"0888123456",NULL);
Department d2("ABCD-EFGH-8965","Mechanical",452,"0963258741",NULL);
Department d3("ABCD-EFGH-7412","IT",153,"0258777412",&d1);
//add department object to the vector
department.push_back(d1);
department.push_back(d2);
department.push_back(d3);
//create object of products
Product p1("PC Tools","Tools used to design, repair or develop an PC","Games",d3,185.99,30,amount);
Product p2("Car Tools","Tools used to repair cars i.e. open and closing","Repair",d2,199.99,15,percentage);
Product p3("Bulb","Led bulb operate on very low power","Electonics",d1,400.56,80,amount);
//add product objects to the vector
product.push_back(p1);
product.push_back(p2);
product.push_back(p3);
//Create objects of service
Service s1("Repairing Cars","Repairing of Cars and automobile","Cars",d2,2,20.20,10,percentage);
Service s2("Web Developer","crete any web app or android app","IT",d3,12,500,50,amount);
Service s3("Repairing of Electronics tools","Repairing of CPU,montior","Electornics",d1,1.5,8.50,30,percentage);
//add service objects to vector
service.push_back(s1);
service.push_back(s2);
service.push_back(s3);
//print product vector data using for loop
cout<<"Products ";
cout<<"================================ ";
for(int i=0;i cout< cout< cout<<"======================================== "; } //print service vector using for loop cout<<" Services "; cout<<"================================ "; for(int i=0;i cout< cout< cout<<"======================================== "; } //check duplication of id cout<<" Check duplication of id "; Department d4("ABCD-EFGH-7412","IT",153,"0258777412",&d1); //check for negative price entered cout<<" check for negative price entered "; Product p4("PC Tools","Tools used to design, repair or develop an PC","Games",d3,-185.99,30,amount); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started