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.
#include <iostream>
#include <iomanip>
using namespace std;
class UsedFurnitureItem
{
private:
// Declaring instance variables
double age;
double brandNewPrice;
string description;
char condition;
double size;
double weight;
public:
// Zero argumented constructor
UsedFurnitureItem()
{
this->age = 1.0;
this->brandNewPrice = 10.00;
this->description = "Not available";
this->condition = 'A';
this->size = 1.0;
this->weight = 1.0;
}
// Parameterized constructor
UsedFurnitureItem(double age, double brandNewPrice, string
description, char condition,
double size, double weight)
{
this->age;
this->brandNewPrice = 10.00;
this->description = "Not available";
this->condition = 'A';
this->size = 1.0;
this->weight = 1.0;
}
// This function will calculate the current price
double calculateCurrentPrice()
{
double decAmt = 0.0;
if (condition == 'A')
{
decAmt = brandNewPrice * 0.10;
if (age <= 7)
{
for (int i = 1; i <= age; i++)
{
brandNewPrice -= decAmt;
}
}
else if (age > 7)
{
for (int i = 1; i <= 7; i++)
{
brandNewPrice -= decAmt;
}
}
}
else if (condition == 'B')
{
decAmt = brandNewPrice * 0.15;
if (age <= 5)
{
for (int i = 1; i <= age; i++)
{
brandNewPrice -= decAmt;
}
}
else if (age > 5)
{
for (int i = 1; i <= 5; i++)
{
brandNewPrice -= decAmt;
}
}
}
else if (condition == 'C')
{
decAmt = brandNewPrice * 0.10;
for (int i = 1; i <= age; i++)
{
if (brandNewPrice - decAmt >= 0)
brandNewPrice -= decAmt;
}
}
}
// This function will calculate the shipping cost
double CalculateShippingCost(int miles)
{
int cents = 0;
if (size < 1000 || weight < 20)
{
cents = miles;
}
else if (size >= 1000 || weight >= 20)
{
cents = miles * 2;
}
return (cents / 100.0);
}
// Setters and getters
double getAge()
{
return age;
}
void setAge(double age)
{
this->age = age;
}
double getBrandNewPrice()
{
return brandNewPrice;
}
void setBrandNewPrice(double brandNewPrice)
{
this->brandNewPrice = brandNewPrice;
}
string getDescription()
{
return description;
}
void setDescription(string description)
{
this->description = description;
}
char getCondition()
{
return condition;
}
void setCondition(char condition)
{
this->condition = condition;
}
double getSize()
{
return size;
}
void setSize(double size)
{
this->size = size;
}
double getWeight()
{
return weight;
}
void setWeight(double weight)
{
this->weight = weight;
}
};
int main()
{
//setting the precision to two
decimal places
std::cout << std::setprecision(2) <<
std::fixed;
// Creating an instance of UsedFurnitureItem class
UsedFurnitureItem ufi;
// Calling the setters on the UsedFurnitureItem instance
ufi.setAge(12);
ufi.setBrandNewPrice(120);
ufi.setCondition('A');
ufi.setDescription("Sofa Set");
ufi.setSize(1500);
ufi.setWeight(25);
// Calling the getters on the UsedFurnitureItem instance
cout << "Description :" << ufi.getDescription()
<< endl;
cout << "Age :" << ufi.getAge() << endl;
cout << "Brand New Price :$" << ufi.getBrandNewPrice()
<< endl;
cout << "Condition :" << ufi.getCondition() <<
endl;
cout << "Size :" << ufi.getSize() << " cubic
Inches." << endl;
cout << "Weight " << ufi.getWeight() << " pounds"
<< endl;
cout << "\nCurrent Price :$" <<
ufi.calculateCurrentPrice() << endl;
cout << "Shipping Cost :$" <<
ufi.CalculateShippingCost(1500) << endl;
return 0;
}
/***************************************************/
/***************************************************/
output:
/***************************************************/