Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.44 KB | None | 0 0
  1. /*Program Description: A user can choose to create a file containing
  2. stock information for an inventory, or read the stock information
  3. from a file. The user can then request various data from the file via
  4. a menu function.*/
  5.  
  6.  
  7. #include "stdafx.h"
  8. #include <iostream>
  9. #include <sstream>
  10. #include <string>
  11. #include <fstream>
  12. #include <vector>
  13. #include <stdlib.h>
  14.  
  15. using namespace std;
  16.  
  17. double profit(int num_items, double buy_price, double sell_price, double store_fee);
  18.  
  19.  
  20. class Table_item //an object of type Table_item represents one whole row in the inventory table
  21. {
  22.  
  23. public:
  24.  
  25. int item_ID;
  26. int num_items;
  27. double buy_price;
  28. double sell_price;
  29. double store_fee;
  30.  
  31.  
  32. Table_item(int id, int num, double buy, double sell, double store){ //constructor
  33.  
  34. item_ID = id;
  35. num_items = num;
  36. buy_price = buy;
  37. sell_price = sell;
  38. store_fee = store;
  39.  
  40. }
  41.  
  42. friend Table_item operator>>(istream &in, Table_item& item){ //defines input stream for table items
  43.  
  44. in >> item.item_ID >> item.num_items >> item.buy_price >> item.sell_price >> item.store_fee;
  45. return item;
  46.  
  47. }
  48.  
  49. friend Table_item operator<<(ostream &out, Table_item& item){ //defines output stream for table items
  50.  
  51. out << item.item_ID << item.num_items << item.buy_price << item.sell_price << item.store_fee;
  52. return item;
  53.  
  54. }
  55.  
  56.  
  57. };
  58.  
  59.  
  60. int main(){
  61.  
  62.  
  63.  
  64. char option;
  65. char menu_choice;
  66. int store_num;
  67.  
  68. vector <Table_item> inv_vect;
  69.  
  70. cout << "Would you like to enter data by hand (H) or read it from a file (F)" << endl;
  71. cin >> option;
  72.  
  73. if(option == 'H'){
  74.  
  75. fstream inv_table("Tablefile.txt", fstream::in | fstream::out | fstream::trunc);
  76.  
  77. cout << "Please enter the number of items you would like to store:" << endl; //capped at 100 throughout program
  78. cin >> store_num;
  79.  
  80. int i = 1;
  81.  
  82. while(i <= store_num){
  83.  
  84. //inv_table.open();
  85.  
  86. int id_loc;
  87. int num_loc;
  88. double buy_loc, sell_loc, fee_loc;
  89.  
  90. cout << "Please enter the item ID for item " << i << ":";
  91. cin >> id_loc;
  92. cout << endl;
  93.  
  94. cout << "Please enter the number of items for item " << i << ":";
  95. cin >> num_loc;
  96. cout << endl;
  97.  
  98. cout << "Please enter the buying price for item " << i << ":" << endl;
  99. cin >> buy_loc;
  100. cout << endl;
  101.  
  102. cout << "Please enter the selling price for item " << i << ":" << endl;
  103. cin >> sell_loc;
  104. cout << endl;
  105.  
  106. cout << "Please enter the storage fee for item " << i << ":" << endl;
  107. cin >> fee_loc;
  108. cout << endl;
  109.  
  110. Table_item item(id_loc, num_loc, buy_loc, sell_loc, fee_loc);
  111.  
  112. inv_table << item;
  113. inv_table << endl;
  114. //inv_table.flush();
  115.  
  116. inv_vect.push_back(item);
  117.  
  118. i++;
  119.  
  120. }//end while for writing inventory file
  121.  
  122. //inv_table.close();
  123.  
  124. cout << inv_table;
  125. cout << endl;
  126. cout << inv_vect[0];
  127. cout << endl;
  128.  
  129.  
  130. cout << "Menu | Please choose one of the following options: \n" ;
  131. cout << "Option a: Print stock information. \n";
  132. cout << "Option b: Print general information. \n";
  133. cout << "Option c: Print all the items in a table. \n";
  134. cout << "Option d: Edit an item’s information, store in a file. \n"; //options d, e in the syllabus have been merged into a single block
  135. cout << "Option e: Exit. \n";
  136.  
  137. cin >> menu_choice;
  138.  
  139. if (menu_choice == 'a'){
  140.  
  141. cout<< "Stock Information: \n";
  142.  
  143. for(int i=1; i<5; i++){
  144.  
  145. double profit_indexed;
  146.  
  147. profit_indexed = profit(inv_vect[i].num_items, inv_vect[i].sell_price, inv_vect[i].buy_price, inv_vect[i].store_fee);
  148.  
  149. cout << "Item" << i << endl;
  150.  
  151. cout<<"* Item ID:"<< inv_vect[i].item_ID << endl;
  152. cout<< "* Number of items:"<< inv_vect[i].num_items << endl;
  153. cout<< "* Buying price: "<< inv_vect[i].buy_price << endl;
  154. cout<< "* Selling price"<< inv_vect[i].sell_price << endl;
  155. cout<< "* Storage fees:"<< inv_vect[i].store_fee << endl;
  156. cout << "* Profit:" << profit_indexed << endl;
  157. }
  158. }
  159.  
  160.  
  161. if(menu_choice == 'b'){
  162.  
  163. int total_items = 0;
  164. double profit_total = 0;
  165. double buying_total = 0, selling_total = 0;
  166.  
  167. for (int i=1; i<5; i++){
  168.  
  169. total_items += inv_vect[i].num_items;
  170. }
  171.  
  172. cout << "Total number of items: " << total_items << endl;
  173.  
  174. for ( int i=1; i<5; i++){
  175.  
  176. double profit_total_indexed;
  177.  
  178. profit_total_indexed = profit(inv_vect[i].num_items, inv_vect[i].sell_price, inv_vect[i].buy_price, inv_vect[i].store_fee);
  179.  
  180. profit_total += profit_total_indexed;
  181. }
  182.  
  183. cout << "Total Profit:" << profit_total << endl;
  184. cout << "Average Profit:" << (profit_total)/4 << endl;
  185.  
  186.  
  187. for ( int i=1; i<5; i++){
  188.  
  189. buying_total += inv_vect[i].buy_price;
  190.  
  191. selling_total += inv_vect[i].sell_price;
  192. }
  193.  
  194. cout << "Buying price average:" << (buying_total)/4 << endl;
  195. cout << "Selling price average:" << (selling_total)/4 << endl;
  196.  
  197. }
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204. if(menu_choice == 'c') //Print all inventory items in a table
  205. {
  206.  
  207. int k=0;
  208.  
  209. while(k<100){
  210.  
  211. cout << inv_vect[k]; //automatically tabular; see definition of << operator for objects of type Table_item
  212. cout << endl;
  213. k++;
  214. }
  215.  
  216. }
  217.  
  218.  
  219. if(menu_choice == 'd') //Edit an item from user input, given an item's ID number
  220. {
  221.  
  222. int edit_id;
  223.  
  224. cout << "Please enter the ID of the item you wish to edit" << endl;
  225. cin >> edit_id;
  226.  
  227. int k=0;
  228.  
  229. while(inv_vect[k].item_ID != edit_id){ //traversing array for item with ID edit_id, for loop
  230.  
  231. k++;
  232. }
  233.  
  234. int new_id, new_num;
  235. double new_buyp, new_sellp, new_storef;
  236.  
  237. cout << "The current item information is:" << endl;
  238. cout << inv_vect[k]; //k+1 used since above while loop exits before incrementing k
  239.  
  240. cout << "Enter a new item ID: ";
  241. cin >> new_id;
  242. cout << endl;
  243.  
  244. cout << "Enter a new number of items: ";
  245. cin >> new_num;
  246. cout << endl;
  247.  
  248. cout << "Enter a new buying price: ";
  249. cin >> new_buyp;
  250. cout << endl;
  251.  
  252. cout << "Enter a new selling price: ";
  253. cin >> new_sellp;
  254. cout << endl;
  255.  
  256. cout << "Enter a new storage fee: ";
  257. cin >> new_storef;
  258. cout << endl;
  259.  
  260. Table_item item(new_id, new_num, new_buyp, new_sellp, new_storef);
  261.  
  262. inv_vect.push_back(item);
  263.  
  264. char response;
  265.  
  266. cout << "Item successfully edited. Would you like to store updated item in a file? (Y/N)" << endl;
  267.  
  268. if(response == 'Y')
  269. {
  270. int h=0;
  271.  
  272. while(h<100){
  273.  
  274. inv_table << inv_vect[h];
  275. h++;
  276. }
  277.  
  278.  
  279.  
  280. }
  281.  
  282.  
  283.  
  284. if(response == 'N'){
  285.  
  286. cout << "Okay then." << endl;
  287. }
  288.  
  289. }
  290.  
  291.  
  292. if(menu_choice == 'e') //Closes the program and exits
  293. {
  294.  
  295. char a;
  296. cout << "Enter any char to exit." << endl;
  297. cin >> a;
  298.  
  299. } //exit
  300.  
  301.  
  302. }//end if block for option H (entering by hand)
  303.  
  304.  
  305.  
  306.  
  307. if (option == 'F'){
  308.  
  309. fstream inv_table;
  310.  
  311. string file_location;
  312.  
  313. cout << "Please enter the location of your file (full path):";
  314. getline(cin, file_location);
  315.  
  316. inv_table.open(file_location.c_str());
  317.  
  318. /*ifstream inv_file_i;
  319. ofstream inv_file_o;*/
  320.  
  321. if(inv_table.fail()){
  322.  
  323. cout << "Input file failed to open.\n";
  324. exit(1);
  325.  
  326. }
  327.  
  328. inv_table.open("Tablefile.txt");
  329.  
  330. int k=0;
  331. int j=0, n=0;
  332. double b=0, s=0, f=0; //dummy parameters for constructor; needed to read from file into array
  333.  
  334. Table_item item(j, n, b, s, f);
  335.  
  336. vector <Table_item> inv_vect;
  337.  
  338. while(k < 100){//building array from file
  339.  
  340.  
  341. inv_table >> item;
  342.  
  343. item = inv_vect[k];
  344.  
  345. k++;
  346.  
  347.  
  348. }
  349.  
  350. cout << "Menu | Please choose one of the following options: \n" ;
  351. cout << "Option a: Print stock information. \n";
  352. cout << "Option b: Print general information. \n";
  353. cout << "Option c: Print all the items in a table. \n";
  354. cout << "Option d: Edit an item’s information, store in a file. \n"; //options d, e in the syllabus have been merged into a single block
  355. cout << "Option e: Exit. \n";
  356.  
  357. cin >> menu_choice;
  358.  
  359. if (menu_choice == 'a')
  360. {
  361. cout<< "Stock Information: \n";
  362.  
  363. for(int i=1; i<5; i++)
  364. {
  365. double profit_indexed;
  366.  
  367. profit_indexed = profit(inv_vect[i].num_items, inv_vect[i].sell_price, inv_vect[i].buy_price, inv_vect[i].store_fee);
  368.  
  369. cout << "Item" << i << endl;
  370.  
  371. cout<<"* Item ID:"<< inv_vect[i].item_ID << endl;
  372. cout<< "* Number of items:"<< inv_vect[i].num_items << endl;
  373. cout<< "* Buying price: "<< inv_vect[i].buy_price << endl;
  374. cout<< "* Selling price"<< inv_vect[i].sell_price << endl;
  375. cout<< "* Storage fees:"<< inv_vect[i].store_fee << endl;
  376. cout << "* Profit:" << profit_indexed << endl;
  377. }
  378. }
  379.  
  380.  
  381. if(menu_choice == 'b')
  382. {
  383.  
  384. int total_items = 0;
  385. double profit_total = 0;
  386. double buying_total = 0, selling_total = 0;
  387.  
  388. for (int i=1; i<5; i++)
  389. {
  390.  
  391. total_items += inv_vect[i].num_items;
  392. }
  393.  
  394. cout << "Total number of items: " << total_items << endl;
  395.  
  396. for ( int i=1; i<5; i++)
  397. {
  398. double profit_total_indexed;
  399.  
  400. profit_total_indexed = profit(inv_vect[i].num_items, inv_vect[i].sell_price, inv_vect[i].buy_price, inv_vect[i].store_fee);
  401.  
  402. profit_total += profit_total_indexed;
  403. }
  404.  
  405. cout << "Total Profit:" << profit_total << endl;
  406. cout << "Average Profit:" << (profit_total)/4 << endl;
  407.  
  408.  
  409. for ( int i=1; i<5; i++)
  410. {
  411. buying_total += inv_vect[i].buy_price;
  412.  
  413. selling_total += inv_vect[i].sell_price;
  414. }
  415.  
  416. cout << "Buying price average:" << (buying_total)/4 << endl;
  417. cout << "Selling price average:" << (selling_total)/4 << endl;
  418.  
  419. }
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426. if(menu_choice == 'c') //Print all inventory items in a table
  427. {
  428.  
  429. int k=0;
  430.  
  431. while(k<100){
  432.  
  433. cout << inv_vect[k]; //automatically tabular; see definition of << operator for objects of type Table_item
  434. cout << endl;
  435. k++;
  436. }
  437.  
  438. }
  439.  
  440.  
  441. if(menu_choice == 'd') //Edit an item from user input, given an item's ID number
  442. {
  443.  
  444. int edit_id;
  445.  
  446. cout << "Please enter the ID of the item you wish to edit" << endl;
  447. cin >> edit_id;
  448.  
  449. int k=0;
  450.  
  451. while(inv_vect[k].item_ID != edit_id){ //traversing array for item with ID edit_id
  452.  
  453. k++;
  454.  
  455. }
  456.  
  457. int new_id, new_num;
  458. double new_buyp, new_sellp, new_storef;
  459.  
  460. cout << "The current item information is:" << endl;
  461. cout << inv_vect[k+1]; //k+1 used since above while loop exits before incrementing k
  462.  
  463. cout << "Enter a new item ID: ";
  464. cin >> new_id;
  465. cout << endl;
  466.  
  467. cout << "Enter a new number of items: ";
  468. cin >> new_num;
  469. cout << endl;
  470.  
  471. cout << "Enter a new buying price: ";
  472. cin >> new_buyp;
  473. cout << endl;
  474.  
  475. cout << "Enter a new selling price: ";
  476. cin >> new_sellp;
  477. cout << endl;
  478.  
  479. cout << "Enter a new storage fee: ";
  480. cin >> new_storef;
  481. cout << endl;
  482.  
  483.  
  484. Table_item item(new_id, new_num, new_buyp, new_sellp, new_storef);
  485.  
  486. inv_vect.push_back(item);
  487.  
  488. char response;
  489.  
  490.  
  491. cout << "Item successfully edited. Would you like to store updated item in a file? (Y/N)" << endl;
  492.  
  493. if(response == 'Y')
  494. {
  495. int h=0;
  496.  
  497. while(h<100){
  498.  
  499.  
  500. inv_table << inv_vect[h];
  501.  
  502. h++;
  503.  
  504. }
  505.  
  506.  
  507.  
  508. }
  509.  
  510.  
  511.  
  512. if(response == 'N')
  513.  
  514. {
  515.  
  516. cout << "Okay then." << endl;
  517.  
  518. }
  519.  
  520. }
  521.  
  522.  
  523. if(menu_choice == 'e') //Closes the program and exits
  524.  
  525. {
  526.  
  527. char a;
  528. cout << "Enter any char to exit." << endl;
  529. cin >> a;
  530.  
  531. } //exit
  532.  
  533.  
  534. }//end if block for option F (opening file at location)
  535.  
  536.  
  537.  
  538. }//end main
  539.  
  540.  
  541.  
  542. double profit (int num_items, double buy_price, double sell_price, double store_fee)
  543. {
  544.  
  545. using namespace std ;
  546.  
  547. double a;
  548.  
  549. a = (num_items) * ((sell_price ) - (buy_price + store_fee));
  550.  
  551. return a;
  552.  
  553. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement