I have a c++ project that involves making a invetory of items (like a store) wit
ID: 3843823 • Letter: I
Question
I have a c++ project that involves making a invetory of items (like a store) with the items names, prices, add item, remove item, sell item, buy item and save all the data. The project must must be completed using maps, vector, stacks, and queue. please I need help in completing this project I have not idea of how to start it, I was thinking in doing classes and a final main.cpp. I could use maps to list all the items and a vector to store those items but I dont know how to do it, please I need help is fine if not all the structures are use such as maps, vector, stacks, and queue but at least two of the four .
Explanation / Answer
#include "stdafx.h"
#include "Player.h"
#include "ShopKeeper.h"
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
int main()
{
Player player; //The player
ShopKeeper shopKeeper; //The shop keeper
int responce; //Menu navigation
std::cout << "Greetings " << player.GetName() << ". Feel free to browse my wares." << " ";
std::cout << "1: Purchase Items. 2: Sell Items. 3: List Your Items. 4: Show Gold. 5: Exit" << " ";
do
{
std::cin >> responce;
switch (responce)
{
case 1:
shopKeeper.PurchaseItem(player);
break;
case 2:
shopKeeper.SellItem(player);
break;
case 3:
player.ListInventory();
break;
case 4:
std::cout << "You have " << player.GetGold() << " gold coins." << " ";
break;
case 5:
std::cout << "Thank you for shopping." << " ";
break;
default:
std::cout << "Please enter valid data." << " ";
std::cout << "1: Purchase Items. 2: Sell Items. 3: List Your Items. 4: Show Gold. 5: Exit" << " ";
}
std::cout << "1: Purchase Items. 2: Sell Items. 3: List Your Items. 4: Show Gold. 5: Exit" << " ";
} while (responce != 5);
/*
//This works
player.AddItem("Mace", 30);
player.ListInventory();
std::cout << player.GetGold();
*/
//Keep window open
std::string barn;
std::cin >> barn;
return 0;
}
ShopKeeper.h
#pragma once
#include "Player.h"
#include <string>
class ShopKeeper
{
private:
public:
void PurchaseItem(Player& player); //Shop keeper has player buy items from them
void SellItem(Player& player); //Shop keeper sells item to player
ShopKeeper();
~ShopKeeper();
};
ShopKeeper.cpp
#include "stdafx.h"
#include "ShopKeeper.h"
#include "Player.h"
#include <iostream>
//Player purchases item from shop keeper
void ShopKeeper::PurchaseItem(Player& player)
{
//Player player;
int responce = 0; //Menu navigation
std::cout << "1: Mace - 30 gold. 2: Bow - 50 gold. 3: Boots - 10 gold. 4: Bearskin - 75 gold. 5: Helmet - 25 gold." << " ";
do
{
std::cin >> responce;
switch (responce)
{
case 1:
player.AddItem("Mace", 30);
break;
case 2:
player.AddItem("Bow", 50);
break;
case 3:
player.AddItem("Boots", 10);
break;
case 4:
player.AddItem("Bearskin", 75);
break;
case 5:
player.AddItem("Helmet", 25);
break;
default:
std::cout << "Please enter valid data." << " ";
std::cout << "1: Mace - 30 gold. 2: Bow - 50 gold. 3: Boots - 10 gold. 4: Bearskin - 75 gold. 5: Helmet - 25 gold." << " ";
}
} while (responce > 5 || responce < 1);
}
//Shop keeper sells item to player
void ShopKeeper::SellItem(Player& player)
{
//Player player;
int responce = 0;
player.ListInventory();
if (responce < player.GetNumbOfItems())
{
std::cin >> responce;
switch (responce)
{
case 1:
player.SellItem(0, 20);
break;
case 2:
player.SellItem(1, 20);
break;
case 3:
player.SellItem(2, 20);
break;
case 4:
player.SellItem(3, 20);
break;
case 5:
player.SellItem(4, 20);
break;
default:
std::cout << "Please enter valid data." << " ";
player.ListInventory();
}
}
else
{
std::cout << "Item doesn't exist.";
}
}
ShopKeeper::ShopKeeper()
{
}
ShopKeeper::~ShopKeeper()
{
}
Player.h
#pragma once
#include <vector>
class Player
{
private:
const int maxNumbItems = 5; //Maximum number of items that inventory can store
int goldCoins = 150, //Amount of gold coins the player has
numbOfItems = 0; //Number of con-current items player holds
std::vector<std::string> inventory; //Players inventory
std::string name = "Gorrex"; //Players name
public:
std::string GetName(); //Get the players name
std::string AddItem(std::string item, int itemPrice); // Add item to players inventory
void Player::SellItem(int itemNum, int itemPrice); //Sell item
bool IsInventoryFull(); //Check to see if players inventory is full
int InventoryCapacity(); //Get capacity of inventory
int GetGold(); //Get players gold
void ListInventory();
int GetNumbOfItems();
Player();
~Player();
};
Player.cpp
#include "stdafx.h"
#include "Player.h"
#include <iostream>
#include <ostream>
#include <string>
//Get the players name
std::string Player::GetName()
{
return name;
}
//Add item to players inventory
std::string Player::AddItem(std::string item, int itemPrice)
{
//Is players inventory not full?
if (IsInventoryFull())
{
std::cout << "Inventory is full.";
}
else
{
//Can player afford item?
if (goldCoins >= itemPrice)
{
goldCoins -= itemPrice;
numbOfItems++;
std::cout << "You have purchased " << item << "." << " ";
inventory.push_back(item); //Add item to inventory
return item;
}
//If player cant afford item
else
{
std::cout << "You cannot afford this item." << " ";
}
}
}
void Player::SellItem(int itemNum, int itemPrice)
{
char responce;
std::cout << "Are you sure you want to sell: " << inventory[itemNum] << "? 'y' - Yes. 'n' - No." << " ";
std::cin >> responce;
switch (responce)
{
case 'y':
numbOfItems++;
goldCoins += itemPrice;
inventory.erase(inventory.begin() + itemNum);
break;
case 'n':
std::cout << "That is ok." << " ";
break;
default:
std::cout << "Please enter correct data." << " ";
}
}
//Check to see if players inventory is full
bool Player::IsInventoryFull()
{
//If players inventory isnt full
if (numbOfItems < maxNumbItems)
{
return false;
}
//If players inventory is full
else
{
return true;
}
}
//Return size of players inventory
int Player::InventoryCapacity()
{
return inventory.size();
}
//Get the players gold
int Player::GetGold()
{
return goldCoins;
}
//List the players inventory
void Player::ListInventory()
{
int itemNumb = 0; //item number in menu
for (int i = 0; i < inventory.size(); i++)
{
itemNumb++;
std::cout << itemNumb << ": " << inventory[i] << " ";
}
/* //If inventory is empty
if (inventory.empty())
{
std::cout << "inventory is empty" << " ";
}*/
}
int Player::GetNumbOfItems()
{
return numbOfItems;
}
Player::Player()
{
}
Player::~Player()
{
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.