c# - 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
- Group: Gold has only name and quantity.
- Group: Gems Minerals have a variety, for example: Topazgem, EmeraldGem, Rubygem also have their name and quantity each.
- 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>
}
解决方案
推荐阅读
- python - 在 python (Mac) 上安装模块时遇到问题
- twitter-bootstrap - Jodit 编辑器弹出窗口在 Bootstrap 模式下不起作用
- ajax - JQuery在AJAX中乘以表行的输入值
- android - 未找到 ID 为“com.android.model.native”的插件
- php - 重新加载页面后请求ajax正在运行
- selenium-webdriver - 我应该构建什么样的服务器来运行 Selenium 和 Appium 自动化测试?
- javascript - Jest/Enzyme 测试表单组件
- powershell - 在 PowerShell 上保存带有原始标头的已清理日志文件
- c - 这种结构的 LinkedList 有什么问题?
- arrays - C:字符比较失败