Skip to content

Instantly share code, notes, and snippets.

@ahmedalkabir
Created January 17, 2019 00:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmedalkabir/bf7eec3df0c8253c09ca9c63c78959af to your computer and use it in GitHub Desktop.
Save ahmedalkabir/bf7eec3df0c8253c09ca9c63c78959af to your computer and use it in GitHub Desktop.
vending machine
#include <string>
#include <vector>
#include <iostream>
#include <cctype>
#include <memory>
// كلاس الأصناف
class Product
{
public:
Product(std::string n, float p, int s) : name(n), price(p), max_stock(s) {}
Product(){};
std::string get_name() const
{
return this->name;
}
float get_price() const
{
return this->price;
}
int get_stock() const
{
return this->max_stock;
}
bool isExisted() const
{
if (this->max_stock > 0)
{
return true;
}
else
{
return false;
}
}
int took()
{
return --this->max_stock;
}
private:
std::string name;
float price;
int max_stock;
};
// كلاس السلة لأضافة الأصناف وكميتها
class Backet
{
public:
Backet(Product *_p, int _q) : product(_p), quantity(_q){};
Product *get_product() const
{
return product;
}
int get_quantity() const
{
return quantity;
}
private:
Product *product;
int quantity;
};
// كلاس الطلب لحساب سعر الصنف مع كميته وإنهاء الطلب
class Order
{
public:
Order()
{
final_price = 1;
}
bool addToBacket(Product *p, int _quantity)
{
if (_quantity > 0 && _quantity <= p->get_stock())
{
// ضيف الصنف لسلة
Backet b(p, _quantity);
array_of_products.emplace_back(b);
final_price = _quantity * p->get_price();
return true;
}
else
{
return false;
}
}
float getThePrice() const
{
return final_price;
}
// في حالة زبون يبي يتم الطلب نقصوا الكمية من المخزن ونرجعوا قيمة الفاتورة النهائية
float finishTheOrder()
{
final_price = 0;
for (auto &item : array_of_products)
{
// نقصوا الكمية من المخزن
for (int i = 0; i < item.get_quantity(); i++)
{
item.get_product()->took();
// نجمعوا قيمة نهائية للطلب مع كل صنف نلقوه في السلة
final_price += item.get_product()->get_price();
}
}
// نفرغوا السلة
array_of_products.clear();
return final_price;
}
// عرض الفاتورة للمشتري
void printOrder() const
{
for (const auto &item : array_of_products)
{
std::cout << "Name: " << item.get_product()->get_name() << "\tPrice: " << this->getThePrice()
<< "\tQuantity: " << item.get_quantity() << '\n';
}
}
// تفريغ السلة
void emptyTheBacket()
{
array_of_products.clear();
}
private:
std::vector<Backet> array_of_products;
int quantity;
float final_price;
};
int main(int argc, char const *argv[])
{
/* code */
int choice, _q;
char choice_y_n;
float money;
std::vector<Product> arr_of_p;
// أضف الأصناف
arr_of_p.emplace_back(Product("Water", 0.5, 20));
arr_of_p.emplace_back(Product("Pepsi", 2, 15));
arr_of_p.emplace_back(Product("Biscuit", 1.5, 25));
arr_of_p.emplace_back(Product("Choc", 4, 2));
arr_of_p.emplace_back(Product("Coffe", 1.5, 20));
arr_of_p.emplace_back(Product("Milk", 3.75, 10));
arr_of_p.emplace_back(Product("Juice", 1, 20));
arr_of_p.emplace_back(Product("Bravo", 4, 15));
// كائن الطلب ويفضل يكون واحد لأن لكل مشتري عنده كائن واحد
Order ord1;
// تبدا عملية البيع
while (1)
{
// العرض الأصناف المتاحة مع كميتها
std::cout << "No." << '\t' << "Name"
<< "\t"
<< "Price" << '\t' << "Quantity" << '\t' << '\n';
int i = 0;
for (auto &item : arr_of_p)
{
std::cout << ++i << '\t' << item.get_name() << "\t" << item.get_price() << '\t' << item.get_stock() << '\t' << '\n';
}
// بدء عملية البيع
std::cout << std::endl;
std::cout << "Enter a Number of Product Do you want to buy :";
std::cin >> choice;
// نتأكدوا إنه يختار الصنف الصح
if (choice > 0 && choice < 9)
{
// نشوفوا لو قاعد في المخزن
if (arr_of_p[choice - 1].isExisted())
{
std::cout << "Enter a Quantity of Product you selected :";
std::cin >> _q;
// نضيفوا للسلة
if (ord1.addToBacket(&arr_of_p[choice - 1], _q))
{
std::cout << std::endl;
std::cout << "The Order Your requested is \n";
ord1.printOrder();
std::cout << std::endl;
std::cout << "Do you want to complete the process [Y/N]: ";
std::cin >> choice_y_n;
if (std::toupper(choice_y_n) == 'Y')
{
std::cout << "pay " << ord1.getThePrice() << ": ";
std::cin >> money;
if (money >= ord1.getThePrice())
{
// نتموا الطلبية
float price = ord1.finishTheOrder();
float returned = money > price ? money - price : 0;
std::cout << "you purchased " << money << " and the returned money back is " << returned << std::endl;
}
else
{
std::cout << "error, your money isn't suffinient for that " << std::endl;
ord1.emptyTheBacket();
}
}
}
else
{
std::cout << "you ordered unreasonable quantity, " << std::endl;
std::cin.get();
}
}
else
{
std::cout << "The product you ordered is out of stock, sorry" << std::endl;
std::cin.get();
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment