#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <utility>
#include <stdlib.h>
using namespace std;
class Item
{
private:
string m_name;
float m_cost;
public:
Item(string t_name,float t_cost):m_name(t_name),m_cost(t_cost)
{
}
string getName()
{
return m_name;
}
float getCost()
{
return m_cost;
}
};
class VendingMachine
{
private:
vector<Item> m_totalItems;
unordered_map<string,Item>m_products;
float m_remainingCharges{0};
float m_moneyInserted{0};
size_t itemCount{0};
public:
VendingMachine()
{
}
void addItemToVendingMachine(string t_name,size_t t_cost)
{
float temp=static_cast<float>(t_cost)/static_cast<float>(100);
Item item(t_name,temp);
m_totalItems.push_back(item);
m_products.insert(make_pair(t_name,item));
}
bool chooseProduct(string t_name)
{
for(auto item:m_totalItems)
{
if(item.getName()==t_name)
{
m_remainingCharges=item.getCost();
return true;
}
itemCount++;
}
cout<<"Item not currently available: "+t_name<<endl;
return false;
}
void insertCoin(size_t t_coin)
{
float temp=static_cast<float>(t_coin);
if(t_coin<=50)
{
temp/=100;
cout.precision(10);
cout<<scientific<<"temp:"<<temp<<" vs.double "<<0.1<<" or float "<<0.1f<<endl;
}
if(temp==0.01f or temp==0.05f or temp==0.1f or temp==1.00f or temp==2.00f or temp==0.25f or temp==0.50f)
{
m_moneyInserted+=temp;
m_remainingCharges-=m_moneyInserted;
}
else
{
cout<<"Does not accept: "<< t_coin<<" ,please insert correct coin."<<endl;
return;
}
}
pair<Item,float> getProduct()
{
auto item=m_totalItems[itemCount];
auto itemBack=m_totalItems.back();
m_totalItems[itemCount]=itemBack;
m_totalItems.pop_back();
return make_pair(item,abs(m_remainingCharges));
}
float refund()
{
if(m_remainingCharges<0)
return abs(m_remainingCharges);
else
return 0;
}
void resetOperator()
{
m_remainingCharges=0;
m_moneyInserted=0;
itemCount=0;
}
};
int main()
{
Item item("Candy",0.50);
cout<<item.getName()<<" ";
cout<<item.getCost()<<endl;
VendingMachine machine;
machine.addItemToVendingMachine("CANDY",10);
machine.addItemToVendingMachine("SNACK",50);
machine.addItemToVendingMachine("Coke",25);
machine.insertCoin(10);
machine.insertCoin(25);
machine.insertCoin(50);
machine.chooseProduct("CANDY");
auto temp=machine.getProduct();
cout<<temp.first.getName()<<endl;
cout<<temp.second<<endl;
machine.resetOperator();
return 0;
};