Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/* 1. You are to implement the Consumables and the Accessories class, create files for each containing any number of consumables and accessories. (make sure

/*

1. You are to implement the Consumables and the Accessories class, create files for each containing any number

of consumables and accessories. (make sure they contain all the information needed).

2. All files have _ (underscores) for the spaces. I show how you remove the spaces for the weapons, you

should remove the spaces for all of the words. **challenge is placing them back when you write to the file**

3. In the main, have the ability to show all the (weapon / armor / accessories / consumables) depending what

the user wants to see. display those items only.

4. When the program exits, save all the items back in their respective file. To know what each

item is, you will need to create a variable in Item called whatIsIt which holds a string holding the

item type, so if your object is a weapon, it will say weapon. This variable should go between the

name and the description.

You will need to UPDATE all files, all the classes, the way you infile and outfile in order for the program

to work well with this new variable.

** also, make sure you start your files with the number of items in the file, so when you are saving

everything back to the file, you need to know how many of those items there are

*/

#include #include #include

using namespace std;

// This is the base class

class Item

{

private:

string name;

//whatIsIt goes here

string description;

double weight;

public:

//these two are the constructors, they will be changed once you include the

//whatIsIt variable.

Item(){name = "NULL"; description = "NULL"; weight = 0;}

Item(string name, string description, double weight)

{

this->name = name;

this->description = description;

this->weight = weight;

}

string getName(){return name;}

void setName(string name){this->name = name;}

string getDescription(){return description;}

void setDescription(string description){this->description=description;}

double getWeight(){return weight;}

void setWeight(double weight){this->weight = weight;}

virtual void display()

{

cout<<"Name = "<

//what is it display goes here.

cout<<"Description = "<

cout<<"Weight = "<

cout<

}

};

//gear is derived from Item. There are no gear in our program, but weapon, armor and accessories

//are derived from Gear.

class Gear:public Item

{

private:

int dmg;

int def;

int spd;

public:

Gear():Item(){dmg = 0; def = 0; spd = 0;}

//make sure to add the whatIsIt here too.

Gear(string name, string description, double weight,int dmg, int def, int spd)

:Item(name,description,weight)

{

this->dmg = dmg;

this->def = def;

this->spd = spd;

}

int getDmg(){return dmg;}

int getDef(){return def;}

int getSpd(){return spd;}

void setDef(int def){this->def = def;}

void setDmg(int dmg){this->dmg = dmg;}

void setSpd(int spd){this->spd = spd;}

virtual void display()

{

cout<<"Name = "<

//something missing here..

cout<<"Description = "<

cout<<"Weight = "<

cout<<"Defence = "<

cout<<"Damage = "<

cout<<"Speed = "<

cout<

}

};

class Weapon : public Gear

{

private:

int range;

public:

Weapon():Gear(){range = 0;}

//whatIsIt missing in this overloaded constructor too.

Weapon(string name, string description, double weight,int dmg, int def, int spd, int range)

:Gear(name,description,weight,dmg,def,spd)

{this->range = range;}

int getRange(){return range;}

void setRange(int range){this->range = range;}

virtual void display()

{

cout<<"Name = "<

//missing here too.

cout<<"Description = "<

cout<<"Weight = "<

cout<<"Defence = "<

cout<<"Damage = "<

cout<<"Speed = "<

cout<<"Range = "<

cout<

}

};

class Armor : public Gear

{

private:

string type;

public:

Armor():Gear(){type = "NULL";}

//add the whatIsIt here too.

Armor(string name, string description, double weight,int dmg, int def, int spd, string type)

:Gear(name,description,weight,dmg,def,spd)

{this->type = type;}

string getType(){return type;}

void setType(string type){this->type = type;}

virtual void display()

{

cout<<"Name = "<

//missing here too.

cout<<"Description = "<

cout<<"Weight = "<

cout<<"Defence = "<

cout<<"Damage = "<

cout<<"Speed = "<

cout<<"Type = "<

cout<

}

};

int main()

{

//lets create a backpack with 30 size MAX

Item *inventory[30];

int lastitemlocation = 0; //this keeps track of how many items we have currently in inventory

int size; //used when infile to know how many objects are in the file.

string name;

//whatIsIt variable goes here.

string description;

double weight;

int dmg;

int def;

int spd;

int range;

string type;

ifstream inFile;

int choice; // first original choice to select item.

int choice2; // what you want to do with first item.

int choice3;// select second item

inFile.open("weapons.txt");

//FOR ALL THE INFILES, whatIsIt IS ALSO IN THERE, MODIFY THESE.

inFile>>size;

getline(inFile,type);

cout<

for (int x=0; x

{

inFile>>name>>description>>weight>>dmg>>def>>spd>>range;

//changes all the _ to a space for the names.

for (int y=0; y

{

if (name.substr(y,1)=="_")

{

name[y] = ' ';

}

}

//changes all the _ to a space for the descriptions.

for (int y=0; y

{

if (description.substr(y,1)=="_")

{

description[y] = ' ';

}

}

inventory[lastitemlocation] = new Weapon(name,description,weight,dmg,def,spd,range);

lastitemlocation++;

}

inFile.close();

inFile.open("armors.txt");

//how many armors there are.

inFile>>size;

//gets all the data for the armors.

for (int x=0; x

{

inFile>>name>>description>>weight>>dmg>>def>>spd>>type;

inventory[lastitemlocation] = new Armor(name,description,weight,dmg,def,spd,type);

lastitemlocation++;

}

inFile.close();

//main loop of the program.

do

{

//Display all the items in the inventory.

for (int x=0; x

{

//we start the numbers at 1, but remember our array starts at 0!

cout<

}

cout<<"Select an item"<

cin>>choice;

system("cls");

cout<<"You have selected the following item"<

inventory[choice-1]->display();

//here are the original 4 options. you will add a new one here to view all items of

//the same whatIsIt as the one selected. This means the loop will no longer end at 4 since

//4 is now to "view like items", so 5 will be the "exit program"

cout<<"What would you like to do with this item?"<

cout<<"1. Remove"<

cout<<"2. Duplicate"<

cout<<"3. Swap with another item"<

cout<<"4. exit program"<

cin>>choice2;

system("cls");

switch(choice2)

{

case 1:

//will remove the item from the list.

inventory[choice-1] = new Item();

break;

case 2:

//this is to duplicate the item selected to the last location in the array.

//it will then increase the number of items you are holding in the array by 1.

inventory[lastitemlocation] = new Item();

inventory[lastitemlocation] = inventory[choice-1];

lastitemlocation++;

break;

case 3:

//simple swap.

for (int x=0; x

{

if (choice-1 == x)

cout<

else

cout<

}

cout<<"Which are you going to swap with the selected item?"<

cin>>choice3;

system("cls");

swap(inventory[choice-1],inventory[choice3-1]);

break;

case 4:

break;

default:

break;

};

}while (choice2!=4);

//search the array for a null and place it at the end. decrease size of the array

for (int x=0; x

{

for (int y=0; y

{

if (inventory[y]->getName()=="NULL")

{

//inventory[x] = inventory[lastitemlocation-1];

swap(inventory[y],inventory[lastitemlocation-1]);

lastitemlocation--;

}

}

}

//used somewhat a version of bubble sort, which is not like that of your book.

cout<<"Sorted"<

for (int x=0; x

{

for (int y=0; y

{

if (inventory[y]->getName()>inventory[y+1]->getName())

swap(inventory[y],inventory[y+1]);

}

}

//display the sorted list.

for (int x=0; x

{

cout<

}

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems For Advanced Applications 9th International Conference Dasfaa 2004 Jeju Island Korea March 2004 Proceedings Lncs 2973

Authors: YoonJoon Lee ,Jianzhong Li ,Kyu-Young Whang

2004th Edition

3540210474, 978-3540210474

More Books

Students also viewed these Databases questions

Question

Discuss all branches of science

Answered: 1 week ago