In: Computer Science
In C++
Antique Class
Each Antique object being sold by a Merchant and will have the following attributes:
And will have the following member functions:
<Antique.name>: $<price.2digits>
Merchant Class
A Merchant class will be able to hold 10 different Antique objects and interact with the user to sell to them. It should include the following attributes:
And will have the following member functions:
You have successfully haggled and everything is 10% off.
Customer cannot haggle more than once. If he tries to haggle more than once, it will print the following:
Sorry, you have already haggled.
Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change.
If you are satisfied with the solution, please rate the answer. Let me know for any help with any other questions.
Thank You !
===========================================================================
#include<iostream>
#include<string>
using namespace std;
class Antique{
private:
string name;
float price;
public:
Antique(string name="", float
price=0.0);
string getName() const;
float getPrice() const;
void setName(string name);
void setPrice(float price);
string toString() ;
};
Antique::Antique(string name, float price):
name(name),price(price){}
string Antique::getName() const{return name;}
float Antique::getPrice() const{return price;}
void Antique::setName(string name){this->name=name;}
void Antique::setPrice(float price){this->price=price;}
string Antique::toString() {
return name +": Price $" +to_string(price);
}
class Merchant{
private:
Antique antique[10];
int quantity[10];
float revenue;
int haggled;
public:
Merchant(Antique antiques[], int
quantities[]);
void haggle();
float getRevenue() const;
};
Merchant::Merchant(Antique antiques[], int quantities[]){
haggled =0;
for(int i=0; i<10;i++){
antique[i]=antiques[i];
quantity[i]=quantities[i];
revenue +=
antique[i].getPrice()*quantity[i];
}
}
void Merchant::haggle(){
if(haggled==1){
cout<<"Sorry, you have
already haggled.\n";
return;
}
double price;
for(int i=0; i<10;i++){
price =
antique[i].getPrice()*0.9;
antique[i].setPrice(price);
}
haggled+=1;
cout<<"You have successfully haggled and
everything is 10% off.\n";
}
int main(){
Antique
antiques[]={Antique("Ruby",12.99),Antique("Ruby",16.99),Antique("Pearl",18.99),Antique("Diamond",10.99),Antique("Pearl",42.99),
Antique("Diamond",555.99),Antique("Diamond",55.99),Antique("Pearl",412.99),Antique("Pearl",12.99),Antique("Diamond",45.99)};
int quantities[] ={1,2,3,4,5,6,7,8,9,0};
Merchant merchant(antiques,quantities);
merchant.haggle();
merchant.haggle();
}
===================================================================
