In: Computer Science
Code in C++
Must show: unit testing
------------------------------------
UsedFurnitureItem
-------------------------------------
Test Program:
The test program will test each setter (for objects of both types in a sequence) by calling the setter to set a value and then call the corresponding getter to print out the set value. This test should be done twice on data members that could be set to invalid values (that have numerical or character data type) – once after trying to set invalid values and subsequently, once after setting them to valid values. The data members with string data types (model, description) can be tested just once.
SOLUTION-
I have solve the problem in C++ code with comments and screenshot for easy understanding :)
CODE-
UsedFurnitureItem.hpp
#ifndef USED_FURNITURE_ITEM_H
#define USED_FURNITURE_ITEM_H
#include<string>
using namespace std;
//class declaration
class UsedFurnitureItem
{
private: //private members
double age;
double brandNewPrice;
string description;
char condition;
double size;
double weight;
double calculateCurrentPrice();
double calculateShippingCost(double);
public: //public members
UsedFurnitureItem(double,double,string,char,double,double);
void printInvoice(double);
};
#endif
UsedFurnitureItem.cpp
#include <iostream>
#include "UsedFurnitureItem.hpp"
using namespace std;
//constructor
UsedFurnitureItem::UsedFurnitureItem(double ag,double price,string
desc,char cond,double sz,double wt )
{
age=ag;
description=desc;
brandNewPrice=price;
condition=cond;
size=sz;
weight=wt;
}
//function to calcul;ate price
double UsedFurnitureItem::calculateCurrentPrice()
{
double price=0;
if(condition=='A' ||condition=='a') //if condition is A
{
if(age<=7) //if age is less than 7
{
double percent=age*10.0; //percentage depreciation
price=((100-percent)/100)*brandNewPrice; //final price
}
else //if age is more than 7
price=0.3*brandNewPrice; //30% of new price
}
else if(condition=='B' ||condition=='b') //if condition is
B
{
if(age<=5) //if age is less than 5
{
int percent=age*15.0; //percentage depreciation
price=((100-percent)/100)*brandNewPrice; //final price
}
else //if age is more than 5
price=0.2*brandNewPrice; //20% of new price
}
else //if condition is C
{
price=0.1*brandNewPrice; //price is 10% of new price
}
return price; //return price
}
//function to calculate shipping cost
double UsedFurnitureItem::calculateShippingCost(double miles)
{
double cost;
if(weight<=20 && size <=1000)
cost=miles; //1 cent per mil
else
cost=2*miles; //2 cents per mile
return cost/100; //returning in dollars
}
//function to print invoice
void UsedFurnitureItem::printInvoice(double miles)
{
cout<<"Age: "<<age<<endl;
cout<<"Brand New Price:
$"<<brandNewPrice<<endl;
cout<<"Description: "<<description<<endl;
cout<<"Condition: "<<condition<<endl;
cout<<"Size: "<<size<<endl;
cout<<"Weight: "<<weight<<endl;
cout<<"Calculated current price:
$"<<calculateCurrentPrice()<<endl; //calling function
fro price calculator
cout<<"Calculated shipping cost:
$"<<calculateShippingCost(miles)<<endl; //calling
function to calculate shipment cost
cout<<endl;
}
main.cpp
#include <iostream>
#include "UsedFurnitureItem.hpp"
using namespace std;
//main
int main()
{
//variables for different inputs
double age;
double brandNewPrice;
string description;
char condition;
double size;
double weight;
double miles;
//prompt for different inputs and reading those input
cout << "Enter the age of Item: ";
cin>>age; //enter age
cout << "Enter the Brand New Price of Item: ";
cin>>brandNewPrice;
cout << "Enter the condition of Item(A,B,C): ";
cin>>condition;
cout << "Enter the Description of Item: ";
cin.ignore(); //clearing the buffer of cin
getline(cin,description);
cout << "Enter the Size of Item: ";
cin>>size; //enter size
cout << "Enter the Weight of Item: ";
cin>>weight;
cout << "Enter the Shipping miles: ";
cin>>miles; //enter miles for shipping
//creating object of UsedFurnitureItem with parameters
UsedFurnitureItem
item(age,brandNewPrice,description,condition,size,weight);
item.printInvoice(miles); //printing invoice for item
return 0;
}
SCREENSHOT -
IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I
WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK
YOU!!!!!!!!----------