8.在库存管理系统中,产品类(Product)包含了一下数据信息:编号(_pid)、名字(_name)、类别(_type)、单价(_price)、库存量(_amount)等。出于数据保护的目的,产品一旦入库,其编号、名字和类别就不能由外部使用者随意修改,但允许读取相关数据信息。请根据上面的叙述,使用C#完成产品类及其构造函数和所有数据成员的合理定义。
9.在库存管理系统中,由于仓库类(Storehouse)保存了所有的产品信息,因此使用一个Product型的数组products来实现,同时设置字段变量number来记录仓库中实际的产品数量,请设计Storehouse类,实现以下功能。
(1)初始化数组products和字段变量number。
(2)能够把某个产品添加到仓库中。
(3)能够根据名称把特定产品从仓库中找出来。
提示:在以下代码的基础之上完成Storehouse的设计。
10.接上题,重载getProduct方法,实现以下功能:能根据产品的编号检索仓库,返回该产品的信息。
7.完善上题,为仓库类(Storehouse)添加索引器,既能用整型索引值读写数组products的数组元素,也能根据产品名称检索数组products。请编写相关实现代码。
设计界面:
编写代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections; //引入命名空间
namespace 库存
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
label6.Text = "";
}
Storehouse s = new Storehouse(3); // 创建一个容量为3的仓库
//添加产品
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "" && textBox4.Text != "" && textBox5.Text != "" && textBox6.Text != "")
{
Product p = new Product(Convert.ToInt32(textBox1.Text), textBox2.Text, textBox3.Text, Convert.ToDouble(textBox4.Text), Convert.ToInt32(textBox5.Text));
int i = Convert.ToInt32(textBox6.Text);
s[i] = p; //向仓库中添加产品
label6.Text = "添加产品成功!!";
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
}
else
{
MessageBox.Show("请输入完整的产品信息及存放位置!!!");
}
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
//根据索引值查询产品
private void button2_Click(object sender, EventArgs e)
{
if (textBox6.Text != "")
{
int i = Convert.ToInt32(textBox6.Text);
Product p = s[i];
if (p != null)
{
label6.Text = string.Format("索引值为{0}的产品信息如下\n编号:{1} \n名称:{2} \n类别:{3}\n单价:{4}\n库存量:{5}", i, p.Id, p.Name, p.Type, p._price, p._amount);
}
else
{
label6.Text = string.Format("没有索引值为{0}的产品!", i);
}
textBox6.Text = "";
}
else
{
MessageBox.Show("请输入您要查询的产品索引值!!");
}
}
//查找产品——根据产品名称
private void button3_Click(object sender, EventArgs e)
{
if (textBox2.Text != "")
{
Product p = s[textBox2.Text];
if (p != null)
{
label6.Text = string.Format("找到名称为{0}的产品信息如下\n编号:{1} \n名称:{2} \n类别:{3}\n单价:{4}\n库存量:{5}", textBox2.Text, p.Id, p.Name, p.Type, p._price, p._amount);
}
else
{
label6.Text = string.Format("没有找到名称为{0}的产品", p.Name);
}
textBox2.Text = "";
}
else
{
MessageBox.Show("请输入您要查询的产品名称!!!");
}
}
}
//定义一个仓库类(保存所有的产品信息)
class Storehouse
{
private Product[] products; //该数组用于存放产品
public Storehouse(int n)
{
products =new Product[n];
}
//定义索引器
public Product this[int index]
{
get
{
if(index <0||index>=products .Length )
{
return null;
}
return products[index]; //对于有限索引,返回请求的信息
}
set
{
if(index <0||index >=products .Length )
{
return;
}
products[index] = value;
}
}
//重载索引器
public Product this[string _name]
{
get
{
foreach (Product p in products )//遍历数组中的所有商品名称
{
if(p.Name.IndexOf(_name)!=-1)//返回符合条件的第一件产品
{
return p;
}
}
return null;
}
}
}
//定义一个产品类
class Product
{
private int _id;
private string _name;
private string _type;
public double _price;
public int _amount;
public Product(int _id, string _name, string _type, double _price, int _amount)
{
this._id = _id;
this._name = _name;
this._price = _price;
this._type = _type;
this._amount = _amount;
}
public int Id
{
get
{
return _id;
}
}
public string Name
{
get
{
return _name;
}
}
public string Type
{
get
{
return _type;
}
}
}
}
运行结果: