Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

hello, can you help with the tasks below using the code i put at the end: Implement a function to apply the Quicksort or Mergesort

hello, can you help with the tasks below using the code i put at the end:
Implement a function to apply the Quicksort or Mergesort sorting algorithm. If your ID number is odd
implement Quicksort, otherwise (if even) implement Mergesort-the ID is200192232
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.
Here is the code:
#include
#include
#include
enum DiscountTypeEnum { AMOUNT, PERCENTAGE };
class Item {
public:
virtual double GetTotalPrice() const =0;
virtual ~Item()= default;
};
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(), Duration(duration), Rate(rate), RateDiscount(rateDiscount), RateDiscountType(rateDiscountType)
std::string artifactGuid = guid;
std::string artifactName = name;
std::string artifactDesc = desc;
Division* artifactDivision = division;
double artifactPrice = price;
double artifactDiscount = discount;
DiscountTypeEnum artifactDiscountType = discountType;
int artifactQuantity = quantity;
Artifact artifact(artifactGuid, artifactName, artifactDesc, category, artifactDivision, artifactPrice, artifactDiscount, artifactDiscountType, artifactQuantity);
}
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, 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, AMOUNT, 10));
// Add services to the items vector
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, PERCENTAGE));
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::end;
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_2

Step: 3

blur-text-image_3

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

Database Theory Icdt 99 7th International Conference Jerusalem Israel January 10 12 1999 Proceedings Lncs 1540

Authors: Catriel Beeri ,Peter Buneman

1st Edition

3540654526, 978-3540654520

More Books

Students also viewed these Databases questions