Homework 2: Avatar Cosmetic
Description
Use Decorator Pattern to design a command-line based avatar customization system for a game.
The user shall select an avatar from two options: a male and a female.
The user shall select various cosmetics for the avatar, including:
The system shall display the description of the avatar at the end of the customization.
Example UI
Welcome to the Avatar 1.0 System!
Please select a cosmetic for your character:
(Type ‘exit’ to finish)
>> 6
You have selected Running Shoes for your character.
Please select a cosmetic for your character:
(Type ‘exit’ to finish)
>> 3
You have selected Jeans for your character.
Please select a cosmetic for your character:
(Type ‘exit’ to finish)
>>exit
Your character has the following items :
Running Shoes, Jeans
Thank you for using Avatar 1.0.
Deliverables
Requirements
Grading Criteria
Folder Structure
Code:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Avatar
{
public:
virtual string getDescription() = 0;
};
class ActualAvatar : public Avatar
{
public:
string getDescription() override;
};
string ActualAvatar::getDescription()
{
return "";
}
class AvatarDecorator : public Avatar
{
protected:
//Avatar being decorated
Avatar* m_avatar;
public:
AvatarDecorator(Avatar* avatar):m_avatar(avatar)
{
}
string getDescription() override;
};
string AvatarDecorator::getDescription()
{
return m_avatar->getDescription();
}
class Jacket : public AvatarDecorator
{
public:
Jacket(Avatar* avatar):AvatarDecorator(avatar)
{
}
string getDescription() override;
};
string Jacket::getDescription()
{
return m_avatar->getDescription() +
"Jacket,";
}
class TShirt : public AvatarDecorator
{
public:
TShirt(Avatar* avatar) :AvatarDecorator(avatar)
{
}
string getDescription() override;
};
string TShirt::getDescription()
{
return m_avatar->getDescription() +
"T-Shirt,";
}
class Jeans : public AvatarDecorator
{
public:
Jeans(Avatar* avatar) :AvatarDecorator(avatar)
{
}
string getDescription() override;
};
string Jeans::getDescription()
{
return m_avatar->getDescription() + "Jeans,";
}
class Shorts : public AvatarDecorator
{
public:
Shorts(Avatar* avatar) :AvatarDecorator(avatar)
{
}
string getDescription() override;
};
string Shorts::getDescription()
{
return m_avatar->getDescription() +
"Shorts,";
}
class Sunglasses : public AvatarDecorator
{
public:
Sunglasses(Avatar* avatar)
:AvatarDecorator(avatar)
{
}
string getDescription() override;
};
string Sunglasses::getDescription()
{
return m_avatar->getDescription() +
"Sunglasses,";
}
class RunningShoes : public AvatarDecorator
{
public:
RunningShoes(Avatar* avatar)
:AvatarDecorator(avatar)
{
}
string getDescription() override;
};
string RunningShoes::getDescription()
{
return m_avatar->getDescription() +
"RunningShoes,";
}
int main()
{
Avatar* anAvatar = new ActualAvatar();
vector<string> vDecorators = {
"Jacket","T-Shirt","Jeans",
"Shorts","Sunglasses","RunningShoes" };
while (true)
{
string choice;
cout << "Please select
cosmetic of your character : \n";
cout << "(Type exit to
finish)\n";
for(string decorator :
vDecorators)
{
cout <<
"\n" << decorator;
}
cout << "\n>> ";
cin >> choice;
if (choice == "exit")
break;
int ch = stoi(choice);
switch (ch)
{
case 1:
anAvatar = new
Jacket(anAvatar);
vDecorators.erase(vDecorators.begin());
break;
case 2:
anAvatar = new
TShirt(anAvatar);
vDecorators.erase(vDecorators.begin() + 1);
break;
case 3:
anAvatar = new
Jeans(anAvatar);
vDecorators.erase(vDecorators.begin() + 2);
break;
case 4:
anAvatar = new
Shorts(anAvatar);
vDecorators.erase(vDecorators.begin() + 3);
break;
case 5:
anAvatar = new
Sunglasses(anAvatar);
vDecorators.erase(vDecorators.begin() + 4);
break;
case 6:
anAvatar = new
RunningShoes(anAvatar);
vDecorators.erase(vDecorators.begin() + 5);
break;
default:
break;
}
}
cout << "Your character has the following items
: \n";
string items = anAvatar->getDescription();
items.pop_back();
cout << items;
cout << "\nThank you for using Avatar
1.0.\n";
return 0;
}
OUTPUT:
Homework 2: Avatar Cosmetic Description Use Decorator Pattern to design a command-line based avatar customization system...