Design and implement a simple console application in C++ that models an online store inventory system, based on the requirements and class relationships described below:
Class Descriptions:
Item: Represents a generic item sold in the online store. Each item is characterized by a unique identifier (ID), a name, and a price. The Item class should provide methods to get and set these properties. Additionally, it should include a method to print item details.
Vendor: Represents a vendor supplying items to the store. A vendor is described by a vendor ID, a name, and an address. This class should provide methods to get and set these properties. The toString() method should return a string representation of the vendor, including all its details.
StoreItem: Extends the Item class. In addition to the properties inherited from Item, StoreItem includes a property to store the vendor (supplier) of the item. The class should override the method to print item details to include vendor information if available. It should also include a method getVendorName() that returns the name of the vendor if set, or "Unknown" if not.
Implementation Details:
•
The StoreItem class should be initialized with an ID, name, and price. Vendor details should be set using a separate method, setVendor(), which takes a Vendor object as a parameter.
•
The toString() method in both Vendor and StoreItem should return a string representation of the object's state in a readable format. For StoreItem, implement two versions of toString(): one that only returns item details and another that also includes vendor details based on a boolean parameter.
Application Flow:
•
Your console application should prompt the user to enter details for a list of items and their corresponding vendors.
•
After entering the details, the application should display the information of each StoreItem using the toString() method, first without vendor details and then with vendor details.
•
It should also demonstrate the use of getVendorName() for each StoreItem.
Here's a simple console application in C++ that models an online store inventory system based on your requirements:
#include <iostream>
#include <vector>
#include <string>
// Item class
class Item {
private:
int id;
std::string name;
double price;
public:
Item(int id, const std::string& name, double price) : id(id), name(name), price(price) {}
int getId() const { return id; }
void setId(int id) { this->id = id; }
std::string getName() const { return name; }
void setName(const std::string& name) { this->name = name; }
double getPrice() const { return price; }
void setPrice(double price) { this->price = price; }
virtual std::string toString() const {
return "ID: " + std::to_string(id) + ", Name: " + name + ", Price: " + std::to_string(price);
}
virtual void print() const {
std::cout << toString() << std::endl;
}
};
// Vendor class
class Vendor {
private:
int vendorId;
std::string name;
std::string address;
public:
Vendor(int vendorId, const std::string& name, const std::string& address)
: vendorId(vendorId), name(name), address(address) {}
int getVendorId() const { return vendorId; }
void setVendorId(int vendorId) { this->vendorId = vendorId; }
std::string getName() const { return name; }
void setName(const std::string& name) { this->name = name; }
std::string getAddress() const { return address; }
void setAddress(const std::string& address) { this->address = address; }
std::string toString() const {
return "Vendor ID: " + std::to_string(vendorId) + ", Name: " + name + ", Address: " + address;
}
};
// StoreItem class
class StoreItem : public Item {
private:
Vendor* vendor;
public:
StoreItem(int id, const std::string& name, double price)
: Item(id, name, price), vendor(nullptr) {}
void setVendor(Vendor* vendor) { this->vendor = vendor; }
std::string getVendorName() const {
return vendor ? vendor->getName() : "Unknown";
}
std::string toString(bool includeVendor = false) const {
std::string result = Item::toString();
if (includeVendor && vendor) {
result += ", Vendor: [" + vendor->toString() + "]";
}
return result;
}
void print() const override {
std::cout << toString() << std::endl;
}
};
int main() {
std::vector<StoreItem> items;
std::vector<Vendor> vendors;
int itemCount, vendorCount;
std::cout << "Enter number of items: ";
std::cin >> itemCount;
std::cout << "Enter number of vendors: ";
std::cin >> vendorCount;
for (int i = 0; i < vendorCount; ++i) {
int id;
std::string name, address;
std::cout << "Enter vendor ID: ";
std::cin >> id;
std::cout << "Enter vendor name: ";
std::cin.ignore();
std::getline(std::cin, name);
std::cout << "Enter vendor address: ";
std::getline(std::cin, address);
vendors.emplace_back(id, name, address);
}
for (int i = 0; i < itemCount; ++i) {
int id;
std::string name;
double price;
std::cout << "Enter item ID: ";
std::cin >> id;
std::cout << "Enter item name: ";
std::cin.ignore();
std::getline(std::cin, name);
std::cout << "Enter item price: ";
std::cin >> price;
StoreItem item(id, name, price);
int vendorChoice;
std::cout << "Select vendor for the item (enter vendor ID, 0 for none): ";
std::cin >> vendorChoice;
if (vendorChoice != 0) {
for (auto& vendor : vendors) {
if (vendor.getVendorId() == vendorChoice) {
item.setVendor(&vendor);
break;
}
}
}
items.push_back(item);
}
std::cout << "\nStore Items:\n";
for (const auto& item : items) {
item.print();
}
std::cout << "\nStore Items with Vendor Details:\n";
for (const auto& item : items) {
std::cout << item.toString(true) << std::endl;
}
std::cout << "\nVendor Names for Each Item:\n";
for (const auto& item : items) {
std::cout << "Item " << item.getId() << " Vendor: " << item.getVendorName() << std::endl;
}
return 0;
}
This C++ program follows the given specifications:
Item
, Vendor
, and StoreItem
classes with appropriate properties and methods.StoreItem
without vendor details, then with vendor details.getVendorName()
for each StoreItem
.To compile and run this program, you can use a C++ compiler like g++
:
g++ -o online_store online_store.cpp
./online_store
Answered By