fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <unordered_map>
  5. #include <utility>
  6. #include <stdlib.h>
  7. using namespace std;
  8.  
  9. class Item
  10. {
  11. private:
  12. string m_name;
  13. float m_cost;
  14. public:
  15. Item(string t_name,float t_cost):m_name(t_name),m_cost(t_cost)
  16. {
  17.  
  18. }
  19. string getName()
  20. {
  21. return m_name;
  22. }
  23. float getCost()
  24. {
  25. return m_cost;
  26. }
  27. };
  28. class VendingMachine
  29. {
  30. private:
  31. vector<Item> m_totalItems;
  32. unordered_map<string,Item>m_products;
  33. float m_remainingCharges{0};
  34. float m_moneyInserted{0};
  35. size_t itemCount{0};
  36. public:
  37. VendingMachine()
  38. {
  39.  
  40. }
  41. void addItemToVendingMachine(string t_name,size_t t_cost)
  42. {
  43. float temp=static_cast<float>(t_cost)/static_cast<float>(100);
  44. Item item(t_name,temp);
  45. m_totalItems.push_back(item);
  46. m_products.insert(make_pair(t_name,item));
  47. }
  48. bool chooseProduct(string t_name)
  49. {
  50. for(auto item:m_totalItems)
  51. {
  52. if(item.getName()==t_name)
  53. {
  54. m_remainingCharges=item.getCost();
  55. return true;
  56. }
  57. itemCount++;
  58. }
  59. cout<<"Item not currently available: "+t_name<<endl;
  60. return false;
  61. }
  62. void insertCoin(size_t t_coin)
  63. {
  64. float temp=static_cast<float>(t_coin);
  65. if(t_coin<=50)
  66. {
  67. temp/=100;
  68. cout.precision(10);
  69. cout<<scientific<<"temp:"<<temp<<" vs.double "<<0.1<<" or float "<<0.1f<<endl;
  70. }
  71. 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)
  72. {
  73. m_moneyInserted+=temp;
  74. m_remainingCharges-=m_moneyInserted;
  75. }
  76. else
  77. {
  78. cout<<"Does not accept: "<< t_coin<<" ,please insert correct coin."<<endl;
  79. return;
  80. }
  81. }
  82. pair<Item,float> getProduct()
  83. {
  84. auto item=m_totalItems[itemCount];
  85. auto itemBack=m_totalItems.back();
  86. m_totalItems[itemCount]=itemBack;
  87. m_totalItems.pop_back();
  88. return make_pair(item,abs(m_remainingCharges));
  89. }
  90.  
  91. float refund()
  92. {
  93. if(m_remainingCharges<0)
  94. return abs(m_remainingCharges);
  95. else
  96. return 0;
  97. }
  98. void resetOperator()
  99. {
  100. m_remainingCharges=0;
  101. m_moneyInserted=0;
  102. itemCount=0;
  103. }
  104. };
  105. int main()
  106. {
  107. Item item("Candy",0.50);
  108. cout<<item.getName()<<" ";
  109. cout<<item.getCost()<<endl;
  110. VendingMachine machine;
  111. machine.addItemToVendingMachine("CANDY",10);
  112. machine.addItemToVendingMachine("SNACK",50);
  113. machine.addItemToVendingMachine("Coke",25);
  114. machine.insertCoin(10);
  115. machine.insertCoin(25);
  116. machine.insertCoin(50);
  117. machine.chooseProduct("CANDY");
  118. auto temp=machine.getProduct();
  119. cout<<temp.first.getName()<<endl;
  120. cout<<temp.second<<endl;
  121. machine.resetOperator();
  122. return 0;
  123. };
  124.  
Success #stdin #stdout 0s 4408KB
stdin
Standard input is empty
stdout
Candy 0.5
temp:1.0000000149e-01 vs.double 1.0000000000e-01 or float 1.0000000149e-01
temp:2.5000000000e-01 vs.double 1.0000000000e-01 or float 1.0000000149e-01
temp:5.0000000000e-01 vs.double 1.0000000000e-01 or float 1.0000000149e-01
CANDY
1.0000000149e-01