.NET大作业之网吧后台管理系统5

huiyuanshangji.cs

界面:

代码:

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;
using System.Data.SqlClient;
namespace WB
{
    public partial class huiyuanshangji : Form
    {
        public huiyuanshangji()
        {
            InitializeComponent();
        }

        private void huiyuanshangji_Load(object sender, EventArgs e)
        {
            
            this.time.Text = DateTime.Now.ToString();
            SqlConnection conn = DBwangba.getConn();
            conn.Open();
            if (conn.State == ConnectionState.Open)
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = "select PCId from PCInfo where PCUse = 0";
                SqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    this.shangji.Items.Add(rd[0].ToString());
                }
                conn.Close();
                if (this.shangji.Items.Count > 0)
                {
                    this.btnshangji.Enabled = true;
                    //this.shangji.SelectedIndex = this.shangji.Items.Count -9;
                }
                else
                {
                    this.shangji.Items.Add("没有空闲的机器");
                    this.btnshangji.Enabled = false;
                }
            }
        }

        private void btn_cancle_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void txt_time_TextChanged(object sender, EventArgs e)
        {

        }

        private void cmbb_pcid_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btn_shangji_Click(object sender, EventArgs e)
        {
            username();
        }
        public void username()
        {
            // 1. 数据的判断 文本框是否为空
            string name = this.name.Text.Trim();
            string password = this.password.Text.Trim();
            if (name == "" || password == "")
            {
                MessageBox.Show("请输入完整的会员号或者密码", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            // 2. 判断用户输入的用户名和密码是否正确
            //  创建连接字符串
            string constr = "server=.;database=NetBarDB;uid=sa;pwd=20194023";
            //  创建连接对象 设置连接对象属性
            SqlConnection conn = new SqlConnection(constr);
            //  调用连接对象的打开方法
            conn.Open();
            //  判断是否连接成功
            if (conn.State == ConnectionState.Open)
            {
                string sql = string.Format("select cardid,cardPassword,cardBalance from cardInfo where cardNumber = '{0}' and cardPassword = '{1}'", name, password);
                SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
                DataSet ds = new DataSet();
                adapter.Fill(ds);

                if (ds.Tables[0].Rows.Count > 0)
                {
                    string cardid = ds.Tables[0].Rows[0]["cardid"].ToString();

                    string cardPassword = ds.Tables[0].Rows[0]["cardPassword"].ToString();
                    string cardBalance = ds.Tables[0].Rows[0]["cardBalance"].ToString();

                    // 创建操作对象
                    SqlCommand cmd = new SqlCommand();
                    // 设置操作对象的属性
                    cmd.Connection = conn;
                    // 3. 判断用户的余额是否大于10
                    if (int.Parse(cardBalance) >= 10)
                    {
                        // 判断会员是否在上机
                        string sql2 = string.Format("select * from recordInfo where cardId='{0}' and beginTime is not null and endTime is null", cardid);
                        cmd.CommandText = sql2;
                        Object obj = cmd.ExecuteScalar();
                        if (obj == null)
                        {
                            // 4. 修改计算机的状态
                            string com = this.shangji.Text;
                            string up = string.Format("update PCInfo set PCUse= 1 where PCId={0}", com);
                            cmd.CommandText = up;
                            // 调用操作对象的改方法
                            int rz = cmd.ExecuteNonQuery();
                            //5.插入上机记录
                            string str3 = this.time.Text;
                            string str4 = this.shangji.Text;
                            string sql3 = string.Format("insert into recordInfo values({0},{1},GETDATE(),null,2)", cardid, str4);
                            cmd.CommandText = sql3;
                            int rd = cmd.ExecuteNonQuery();
                            // 判断操作成功
                            if (rz > 0 && rd > 0)
                            {
                                MessageBox.Show("上机成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                                this.Hide();
                            }
                            else
                            {
                                MessageBox.Show("上机失败", "提示");
                                
                            }

                        }
                        else
                        {
                            MessageBox.Show("该会员正在上机", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }
                    else
                    {
                        MessageBox.Show("您的余额小于10,请及时充值!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    MessageBox.Show("您的账号或者密码有误,请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                conn.Close();
            }
            else
            {
                // 连接失败
                MessageBox.Show("连接失败", "连接提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            }
                 


            
        


        

        private void name_TextChanged(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.time.Text =  DateTime.Now.ToString();
        }

        private void name_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
            {
                e.Handled = true;
            }
            else
            {
                //MessageBox.Show("请输入数字", "错误", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
        }
    }
}

huiyuanhuanji.cs

界面:

代码:

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;
using System.Data.SqlClient;
namespace WB
{
    public partial class huiyuanhuanji : Form
    {
        public huiyuanhuanji()
        {
            InitializeComponent();
        }

        private void huiyuanhuanji_Load(object sender, EventArgs e)
        {
            this.cmbb.Items.Clear();
            this.cnew.Items.Clear();

            SqlConnection conn = DBwangba.getConn();
            conn.Open();
            if (conn.State == ConnectionState.Open)
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = "select * from PCInfo where PCUse = 1";
                SqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    string PCid = rd[0].ToString();
                    this.cmbb.Items.Add(PCid);
                }
                rd.Close();
                if (cmbb.Items.Count > 0)
                {
                    this.cmbb.SelectedIndex = this.cmbb.Items.Count - 1;
                }

                SqlCommand cmd2 = new SqlCommand();
                cmd2.Connection = conn;
                cmd2.CommandText = "select * from PCInfo where PCUse = 0";
                SqlDataReader rd2 = cmd2.ExecuteReader();

                while (rd2.Read())
                {
                    string PCid2 = rd2[0].ToString();
                    this.cnew.Items.Add(PCid2);
                }
                if (cnew.Items.Count > 0)
                {
                    this.cnew.SelectedIndex = this.cnew.Items.Count - 1;
                }
                rd2.Close();
                conn.Close();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_huanji_Click(object sender, EventArgs e)
        {
            string str1 = this.cmbb.Text;
            string str2 = this.cnew.Text;
            SqlConnection conn = DBwangba.getConn();
            conn.Open();
            if (conn.State == ConnectionState.Open)
            {

                string sql2 = string.Format("update PCInfo set PCUse ='0'where PCId = '{0}'", str1);
                int rz = DBwangba.BaseExec(sql2);
                string sql3 = string.Format("update PCInfo set PCUse = '1' where PCId = '{0}'", str2);
                int rd = DBwangba.BaseExec(sql3);
                string hj = string.Format("update recordInfo set PCId = {0} where pcid = {1}  and endTime is null", str2, str1);
                int rf = DBwangba.BaseExec(hj);
                if (rz > 0 && rd > 0 && rf > 0)
                {
                    string aa = string.Format("换机成功,由{0}换到{1}", str1, str2);
                    MessageBox.Show(aa, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    this.Close();
                }
                else
                {
                    MessageBox.Show("换机失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                conn.Close();

            }
            else
            {
                MessageBox.Show("连接失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        
    }
}

huiyuanxiaji.cs

界面:

代码:

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;
using System.Data.SqlClient;
namespace WB
{
    public partial class huiyuanxiaji : Form
    {
        public huiyuanxiaji()
        {
            InitializeComponent();
        }

        private void huiyuanxiaji_Load(object sender, EventArgs e)
        {
            this.xiaji.Items.Clear();

            SqlConnection conn = DBwangba.getConn();
            conn.Open();
            if (conn.State == ConnectionState.Open)
            {
                string sql = "select * from pcInfo where PCUse=1";
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    string pcid = rd[0].ToString();
                    this.xiaji.Items.Add(pcid);

                }

                if (xiaji.Items.Count > 0)
                {
                    this.xiaji.SelectedIndex = this.xiaji.Items.Count - 1;
                }
                rd.Close();
                conn.Close();
            }
            else
            {
                MessageBox.Show("连接失败", "连接提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            DateTime dt = DateTime.Now;
            this.downtime.Text = dt.ToString();
        }
        
        private void btn_quxiao_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void xiaji_SelectedIndexChanged(object sender, EventArgs e)
        {
            string str = this.xiaji.Text;


            SqlConnection conn = DBwangba.getConn();
            conn.Open();
            if (conn.State == ConnectionState.Open)
            {

                string sql = "select recordId,cardNumber,a.PCId,a.beginTime from recordInfo as a join cardInfo as b on a.cardId = b.cardId where endTime is null and pcid=" + str;
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    string cardNumber = reader[1].ToString();
                    string beginTime = reader[3].ToString();
                    this.name.Text = cardNumber;
                    this.time.Text = beginTime;
                    reader.Close();
                    conn.Close();
                    DateTime bt = Convert.ToDateTime(this.time.Text);   //获取上机时间
                    DateTime et = DateTime.Now;                             // 获取下机时间 当前系统时间
                    TimeSpan ts = Convert.ToDateTime(et).Subtract(bt);      // 系统时间减去上机时间
                    int seconds = (int)ts.TotalSeconds;
                    // 得到连个时间间隔的总秒数(将时间转换为秒)
                    int minutes = seconds / 60;                             // 得到分钟数
                    if (seconds % 60 > 0)
                    {
                        minutes += 1;                                       // 如果描述不能被60整数,则分钟数加1
                    }
                    int hours = minutes / 60;                               // 得到小时数
                    if (minutes % 60 > 0)
                    {
                        hours += 1;                                         // 如果分钟数不能被60整除,则小时数加1
                    }
                    int i = hours * 15;
                    string a = string.Format("{0}", hours);
                    this.shangjiyongshi.Text = a;
                    string b = string.Format("{0}", i);
                    this.feiyong.Text = b;
                }
                else
                {
                    MessageBox.Show("查找不到结果", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;

                }
            }
        }

        private void btn_xiaji_Click(object sender, EventArgs e)
        {
            string str1 = this.xiaji.Text;
            string str2 = this.name.Text;
            string str3 = this.time.Text;
            string str4 = this.downtime.Text;
            string str5 = this.shangjiyongshi.Text;
            string str6 = this.feiyong.Text;
            SqlConnection conn = DBwangba.getConn();
            conn.Open();
            if (conn.State == ConnectionState.Open)
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = string.Format("select cardBalance from cardInfo where cardNumber = '{0}'", str2);
                SqlDataReader rd = cmd.ExecuteReader();
                if (rd.Read())
                {
                    string cardBalance = rd[0].ToString();
                    rd.Close();
                    if (int.Parse(cardBalance) < int.Parse(str6))
                    {
                        MessageBox.Show("您的余额不足,请充值", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        chongzhi cz = new chongzhi();
                        cz.Show();

                    }
                    else
                    {

                        string sql = string.Format("update recordInfo set endTime ='{0}',free='{1}' where pcid ={2} and endtime is null", str4, str6,str1);
                        int rz = DBwangba.BaseExec(sql);
                        string sql2 = string.Format("update PCInfo set PCUse = 0 where PCId = '{0}'", str1);
                        int rx = DBwangba.BaseExec(sql2);
                        string sql3 = string.Format("update cardInfo set cardBalance = cardBalance - '{0}' where cardNumber= '{1}'", str6, str2);
                        int rc = DBwangba.BaseExec(sql3);
                        if (rz > 0 && rx > 0 && rc > 0)
                        {
                            MessageBox.Show("下机成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                            this.Hide();
                        }
                        else
                        {
                            MessageBox.Show("下机失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        }

                    }

                }
                conn.Close();

            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;
            this.downtime.Text = dt.ToString();
        }

        private void time_TextChanged(object sender, EventArgs e)
        {

        }

        private void downtime_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
posted @ 2021-11-09 15:52  第厘  阅读(173)  评论(0编辑  收藏  举报