C#集合Collections购物车Shopping Cart
这篇是对象与集合操练,物件的创建,集合的一些基本功能,如添加,编辑,删除等功能。
对象,即是网店的商品物件,Insus.NET只为其添加2个属性,物件的ID的Key和名称ItemName以及2个构造函数,最后一个方法是重写ToString()方法。
class Item
{
private int _key;
public int Key
{
get
{
return _key;
}
set
{
_key = value;
}
} private string _ItemName; public string ItemName
{
get { return _ItemName; }
set { _ItemName = value; }
} public Item()
{ } public Item(int key, string itemName)
{
this._key = key;
this._ItemName = itemName;
} public override string ToString()
{
return string.Format("ID: {0}; Name: {1}。",_key,_ItemName);
}
}
Source Code
有了物件,你可以创建你的购物车Shopping Cart:

class ShoppingCart
{
private SortedList<int, Item> _sl = new SortedList<int, Item>(); public void Add(Item item) //物件添加
{
this._sl.Add(item.Key, item);
} public void Edit(Item item) //编辑物件
{
if (this._sl.ContainsKey(item.Key))
{
this._sl[item.Key] = item;
}
} public void Delete(Item item) //删除物件
{
this._sl.Remove(item.Key);
} public Item this[int key] //索引器
{
get
{
if (!this._sl.ContainsKey(key))
{
return null;
}
else
{
return this._sl[key];
}
}
} public virtual int Count //集合中物件数量
{
get
{
return this._sl.Count;
}
} public virtual IEnumerable<Item> Items //获取所有物件
{
get
{
return this._sl.Values;
}
}
}
Source Code
下面是在控制台测试上面写好的集合购物车:
class Program
{
static void Main(string[] args)
{
ShoppingCart sc = new ShoppingCart(); var item1 = new Collections.Item();
item1.Key = ;
item1.ItemName = "Huawei V8";
sc.Add(item1); var item2 = new Collections.Item();
item2.Key = ;
item2.ItemName = "Huawei V9";
sc.Add(item2); var item3 = new Collections.Item();
item3.Key = ;
item3.ItemName = "Huawei V10";
sc.Add(item3); Console.WriteLine("使用索引器,输出对象:");
Console.WriteLine(sc[].ToString()); Console.WriteLine("集合中对象数量:");
Console.WriteLine(sc.Count); Console.WriteLine("列出所有对象:");
sc.Items.ForEach(delegate (Collections.Item item)
{
Console.WriteLine(item.ToString());
});
}
}
Source Code
按Ctrl + F5输出结果:

最后演示编辑Edit和删除Delete的功能:

var item4 = new Collections.Item();
item4.Key = ;
item4.ItemName = "Huawei Mate10";
sc.Edit(item4); Console.WriteLine("编辑后列出所有对象:");
sc.Items.ForEach(delegate (Collections.Item item)
{
Console.WriteLine(item.ToString());
}); var item5 = new Collections.Item();
item5.Key = ;
sc.Delete(item5); Console.WriteLine("删除后列出所有对象:");
sc.Items.ForEach(delegate (Collections.Item item)
{
Console.WriteLine(item.ToString());
});
Source Code
运行看看结果:

C#集合Collections购物车Shopping Cart的更多相关文章
- 购物车(Shopping cart) —— B2C网站核心产品设计 (二)
购物车是做什么的? 我们先来看一下现实超市中的购物车,一个带四个轱辘的铁筐子,客人推来推去,看到什么东西喜欢,就扔进去,觉得东西差不多了,就推到收银台. 那B2C网站中的购物车又是一个什么东西呢? 从 ...
- shopping cart
#Author:Kevin_hou #定义产品列表 product_list =[ ('HUAWEI',5999), ('Watch',500), ('Nike',800), ('Toyota',20 ...
- Backbone.js 为复杂Javascript应用程序提供模型(models)、集合(collections)、视图(views)的结构
Backbone.js 为复杂Javascript应用程序提供模型(models).集合(collections).视图(views)的结构.其中模型用于绑定键值数据和 自定义事件:集合附有可枚举函数 ...
- Java集合——Collections工具类
Java集合——Collections工具类 摘要:本文主要学习了Collections工具类的常用方法. 概述 Collections工具类主要用来操作集合类,比如List和Set. 常用操作 排序 ...
- 集合-Collections工具
1.定义 Collections是集合类的一个工具类,它提供了一系列静态方法用于对容器中的元素进行排序和搜索等一系列操作. 注:Collection是一个集合接口,而Collections是一个有着操 ...
- Guava 3: 集合Collections
一.引子 Guava 对JDK集合的拓展,是最成熟且最受欢迎的部分.本文属于Guava的核心,需要仔细看. 二.Guava 集合 2.1 Immutable Collections不可变集合 1.作用 ...
- Java 集合-Collections工具类
2017-11-05 23:41:53 Collections类 Collections类:Collections类是针对集合进行操作的工具类,都是静态方法. 常用方法: public static ...
- Java中的集合Collections工具类(六)
操作集合的工具类Collections Java提供了一个操作Set.List和Map等集合的工具类:Collections,该工具类里提供了大量方法对集合元素进行排序.查询和修改等操作,还提供了将集 ...
- java 集合Collections 工具类:排序,查找替换。Set、List、Map 的of方法创建不可变集合
Collections 工具类 Java 提供1个操作 Set List Map 等集合的工具类 Collections ,该工具类里提供了大量方法对集合元素进行排序.查询和修改等操作,还提供了将集合 ...
随机推荐
- js 中三元运算符的运用
外层为false,逐级向内层判断 $scope.nums=700; $scope.result=($scope.nums>300)?($scope.nums>400)?($scope.nu ...
- LyX快捷键管理
快捷键修改:Tools->Preference->Editing->Shortcuts:修改后要Tools->Reconfig生效 快捷键默认保存文件:%appdata%\Ly ...
- JS笔记(二):对象
(一) 对象 对象是JS的基本数据类型,类似于python的字典.然而对象不仅仅是键值对的映射,除了可以保持自有的属性,JS对象还可以从一个称为原型的对象继承属性,对象的方法通常是继承的属性.(这种对 ...
- Win10安装Redis
Redis安装 下载地址:https://github.com/MicrosoftArchive/redis/releases 下载对应的版本:这里下载Redis-x64-3.2.100 解压文件 进 ...
- win7系统保护配置现错误“文件名、目录名或卷标语法不正确。(0x8007007B)
windows7下系统保护功能很是鸡肋,有事会出现一下两个问题: 1.出现错误“文件名.目录名或卷标语法不正确.(0x8007007B) 2.保护设置列表中出现“Windows7_os(c:)(找不到 ...
- 【PAT】B1033 旧键盘打字(20 分)
#include<stdio.h> #include<algorithm> #include<ctype.h> using namespace std; bool ...
- zabbix疑难之时区问题
zabbix疑难总结: 1.zabbix的web界面的时间不对.晚12个小时整 适用于:安装网上的说法来修改,但是时间仍然不对的情况 我们按照以前的网上的那些方法在配置zabbix,需要配置时区: ...
- git merge 步骤
这两天用git比较多,自己学习的过程踩了不少误区,特意记录下来. 当多人合作开发使用git作为代码管理仓库时,要注意自己的更新不能冲掉别人的更新,因为自己一开始不了解的时候就出现了这种情况.首先一定要 ...
- django项目中在settings中配置静态文件
STATICFILES_DIRS = [ os.path.join(BASE_DIR,'static'), ] 写成大写可能看不太懂,但是小写的意思非常明显:staticfiles_dir = [ o ...
- 简易 Token 验证的实现
简易 Token 验证的实现 前言 在我们的服务器和客户端的交互中,由于我们的业务中使用 RESTful API 的形式和客户端交互,而 API 又是无状态的,无法帮助我们识别这一次和上一次的请求由谁 ...