📄 shoppingcart.cs
字号:
using System;
using System.Data;
using System.Data.SqlClient;
using System.ComponentModel;
namespace WebApplication8.firstPage
{
/// <summary>
/// ShoppingCart 的摘要说明。
/// </summary>
public class ShoppingCart
{
DataTable table = new DataTable();
DataRow row;
DataView cart;
int total = 0;
public ShoppingCart()
{
//
// TODO: 在此处添加构造函数逻辑
//
table.Columns.Add(new DataColumn("productid"));
table.Columns.Add(new DataColumn("productName"));
table.Columns.Add(new DataColumn("price"));
table.Columns.Add(new DataColumn("description"));
table.Columns.Add(new DataColumn("studentNO"));
cart = new DataView(table);
}
public void insert(string id,string productName,string price,string description,string studentNO)
{
row = table.NewRow();
row["productid"] = id;
row["productName"] = productName;
row["price"] = price;
row["description"] = description;
row["studentNO"] = studentNO;
table.Rows.Add(row);
try
{
Int32Converter converter = new Int32Converter();
total+=(int)(converter.ConvertFromString(price));
}
catch(Exception o)
{
throw new Exception();}
}
public void delete(string id,string price)
{
cart.RowFilter = "productid='"+id+"'";
if(cart.Count > 0)
{
cart.Delete(0);
}
/*else
{
cart.RowFilter = "";
if(cart.Count > 0)
cart.Delete(0);
}*/
cart.RowFilter = "";
try
{
Int32Converter converter = new Int32Converter();
total-=(int)(converter.ConvertFromString(price));
}
catch(Exception o)
{throw new Exception();}
}
public DataView getCart()
{
return cart;
}
public int getTotalPrice()
{
return total;
}
public int getItemCount()
{
return table.Rows.Count;
}
public void buy(string userName,string studentNO,string c)
{
//try{
Int32Converter converter = new Int32Converter();
SqlDataReader reader;
Connection connection = new Connection(c);
int count = table.Rows.Count-1;
int totalMoney = 0;
while(count >= 0)
{
reader = connection.execute("insert into AccountRecord(createTime,description,IO,money,studentNO,comB,productName) values ('"+System.DateTime.Now.ToString()+"','产品销售','买','"
+ table.Rows[count]["price"]+"','" + studentNO +"','"+table.Rows[count]["studentNO"]+"','"+table.Rows[count]["productName"]+"')");
reader.Close();
reader = connection.execute("select number from Product where productid="+table.Rows[count]["productid"]);
reader.Read();
int number = (int)(converter.ConvertFromString(reader["number"].ToString())) - 1;
reader.Close();
reader = connection.execute("update Product set number='"+number.ToString()+"' where productid="+table.Rows[count]["productid"]);
reader.Close();
reader = connection.execute("insert into Orders values('"+table.Rows[count]["productid"].ToString()+"','1','"+studentNO+"','"+table.Rows[count]["studentNO"].ToString()+"','"+
System.DateTime.Now.ToString()+"')");
reader.Close();
reader = connection.execute("select * from Company where studentNO='"+table.Rows[count]["studentNO"].ToString()+"'");
if(reader.Read())
{
int account = (int)(converter.ConvertFromString(reader["account"].ToString()));
int profit = (Convert.ToInt32(reader["profit"])) + Convert.ToInt32(table.Rows[count]["price"]);
int tot = (Convert.ToInt32(reader["total"])) + Convert.ToInt32(table.Rows[count]["price"]);;
account += (int)(converter.ConvertFromString(table.Rows[count]["price"].ToString()));
reader.Close();
reader = connection.execute("update Company set account='"+account+ "',profit="
+ profit+",total =" + tot + " where studentNO='"+table.Rows[count]["studentNO"]+"'");
reader.Close();
}
totalMoney += (int)(converter.ConvertFromString(table.Rows[count]["price"].ToString()));
count--;
}
reader = connection.execute("select account from Single where studentNO='"+studentNO+"'");
if(reader.Read())
{
int account = (int)(new Int32Converter().ConvertFromString(reader["account"].ToString()));
reader.Close();
reader = connection.execute("update Single set account='"+(account-totalMoney)+"'where studentNO='"+studentNO+"'");
reader.Close();
}
table.Clear();
total = 0;
/*}
catch(Exception o)
{
throw new Exception();
}*/
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -