I had to design something simple in c++ but i don't know if my design is the correct one. I mean in sense of re-usability. How can I find out if I have done a good design and ho can i further optimize the design. The task as a simple e-shop where someone could see the products, order a product and define it's characteristics. And in the end to print out the result of the order. I am posting the hole code.
#pragma once
#include <iostream>
#include <string>
#include <vector>
// Create an Interface for Product Objects
class IProducts
{
public:
// Virtual Function to get the name of the product implementing the interface
virtual std::string getProductName() = 0;
// Virtual Function to Display the names of all components of a class implementing the interface
virtual void DisplayComponents() = 0;
// Virtual Function to display the values of the components of a class implementing the interface
virtual void Display() = 0;
// Virtual Function to set the components to desired values
virtual void setAttributes() = 0;
};
// Concretion of Product Interface
class PC_Towers : public IProducts
{
public:
// Function to set the member variables of the class
void setAttributes ()
{
std::cout << "Please enter Memory size for PC_Tower in GB : ";
// MAke sure that the input in numeric
while(!(std::cin >> this->Memory))
{
std::cout << "All input's must be numeric " << std::endl;
break;
}
std::cout << "Please enter CPU size for PC_Tower in GHz : ";
while (!(std::cin >> this->CPU))
{
std::cout << "All input's must be numeric " << std::endl;
break;
};
}
// Function to get the Name of the product
std::string getProductName() { return this->productName; }
// Function to display the names of the components of the class
void DisplayComponents() { std::cout<<"The Tower is composed from : 1) Memory 2) CPU " << std::endl; }
// Function to display the values of the member variables
void Display()
{
std::cout << "Your Tower has a Memory of " << this->Memory << " GB and a CPU of " << this->CPU << " GHZ" << std::endl;
}
private:
double Memory;
double CPU;
const std::string productName = "PC_Tower";
};
// Another concrition on the IProduct interface the same as the one before
class PC_Screen : public IProducts
{
public:
void setAttributes ()
{
std::cout << "Please enter size of your Screen in inches: " ;
while (!(std::cin >> this->Size_inch))
{
std::cout << "All input's must be numeric " << std::endl;
break;
}
}
std::string getProductName() { return this->productName; }
void DisplayComponents() { std::cout << "The screen is composed from a screen measured in inches " << std::endl; }
void Display()
{
std::cout << "Your screen is " << this->Size_inch << " inches " << std::endl;
}
private:
double Size_inch;
const std::string productName = "PC_Screen";
};
// Concrition of IProducts
class Personal_Computer : public IProducts
{
public:
// Function to set the attributes of the member variable. In this case the function works as a decorator
// arround the setAttributes of the IProduct adding functionalities to it
void setAttributes()
{
Tower.setAttributes();
Screen.setAttributes();
std::cout << " Please enter size of your HardDics in GB : " ;
while (!(std::cin >> this->HardDisc))
{
std::cout << "All input's must be numeric " << std::endl;
break;
}
}
std::string getProductName() { return this->productName; }
// Decorate the DisplayComponents() and add functionalities
void DisplayComponents()
{
std::cout << "Personal Computer is composed from: 1) Tower 2) PC Screen 3) Hard Disc" << std::endl;
Tower.DisplayComponents();
Screen.DisplayComponents();
}
// Decorate the Display() and add functionalities
void Display()
{
Tower.Display();
Screen.Display();
std::cout << "Your Hard Disc has size : " << this->HardDisc << " GB " << std::endl;
}
private:
PC_Towers Tower;
PC_Screen Screen;
double HardDisc;
const std::string productName = "Personal_Computer";
};
// Concretion of Iproduct
class Work_Station : public IProducts
{
public:
void setAttributes()
{
Computer.setAttributes();
std::cout << "Please Enter your Operating System " ;
while (!(std::cin >> this->OperatingSystem))
{
std::cout << "Operating system must be string " << std::endl;
break;
}
}
std::string getProductName() { return this->productName; }
void DisplayComponents()
{
std::cout << "Work station is composed from : 1) Personal computer 2) Operating System (Linux or Windows) " << std::endl;
Computer.DisplayComponents();
}
void Display()
{
Computer.Display();
std::cout << "Your Operating System is :" << this->OperatingSystem << std::endl;
}
private:
Personal_Computer Computer;
std::string OperatingSystem;
std::string productName = "WorkStation";
};
// Interface of Factory to create IProducts
class IProductFactory
{
public:
virtual IProducts* createProduct(std::string myProduct) = 0;
};
// Concretion of Interface for IProduct creation. This Factory produces IProducts based on the an string input
// to the function ( like a user input)
class UserInputFactoryProduct : public IProductFactory
{
public:
IProducts* createProduct(std::string myProduct)
{
if (myProduct == "PC_Tower")
this->product = new PC_Towers;
else if (myProduct == "PC_Screen")
this->product = new PC_Screen;
else if (myProduct == "Personal_Computer")
this->product = new Personal_Computer;
else if (myProduct == "WorkStation")
this->product = new Work_Station;
else
this->product = nullptr;
}
// Function to get the product member variable
IProducts* getProduct() { return this->product; };
private:
IProducts* product;
};
// Class e-shop to add and display all the products of the shop
class e_shop
{
public:
// Function to add products to the shop
void addProduct(IProducts* newProduct) { this->allProducts.push_back(newProduct); }
// Function to display all the products of the shop
void desplayAllProducts()
{
for (int i = 0 ; i < allProducts.size() ; i++)
std::cout << allProducts.at(i)->getProductName() << std::endl;
}
private:
// vector to keep all the products of the shop
std::vector< IProducts* > allProducts;
};
#include "Products.h"
int main()
{
// create some products
IProducts* Product1 = new PC_Towers;
IProducts* Product2 = new PC_Screen;
IProducts* Product3 = new Personal_Computer;
IProducts* Product4 = new Work_Station;
// create an e-shop and add the products created
e_shop myEshop;
myEshop.addProduct(Product1);
myEshop.addProduct(Product2);
myEshop.addProduct(Product3);
myEshop.addProduct(Product4);
myEshop.desplayAllProducts();
std::string choosedProduct;
std::cout << std::endl;
IProducts* myProduct = nullptr;
UserInputFactoryProduct ProductFactory;
// choose a product and use factory to create the object based on the user input
while (myProduct == nullptr)
{
std::cout << "Chose one of the above products : ";
std::cin >> choosedProduct;
ProductFactory.createProduct(choosedProduct);
myProduct = ProductFactory.getProduct();
} ;
// display all the attributes of the product
myProduct->DisplayComponents();
// let the user to add values to components
myProduct->setAttributes();
// display the product ith the values of the user
myProduct->Display();
system("pause");
}
Aucun commentaire:
Enregistrer un commentaire