Guest User

Untitled

a guest
Feb 21st, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.01 KB | None | 0 0
  1. //Kevin Pavao
  2. //Southern Programming Team
  3. //SWISStoresSales.cpp
  4.  
  5. #include<iostream>
  6. #include<string>
  7. #include<iomanip>
  8. #include<fstream>
  9. #include<cstring>
  10. #include<sstream>
  11.  
  12. using namespace std;
  13.  
  14.  
  15. struct date
  16. {
  17. int month,
  18. day,
  19. year;
  20.  
  21. char dash;
  22. };
  23.  
  24.  
  25. void menu();
  26.  
  27. //enter sales by dept. returns filename
  28. string getSales();
  29.  
  30. string getFileName(string storeNum, int month, int day, int year);
  31.  
  32. //calculates sales from number of items sold
  33. double calculateSales(int index, string item, double price);
  34.  
  35. //view the sales file
  36. void viewSales(string fileName);
  37.  
  38. void clear();
  39.  
  40. //converts int to string
  41. string itoa(int number);
  42.  
  43.  
  44.  
  45.  
  46. int main()
  47. {
  48. menu();
  49.  
  50. return 0;
  51. }
  52.  
  53.  
  54. void menu()
  55. {
  56. int choice;
  57. string fileName;
  58.  
  59. clear();
  60.  
  61. cout << "Shoppers World Inc.\n\n";
  62. cout << "1. Enter the days sales by department.\n";
  63. cout << "2. View the days sales\n";
  64. cout << "3. Save the sales data\n";
  65. cout << "4. Quit\n";
  66.  
  67. cout << "\n\nEnter choice: ";
  68. cin >> choice;
  69. switch(choice)
  70. {
  71. case 1:
  72. fileName=getSales();
  73. break;
  74. case 2:
  75. viewSales(fileName);
  76. break;
  77. case 3:
  78. {
  79. cout << "File Written.\n";
  80. cout << "Press enter to continue.";
  81. cin.get();
  82. cin.get();
  83. clear();
  84. menu();
  85. break;
  86. }
  87. case 4:
  88. {
  89. cout << "Press enter to quit.";
  90. cin.get();
  91. cin.get();
  92. break;
  93. }
  94. default:
  95. {
  96. clear();
  97. menu();
  98. break;
  99. }
  100. }
  101. }
  102.  
  103.  
  104. string getSales()
  105. {
  106. ofstream outfile;
  107.  
  108. double sales[3][8], deptTtl[3]={0,0,0}, storeTtl=0;
  109. string department, storeNumber;
  110. date salesDate;
  111. string fileName;
  112.  
  113.  
  114. //items being sold
  115. string electronicItems[8]={ "Nintendos","TVs","Dell Computers","Keyboards","Media Players","Speakers","Playstation Games","DVD-Rs" };
  116. string clothingItems[8]={ "Shirts","Pants","Jeans","Hats","Sweaters","Shorts","Mens Underwear","Womens Underwear"};
  117. string outdoorItems[8]={ "Tents","Fishing Poles","Bait","Guns","Sleeping Bags","Lanterns","Flashlights","Stoves" };
  118.  
  119. //item prices
  120. double electronicPrices[8]={100,250,500,25,200,50,59,20};
  121. double clothingPrices[8]={12,25,20,15,30,25,15,20};
  122. double outdoorPrices[8]={70,80,150,25,30,15,25,10};
  123.  
  124. clear();
  125. cout << "Enter store number: ";
  126. cin >> storeNumber;
  127.  
  128. cout << "Enter date (MM/DD/YYYY): ";
  129. cin >> salesDate.month >> salesDate.dash >> salesDate.day >> salesDate.dash >> salesDate.year;
  130.  
  131. //check to see if date is valid
  132. while(salesDate.month < 1 || salesDate.month > 12 || salesDate.day < 1 || salesDate.day > 31 || salesDate.year < 1900)
  133. {
  134. cout << "Invalid date entered.\nPlease enter again (MM/DD/YYYY): ";
  135. cin >> salesDate.month >> salesDate.dash >> salesDate.day >> salesDate.dash >> salesDate.year;
  136. }
  137.  
  138. //convert int variables in salesDate to string for the file name
  139. fileName=itoa(salesDate.month) + salesDate.dash + itoa(salesDate.day) + salesDate.dash + itoa(salesDate.year) + "_" + storeNumber + "_store.txt";
  140. cout << fileName;
  141.  
  142. outfile.open(fileName.c_str());
  143. //outfile.open("testing.txt");
  144. if(!outfile) cout << "error";
  145. outfile << salesDate.month << salesDate.dash << salesDate.day << salesDate.dash << salesDate.year << endl;
  146. outfile << "Store Number: " << storeNumber << endl;
  147. for(int x=0; x<3; x++)
  148. {
  149. clear();
  150. if(x==0) department="Electronics";
  151. if(x==1) department="Clothing";
  152. if(x==2) department="Outdoor";
  153.  
  154. cout << department << " Department Sales\n";
  155. outfile << endl << department << " Department Sales\n\n";
  156. outfile << setw(20) << left << "Item" << setw(15) << right << "Sales" << endl;
  157.  
  158. outfile << setprecision(2) << setiosflags(ios::showpoint) << setiosflags(ios::fixed);
  159.  
  160. for(int y=0; y<8; y++)
  161. {
  162. if(x==0)
  163. {
  164. //x=department, y=item
  165. sales[x][y]=calculateSales(y,electronicItems[y],electronicPrices[y]);
  166. deptTtl[x]+=sales[x][y];
  167. outfile << setw(20) << left << electronicItems[y];
  168. //outfile << setw(15) << left << static_cast<int>(sales[x][y]/electronicPrices[y]);
  169. outfile << setw(5) << right << "$" << setw(10) << right << sales[x][y] << endl;
  170. }
  171. else if(x==1)
  172. {
  173. sales[x][y]=calculateSales(y,clothingItems[y],clothingPrices[y]);
  174. deptTtl[x]+=sales[x][y];
  175. outfile << setw(20) << left << clothingItems[y];
  176. //outfile << setw(15) << left << static_cast<int>(sales[x][y]/clothingPrices[y]);
  177. outfile << setw(5) << right << "$" << setw(10) << right << sales[x][y] << endl;
  178. }
  179. else if(x==2)
  180. {
  181. sales[x][y]=calculateSales(y,outdoorItems[y],outdoorPrices[y]);
  182. deptTtl[x]+=sales[x][y];
  183. outfile << setw(20) << left << outdoorItems[y];
  184. //outfile << setw(15) << left << static_cast<int>(sales[x][y]/outdoorPrices[y]);
  185. outfile << setw(5) << right << "$" << setw(10) << right << sales[x][y] << endl;
  186. }
  187.  
  188. }
  189.  
  190. //outfile << endl;
  191. outfile << setw(20) << setfill('.') << left << "Dept. Total";
  192. outfile << setw(5) << right << "$" << setfill(' ') << setw(10) << right << deptTtl[x] << endl;
  193. storeTtl+=deptTtl[x];
  194.  
  195.  
  196. }
  197.  
  198. outfile << endl;
  199. outfile << setw(20) << left << "Store Total ";
  200. outfile << setw(5) << right << "$" << setw(10) << storeTtl << endl;
  201. outfile.close();
  202.  
  203. menu();
  204. return fileName.c_str();
  205. }
  206.  
  207.  
  208. double calculateSales(int index, string item, double price)
  209. {
  210. int numSold;
  211. double sales;
  212.  
  213. cout << "How many " << item << " sold ($" << price << "/piece): ";
  214. cin >> numSold;
  215. sales=numSold*price;
  216.  
  217. return sales;
  218. }
  219.  
  220.  
  221. void viewSales(string fileName)
  222. {
  223. ifstream infile(fileName.c_str());
  224. cout << fileName;
  225. //ifstream infile("testing.txt");
  226. char ch;
  227.  
  228. clear();
  229.  
  230. if(!infile)
  231. {
  232. cout << "Error reading file, please re-create.\n(Enter)";
  233. cin.get();
  234. cin.get();
  235. menu();
  236. }
  237.  
  238. infile.get(ch);
  239. while(infile)
  240. {
  241. cout << ch;
  242. infile.get(ch);
  243. }
  244. infile.close();
  245. cout << "Press enter to continue.";
  246. cin.get();
  247. cin.get();
  248. menu();
  249. }
  250.  
  251.  
  252. void clear()
  253. {
  254. for(int x=0; x<=50; x++)
  255. cout << endl;
  256. }
  257.  
  258.  
  259. string itoa(int number)
  260. {
  261. ostringstream oss;
  262.  
  263. oss << number;
  264. return oss.str();
  265. }
Add Comment
Please, Sign In to add comment