2. Create a class for an inventory system for an RPG. Your solution should conta
ID: 3674869 • Letter: 2
Question
2. Create a class for an inventory system for an RPG.
Your solution should contain the following classes
An Interface called IContainer - items that implement IContainer can have other things put inside them
Add method - adds an item to the container
TotalCount property - returns how many items are in the container including any containers inside
TotalWeight property - returns the total weight of the container including any containers inside
A class called Inventory that implements IContainer
The constructor should pass in the number of items in the inventory
An abstract base class called Item
Each item should have a cost and a weight property
Several inherited objects from a typical RPG. Each item should have a different cost and weight
A BagOfHolding class which inherits from Item and implements IContainer
You should write some test code to test all of your classes. For example it might look something like this. Make sure that you test the recursive TotalCount and TotalWeight methods
var inventory = new Inventory(3);
inventory.Add(new Sword());
inventory.Add(new Potion());
var bagOfHolding = new BagOfHolding(4);
inventory.Add(bagOfHolding);
bagOfHolding.Add(new Sword());
Console.WriteLine( "The number of items in the inventory is {0} ", inventory.TotalCount); //There are 4 items in this example
Console.WriteLine( "The total weight of the inventory is {0} ", inventory.TotalWeight); //Weight will depend on the weights of each item
Explanation / Answer
interface IContainer
{
public add(int TotalCount())
{
int a;
a=TotalCount();
}
vod TotalCount()
{
intb=10;
System.out.println(“total count is “+b);
}
public add(float TotalWeight())
{
float c;
c=TotalWeight();
void TotalWeight()
{
float wei=14.5;
System.out.println(“total weight is “+wei);
}
}
public class Inventory implements IContainer
{
public Inventory (int total)
{
int x;
x=toal;
System.ot.println(“The total is “+x);
}
}
abstract class item
{
int cost;
float weight;
}
public class BagOfHolding extends item implements IContainer
{
public BagOfHolding(int a);
{
int b;
System.out.println(“The value is “+b);
}
}
class abc
{
Public static void main(String args[])
{
Inventory inventory;
inventory.Add(new Sword());
inventory.Add(new Potion());
var bagOfHolding = new BagOfHolding(4);
inventory.Add(bagOfHolding);
bagOfHolding.Add(new Sword());
Console.WriteLine( "The number of items in the inventory is {0} ", inventory.TotalCount);
Console.WriteLine( "The total weight of the inventory is {0} ", inventory.TotalWeight);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.