Thursday, May 8, 2014

Aggregation

Aggregation is a whole part relationship in which the parts can survive if the whole is destroyed. Here is a simple inventory example

Here is the code

Item


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace aggregation
{
    class Item
    {
        public int ItemNumber { get; set; }
        public string ItemName { get; set; }
        public string ItemDescription { get; set; }
        public string Catagory { get; set; }
        public double PurchasePrice { get; set; }
        public string Condition { get; set; }
    }
}

Inventory


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace aggregation
{
    class Inventory
    {
        private List<Item> itemList;

        public Inventory()
        {
            itemList = new List<Item>();
        }

        public void AddItem(Item i)
        {
            itemList.Add(i);
        }

        public void EditItem(Item i)
        {
            foreach(Item j in itemList)
            {
                int counter = 0;
                if(j.ItemNumber==i.ItemNumber)
                {
                    itemList[counter] = i;
                    counter++;
                }
            }
        }

        public List GetItems()
        {
            return itemList;
        }
    }
}


Here is the Program class


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace aggregation
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("How many items do you want to enter");
            int number = int.Parse(Console.ReadLine());
            Inventory inv = new Inventory();
            for(int i=0;i<number;i++)
            {
                Item item = new Item();
                Console.WriteLine("Enter item number");
                item.ItemNumber = int.Parse(Console.ReadLine());
                Console.WriteLine("Enter Item name");
                item.ItemName = Console.ReadLine();
                Console.WriteLine("enter purchase price");
                item.PurchasePrice = double.Parse(Console.ReadLine());

                inv.AddItem(item);
            }

            Console.Clear();
            List<Item>items = inv.GetItems();
            foreach(Item i in items)
            {
                Console.WriteLine("{0}, {1}, {2}", i.ItemNumber, i.ItemName, i.PurchasePrice);
            }

            Console.ReadKey();
        }
    }
}

No comments:

Post a Comment