Practical
Learning: Adding Items to a Collection |
|
- Change the FlowerInventory.cs file as follows:
using System;
namespace FlowerShop4
{
public abstract class AFlower
{
protected int items;
public AFlower()
{
items = 0;
}
public int Count
{
get { return items; }
}
public abstract int Add(Flower obj);
}
public class FlowerInventory : AFlower
{
public Flower Head;
public Flower Inventory;
public FlowerInventory()
{
Head = null;
}
public override int Add(Flower NewFlower)
{
Flower Sample = new Flower();
Sample = NewFlower;
Sample.Next = Head;
Head = Sample;
return items++;
}
}
}
|
- Access the Program.cs and change it as follows:
using System;
namespace FlowerShop4
{
public class Program
{
static int Main(string[] args)
{
FlowerInventory flowers = new FlowerInventory();
Flower nice;
nice = new Flower();
nice.Type = FlowerType.Lilies;
nice.Color = FlowerColor.White;
nice.Arrangement = FlowerArrangement.Bouquet;
nice.UnitPrice = 39.95M;
flowers.Add(nice);
nice = new Flower();
nice.Type = FlowerType.Daisies;
nice.Color = FlowerColor.Mixed;
nice.Arrangement = FlowerArrangement.Bouquet;
nice.UnitPrice = 40.50M;
flowers.Add(nice);
nice = new Flower();
nice.Type = FlowerType.Carnations;
nice.Color = FlowerColor.Lavender;
nice.Arrangement = FlowerArrangement.Any;
nice.UnitPrice = 34.85M;
flowers.Add(nice);
nice = new Flower();
nice.Type = FlowerType.Roses;
nice.Color = FlowerColor.Pink;
nice.Arrangement = FlowerArrangement.Bouquet;
nice.UnitPrice = 29.95M;
flowers.Add(nice);
nice = new Flower();
nice.Type = FlowerType.Daisies;
nice.Color = FlowerColor.Yellow;
nice.Arrangement = FlowerArrangement.Vase;
nice.UnitPrice = 29.95M;
flowers.Add(nice);
return 0;
}
}
}
|
|
- Save all
Once a list exists, the user can explore it. One of the operations performed on items is to locate and retrieve one. To do this, you can
create a method that takes as argument an index. The method would examine the argument with regards to the number of items in the list to make sure the argument's value is in the range of
the current items of the list. If the number is too low or too high, the method can return null or 0. If the number is in the range, the method can return the item at that position. Here is an example:
public class Numbers
{
int size;
Number Sample;
public Number Head;
public Numbers()
{
size = 0;
Head = null;
}
public int Count
{
get { return size; }
}
public int Add(Number NewItem)
{
Number Sample = new Number();
Sample = NewItem;
Sample.Next = Head;
Head = Sample;
return size++;
}
public Number Retrieve(int Position)
{
Number Current = Head;
for (int i = Count - 1; i > Position && Current != null; i--)
Current = Current.Next;
return Current;
}
}
Practical
Learning: Retrieving the Items of a Collection |
|
- Access the FlowerInventory.cs file and add the following method:
using System;
namespace FlowerShop4
{
public abstract class AFlower
{
protected int items;
public AFlower()
{
items = 0;
}
public int Count
{
get { return items; }
}
public abstract int Add(Flower obj);
public abstract Flower Get(int index);
}
public class FlowerInventory : AFlower
{
public Flower Head;
public Flower Inventory;
public FlowerInventory()
{
Head = null;
}
public override int Add(Flower NewFlower)
{
Flower Sample = new Flower();
Sample = NewFlower;
Sample.Next = Head;
Head = Sample;
return items++;
}
public override Flower Get(int index)
{
Flower Current = Head;
for(int i = Count - 1;
i > index && Current != null;
i--)
Current = Current.Next;
return Current;
}
}
}
|
- Access the Program.cs file and change it as follows:
using System;
namespace FlowerShop4
{
public class Program
{
static int Main(string[] args)
{
FlowerInventory flowers = new FlowerInventory();
Flower nice;
nice = new Flower();
nice.Type = FlowerType.Lilies;
nice.Color = FlowerColor.White;
nice.Arrangement = FlowerArrangement.Bouquet;
nice.UnitPrice = 39.95M;
flowers.Add(nice);
nice = new Flower();
nice.Type = FlowerType.Daisies;
nice.Color = FlowerColor.Mixed;
nice.Arrangement = FlowerArrangement.Bouquet;
nice.UnitPrice = 40.50M;
flowers.Add(nice);
nice = new Flower();
nice.Type = FlowerType.Carnations;
nice.Color = FlowerColor.Lavender;
nice.Arrangement = FlowerArrangement.Any;
nice.UnitPrice = 34.85M;
flowers.Add(nice);
nice = new Flower();
nice.Type = FlowerType.Roses;
nice.Color = FlowerColor.Pink;
nice.Arrangement = FlowerArrangement.Bouquet;
nice.UnitPrice = 29.95M;
flowers.Add(nice);
nice = new Flower();
nice.Type = FlowerType.Daisies;
nice.Color = FlowerColor.Yellow;
nice.Arrangement = FlowerArrangement.Vase;
nice.UnitPrice = 42.75M;
flowers.Add(nice);
Console.WriteLine("//=//=//=//=//=//=//=//=//=//=//=//=//=//=//");
Console.WriteLine("Total: {0} flower items in current inventory",
flowers.Count);
Console.WriteLine("--------------------------------------------");
Console.WriteLine("Inventory Summary");
for (int i = 0; i < flowers.Count; i++)
{
Console.WriteLine("------------------------");
Console.WriteLine("Flower Information");
Console.WriteLine("Type: {0}", flowers.Get(i).Type);
Console.WriteLine("Color: {0}", flowers.Get(i).Color);
Console.WriteLine("Arrangement: {0}",
flowers.Get(i).Arrangement);
Console.WriteLine("Unit Price: {0:F}",
flowers.Get(i).UnitPrice);
}
Console.WriteLine("//=//=//=//=//=//=//=//=//=//=//=//=//=//=//");
return 0;
}
}
}
|
- Execute the application to view the result:
//=//=//=//=//=//=//=//=//=//=//=//=//=//=//
Total: 5 flower items in current inventory
--------------------------------------------
Inventory Summary
------------------------
Flower Information
Type: Lilies
Color: White
Arrangement: Bouquet
Unit Price: 39.95
------------------------
Flower Information
Type: Daisies
Color: Mixed
Arrangement: Bouquet
Unit Price: 40.50
------------------------
Flower Information
Type: Carnations
Color: Lavender
Arrangement: Any
Unit Price: 34.85
------------------------
Flower Information
Type: Roses
Color: Pink
Arrangement: Bouquet
Unit Price: 29.95
------------------------
Flower Information
Type: Daisies
Color: Yellow
Arrangement: Vase
Unit Price: 42.75
//=//=//=//=//=//=//=//=//=//=//=//=//=//=//
Press any key to continue . . .
|
- Close the DOS window
Deleting an item consists of removing it from the list. There are two main
approaches you can use. You can simply ask the class to delete an item. In this case, it is usually the item at the end that gets deleted. If you do this, make sure you perform other routines operations such as decrementing the count of items in the list. Here is an example:
public class Numbers
{
. . . No Change
public bool Delete()
{
if (Head == null)
{
Console.WriteLine("The list is empty");
return false;
}
Number Current;
Current = Head.Next;
Head.Next = Current.Next;
size--;
return true;
}
}
|
|