Может ли кто-нибудь предложить, как объединить элемент, клонировать элемент itemtostring()
<pre> Items.cs class Items { public int ItemNumber { get; set; } public DateTime ItemDate { get; set; } //public int Quantity { get; set; } //public double Cost { get; set; } public List<ItemsLine> ItemsLines { get; set; } = new List<ItemsLine>(); //public List<ItemsLine> sourceItem; public void AddItemLine(ItemsLine itemLine) { ItemsLines.Add(itemLine); } public void RemoveItemLine(int SOMEID) { ItemsLines.RemoveAt(1); } /// GetTotal should return the sum of (Cost * Quantity) for each line item public decimal GetTotal() { //return Convert.ToInt32(this.Cost) * this.Quantity; return ItemsLines.Sum(i => Convert.ToInt32(i.Cost) * i.Quantity); } /// MergeItem appends the items from the sourceItem to the current invoice /// </summary> /// <param name="sourceItem">Item to merge from</param> public void MergeItem(Items sourceItem) { //IEnumerable<ItemsLine> items = new List<ItemsLine>(); IEnumerable<Items> items = //throw new NotImplementedException(); } /// Creates a deep clone of the current item (all fields and properties) /// </summary> //public Items Clone() //{ // //throw new NotImplementedException(); //} /// Outputs string containing the following (replace [] with actual values): /// Invoice Number: [ItemNumber], ItemDate: [dd/MM/yyyy], ItemsLineCount: [Number of items in LineItems] public override string ToString() { string result = "Item Number\tQuantity\tCost\n"; foreach (ItemsLine item in ItemsLines) result += (item.ItemLineId + "\t\t" + item.Quantity + "\t\t" + item.Cost.ToString() + /*item.Total.ToString()+*/ "\n"); return result; } }
class ItemsLine { public int ItemLineId { get; set; } public int Quantity { get; set; } public double Cost { get; set; } // public double Total { get { return this.Cost * this.Quantity; } } public ItemsLine(int itemLineId, int quantity, double cost) { this.ItemLineId = itemLineId; this.Quantity = quantity; this.Cost = cost; } public ItemsLine() { } } <pre lang="c#"> <pre> class Program { static void Main(string[] args) { Console.WriteLine("Welcome to ItemBilling Appliation...."); CreateItemWithOneItem(); CreateItemWithMultipleItemsAndQuantities(); RemoveItem(); //MergeItem(); //CloneItem(); //ItemToString(); } private static void CreateItemWithOneItem() { var item = new Items(); item.AddItemLine(new ItemsLine() { ItemLineId = 1, Cost = 6.99, Quantity = 1, }); Console.WriteLine(item.ToString()); Console.WriteLine(item.GetTotal()); } private static void CreateItemWithMultipleItemsAndQuantities() { var item = new Items(); item.AddItemLine(new ItemsLine() { ItemLineId = 1, Cost = 10.21, Quantity = 4, }); item.AddItemLine(new ItemsLine() { ItemLineId = 2, Cost = 5.21, Quantity = 1, }); Console.WriteLine(item.ToString()); Console.WriteLine(item.GetTotal()); } private static void RemoveItem() { var item = new Items(); item.AddItemLine(new ItemsLine() { ItemLineId = 1, Cost = 5.21, Quantity = 1, }); item.AddItemLine(new ItemsLine() { ItemLineId = 2, Cost = 10.99, Quantity = 4, }); item.RemoveItemLine(1); Console.WriteLine(item.ToString()); Console.WriteLine(item.GetTotal()); } private static void MergeItem() { var item1 = new Items(); item1.AddItemLine(new ItemsLine() { ItemLineId = 1, Cost = 10.33, Quantity = 4, }); var item2 = new Items(); item2.AddItemLine(new ItemsLine() { ItemLineId = 2, Cost = 5.22, Quantity = 1, }); item2.AddItemLine(new ItemsLine() { ItemLineId = 3, Cost = 6.27, Quantity = 3, }); item1.MergeItem(item2); Console.WriteLine(item1.GetTotal()); } private static void CloneItem() { var item = new Items(); item.AddItemLine(new ItemsLine() { ItemLineId = 1, Cost = 6.99, Quantity = 1, }); item.AddItemLine(new ItemsLine() { ItemLineId = 2, Cost = 6.27, Quantity = 3, }); var clonedItem = item.Clone(); Console.WriteLine(clonedItem.GetTotal()); } private static void ItemToString() { var item = new Items() { ItemDate = DateTime.Now, ItemNumber = 1000, ItemsLines = new List<ItemsLine>() { new ItemsLine() { ItemLineId = 1, Cost = 6.99, Quantity = 1, } } }; Console.WriteLine(item.ToString()); } }
Что я уже пробовал:
Я немного запутался , как объединить, клонировать и элемент в Tostring