📄 salebooks.cs
字号:
using System;
using bookShop.classLib.dbLib;
using bookShop.classLib.exceptions;
namespace bookShop.classLib.dataLayer
{
/// <summary>
/// SaleBooks 的摘要说明。
/// </summary>
public class SaleBooks
{
private string bookNo;
private int quantity;
private bool customerType;
private string customerID;
private string customerName;
private string phoneNo;
private string address;
private decimal totalMoney;
private decimal salePrice;
private static int saleNo=0;
public SaleBooks(string bookNo,int quantity,bool customerType,string customerID,string customerName,string phoneNo,string address )
{
this.bookNo=bookNo;
this.quantity=quantity;
this.customerType=customerType;
this.customerID=customerID;
this.customerName=customerName;
this.phoneNo=phoneNo;
this.address=address;
ReaderDBConnect rc=new ReaderDBConnect();
rc.StrSQL="select Max(SaleNo) from Sale";
rc.Do();
//rc.ObjDataReader.Read();
if(rc.ObjDataReader.HasRows)
{
rc.ObjDataReader.Read();
try
{
saleNo=(int)rc.ObjDataReader[0]+1;
}
catch
{
saleNo=0;
}
}
rc.ObjDataReader.Close();
rc.Close();
}
public decimal SalePrice
{
get{return salePrice;}
}
public decimal TotalMoney
{
get{return totalMoney;}
}
public void Sale()
{
int stock;
DataSetDBConnect ds=new DataSetDBConnect("Book");
ds.StrSQL="select * from Book where BookNo='"+bookNo+"'";
ds.Do();
if(ds.ObjDataSet.Tables["Book"].Rows.Count==0) //查无此书
throw new NoBookException();
else
{
stock=(int)ds.ObjDataSet.Tables["Book"].Rows[0]["Stocks"];
if(stock<quantity) //无足够的书
throw new NotEnoughBooksException(stock,quantity);
else //开始售书
{
if(customerType)//零售
salePrice=(decimal)ds.ObjDataSet.Tables["Book"].Rows[0]["RetailPrice"];
else//批发
salePrice=(decimal)ds.ObjDataSet.Tables["Book"].Rows[0]["WholePrice"];
totalMoney=salePrice*quantity;
//改变书库中书的记录
ds.ObjDataSet.Tables["Book"].Rows[0]["Stocks"]=stock-quantity;
int saledNoTemp=(int)ds.ObjDataSet.Tables["Book"].Rows[0]["SaledNo"];
ds.ObjDataSet.Tables["Book"].Rows[0]["SaledNo"]=saledNoTemp+quantity;
if((int)ds.ObjDataSet.Tables["Book"].Rows[0]["SaledNo"]==0)
ds.ObjDataSet.Tables["Book"].Rows[0].Delete();
ds.WriteBack();
ds.Close();
DirectDBConnect ddbc=new DirectDBConnect();
string customerTypeStr=customerType?"零售":"批发";
if(customerID!=String.Empty)//填了CustomerID则先看是否存在该customer,存在则更新customer信息,不存在则插入customer信息,最后添加销售记录
{
ReaderDBConnect rdbc=new ReaderDBConnect();
rdbc.StrSQL="select * from Customer where CustomerID='"+customerID+"'";
rdbc.Do();
if(!rdbc.ObjDataReader.HasRows)//不存在该customer
{
ddbc.StrSQL="insert into Customer values('"+customerID+"',";
if(customerName==String.Empty)
ddbc.StrSQL+=null+",";
else
ddbc.StrSQL+="'"+customerName+"',";
if(phoneNo==String.Empty)
ddbc.StrSQL+=null+",";
else
ddbc.StrSQL+="'"+phoneNo+"',";
if(address==String.Empty)
ddbc.StrSQL+=null+",";
else
ddbc.StrSQL+="'"+address+"')";
ddbc.Do();
}
else//存在该customer则对填写的信息要更新,没填的保持不变
{
//ddbc.StrSQL="update Customer set CustomerID='"+customerID+"',PhoneNo='"+phoneNo+"',Address='"+address+"' where CustomerID='"+customerID+"'";
ddbc.StrSQL="update Customer set CustomerID='"+customerID+"'";
if(customerName!=String.Empty)
ddbc.StrSQL+=",CustomerName='"+customerName+"'";
if(phoneNo!=String.Empty)
ddbc.StrSQL+=",PhoneNo='"+phoneNo+"'";
if(address!=String.Empty)
ddbc.StrSQL+=",Address='"+address+"'";
ddbc.StrSQL+=" where CustomerID='"+customerID+"'";
ddbc.Do();
}
rdbc.ObjDataReader.Close();
rdbc.Close();
//添加销售记录
ddbc.StrSQL="insert into Sale values("+saleNo+",'"+DateTime.Now.ToString()+"','"+bookNo+"',"+quantity+",'"+customerTypeStr+"','"+customerID+"')";
ddbc.Do();
}
else
{
//直接添加销售记录
ddbc.StrSQL="insert into Sale(SaleNo,SaleTime,BookNo,Quantity,CustomerType) values("+saleNo+",'"+DateTime.Now.ToString()+"','"+bookNo+"',"+quantity+",'"+customerTypeStr+"')";
ddbc.Do();
}
ddbc.Close();
saleNo++;
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -