C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > C#超市收银系统设计

C#超市收银系统设计

作者:懒懒的

这篇文章主要为大家详细介绍了C#超市收银系统设计,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C#超市收银系统设计的具体代码,供大家参考,具体内容如下

1.登录界面

代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace 梦之翼小组项目
{
 public partial class denglu : Form
 {
  public denglu()
  {
   InitializeComponent();
  }
  model db = new model(); //实例化数据库对象
 
  private void button1_Click(object sender, EventArgs e)
  {
   string strConn = "Data Source=.;Initial Catalog=shopInfo;Integrated Security=True"; //连接数据库
   SqlConnection Connection = new SqlConnection(strConn);
 
   try
   {
 
    string sqlStr = "select userName,userPassword from register where userName=@userName"; //查询
    DataSet ds = new DataSet();
 
    Connection.ConnectionString = Connection.ConnectionString;
    Connection.Open();
 
    SqlCommand cmd = new SqlCommand(sqlStr, Connection);
    cmd.Parameters.Add(new SqlParameter("@userName", SqlDbType.VarChar, 30)); //传参
    cmd.Parameters["@userName"].Value = userName.Text;   //给user文本框赋值
    SqlDataReader dater = cmd.ExecuteReader();
 
    if (userName.Text.Trim() == "")   //如果user的值等于空
    {
     MessageBox.Show( "用户名不允许为空!");
 
    }
    else if (passWord.Text.Trim() == "")   //同上
    {
     MessageBox.Show( "密码不能为空!");
    }
    else if (!dater.Read())    //如果输入的用户名没有被dater读到,则用户名不存在
    {
     MessageBox.Show( "用户名不存在!");
     userName.Text = "";
     passWord.Text = "";
    }
    else if (dater["userPassWord"].ToString().Trim() == passWord.Text.Trim()) //输入密码等于数据库密码登录成功且弹出音乐框
    {
     MessageBox.Show( "登录成功!");
     userName.Text = "";
     passWord.Text = "";
     caozuoyemain frm = new caozuoyemain();
     frm.ShowDialog();
    }
    else
    {
     MessageBox.Show("密码错误!");  //否则密码错误
     userName.Text = "";
     passWord.Text = "";
 
    }
   }
   catch (Exception)
   {
    throw;    //抛出异常
   }
   finally
   {
    Connection.Close();  //关闭数据库
   }
 
  }
 
  private void label4_Click(object sender, EventArgs e)
  {
   zhuce fra = new zhuce();
   fra.ShowDialog();
  }
 
  private void label6_Click(object sender, EventArgs e)
  {
   zhaohuimima fra = new zhaohuimima();
   fra.ShowDialog();
  }
 
  private void denglu_Load(object sender, EventArgs e)
  {
 
  }
 }
}


2.操作界面:

代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace 梦之翼小组项目
{
 public partial class caozuoyemain : Form
 {
  public caozuoyemain()
  {
   InitializeComponent();
  }
  public double totalPrice;//每种商品的总价
  public double total; //所有商品的总价
  public double shijijin;//顾客给的钱数
  public double yingzhao;// 找给顾客的钱数
  public string mingcheng;//每件商品的名称
  public double shuliang;//每件商品的数量
  public double jiage;//每件商品的价格
  public int i = 0; //商品收费的id
 
  model db = new model(); //实例化数据库对象
 
  public void fanli() //单件物品的返利方法
  {
   if (jiage * shuliang < 600 && jiage * shuliang > 300)
   {
    totalPrice = jiage * shuliang - 100;
   }
   else
   {
    totalPrice = jiage * shuliang;
   }
  }
 
  private void confirm_Click(object sender, EventArgs e) //单击确定按钮的事件
  {
   totalPrice = 0; //每一次商品的单个金额
 
   jiage = Convert.ToDouble(price.Text);
   shuliang = Convert.ToDouble(number.Text); //数据类型的转换
   
   switch (jisuanfangshi.SelectedIndex)
   {
    case 0:
     totalPrice = jiage * shuliang;
      
     break;
    case 1:
      totalPrice = jiage * shuliang*0.8;
      
     break;
    case 2:
     fanli(); //调用单个物品的返利方法
     
     break;
   }
 
 
   total = totalPrice + total; //所有商品的总金额
 
   zongjine.Text = total.ToString();//总金额转换数据类型,显示到文本框里面
 
   
   i++; //每一次的商品id加1,为了调出所有商品的最后一个数据
 
   db.dbcon();
   try
   {
    string insertInfo = "insert wupin values('" + i.ToString() + "','" + tradeName.Text + "','" + price.Text + "','" +
     number.Text + "','" + jisuanfangshi.Text + "','" + totalPrice.ToString() + "')";
    db.dbInsert(insertInfo);
 
    string selstr = "select top "+i+" * from wupin order by 物品ID desc";
    db.dbFill(selstr);
    dataGridView1.DataSource = db.dt;  
   }
   catch (Exception)
   {
    MessageBox.Show("不好意思,信息有误,注册失败");
   }
  }  
 
  private void caozuoyemain_Load(object sender, EventArgs e)
  {
   db.dbcon();
   string qingkong = "TRUNCATE TABLE wupin";
   db.dbInsert(qingkong);
   jisuanfangshi.SelectedIndex = 0;  
  }
 
  private void jiesuan_Click(object sender, EventArgs e)
  {
   shijijin = Convert.ToDouble(shishoujine.Text);
   yingzhao=Convert.ToDouble(zongjine.Text) ;
   yingzhao = shijijin - total;
   zhaojine.Text = yingzhao.ToString()+"元";
  }
 
  private void resetting_Click(object sender, EventArgs e)
  {
   tradeName.Text = "";
   price.Text = "";
   number.Text = "";
  }  
 }
}


3.收银员注册界面:

代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace 梦之翼小组项目
{
 public partial class zhuce : Form
 {
  public zhuce()
  {
   InitializeComponent();
  }
  public string sexValue; //接收性别的字段
  model db = new model(); //实例化数据库对象
 
  private bool testuserName(string strUsername) //检测用户名的方法
  {
   bool strResult;// 判断结果的接收
   Regex exStrUserName = new Regex(@"^[a-zA-Z][a-zA-Z0-9]{3,8}$");
   //正则表达式的书写
   if (strResult = exStrUserName.Match(strUsername).Success)
   {
    yonghuming_test.Text = "✔";
   }
   else
   {
    MessageBox.Show("用户名由3—6位的字母和数字组成,必须以字母开头");
 
   }
   return strResult;
  }
 
  private bool testPassWord(string strUsername) //检测密码的方法
  {
   bool strResult;// 判断结果的接收
   Regex exStrUserName = new Regex(@"^[\w\-~!@#$%^&*()+{}[ \]:]{6,16}");
   //正则表达式的书写
   if (strResult = exStrUserName.Match(strUsername).Success)
   {
    mima_test.Text = "✔";
   }
   else
   {
    mima_test.Text = "✘";
    MessageBox.Show("密码由6—16位的字母和数字或符号组成");
 
   }
   return strResult;
  }
  private bool testName(string strUsername) //检测姓名的方法
  {
   bool strResult;// 判断结果的接收
   Regex exStrUserName = new Regex(@"^([a-zA-Z0-9\u4e00-\u9fa5\·]{2,3})$");
   //正则表达式的书写
   if (strResult = exStrUserName.Match(strUsername).Success)
   {
    name_test.Text = "✔";
   }
   else
   {
    name_test.Text = "✘";
    MessageBox.Show("请文明用语,输入合法的中文姓名哦");
 
   }
 
  private bool testPhonnumber(string strUsername) //检测手机号的方法
  {
   bool strResult;// 判断结果的接收
   Regex exStrUserName = new Regex(@"^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$");
   //正则表达式的书写
   if (strResult = exStrUserName.Match(strUsername).Success)
   {
    phonnumber_test.Text = "✔";
   }
   else
   {
    phonnumber_test.Text = "✘";
    MessageBox.Show("请输入正确的手机号码");
 
   }
   return strResult;
  }
 
  private void nan_rb_CheckedChanged(object sender, EventArgs e) //选择性别的事件
  {
   sexValue = nan_rb.Text;
  }
  private void nv_rb_CheckedChanged(object sender, EventArgs e)
  {
   sexValue = nv_rb.Text;
  }
   
 
  private void zhuce_bt_Click(object sender, EventArgs e) //注册的点击事件
  {
   db.dbcon();
   try
   {
    string insertInfo = "insert register values('" + username_text.Text + "','" + password_text.Text+ "','" + name_text.Text+
      "','" + sexValue + "','" + phonnumber_text.Text + "')";
    db.dbInsert(insertInfo);
    DialogResult dr=MessageBox.Show("恭喜你注册成功,是否转到登录界面","注册成功对话框",MessageBoxButtons.OKCancel,MessageBoxIcon.Information);
    if (dr == DialogResult.OK)
    {
     this.Close();
     denglu fra = new denglu();
     fra.ShowDialog();
     
    }
    else if (dr == DialogResult.Cancel)
    {
     this.Close();
    }
 
   }
   catch (Exception)
   {
    MessageBox.Show("不好意思,信息有误,注册失败");
   }
  }
 
  private void username_text_Leave(object sender, EventArgs e) //用户名文本框的光标事件
  {
   if (username_text.Text == "")
   {
    MessageBox.Show("用户名不能为空");
   }
   else
   {
    testuserName(username_text.Text);
   }
  }
 
  private void password_text_Leave(object sender, EventArgs e)//密码文本框的光标事件
  {
   if (password_text.Text == "")
   {
    MessageBox.Show("密码不能为空");
   }
   else
   {
    testPassWord(password_text.Text);
   }
  }
 
  private void name_text_Leave(object sender, EventArgs e)//姓名文本框的光标事件
  {
   if (name_text.Text == "")
   {
    MessageBox.Show("姓名不能为空");
   }
   else
   {
    testName(name_text.Text);
   }
  }
 
  private void phonnumber_text_Leave(object sender, EventArgs e)//手机号文本框的光标事件
  {
   if (phonnumber_text.Text == "")
   {
    MessageBox.Show("手机号不能为空");
   }
   else
   {
    testPhonnumber(phonnumber_text.Text);
   }
  }
 
  private void chongzhi_bt_Click(object sender, EventArgs e) //重置按钮
  {
   username_text.Text = "";
   phonnumber_text.Text = "";
   name_text.Text = "";
   phonnumber_text.Text = "";
  }
 
  private void quxiao_bt_Click(object sender, EventArgs e)// 取消事件
  {
   this.Close();
  }
 
  private void zhuce_Load(object sender, EventArgs e)
  {
 
  }
  
 }
}


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。  

阅读全文