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 ,该工具类里提供了大量方法对集合元素进行排序.查询和修改等操作,还提供了将集合 ...
随机推荐
- 使用node.js进行API自动化回归测试
概述 传统的QA自动化测试通常是基于GUI的,比如使用Selenium,模拟用户在界面上操作.但GUI测试的开发.维护成本和运行的稳定性一直是测试界的老大难问题.投入大量的人力物力开发.维护.运行,却 ...
- 安装Django(1)
安装Django 注意:因为这是web项目将来要部署在Linux上,所以使用centos/ubuntu:因为Python3是将来的趋势,所以使用Python3做为开发语言.本人使用的开发模式操作系统: ...
- web应用
实现HttpServlet的Web应用 <dependency> <groupId>javax.servlet</groupId> <artifactId&g ...
- Sqlserver精简安装选项
- WindowsErrorCode
0 操作成功完成.1 功能错误.2 系统找不到指定的文件.3 系统找不到指定的路径.4 系统无法打开文件.5 拒绝访问.6 句柄无效.7 存储控制块被损坏.8 存储空间不足, 无法处理此命令.9 存储 ...
- SMP多核启动
在 Linux系统中,对于多核的ARM芯片而言,在Biotron代码中,每个CPU都会识别自身ID,如果ID是0,则引导Bootloader和 Linux内核执行,如果ID不是0,则Biotron一般 ...
- python基础、字符串和if条件语句,while循环,跳出循环、结束循环
一:Python基础 1.文件后缀名: .py 2.Python2中读中文要在文件头写: -*-coding:utf8-*- 3.input用法 n为变量,代指某一变化的值 n = inpu ...
- MATLAB用二分法、不动点迭代法及Newton迭代(切线)法求非线性方程的根
MATLAB用二分法.不动点迭代法及Newton迭代(切线)法求非线性方程的根 作者:凯鲁嘎吉 - 博客园http://www.cnblogs.com/kailugaji/ 一.实验原理 二.实验步骤 ...
- 【 nginx 】怎么安装nginx
一,下载地址:http://nginx.org/en/download.html 二,下载完成之后,是一个安装包,解压之后就能直接使用 三,点击进去我们刚刚解压好的nginx的安装包,打开nginx. ...
- 捕获海康威视IPCamera图像,转成OpenCV能够处理的图像(二)
海康威视IPCamera图像捕获 捕获海康威视IPCamera图像.转成OpenCV能够处理的IplImage图像(一) 捕获海康威视IPCamera图像.转成OpenCV能够处理的IplImage图 ...