Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 3 additions & 14 deletions Solutions/in010210/Game Shop/Part 2/Item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,15 @@ void Item::Describe()
cout << setw(13) << "Description" << "= " << description << endl;
cout << setw(13) << "Weight" << "= " << weight << endl;
cout << setw(13) << "Value" << "= " << value << endl;
cout << "\n";
}

Weapon::Weapon(string name, string description, int weight, int value, int damage)
: damage(damage), Item(name, description, weight, value) {}

void Weapon::Describe()
void Weapon::Describe()
{
cout.setf(ios::left);
cout << setw(13) << "Name" << "= " << name << endl;
cout << setw(13) << "Description" << "= " << description << endl;
cout << setw(13) << "Weight" << "= " << weight << endl;
cout << setw(13) << "Value" << "= " << value << endl;
Item::Describe();
cout << setw(13) << "Damage" << "= " << damage << endl;
cout << "\n";
}


Expand All @@ -36,11 +30,6 @@ Armor::Armor(string name, string description, int weight, int value, int defense

void Armor::Describe()
{
cout.setf(ios::left);
cout << setw(13) << "Name" << "= " << name << endl;
cout << setw(13) << "Description" << "= " << description << endl;
cout << setw(13) << "Weight" << "= " << weight << endl;
cout << setw(13) << "Value" << "= " << value << endl;
Item::Describe();
cout << setw(13) << "Defense" << "= " << defense << endl;
cout << "\n";
}
4 changes: 2 additions & 2 deletions Solutions/in010210/Game Shop/Part 2/Item.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Weapon :public Item
int damage;
public:
Weapon(string name, string description, int weight, int value, int damage);
virtual void Describe();
virtual void Describe() override;
};

class Armor :public Item
Expand All @@ -32,5 +32,5 @@ class Armor :public Item
int defense;
public:
Armor(string name, string description, int weight, int value, int defense);
virtual void Describe();
virtual void Describe() override;
};
7 changes: 4 additions & 3 deletions Solutions/in010210/Game Shop/Part 2/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ int main()
{
Item* item = new Weapon("Excalibur", "The legendary sword of King Arthur", 12, 1024, 24);
item->Describe();



item = new Armor("Steel Armor", "Protective covering made by steel", 15, 805, 18);
item->Describe();

item = new Item("HP Potion", "Restores HP", 1, 3);
item->Describe();

delete item;
}
60 changes: 60 additions & 0 deletions Solutions/in010210/Game Shop/Part 3/Game Shop.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "Game Shop.h"

#include <iostream>
#include <iomanip>

Item::Item(string name, string description, int weight, int value)
:name(name), description(description), weight(weight), value(value) {}

void Item::Describe() const
{
cout.setf(ios::left);
cout << setw(13) << "Name" << "= " << name << endl;
cout << setw(13) << "Description" << "= " << description << endl;
cout << setw(13) << "Weight" << "= " << weight << endl;
cout << setw(13) << "Value" << "= " << value << endl;
}

Weapon::Weapon(string name, string description, int weight, int value, int damage)
: damage(damage), Item(name, description, weight, value) {}

void Weapon::Describe() const
{
Item::Describe();
cout << setw(13) << "Damage" << "= " << damage << endl;
}


Armor::Armor(string name, string description, int weight, int value, int defense)
: defense(defense), Item(name, description, weight, value) {}

void Armor::Describe() const
{
Item::Describe();
cout << setw(13) << "Defense" << "= " << defense << endl;
}

Potion::Potion(string name, string description, int weight, int value, string type, int capacity)
:Item(name, description, weight, value), type(type), capacity(capacity) {}

void Potion::Describe() const
{
Item::Describe();
cout << setw(13) << "type" << "= " << type << endl;
cout << setw(13) << "capacity" << "= " << capacity << endl;
}

Shop::Shop(string name, vector<Item*> items)
:name(name), items(items) {}

void Shop::ShowItemList()
{
cout << "\n" << name;
cout << "\n- Item List -\n";


for_each(begin(items), end(items), [](const Item* itemptr) {
itemptr->Describe();
cout << "\n";
});
}
61 changes: 61 additions & 0 deletions Solutions/in010210/Game Shop/Part 3/Game Shop.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#pragma once

#include <vector>
#include <algorithm>
#include <string>

using namespace std;

class Item;

class Shop
{
private:
string name;
vector<Item*> items;

public:
explicit Shop(string name, vector<Item*> items);
void ShowItemList();
};

class Item
{
protected:
string name;
string description;
int weight;
int value;
public:
Item(string name, string description, int weight, int value);
virtual void Describe() const;
};


class Weapon :public Item
{
private:
int damage;
public:
Weapon(string name, string description, int weight, int value, int damage);
virtual void Describe() const override;
};

class Armor :public Item
{
private:
int defense;
public:
Armor(string name, string description, int weight, int value, int defense);
virtual void Describe() const override;
};

class Potion :public Item
{
private:
string type;
int capacity;
public:
Potion(string name, string description, int weight, int value, string type, int capacity);
virtual void Describe() const override;
};
35 changes: 35 additions & 0 deletions Solutions/in010210/Game Shop/Part 3/Main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once
#include "Game Shop.h"
#include <iostream>

int main()
{
vector<Item*> WeaponArmorShopList = {
new Weapon("Sword", "Medium DMG", 3, 10, 10),
new Armor("Cap", "Light Armor", 1, 5, 5),
new Armor("Gloves", "Light Armor", 1, 5, 5),
new Weapon("Axe", "HIgh DMG", 5, 15, 15),
new Armor("Boots", "Light Armor", 1, 5, 5)
};
Shop* WeaponArmorShop = new Shop("\nWelcome to Weapon/Armor Shop!", WeaponArmorShopList);

vector<Item*> PotionShopList{
new Potion("Small Mana Potion", "Recovery 50 MP", 1, 30, "Mana", 50),
new Potion("Medium Health Potion", "Recovery 200 HP", 4, 120, "Health", 200),
new Potion("Medium Mana Potion", "Recovery 100 MP", 2, 75, "Mana", 100),
new Potion("Large Health Potion", "Recovery 300 HP", 6, 200, "Health", 300)
};
Shop* PotionShop = new Shop("\nWelcome to Potion Shop!", PotionShopList);

int n=0;
while (n != 3) {
std::cout << "- Shop Select -\n 1. Weapon/Armor Shop\n 2. Potion Shop\n 3. Exit\n\n Select : ";
std::cin >> n;
if(n==1)WeaponArmorShop->ShowItemList();
else if (n == 2)PotionShop->ShowItemList();
else if (n != 3)std::cout << "\nInvalid number! Try again\n\n";
}

delete WeaponArmorShop;
delete PotionShop;
}