Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.50 KB | None | 0 0
  1. //--------------------------------------------------------------------
  2. // CS215-401 PROJECT 2
  3. //--------------------------------------------------------------------
  4. // Author: Ash Rule
  5. // Date: 10/11/19
  6. // Description: 2nd project using arrays and functions.
  7. //--------------------------------------------------------------------
  8.  
  9. #include <iostream>
  10. #include <iomanip>
  11. #include <string>
  12. #include <ctype.h>
  13. #include <fstream>
  14.  
  15. using namespace std;
  16.  
  17. const int READ_ERROR = -1;
  18. const int MAX_INV_ITEMS = 10;
  19. const int MAX_ORDERS = 7;
  20. const int MAX_ORDER_ITEMS = 5;
  21. //int numberOfInvItems = 0;
  22. //int lastOrderNum = 0;
  23. //int totalOrder = 0;
  24. //int selectItemNum;
  25. //int numOfOrders = 0;
  26. //int numBasket = 0;
  27.  
  28. ifstream fin;
  29. ofstream fout;
  30.  
  31. // describes a single item in the inventory, and on an order
  32. struct item {
  33. string prodCode; // product code: length 12, no spaces
  34. string description; // product description: max length 28, has spaces
  35. double price = 0; // price of the product, max 999.99
  36. };
  37.  
  38. // describes a customer order or "basket"
  39. struct order {
  40. long orderNumber = 0; // unique order number for this order
  41. string custName; // customer name
  42. double totalPrice = 0; // price of all items purchased
  43. item items[MAX_ORDER_ITEMS]; // list of items purchased
  44. int numItems = 0; // number of items purchased
  45. };
  46.  
  47. //----------------------------------------------------------------------------
  48. // readInventory
  49. //----------------------------------------------------------------------------
  50. // Modifies: inventory list (sets numberOfItems to -1 on READ ERROR)
  51. // lastOrderNum
  52. // Reads inventory data from a file into an array of inventory items
  53. //----------------------------------------------------------------------------
  54. void readInventory(item inv[], int& numberOfInvItems, int& lastOrderNum) {
  55. ifstream f;
  56.  
  57. // open the inventory file
  58. f.open("inventory.txt");
  59. if (f.fail()) {
  60. cout << "readFile:: error opening inventory.txt\n";
  61. numberOfInvItems = READ_ERROR;
  62. return;
  63. }
  64.  
  65. // read number of items from first line
  66. f >> numberOfInvItems >> lastOrderNum;
  67.  
  68. // for each item, read the data for the item
  69. for (int i = 0; i < numberOfInvItems; i++) {
  70. f >> inv[i].prodCode >> inv[i].price;
  71. f.ignore(); // finished reading integer, getline() on string is next
  72. getline(f, inv[i].description);
  73. }
  74. f.close();
  75. } // readInventory()
  76.  
  77.  
  78.  
  79. /*bool isValidOption(char option, string validOptions) {
  80. for (int i = 0; i < validOptions.length(); i++)
  81. return true;
  82. */
  83.  
  84.  
  85. bool isValidOption(char option, string validOptions) {
  86. for (int i = 0; i < validOptions.length(); i++)
  87. if (validOptions[i] == option) {
  88. return true;
  89. }
  90. return false;
  91. }
  92.  
  93.  
  94. char getMainOption() {
  95. string userInput, validOptions = "IOLX";
  96. char option;
  97.  
  98.  
  99. cout << "+--------------------------------------------------+" << endl;
  100. cout << "+ AMZN.COM +" << endl;
  101. cout << "+ by Ash Rule +" << endl;
  102. cout << "+--------------------------------------------------+" << endl;
  103. cout << "I - List our Inventory" << endl;
  104. cout << "O - Make an order" << endl;
  105. cout << "L - List all Orders made" << endl;
  106. cout << "X - Exit" << endl;
  107.  
  108. cout << "Enter an option: ";
  109. cin >> userInput;
  110. option = toupper(userInput[0]);
  111.  
  112. while (!isValidOption(option, validOptions)) {
  113. cout << "Invalid option, enter I, O, L, or X!" << endl;
  114. cout << "Enter an option: ";
  115. cin >> userInput;
  116. option = toupper(userInput[0]);
  117. }
  118.  
  119. return option;
  120. }
  121.  
  122. void displayList(item inventory[], int numberOfInvItems) {
  123. for (int i = 0; i < numberOfInvItems; i++)
  124. cout << fixed << setw(3) << right << i << " " << setw(12) << left << inventory[i].prodCode << " $" << setw(6) << right << setprecision(2) << inventory[i].price << " " << inventory[i].description << endl;
  125.  
  126. }
  127.  
  128. void displayInventory(item inventory[], int numberOfInvItems) {
  129. cout << "+--------------------------------------------------+" << endl;
  130. cout << "+ Products +" << endl;
  131. cout << "+--------------------------------------------------+" << endl;
  132. cout << " # PRODUCT CODE PRICE PRODUCT DESCRIPTION " << endl;
  133. cout << "--- ------------ ------- ------------------------" << endl;
  134.  
  135. displayList(inventory, numberOfInvItems);
  136.  
  137. cout << "Number of items in inventory: " << numberOfInvItems << endl << endl;
  138.  
  139. }
  140.  
  141. void displayOrder(order basket) {
  142. cout << "ORDER: " << basket.orderNumber << " " << basket.custName << endl;
  143. displayList(basket.items, basket.numItems);
  144. cout << "Total $" << basket.totalPrice << endl;
  145. //cout << "Total Number of Orders = " << basket[numBasket] << endl << endl;
  146. //cout << "Total number of orders: " << basket.numItems << endl;
  147. }
  148.  
  149. void startOrder(order basket[], int &numBasket, int &lastOrderNum) {
  150. lastOrderNum++;
  151. basket[numBasket].totalPrice = 0;
  152. basket[numBasket].numItems = 0;
  153. basket[numBasket].orderNumber = lastOrderNum;
  154. cout << "Order Number: " << lastOrderNum << endl;
  155. cout << "Enter customer name: ";
  156. if (cin.peek() == '\n') cin.ignore();
  157. getline(cin, basket[numBasket].custName);
  158.  
  159. }
  160.  
  161.  
  162. bool orderItem(item inventory[], order &basket, int numInvItems) {
  163. int selectItemNum;
  164. cout << "Enter item number (-1 to end): ";
  165. cin >> selectItemNum;
  166.  
  167. while (selectItemNum >= numInvItems || selectItemNum < -1) {
  168. cout << "Invalid entry. Enter number -1 to " << numInvItems - 1 << endl;
  169. cout << "Enter item number (-1 to end): ";
  170. cin >> selectItemNum;
  171. }
  172.  
  173. if (selectItemNum < numInvItems && selectItemNum > -1) {
  174. basket.items[basket.numItems].prodCode = inventory[selectItemNum].prodCode;
  175. basket.items[basket.numItems].description = inventory[selectItemNum].description;
  176. basket.items[basket.numItems].price = inventory[selectItemNum].price;
  177. basket.totalPrice = basket.totalPrice += basket.items[basket.numItems].price;
  178.  
  179. cout << inventory[selectItemNum].description << " added to your basket. Current total is $ " << basket.totalPrice << endl;
  180. basket.numItems++;
  181.  
  182.  
  183. //return false;
  184. }
  185.  
  186. else if (selectItemNum == -1) {
  187. cout << "Thank you for your order!" << endl;
  188. displayOrder(basket);
  189.  
  190. system("pause");
  191. return true;
  192. }
  193.  
  194.  
  195. }
  196.  
  197.  
  198. void makeOrder(item inventory[], order basket[], int numberOfInvItems, int &numOfOrders, int &lastOrderNum){
  199.  
  200. bool check = false;
  201. if (numOfOrders < MAX_ORDERS) {
  202. startOrder(basket, numOfOrders, lastOrderNum);
  203. displayInventory(inventory, numberOfInvItems);
  204.  
  205. while (basket[numOfOrders].numItems < MAX_ORDER_ITEMS && check != true){
  206. check = orderItem(inventory, basket[numOfOrders], numberOfInvItems);
  207.  
  208. }
  209.  
  210. numOfOrders++;
  211. }
  212.  
  213. else {
  214. cout << "Sorry, we can not take more orders today." << endl;
  215. }
  216.  
  217. }
  218.  
  219.  
  220. void listOrders(order basket[], int& numBasket) {
  221. cout << "+--------------------------------------------------+" << endl;
  222. cout << "+ Orders +" << endl;
  223. cout << "+--------------------------------------------------+" << endl;
  224.  
  225. for (int i = 0; i < numBasket; i++) {
  226. displayOrder(basket[i]);
  227. }
  228. cout << endl;
  229. cout << "Total Number of Orders = " << numBasket << endl << endl;
  230.  
  231.  
  232. }
  233.  
  234. /*void writeOrders(basket[]) {
  235.  
  236. fin.open("orders.txt");
  237. if (fin.fail()) {
  238. cout << "File open failed." << endl;
  239.  
  240. system("pause");
  241. return;
  242. }
  243.  
  244. cout << totalOrder << endl;
  245. for (int i = 0; i < totalOrder; i++) {
  246. cout << basket[i].orderNumber << basket[i].custName;
  247.  
  248. }
  249.  
  250. fin.close();
  251. }*/
  252.  
  253.  
  254.  
  255.  
  256. int main(){
  257. order basket[MAX_ORDERS];
  258. item inventory[MAX_INV_ITEMS];
  259. int numberOfInvItems = 0;
  260. int lastOrderNum; //moved some global variables here
  261. int numOfOrders = 0;
  262. int numBasket = 0;
  263.  
  264. readInventory(inventory, numberOfInvItems, lastOrderNum);
  265.  
  266. char option = getMainOption();
  267.  
  268.  
  269. while (option != 'X') {
  270.  
  271. if (option == 'I') {
  272. displayInventory(inventory, numberOfInvItems); // added numberOfInvItems
  273.  
  274. }
  275. else if (option == 'O') {
  276. makeOrder(inventory, basket, numberOfInvItems, numOfOrders, lastOrderNum);
  277.  
  278. }
  279. else if (option == 'L') {
  280. listOrders(basket, numOfOrders);
  281. }
  282. option = getMainOption();
  283. }
  284.  
  285. cout << "This is where the orders should be printed out to an output file." << endl;
  286.  
  287.  
  288.  
  289.  
  290. system("pause");
  291. return 0;
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement