8.在库存管理系统中,产品类(Product)包含了一下数据信息:编号(_pid)、名字(_name)、类别(_type)、单价(_price)、库存量(_amount)等。出于数据保护的目的,产品一旦入库,其编号、名字和类别就不能由外部使用者随意修改,但允许读取相关数据信息。请根据上面的叙述,使用C#完成产品类及其构造函数和所有数据成员的合理定义。
9.在库存管理系统中,由于仓库类(Storehouse)保存了所有的产品信息,因此使用一个Product型的数组products来实现,同时设置字段变量number来记录仓库中实际的产品数量,请设计Storehouse类,实现以下功能。
(1)初始化数组products和字段变量number。
(2)能够把某个产品添加到仓库中。
(3)能够根据名称把特定产品从仓库中找出来。
提示:在以下代码的基础之上完成Storehouse的设计。
10.接上题,重载getProduct方法,实现以下功能:能根据产品的编号检索仓库,返回该产品的信息。
首先设计如下界面:
编写如下
代码
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;
namespace 库存
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
Product[] pro = new Product[10];
Storehouse sto=new Storehouse(10) ;
int i = 0;
//添加产品
private void button1_Click(object sender, EventArgs e)
{
try
{
pro[i] = new Product(Convert.ToInt32(textBox1.Text), textBox2.Text, textBox3.Text, int.Parse(textBox4.Text), Convert.ToInt32(textBox5.Text));
sto.Add(pro[i]);
i++;
label6.Text = "添加成功" + i + "个产品到仓库!!!";
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
}
catch
{
MessageBox.Show("请正确添加产品信息!!!");
}
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
//查找产品
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text.Trim() != "") //根据编号查询(如果编号不为空)
{
Product pro1 = sto.getProduct(Convert .ToInt32( textBox1.Text));
label6.Text = "根据编号查找结果:\n\n编号:" + Convert.ToInt32(pro1.Id) + "\n\n姓名:" + pro1.Name + "\n\n类型:" +
pro1.Type + "\n\n单价:" + Convert.ToString(pro1._price) + "\n\n库存量:" + Convert.ToString(pro1._amount);
}
else if(textBox2.Text.Trim() != "") //根据名称查询(如果编号为空)
{
Product pro2 = sto.getProduct(textBox2.Text);
label6.Text = "根据名称查找结果:\n\n编号:" + Convert.ToInt32(pro2.Id) + "\n\n姓名:" + pro2.Name + "\n\n类型:" +
pro2.Type + "\n\n单价:" + Convert.ToString(pro2._price) + "\n\n库存量:" + Convert.ToString(pro2._amount);
}
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
}
}
//定义一个仓库类(保存所有的产品信息)
class Storehouse
{
private Product[] products;
public int number; //仓库中实际的产品数量
public Storehouse(int n) //构造函数,n代表仓库的库存限额,即最多存放n个产品
{
number = 0;
products =new Product[n];
}
public bool Add(Product a) //把产品a存入到仓库中,实际库存量增加1
{
products[number] = a;
number++;
return true;
}
int k = 0;
public Product getProduct(string name) //根据产品名称索引仓库,返回该产品的信息
{
bool flag = false;
for (int x = 0; x < number;x++ )
{
if(products [x].Name==name)
{
flag = true;
k = x;
}
}
if (flag == true)
{
return products[k];
}
else
{
products [number]=new Product(0,"无","无",0,0);
return products [number];
}
}
public Product getProduct(int id) //根据产品编号索引仓库,返回该产品的信息
{
bool flag = false;
for (int x = 0; x < number; x++)
{
if (products[x].Id == id)
{
flag = true;
k = x;
}
}
if (flag == true)
{
return products[k];
}
else
{
products[number] = new Product(0, "无", "无", 0, 0);
return products[number];
}
}
}
//定义一个产品类
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;
}
}
}
}
运行结果: