Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.45 KB | None | 0 0
  1. //
  2. //  itemInfo.h FILE
  3. //  InventoryCode Assignment 2
  4.  
  5. #ifndef itemInfo_h
  6. #define itemInfo_h
  7. #include <iostream>
  8. #include <string>
  9. #include <iomanip>
  10. #include <array>
  11. #include <cmath>
  12.  
  13. using namespace std;
  14.  
  15. class item{
  16. private:
  17.     //made 4 private variables that hold
  18.     string food;
  19.     string foodID;
  20.     float retailVal;
  21.     int quantity;
  22. public:
  23.    
  24.  
  25.     //made mutators and accessors of all the private variables so i can set and retrieve that data
  26.     void setfood(string f){
  27.         food=f;
  28.     }
  29.    
  30.     string getfood(){
  31.         return food;
  32.     }
  33.    
  34.     void setfoodID(string fID){
  35.         foodID = fID;
  36.     }
  37.     string getfoodID(){
  38.         return foodID;
  39.     }
  40.     void setretailVal(float rV){
  41.            retailVal = rV;
  42.        }
  43.    
  44.     float getretailVal(){
  45.         return retailVal;
  46.     }
  47.    
  48.     void setquantity(int q){
  49.           quantity =q;
  50.       }
  51.      
  52.     int getquantity(){
  53.         return quantity;
  54.     }
  55.  
  56.  
  57.   item();
  58.   item(string f, string ID, float v, int q);
  59.    
  60. };
  61. //my default constructer so that the computer will assign these values if nothing is retrieved
  62. item::item() {
  63.      food = "Unnamed";
  64.      foodID= "0";
  65.      retailVal = 0;
  66.      quantity = 0;
  67.    }
  68.  //my overloaded constructor which allows item1,item2, item3, item4 to take in these 4 attributes in main
  69. item::item(string f, string ID, float v, int q) {
  70.      food = f;
  71.      foodID= ID;
  72.      retailVal = v;
  73.      quantity = q;
  74. }
  75.  
  76.  
  77.    
  78. void BuyItems(item* current);
  79. void DiscountItems(item* current);
  80. void displayStock(item* current);
  81.  
  82. #endif /* itemInfo_h */
  83.  
  84. //=================================================================================================
  85.  
  86. //
  87. //  functions.h FILE
  88. //
  89.  
  90.  
  91. #ifndef functions_h
  92. #define functions_h
  93. #include "itemInfo.h"
  94.  
  95. //item*current points the info within the function each time its updated in the function
  96. //buyItems completes a transaction
  97. void BuyItems(item* current){
  98.      int choice;
  99.      cout<< "====Menu===="<<endl;
  100.      cout << "Enter the item you would like to purchase"<<endl;
  101.      for (int x=0; x<4; x++) {
  102.      cout << x +1 << ". " << current[x].getquantity() << " " << current[x].getfood() << " left in stock at $" <<
  103.      current[x].getretailVal() << " item ID " << current[x].getfoodID() << endl;
  104.     }
  105.         cin >> choice;
  106.         int item1;
  107.            //  runs a do while wwhich runs til the user gets an amount that is below the set quantity
  108.           do {
  109.           cout <<"How many would you like to buy?"<<endl;
  110.           cin >> item1;
  111.                
  112.               if (item1 > current[choice - 1].getquantity()) {
  113.                    cout << "Not enough "<<current[choice - 1].getfood()<< " in stock ("<<current[choice - 1].getquantity()
  114.                    <<") total stock"<<endl;
  115.                    
  116.               }else{
  117.                   //this multiplies the quantity with the retail value
  118.                   //then prints the amount sold and how many
  119.                    double total = (current[choice - 1].getretailVal() * (item1));
  120.                    cout << "SOLD "<<item1<<" "<<current[choice - 1].getfood()<<" for $"<<total<<endl;
  121.                    current[choice-1].setquantity(current[choice - 1].getquantity() - item1);
  122.                    break;
  123.               }
  124.           } while (item1 > current[choice - 1].getquantity()) ;
  125.  
  126.        
  127.     }
  128.  
  129. //this discounts the items after two items have been sold
  130. void DiscountItems(item* current){
  131.      cout << endl;
  132.      cout <<"2 items sold!"<<endl;
  133.      cout << "Sale starting!"<<endl;
  134.  
  135.      cout << "Would you like to enter a discount %?"<<endl;
  136.      float discount;
  137.      cin >> discount;
  138.     //runs a for loop to calculate the discount with the retail value
  139.     for (int x = 0; x<4; x++){
  140.     cout <<fixed<<showpoint<<setprecision(2);
  141.     //the static cast ensures the percentage wont be rounded but exact
  142.     //i multiply the discount inputted then subtract thar from the current retail value for each item in inventory
  143.     //then finally it subtracts the result from retail value
  144.     float percentage = static_cast<float>(current[x].getretailVal()) * discount;
  145.     float result = current[x].getretailVal() - percentage;
  146.     cout << "Price for "<<current[x].getfood()<<" on sale for $"<<result<<endl;
  147.     current[x].setretailVal(current[x].getretailVal()- result);
  148.     }
  149.    
  150. }
  151. //this prints my stock that gets updated over time wihtin main and prints my values for the 4 attributes
  152. void displayStock(item* current) {
  153.     for (int x =0; x<4; x++) {
  154.     cout << current[x].getquantity() << " " << current[x].getfood() << " left in stock at $" << current[x].getretailVal() <<
  155.     " item ID " << current[x].getfoodID() << endl;
  156.     }
  157. }
  158. #endif /* functions_h */
  159.  
  160. //====================================================================================
  161.  
  162. //
  163. //  main.cpp FILE
  164. //  InventoryCode
  165.  
  166.  
  167.  
  168. #include "functions.h"
  169.  
  170.  
  171. int main(){
  172.     cout << "==Welcome to the inventory helper=="<<endl;
  173.     cout << "-store hours will begin shortly"<<endl;
  174.     cout << "Please update your inventory..."<<endl;
  175.     cout<<endl;
  176.  
  177.   //initializing to default values
  178.     char letter;
  179.     string food = "No name";
  180.     string foodID = "0000";
  181.     float retailVal = 0.00;
  182.     int quantity= 0;
  183.    //made 4 separate objects of item with 4 attributes that are contained within my overloaded constructor
  184.     item item1(food, foodID, retailVal, quantity);
  185.     item item2(food, foodID, retailVal, quantity);
  186.     item item3(food, foodID, retailVal, quantity);
  187.     item item4(food, foodID, retailVal, quantity);
  188.     //set my inventory [4] to the 4 objects
  189.     item inventory[4] = { item1, item2, item3, item4 };
  190.    
  191.     //gets the input from user for the store and sets that info inside my array
  192.      for (int x = 0; x < 4; x++) {
  193.      cout << "Enter item " << x + 1 << " to add to inventory:" << endl;
  194.      cin >> food;
  195.      cout << "Enter product manufacturer's id:" << endl;
  196.      cin >> foodID;
  197.      cout << "Enter the retail value:" << endl;
  198.      cin >> retailVal;
  199.      cout << "Enter the quantity available:" << endl;
  200.      cin >> quantity;
  201.      inventory[x].setfood(food);
  202.      inventory[x].setfoodID(foodID);
  203.      cout<<fixed<<setprecision(2);
  204.      inventory[x].setretailVal(retailVal);
  205.       inventory[x].setquantity(quantity);
  206.      cout << endl;
  207.      }
  208.  
  209.  
  210.     displayStock(inventory);
  211.    
  212.     cout <<endl;
  213.    
  214.     cout<< "Business hours are now open"<<endl;
  215.    
  216.    
  217.    
  218.    //this wile loop runs as long as the user does not select 'n' for no to another transaction
  219.     do {
  220.     cout << "Would you like to perform a transaction? (y/n)"<<endl;
  221.     cin >> letter;
  222.         //if the user selects 'n' then the 4 items will be displayed and the program will end
  223.     if (letter == 'n' || letter == 'N') {
  224.         break;
  225.     }
  226.        
  227.     cout <<endl;
  228.     BuyItems(inventory);
  229.     cout << "Would you like to perform another transaction? (y/n)"<<endl;
  230.     cin >> letter;
  231.     if (letter == 'n' || letter == 'N') {
  232.         break;
  233.     }
  234.  
  235.     BuyItems(inventory);
  236.  
  237.     cout << "Would you like to perform another transaction? (y/n)"<<endl;
  238.     cin >> letter;
  239.     if (letter == 'n' || letter == 'N') {
  240.        break;
  241.        }
  242.      
  243.     DiscountItems(inventory);
  244.        
  245.     } while (letter != 'n' || letter !='N');
  246.    
  247.  
  248. //WHILE LOOP ENDS HERE
  249.     //
  250.     cout << endl;
  251.     cout <<endl;
  252.     cout << "Closing Shop -- invenetory left"<<endl;
  253.     cout<<endl;
  254.     displayStock(inventory);
  255.  
  256.     return 0;
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement