Question

Homework 2: Avatar Cosmetic Description Use Decorator Pattern to design a command-line based avatar customization system...

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:

  • Jacket
  • T-shirt
  • Jeans
  • Shorts
  • Sunglasses
  • Running Shoes

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)

  1. Jacket
  2. T-shirt
  3. Jeans
  4. Shorts
  5. Sunglasses
  6. Running Shoes

>> 6

You have selected Running Shoes for your character.

Please select a cosmetic for your character:

(Type ‘exit’ to finish)

  1. Jacket
  2. T-shirt
  3. Jeans
  4. Shorts
  5. Sunglasses

>> 3

You have selected Jeans for your character.

Please select a cosmetic for your character:

(Type ‘exit’ to finish)

  1. Jacket
  2. T-shirt
  3. Shorts
  4. Sunglasses

>>exit

Your character has the following items :

Running Shoes, Jeans

Thank you for using Avatar 1.0.

Deliverables

  1. C++ Source Code (including all .h header files and .cpp files)

  1. An executable exe file named ‘avatar.exe’

  1. The .gitignore file (optional)

  1. The class diagram image file (in .png or .jpg format)

  1. DO NOT submit any other files such as .o files or IDE project files.

Requirements

  1. Commit all files to Git and push to your bitbucket account. See ‘Folder Structure’ below for detailed format requirements.

  1. Do not leave any identify information in the submitted files which could be used to infer the author’s (your) identity.

Grading Criteria

  1. Functionality: Code has no compile or link error, can run and function properly according to the requirements defined above. (0.5 point)

  1. Format: Code is organized in the required folder structure. (0.5 point)

  1. Version Control: all files properly committed to Git and pushed to Bitbucket. (0.5 point)

  1. Class Diagram: class diagram has correct UML syntax and accurately represent the actual code. (0.5 point)
  1. Smart Pointers: no raw pointer is used. (0.5 point)

  1. Code is following the DRY and the SOLID Principles and the Decorator Pattern. (2.5 point)

Folder Structure

  1. Create a folder named ‘Homework2’

  1. Put your ‘src’ folder (structure requirements specified in the PPT slide 19), the ‘avatar.exe’ file, the .gitignore file(optional), and the class diagram image file (*.png or *.jpg) under ‘Homework2’ .

  1. Commit and push the ‘Homework2’ folder to bitbucket. Use the same bitbucket repo you created for homework1.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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:

Add a comment
Know the answer?
Add Answer to:
Homework 2: Avatar Cosmetic Description Use Decorator Pattern to design a command-line based avatar customization system...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT