1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
#include "pch.h"
#include <iostream>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
class Item
{
public:
Item(std::string itemName, int itemBaseCost)
: mItemName(itemName), mItemBaseCost(itemBaseCost) {}
int ItemCost() const { return mItemBaseCost; }
std::string ItemName() const { return mItemName; }
friend std::ostream &operator<<(std::ostream& os, Item &item)
{
os << item.mItemName << '\n';
os << "Cost: " << item.mItemBaseCost << " Credits\n";
return os;
}
private:
std::string mItemName;
int mItemBaseCost;
};
class Player
{
public:
Player(std::string playerName, int playerCredits)
: mPlayerName(playerName), mPlayerCredits(playerCredits){}
std::string GetPlayerName() const { return mPlayerName; }
int GetPlayerCredits() const { return mPlayerCredits; }
void AddItemToPlayerInventory(const Item& item)
{
mPlayerInventory.push_back(item);
mPlayerCredits -= item.ItemCost();
}
std::vector<Item> ShowPlayerInventory() const
{
return mPlayerInventory;
}
private:
std::string mPlayerName;
std::pair<std::string, int> mPlayerAttackNameAndPower;
std::vector<Item> mPlayerInventory;
int mPlayerCredits; //Money
};
class Shop
{
public:
void AddItems(std::string itemName, int itemBaseCost)
{
mShopInventory.push_back(Item(itemName, itemBaseCost));
}
void ShowShopInventory()
{
int number = 1;
for (unsigned int i = 0; i < mShopInventory.size(); i++)
std::cout << number++ << ") " << mShopInventory[i] << "\n";
}
void Buy(Player& player)
{
bool isTransactionComplete = false;
int choice{};
while (!isTransactionComplete)
{
std::cout << "Welcome to the shop, what would you like to buy?\n\n";
std::cout << "Your current credit balance is: " << player.GetPlayerCredits() << " Credits\n\n";
ShowShopInventory();
std::cout << ">";
std::cin >> choice;
std::size_t index = choice - 1;
if (index < mShopInventory.size() && player.GetPlayerCredits() >= mShopInventory[index].ItemCost())
{
player.AddItemToPlayerInventory(mShopInventory[index]);
std::cout << "Your new balance is: " << player.GetPlayerCredits() << " Credits\n";
std::cout << "Your inventory now has: " << player.ShowPlayerInventory()[index].ItemName() << '\n';
isTransactionComplete = true;
}
else if (player.GetPlayerCredits() < mShopInventory[index].ItemCost())
{
std::cout << "You do not have the necessary funds to purchase that item.\n";
std::cout << "You need " << mShopInventory[index].ItemCost() << " credits, and you have " << player.GetPlayerCredits() << ".\n";
std::cout << "You need " << mShopInventory[index].ItemCost() - player.GetPlayerCredits() << " more credits to purchase this item.\n\n";
}
}
}
private:
std::vector<Item> mShopInventory;
};
int main()
{
Shop shop;
shop.AddItems("Golden Necklace of Speak Good", 450);
shop.AddItems("Helmet of Protect Head", 800);
shop.AddItems("Staff of Magic Stuff", 8000);
shop.AddItems("Mask of Suffocation", 420);
shop.AddItems("Dagger of Major Stabness", 230);
Player player("Chay", 600);
shop.Buy(player);
return 0;
}
|