Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // itemInfo.h FILE
- // InventoryCode Assignment 2
- #ifndef itemInfo_h
- #define itemInfo_h
- #include <iostream>
- #include <string>
- #include <iomanip>
- #include <array>
- #include <cmath>
- using namespace std;
- class item{
- private:
- //made 4 private variables that hold
- string food;
- string foodID;
- float retailVal;
- int quantity;
- public:
- //made mutators and accessors of all the private variables so i can set and retrieve that data
- void setfood(string f){
- food=f;
- }
- string getfood(){
- return food;
- }
- void setfoodID(string fID){
- foodID = fID;
- }
- string getfoodID(){
- return foodID;
- }
- void setretailVal(float rV){
- retailVal = rV;
- }
- float getretailVal(){
- return retailVal;
- }
- void setquantity(int q){
- quantity =q;
- }
- int getquantity(){
- return quantity;
- }
- item();
- item(string f, string ID, float v, int q);
- };
- //my default constructer so that the computer will assign these values if nothing is retrieved
- item::item() {
- food = "Unnamed";
- foodID= "0";
- retailVal = 0;
- quantity = 0;
- }
- //my overloaded constructor which allows item1,item2, item3, item4 to take in these 4 attributes in main
- item::item(string f, string ID, float v, int q) {
- food = f;
- foodID= ID;
- retailVal = v;
- quantity = q;
- }
- void BuyItems(item* current);
- void DiscountItems(item* current);
- void displayStock(item* current);
- #endif /* itemInfo_h */
- //=================================================================================================
- //
- // functions.h FILE
- //
- #ifndef functions_h
- #define functions_h
- #include "itemInfo.h"
- //item*current points the info within the function each time its updated in the function
- //buyItems completes a transaction
- void BuyItems(item* current){
- int choice;
- cout<< "====Menu===="<<endl;
- cout << "Enter the item you would like to purchase"<<endl;
- for (int x=0; x<4; x++) {
- cout << x +1 << ". " << current[x].getquantity() << " " << current[x].getfood() << " left in stock at $" <<
- current[x].getretailVal() << " item ID " << current[x].getfoodID() << endl;
- }
- cin >> choice;
- int item1;
- // runs a do while wwhich runs til the user gets an amount that is below the set quantity
- do {
- cout <<"How many would you like to buy?"<<endl;
- cin >> item1;
- if (item1 > current[choice - 1].getquantity()) {
- cout << "Not enough "<<current[choice - 1].getfood()<< " in stock ("<<current[choice - 1].getquantity()
- <<") total stock"<<endl;
- }else{
- //this multiplies the quantity with the retail value
- //then prints the amount sold and how many
- double total = (current[choice - 1].getretailVal() * (item1));
- cout << "SOLD "<<item1<<" "<<current[choice - 1].getfood()<<" for $"<<total<<endl;
- current[choice-1].setquantity(current[choice - 1].getquantity() - item1);
- break;
- }
- } while (item1 > current[choice - 1].getquantity()) ;
- }
- //this discounts the items after two items have been sold
- void DiscountItems(item* current){
- cout << endl;
- cout <<"2 items sold!"<<endl;
- cout << "Sale starting!"<<endl;
- cout << "Would you like to enter a discount %?"<<endl;
- float discount;
- cin >> discount;
- //runs a for loop to calculate the discount with the retail value
- for (int x = 0; x<4; x++){
- cout <<fixed<<showpoint<<setprecision(2);
- //the static cast ensures the percentage wont be rounded but exact
- //i multiply the discount inputted then subtract thar from the current retail value for each item in inventory
- //then finally it subtracts the result from retail value
- float percentage = static_cast<float>(current[x].getretailVal()) * discount;
- float result = current[x].getretailVal() - percentage;
- cout << "Price for "<<current[x].getfood()<<" on sale for $"<<result<<endl;
- current[x].setretailVal(current[x].getretailVal()- result);
- }
- }
- //this prints my stock that gets updated over time wihtin main and prints my values for the 4 attributes
- void displayStock(item* current) {
- for (int x =0; x<4; x++) {
- cout << current[x].getquantity() << " " << current[x].getfood() << " left in stock at $" << current[x].getretailVal() <<
- " item ID " << current[x].getfoodID() << endl;
- }
- }
- #endif /* functions_h */
- //====================================================================================
- //
- // main.cpp FILE
- // InventoryCode
- #include "functions.h"
- int main(){
- cout << "==Welcome to the inventory helper=="<<endl;
- cout << "-store hours will begin shortly"<<endl;
- cout << "Please update your inventory..."<<endl;
- cout<<endl;
- //initializing to default values
- char letter;
- string food = "No name";
- string foodID = "0000";
- float retailVal = 0.00;
- int quantity= 0;
- //made 4 separate objects of item with 4 attributes that are contained within my overloaded constructor
- item item1(food, foodID, retailVal, quantity);
- item item2(food, foodID, retailVal, quantity);
- item item3(food, foodID, retailVal, quantity);
- item item4(food, foodID, retailVal, quantity);
- //set my inventory [4] to the 4 objects
- item inventory[4] = { item1, item2, item3, item4 };
- //gets the input from user for the store and sets that info inside my array
- for (int x = 0; x < 4; x++) {
- cout << "Enter item " << x + 1 << " to add to inventory:" << endl;
- cin >> food;
- cout << "Enter product manufacturer's id:" << endl;
- cin >> foodID;
- cout << "Enter the retail value:" << endl;
- cin >> retailVal;
- cout << "Enter the quantity available:" << endl;
- cin >> quantity;
- inventory[x].setfood(food);
- inventory[x].setfoodID(foodID);
- cout<<fixed<<setprecision(2);
- inventory[x].setretailVal(retailVal);
- inventory[x].setquantity(quantity);
- cout << endl;
- }
- displayStock(inventory);
- cout <<endl;
- cout<< "Business hours are now open"<<endl;
- //this wile loop runs as long as the user does not select 'n' for no to another transaction
- do {
- cout << "Would you like to perform a transaction? (y/n)"<<endl;
- cin >> letter;
- //if the user selects 'n' then the 4 items will be displayed and the program will end
- if (letter == 'n' || letter == 'N') {
- break;
- }
- cout <<endl;
- BuyItems(inventory);
- cout << "Would you like to perform another transaction? (y/n)"<<endl;
- cin >> letter;
- if (letter == 'n' || letter == 'N') {
- break;
- }
- BuyItems(inventory);
- cout << "Would you like to perform another transaction? (y/n)"<<endl;
- cin >> letter;
- if (letter == 'n' || letter == 'N') {
- break;
- }
- DiscountItems(inventory);
- } while (letter != 'n' || letter !='N');
- //WHILE LOOP ENDS HERE
- //
- cout << endl;
- cout <<endl;
- cout << "Closing Shop -- invenetory left"<<endl;
- cout<<endl;
- displayStock(inventory);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement