首页 > 解决方案 > Do I properly approach this logic and which collection to choose?

问题描述

I have homework from the Abstraction section: I need to add items in a bag, such as

  1. Group: Gold has only name and quantity.
  2. Group: Gems Minerals have a variety, for example: Topazgem, EmeraldGem, Rubygem also have their name and quantity each.
  3. Group: Currencies are also different, for example, USD, Euro as also have name and quantity individually.

Тhe result must finally produce the total quantity separately of gold, minerals, and currency, as for minerals and currency separately for each of the subunits.

My confusion is as follows: have I properly expressed the abstraction, and how to suit my choosing for a collection when added to the bag.

Here is the code:

abstract class Item
{
  public string Name { get; set; }
  public long Quantity { get; set; }

  public void IncreaseQuantity(long quantity)
  {
      Quantity += quantity;
  }   

Class Gold, Gem, Currencies inherit Item

class Bag
{
  //and here comes my confusion and my choice of how to fit in adding 
 //items to bag
//Variant1: When choosing List<Item> and add to bag
//variant2: When choosing Dictionary<string, Item>
}

标签: c#

解决方案


推荐阅读