Here is our partial class diagram
Here is the code
IFRed the interface
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace InterfacesExample { public interface IFred { void Add(object o); void Edit(object o); void Remove(object o); List<object> GetList(); } }
Barney class
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace InterfacesExample { class Barney:IFred { List<object> itemList; public Barney() { itemList = new List<object>(); } public void Add(object o) { //Item i = (Item)o; itemList.Add(o); } public void Edit(object o) { throw new NotImplementedException(); } public void Remove(object o) { throw new NotImplementedException(); } public List<object> GetList() { return itemList; } } }
The Programclass
the Item class
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace InterfacesExample { class Item { public string Name { get; set; } public double Price { get; set; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace InterfacesExample { class Program { static void Main(string[] args) { Barney b = new Barney(); Console.WriteLine("how many items?"); int number = int.Parse(Console.ReadLine()); for (int i = 0; i < number; i++) { Item item = new Item(); Console.WriteLine("Enter item name"); item.Name = Console.ReadLine(); Console.WriteLine("Enter Price"); item.Price = double.Parse(Console.ReadLine()); b.Add(item); } Console.Clear(); List<object> items = b.GetList(); foreach(object o in items) { Item i = (Item)o; Console.WriteLine(i.Name + " " + i.Price); } Console.ReadKey(); } } }
No comments:
Post a Comment