Design class Seller to determine whether the seller has made profit or incurred loss. Also determine how much profit he made or loss he incurred. Cost price and selling price of an item is the data member of the class which is input data given by the user. Include the following member function
getItemDetails()- to get item details
dtermineProfitLoss()- to determine whether the seller has made profit or incurred loss
showItemDetails()- to get item details with Profit or loss.
Sample Input and Output
Test cases
Input
Output
Test case 1
800
800
No profit no loss
Test case 2
800
900
profit=100
Test case 3
800
700
#include <iostream>
#include <string>
class Seller
{
public:
double costprise;
double sellingprice;
std::string profit;
Seller() {}
Seller(double costprise, double sellingprice)
: m_costprice(costprise), m_sellingprice(sellingprice)
{}
void dtermineProfitLoss()
{
if (m_sellingprice == m_costprice)
m_profit = "No profit no loss";
else if (m_sellingprice > m_costprice)
m_profit = "profit " + std::to_string(m_sellingprice - m_costprice);
else if (m_sellingprice < m_costprice)
m_profit = "loss " + std::to_string(m_costprice - m_sellingprice);
}
void getItemDetails()
{
costprise = m_costprice;
sellingprice = m_sellingprice;
dtermineProfitLoss();
profit = m_profit;
}
void showItemDetails()
{
getItemDetails();
std::cout << costprise << std::endl;
std::cout << sellingprice<< std::endl;
std::cout << profit<< std::endl;
}
private:
double m_costprice{0};
double m_sellingprice{0};
std::string m_profit;
};
int main()
{
// example test
Seller t1(800, 800);
Seller t2(800, 900);
Seller t3(800, 700);
t1.showItemDetails();
t2.showItemDetails();
t3.showItemDetails();
return 0;
}
Comments
Leave a comment