James, AKA the Kidd is back providing lesson 5 of his C# programming guide this time focusing on class interaction. The text below is to allow you to easily copy and paste things in if you’re missing something or to easily study the code. As usual for an in-depth explanation please check his lesson on youtube (embedded into this very web page)
C# The Program Class
Code
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“Name thine wizard:”);
Console.WriteLine(“”);
string name = Console.ReadLine();
Wizard wizard1 = new Wizard(name);
Shop shop1 = new Shop();
Item item1 = new Item(“Potion”, 60);
Item item2 = new Item(“Staff”, 50);
Item item3 = new Item(“Burger”, 20);
shop1.addToInventory(item1);
shop1.addToInventory(item2);
shop1.addToInventory(item3);
shop1.addToInventory(item3);
Console.Clear();
Console.WriteLine(wizard1.name + ” arrives at a shop, weary from battle.”);
Console.WriteLine(“He is battered and bruised, but decides to buy some shlock for some reason…”);
Console.WriteLine(“”);
Console.WriteLine(“Press any key…”);
Console.ReadLine();
Console.Clear();
while (true)
{
Console.Clear();
Console.WriteLine(shop1.name + ” has ” + shop1.money + ” money and:”);
Console.WriteLine(“”);
shop1.outputInventory();
Console.WriteLine(“”);
Console.WriteLine(wizard1.name + ” has: ” + wizard1.money + ” money, ” + wizard1.health + ” health and ” + wizard1.mana + ” mana as well as:”);
Console.WriteLine(“”);
wizard1.outputInventory();
Console.WriteLine(“”);
Console.WriteLine(“What will ” + wizard1.name + ” buy?”);
Console.WriteLine(“”);
Console.WriteLine(“1: Potion, 2: Staff, 3: Burger”);
Console.WriteLine(“”);
string option = Console.ReadLine();
switch (option)
{
case “1”:
wizard1.buy(item1, shop1);
break;
case “2”:
wizard1.buy(item2, shop1);
break;
case “3”:
wizard1.buy(item3, shop1);
break;
case “exit”:
Environment.Exit(0);
break;
}
}
}
}
Explanation:
This class forms the main function and entry point of the program, the first thing we need to do is instantiate our Wizard, Shop and Items from their respective classes (the classes are declared further down in the code):
Code:
Console.WriteLine(“Name thine wizard:”);
Console.WriteLine(“”);
string name = Console.ReadLine();
Wizard wizard1 = new Wizard(name);
Shop shop1 = new Shop();
Item item1 = new Item(“Potion”, 60);
Item item2 = new Item(“Staff”, 50);
Item item3 = new Item(“Burger”, 20);
Explanation:
We are doing two things here, first we are using the Console.WriteLine function (this function is a part of the standard Console library, this is included in your project automatically when you create a new console application) to prompt the user to input a string which is stored in the “name” variable of type string. We then instantiate our Wizard, passing its constructor the string we just prompted the user to input. We then instantiate our Shop and three Items. The Shop constructor takes no arguments, while the Item constructor requires two, which we are providing (the Item name and price).
Next, we call the Shop’s addToInventory function four times, passing it a copy of an Item we just instantiated each time:
Code:
shop1.addToInventory(item1);
shop1.addToInventory(item2);
shop1.addToInventory(item3);
shop1.addToInventory(item3);
Explanation:
Next up we are just outputting some arbitrary description of the scene to tell the user what is happening, as well as clearing the console after the user has pressed a key to keep things tidy:
Console.Clear();
Console.WriteLine(wizard1.name + ” arrives at a shop, weary from battle.”);
Console.WriteLine(“He is battered and bruised, but decides to buy some shlock for some reason…”);
Console.WriteLine(“”);
Console.WriteLine(“Press any key…”);
Console.ReadLine();
Console.Clear();
Explanation:
Now we need to call the outputInventory functions of the Wizard and Shop, with some Console.WriteLine calls for formatting, to write the contents of each object’s inventory to the user. We will then ask the user for some input to tell us which item the Wizard will purchase:
while (true)
{
Console.Clear();
Console.WriteLine(shop1.name + ” has ” + shop1.money + ” money and:”);
Console.WriteLine(“”);
shop1.outputInventory();
Console.WriteLine(“”);
Console.WriteLine(wizard1.name + ” has: ” + wizard1.money + ” money, ” + wizard1.health + ” health and ” + wizard1.mana + ” mana as well as:”);
Console.WriteLine(“”);
wizard1.outputInventory();
Console.WriteLine(“”);
Console.WriteLine(“What will ” + wizard1.name + ” buy?”);
Console.WriteLine(“”);
Console.WriteLine(“1: Potion, 2: Staff, 3: Burger”);
Console.WriteLine(“”);
string option = Console.ReadLine();
Explanation:
Now that we have our user’s input stored in this new string variable, called “option”, we need to use a switch statement to determine what to do with this input:
switch (option)
{
case “1”:
wizard1.buy(item1, shop1);
break;
case “2”:
wizard1.buy(item2, shop1);
break;
case “3”:
wizard1.buy(item3, shop1);
break;
case “exit”:
Environment.Exit(0);
break;
}
}
Explanation:
We use this statement to handle the various options we gave the player, think of a switch statement as a simpler form of the if statement that is far more concise and allows for handling of multiple possible cases. When the user inputs an option which matches a case in the switch statement, we run the corresponding block of code (in this case a call the the Wizard’s buy function) and then break out of the while loop, prompting it to restart and run again.
This concludes the contents of the Program class.
C# The Item Class
The item class describes objects of type “Item”. This class is small and only holds a couple of variables and a constructor:
Code:
class Item
{
public string name;
public int price;
public Item(string name, int price)
{
this.name = name;
this.price = price;
}
}
Explanation:
The constructor requires two arguments, a string and an integer, which are placed into the corresponding variables within the Item class when the class is instantiated. Since the Item class has only one constructor, the class cannot be instantiated without these arguments being provided.
This concludes the contents of the Item class.
Part 3: The Wizard Class
The Wizard class describes objects of type “Wizard”, it has a few variables and several functions:
Code:
class Wizard
{
public string name;
public int mana;
public int health;
public int money;
public Item[] inventory;
public Wizard(string name)
{
this.name = name;
this.health = 68;
this.mana = 81;
this.money = 100;
this.inventory = new Item[10];
for (int count = 0; count <= 9; count++)
{
this.inventory[count] = new Item(“Nothing”, 0);
}
}
public void addToInventory(Item newItem)
{
for (int count = 0; count <= 9; count++)
{
if (this.inventory[count].name == “Nothing”)
{
this.inventory[count] = newItem;
break;
}
}
}
public void removeFromInventory(Item newItem)
{
for (int count = 0; count <= 9; count++)
{
if (this.inventory[count].name == newItem.name)
{
this.inventory[count] = new Item(“Nothing”,0);
break;
}
}
}
public void buy(Item newItem, Shop shop)
{
int shopCheckIterator = 0;
for (int count2 = 0; count2 <= 9; count2++)
{
if (shop.inventory[count2].name == newItem.name)
{
shopCheckIterator++;
}
}
if (shopCheckIterator == 0)
{
Console.WriteLine(“”);
Console.WriteLine(shop.name + ” doesn’t have that!”);
Console.WriteLine(“”);
Console.ReadLine();
}
else
{
for (int count = 0; count <= 9; count++)
{
if (this.inventory[count].name == “Nothing”)
{
if (this.money >= newItem.price)
{
addToInventory(newItem);
this.money -= newItem.price;
shop.removeFromInventory(newItem);
break;
}
else if (this.money < newItem.price)
{
Console.WriteLine(“”);
Console.WriteLine(this.name + ” can’t afford that!”);
Console.WriteLine(“”);
Console.ReadLine();
break;
}
}
}
}
}
public void outputInventory()
{
for (int count = 0; count <= 9; count++)
{
Console.WriteLine(this.inventory[count].name);
}
}
}
Explanation
The first thing we do within this class declaration is to declare the variables that exist within this class, as well as the class’ constructor:
Code:
public string name;
public int mana;
public int health;
public int money;
public Item[] inventory;
public Wizard(string name)
{
this.name = name;
this.health = 68;
this.mana = 81;
this.money = 100;
this.inventory = new Item[10];
for (int count = 0; count <= 9; count++)
{
this.inventory[count] = new Item(“Nothing”, 0);
}
}
Explanation:
This class has four regular variables, a string (name) and three integers (mana, health and money). The health and mana variables exist solely for show in this program, they are assigned a value in the constructor, but are never used elsewhere aside from being output.
The Wizard class also has an array variable (inventory) of type Item, this variable is a set of Item objects that we can manipulate from within this class.
The constructor takes one argument, a string, which is placed into the object’s “name” variable. The constructor then initializes the three integer variables with a value. The constructor then inserts ten blank Item objects into the inventory array, iterates over them and instantiates them with a name and price as per the requirements of the Item constructor seen above.
We now start declaring the various functions of the Wizard class:
Code:
public void addToInventory(Item newItem)
{
for (int count = 0; count <= 9; count++)
{
if (this.inventory[count].name == “Nothing”)
{
this.inventory[count] = newItem;
break;
}
}
}
Explanation:
First up is the addToInventory function. This function takes one argument, an object of type Item. This function uses a for loop to iterate over the Items in the Wizard’s inventory array until it finds an Item with a name variable containing the string “Nothing”, it then overwrites this Item with the Item that was passed as the single argument to the function. (Remember that when we initialized the Wizard’s inventory array in the constructor, we filled it with Items with “Nothing” in their name variable.)
Code:
public void removeFromInventory(Item newItem)
{
for (int count = 0; count <= 9; count++)
{
if (this.inventory[count].name == newItem.name)
{
this.inventory[count] = new Item(“Nothing”,0);
break;
}
}
}
Explanation:
Next comes the removeFromInventory function. This function is essentially the inverse of the addToInventory function. It also takes one argument, an object of type Item. Like the addToInventory function, this function uses a for loop to iterate over the inventory array, this time looking for an Item with the same value in its name variable as the Item that was passed as the argument to the function. When a matching Item is found, it is replaced with a blank Item with the name “Nothing” and a price of 0.
Code:
public void buy(Item newItem, Shop shop)
{
int shopCheckIterator = 0;
for (int count2 = 0; count2 <= 9; count2++)
{
if (shop.inventory[count2].name == newItem.name)
{
shopCheckIterator++;
}
}
if (shopCheckIterator == 0)
{
Console.WriteLine(“”);
Console.WriteLine(shop.name + ” doesn’t have that!”);
Console.WriteLine(“”);
Console.ReadLine();
}
else
{
for (int count = 0; count <= 9; count++)
{
if (this.inventory[count].name == “Nothing”)
{
if (this.money >= newItem.price)
{
addToInventory(newItem);
this.money -= newItem.price;
shop.removeFromInventory(newItem);
break;
}
else if (this.money < newItem.price)
{
Console.WriteLine(“”);
Console.WriteLine(this.name + ” can’t afford that!”);
Console.WriteLine(“”);
Console.ReadLine();
break;
}
}
}
}
}
Explanation:
This function is relatively large but simple in practice. The function takes two arguments, an object of type Item, and an object of type Shop. The function first creates a local variable of type integer called shopCheckIterator. This integer will be used to count how many of the Item passed as an argument to the function the Shop has within its inventory in the next step, which iterates over the Shop’s (the one passed as an argument to the function) inventory array and increments the shopCheckIterator each time an Item with a name variable matching the Item passed as an argument is found.
Next, we use an “if” statement to handle cases where the Shop has none of the Item passed as an argument (if after the preceding for loop, the shopCheckIterator is still equal to 0). All we do within this if statement is output a message stating that the Shop does not have any of the Item in question.
The “else” statement handles cases where the Shop has one or more of the Item in question. Within this else statement, we do a few things; first we iterate over the Wizard’s inventory array until we find an Item with the name “Nothing”, we then look at the Item we are attempting to buy’s “price” variable and check whether the Wizard has more money than the Item’s price. If he/she does, we can proceed, if not, we output a message stating that the Wizard cannot afford the Item in question and break out of the function.
Provided that the Wizard can afford the Item in question, we then call the Wizard’s addToInventory function, passing the Item in question to it as an argument, deduct the Item’s price value from the Wizard’s money variable, then call the Shop’s “sell” function, which is declared lower down in the code. We then break out of the function.
Code:
public void outputInventory()
{
for (int count = 0; count <= 9; count++)
{
Console.WriteLine(this.inventory[count].name);
}
}
Explanation:
The final function of the Wizard class is small, it simply iterates over the Wizard’s inventory array using a for loop, and outputs the value of each Item’s name variable using a Console.WriteLine call.
This concludes the contents of the Wizard class.
C# The Shop Class
The Shop class describes objects of type “Shop”, like the Wizard class, it has several variables and functions.
Code:
class Shop
{
public string name;
public int money;
public Item[] inventory;
public Shop()
{
this.name = “Dave’s Shlock Emporium”;
this.money = 500;
this.inventory = new Item[10];
for (int count = 0; count <= 9; count++)
{
this.inventory[count] = new Item(“Nothing”, 0);
}
}
public void addToInventory(Item newItem)
{
for (int count = 0; count <= 9; count++)
{
if (this.inventory[count].name == “Nothing”)
{
this.inventory[count] = newItem;
break;
}
}
}
public void removeFromInventory(Item newItem)
{
for (int count = 0; count <= 9; count++)
{
if (this.inventory[count].name == newItem.name)
{
this.inventory[count] = new Item(“Nothing”, 0);
break;
}
}
}
public void outputInventory()
{
for (int count = 0; count <= 9; count++)
{
Console.WriteLine(this.inventory[count].name);
}
}
}
Explanation:
The Shop class has three variables; name, money and inventory. Name and money are a string and an integer respectively. The inventory variable is an array of Items identical to the Wizard’s. After we declare these variables, we declare the Shop’s constructor.
Code:
public Shop()
{
this.name = “Dave’s Shlock Emporium”;
this.money = 500;
this.inventory = new Item[10];
for (int count = 0; count <= 9; count++)
{
this.inventory[count] = new Item(“Nothing”, 0);
}
}
Explanation:
The Shop’s constructor takes no arguments. It sets the Shop’s name variable to a pre-defined value, as well as the Shop’s money variable. The constructor then uses a for loop, just like the Wizard’s constructor, to iterate over the Shop’s inventory array and populate it with ten blank Item objects.
Code:
public void addToInventory(Item newItem)
{
for (int count = 0; count <= 9; count++)
{
if (this.inventory[count].name == “Nothing”)
{
this.inventory[count] = newItem;
break;
}
}
}
public void removeFromInventory(Item newItem)
{
for (int count = 0; count <= 9; count++)
{
if (this.inventory[count].name == newItem.name)
{
this.inventory[count] = new Item(“Nothing”, 0);
break;
}
}
}
Explanation:
The Shop class’ addToInventory and removeFromInventory functions are identical to the Wizard’s, and so require no further description, they are 1/1 copies of each other and function exactly the same way.
Code:
public void outputInventory()
{
for (int count = 0; count <= 9; count++)
{
Console.WriteLine(this.inventory[count].name);
}
}
Explanation:
As, indeed, is the outputInventory function! Again, it is a 1/1 copy of the Wizard’s outputInventory function and functions in exactly the same way. This concludes the contents of the Shop class, and the code for this lesson. Below you will find the complete code for this lesson for your reference.
C# Lesson 5 Full Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClassInteraction
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“Name thine wizard:”);
Console.WriteLine(“”);
string name = Console.ReadLine();
Wizard wizard1 = new Wizard(name);
Shop shop1 = new Shop();
Item item1 = new Item(“Potion”, 60);
Item item2 = new Item(“Staff”, 50);
Item item3 = new Item(“Burger”, 20);
shop1.addToInventory(item1);
shop1.addToInventory(item2);
shop1.addToInventory(item3);
shop1.addToInventory(item3);
Console.Clear();
Console.WriteLine(wizard1.name + ” arrives at a shop, weary from battle.”);
Console.WriteLine(“He is battered and bruised, but decides to buy some shlock for some reason…”);
Console.WriteLine(“”);
Console.WriteLine(“Press any key…”);
Console.ReadLine();
Console.Clear();
while (true)
{
Console.Clear();
Console.WriteLine(shop1.name + ” has ” + shop1.money + ” money and:”);
Console.WriteLine(“”);
shop1.outputInventory();
Console.WriteLine(“”);
Console.WriteLine(wizard1.name + ” has: ” + wizard1.money + ” money, ” + wizard1.health + ” health and ” + wizard1.mana + ” mana as well as:”);
Console.WriteLine(“”);
wizard1.outputInventory();
Console.WriteLine(“”);
Console.WriteLine(“What will ” + wizard1.name + ” buy?”);
Console.WriteLine(“”);
Console.WriteLine(“1: Potion, 2: Staff, 3: Burger”);
Console.WriteLine(“”);
string option = Console.ReadLine();
switch (option)
{
case “1”:
wizard1.buy(item1, shop1);
break;
case “2”:
wizard1.buy(item2, shop1);
break;
case “3”:
wizard1.buy(item3, shop1);
break;
case “exit”:
Environment.Exit(0);
break;
/*default:
Console.WriteLine(“Huh?”);
break;*/
}
}
}
}
class Item
{
public string name;
public int price;
public Item(string name, int price)
{
this.name = name;
this.price = price;
}
}
class Wizard
{
public string name;
public int mana;
public int health;
public int money;
public Item[] inventory;
public Wizard(string name)
{
this.name = name;
this.health = 68;
this.mana = 81;
this.money = 100;
this.inventory = new Item[10];
for (int count = 0; count <= 9; count++)
{
this.inventory[count] = new Item(“Nothing”, 0);
}
}
public void addToInventory(Item newItem)
{
for (int count = 0; count <= 9; count++)
{
if (this.inventory[count].name == “Nothing”)
{
this.inventory[count] = newItem;
break;
}
}
}
public void removeFromInventory(Item newItem)
{
for (int count = 0; count <= 9; count++)
{
if (this.inventory[count].name == newItem.name)
{
this.inventory[count] = new Item(“Nothing”,0);
break;
}
}
}
public void buy(Item newItem, Shop shop)
{
int shopCheckIterator = 0;
for (int count2 = 0; count2 <= 9; count2++)
{
if (shop.inventory[count2].name == newItem.name)
{
shopCheckIterator++;
}
}
if (shopCheckIterator == 0)
{
Console.WriteLine(“”);
Console.WriteLine(shop.name + ” doesn’t have that!”);
Console.WriteLine(“”);
Console.ReadLine();
}
else
{
for (int count = 0; count <= 9; count++)
{
if (this.inventory[count].name == “Nothing”)
{
if (this.money >= newItem.price)
{
addToInventory(newItem);
this.money -= newItem.price;
shop.removeFromInventory(newItem);
break;
}
else if (this.money < newItem.price)
{
Console.WriteLine(“”);
Console.WriteLine(this.name + ” can’t afford that!”);
Console.WriteLine(“”);
Console.ReadLine();
break;
}
}
}
}
}
public void outputInventory()
{
for (int count = 0; count <= 9; count++)
{
Console.WriteLine(this.inventory[count].name);
}
}
}
class Shop
{
public string name;
public int money;
public Item[] inventory;
public Shop()
{
this.name = “Dave’s Shlock Emporium”;
this.money = 500;
this.inventory = new Item[10];
for (int count = 0; count <= 9; count++)
{
this.inventory[count] = new Item(“Nothing”, 0);
}
}
public void addToInventory(Item newItem)
{
for (int count = 0; count <= 9; count++)
{
if (this.inventory[count].name == “Nothing”)
{
this.inventory[count] = newItem;
break;
}
}
}
public void removeFromInventory(Item newItem)
{
for (int count = 0; count <= 9; count++)
{
if (this.inventory[count].name == newItem.name)
{
this.inventory[count] = new Item(“Nothing”, 0);
break;
}
}
}
public void outputInventory()
{
for (int count = 0; count <= 9; count++)
{
Console.WriteLine(this.inventory[count].name);
}
}
}
}