Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you find the mistake in the code i pasted bellow and help me with the following task: Implement a function to apply the Quicksort

Can you find the mistake in the code i pasted bellow and help me with the following task:
Implement a function to apply the Quicksort or Mergesort sorting algorithm. If your ID number is odd
implement Quicksort, otherwise (if even) implement Mergesort.
Driver Program:
(1) Modify the classes code (if needed) and write a program to sort the collection of Items based on
their total price.
(2) After the collection is sorted, use a loop to print the name, category and total price of each
element in the Items list.
#include
#include
#include
#include
enum DiscountTypeEnum { AMOUNT, PERCENTAGE };
class Item {
public:
virtual double GetTotalPrice() const =0;
virtual ~Item()= default; // Use the default virtual destructor for proper cleanup
};
class Division {
public:
std::string GUID;
std::string Name;
std::string PhoneNumber;
std::string Description;
Division* Parent;
Division(std::string guid, std::string name, std::string phone, std::string desc, Division* parent)
: GUID(guid), Name(name), PhoneNumber(phone), Description(desc), Parent(parent){}
};
class Artifact : public Item {
public:
std::string GUID;
std::string Name;
std::string Description;
std::string Category;
Division* DivisionPtr;
double Price;
double Discount;
DiscountTypeEnum DiscountType;
int Quantity;
Artifact(std::string guid, std::string name, std::string desc, std::string category,
Division* division, double price, double discount, DiscountTypeEnum discountType, int quantity)
: Item(), GUID(guid), Name(name), Description(desc), Category(category),
DivisionPtr(division), Price(price), Discount(discount), DiscountType(discountType), Quantity(quantity){}
double GetEffectivePrice() const {
if (DiscountType == AMOUNT)
return std::max(0.0, Price - Discount);
else
return std::max(0.0, Price - Price * Discount /100.0);
}
double GetTotalPrice() const override {
return std::max(0.0, Quantity * GetEffectivePrice());
}
};
class Service : public Item {
public:
double Duration;
double Rate;
double RateDiscount;
DiscountTypeEnum RateDiscountType;
Service(std::string guid, std::string name, std::string desc, std::string category,
Division* division, double price, double discount, DiscountTypeEnum discountType, int quantity,
double duration, double rate, double rateDiscount, DiscountTypeEnum rateDiscountType)
: Item(), Artifact(guid, name, desc, category, division, price, discount, discountType, quantity),
Duration(duration), Rate(rate), RateDiscount(rateDiscount), RateDiscountType(rateDiscountType){
}
double GetEffectiveRate() const {
if (RateDiscountType == AMOUNT)
return std::max(0.0, Rate - RateDiscount);
else
return std::max(0.0, Rate - Rate * RateDiscount /100.0);
}
double GetTotalPrice() const override {
return std::max(0.0, Artifact::GetTotalPrice()+ GetEffectiveRate()* Duration);
}
};
int main(){
std::vector items;
items.push_back(new Artifact("ART1", "Laptop", "Sony Vaio", "Electronics",
nullptr, 185.99,30, PERCENTAGE, 5));
items.push_back(new Artifact("ART2", "Smartphone", "iPhone 12", "Electronics",
nullptr, 999.99,50, AMOUNT, 3));
items.push_back(new Artifact("ART3","T-shirt", "Cotton Casual Tee", "Clothing",
nullptr, 19.99,5,
items.push_back(new Service("SER1", "Custom Software Development", "Bespoke software solutions",
"Custom Development", nullptr, 0.0,0, AMOUNT, 1,10.5,50.0,10, PERCENTAG));
items.push_back(new Service("SER2", "Graphic Design Service", "Creative graphic design services",
"Design", nullptr, 0.0,0, AMOUNT, 2,5.0,20.0,5, PERCENTAGE));
items.push_back(new Service("SER3", "Furniture Assembly Service", "Professional furniture assembly",
"Assembly", nullptr, 0.0,0, AMOUNT, 1,2.5,15.0,3, PERCENTAGE));
std::cout << "Total Prices of Items:
";
double totalPrices =0.0;
for (Item* item : items){
double itemTotalPrice = item->GetTotalPrice();
totalPrices += itemTotalPrice;
std::cout << "Total Price: $"<< itemTotalPrice << std::endl;
}
std::cout << "Overall Total Price: $"<< totalPrices << std::endl;
for (auto& item : items)
delete item;
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Learning MySQL Get A Handle On Your Data

Authors: Seyed M M Tahaghoghi

1st Edition

0596529465, 9780596529468

More Books

Students also viewed these Databases questions