Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

checkout simulator

By: a guest on Apr 18th, 2013  |  syntax: C++  |  size: 3.16 KB  |  hits: 47  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <iostream>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <cstdlib>
  6. #include <vector>
  7. using namespace std;
  8.  
  9. class Product
  10. {
  11. private:
  12.         int plu;
  13.         string productName;
  14.         int type;
  15.         double price;
  16.         double inventory;
  17. public:
  18.         Product();
  19.         Product(int, string, int, double, double);
  20.         int getPLU();
  21.         string getProductName();
  22.         int getType();
  23.         double getPrice();
  24.         double getInventory();
  25.         double updateInventory(double);
  26.    
  27. };
  28.  
  29.  
  30. Product::Product()
  31. {
  32.         plu = 0;
  33.         productName = "none yet";
  34.         type = 0;
  35.         price = 0;
  36.         inventory = 0;
  37. }
  38. Product::Product(int pl, string pn, int t, double pr, double i)
  39. {
  40.         plu = pl;
  41.         productName = pn;
  42.         type = t;
  43.         price = pr;
  44.         inventory = i;
  45. }
  46. int Product::getPLU()
  47. {       return plu;}
  48.  
  49. string Product::getProductName()
  50. {       return productName;}
  51.  
  52. int Product::getType()
  53. {       return type;}
  54.  
  55. double Product::getPrice()
  56. {       return price;}
  57.  
  58. double Product::getInventory()
  59. {       return inventory;}
  60.  
  61. double Product::updateInventory(double quantity)
  62. {
  63.     if (quantity > inventory)
  64.         cout << "This item is not in stock." << endl;
  65.     else inventory -= quantity;
  66.     return inventory;
  67. }
  68.  
  69.  
  70. Product inv[100]; //array of 100 objects
  71. int c = 100; //max product is 100
  72.  
  73. int menu(void) //user options
  74. {
  75. int option;
  76. cout << "Enter 1 to begin checkout or 0 to exit" << endl;
  77.  
  78.  
  79. cin >> option;
  80. return option;
  81. };
  82. void updateFile() //output to a new file
  83. {
  84. ofstream newInvFile("newinventory.txt");
  85.  
  86. for (int p = 0; p < c; p++)
  87. {
  88. newInvFile << inv[p].getPLU() << " " << inv[p].getProductName() << " " << inv[p].getType() << " " << inv[p].getPrice() << " " << inv[p].getInventory() << endl;
  89. }
  90.  
  91. }
  92. void checkout(void)
  93. {
  94. double total = 0;
  95. double quantity;
  96. int input;
  97. int PLU = 0;
  98.  
  99. do
  100. {
  101. PLU = 0;
  102. cout << "Enter PLU code or (0) to exit" << endl;
  103. cin >> input;
  104. if (input == 0)
  105. break;
  106. for (int p = 0; p < c; p++)
  107. {
  108. if (inv[p].getPLU() == input)
  109. {
  110. PLU = p;
  111. break;
  112. }
  113. }
  114.  
  115. if (inv[PLU].getType() == 1)// Determine whether product is sold by weight
  116. {
  117. cout << "Weight: ";
  118. }
  119. else
  120. {
  121. cout << "Quantity: ";
  122. }
  123.  
  124. cin >> quantity;
  125. total += quantity * inv[PLU].getPrice();
  126. inv[PLU].updateInventory(quantity);
  127. }
  128. while (input != 0);
  129.  
  130. cout << "Total: $" << total << endl;
  131.  
  132. if (total > 50) //apply discount if total is over $50
  133. {
  134. total = total * 0.95;
  135. cout << "YAY! Your purchase of over $50 qualifies for a 5% discount. Total: $" << total << endl;
  136. }
  137.  
  138. }
  139. int main()
  140. {
  141. int plu;
  142. string productName;
  143. int type;
  144. double price;
  145. double inventory;
  146. c = 0;
  147.  
  148. ifstream original ("product.txt"); //read from original product file
  149. if (original.is_open())
  150. {
  151. while (!original.eof())
  152. {
  153. original >> plu >> productName >> type >> price >> inventory;
  154.  
  155. Product temp = Product(plu, productName, type, price, inventory);
  156. inv[c] = temp;
  157. c++;
  158. }
  159. original.close();
  160. }
  161. int option;
  162.  
  163. do
  164. {
  165. cout << "WELCOME!" << endl;
  166.  
  167. option = menu();
  168. if (option == 1)
  169. checkout();
  170. else
  171. updateFile();
  172. }
  173. while(option != 0);
  174.  
  175. system ("pause");
  176. }