光辉超市管理系统

 

 

 

   

2、实现菜单选项功能,如下图:

 

 

 

 

       对于这个我是满怀激动的,因为这个项目好像涵盖了C#和数据库基础这本书的大部分内容,可以说是全部内容都有涉及,,所以我是很想写好。

 首先:搭框架

在写代码的时候

按题目要求:功能1是超市购物先输出商品列表,然后根据购买情况打印小票,

 分析得到:1:输出输出商品列表

    方法可以用Command的ExecuteReader()方法如下

    

//调用Command 的ExecuteReader()方法得到SqlDataReader类对象r
#region 输出商品列表
        SqlDataReader r = cmd.ExecuteReader();
              if (r != null)
             {
                 if (r.HasRows)
                 {
                          //GoodsName, Price, TypeId, ProdureCounts
                          Console.WriteLine("编号\t\t商品名\t\t商品类型");
                         Console.WriteLine("");
                         //循环输出现有商品
                          while (r.Read())
                           {
                               int id = Convert.ToInt32(r["GoodsId"]);
                               string sPName = Convert.ToString(r["GoodsName"]);
                               string typeName = Convert.ToString(r["TypeName"]);
                               Console.WriteLine("{0,-8}\t{1,-8}\t{2}", id, sPName, typeName);

                           }
                          r.Close();

                  }
            }

         

       

     然后打印小票,因为发要打印的数据,并非在一张表里所以并且有多次打印的可能,我以为只用连接数据库的方法们(我们学过的)可以但并不方便。所以我用了数组

    类类型的数组,把存入数据库的数据在存入该数组中,打印时输出数组即可,这样数据库里还有购物数据。

   具体方法如下:

    

 //do 循环 控制是否继续购物
                #region 购物过程
                do
                {
                    Console.WriteLine("请输入要购买的商品编号:");
                    bianHao = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("请输入要购买的商品数量:");
                    num = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("输入e停止购物输入其他字符继续购物:");

                    //#region 把信息存在数组里
                    //xiaopiaos[index] = new XiaoPiao();

                    ////把数量赋给数组对象的.Num字段
                    //xiaopiaos[index].Num = num;

                    ////把购买的物品姓名赋给数组的.Name字段
                    //string sqlThree = "select GoodsName from Goods where GoodsId=" + bianHao;
                    //SqlCommand cmdThree = new SqlCommand(sqlThree, c);
                    //xiaopiaos[index].Name = Convert.ToString(cmdThree.ExecuteScalar());

                    ////把商品的单价的值赋给数组的.DanJia字段
                    //string sqlFour = "select Price from Goods where GoodsId=" + bianHao;
                    //SqlCommand cmdFour = new SqlCommand(sqlFour, c);
                    //xiaopiaos[index].DanJia = Convert.ToDouble(cmdFour.ExecuteScalar());

                    ////把商品的类型编号赋给xiaopiaos数组的.ShangPinTypeNo字段
                    //string sqlFive = "select TypeId from Goods where GoodsId=" + bianHao;
                    //SqlCommand cmdFive = new SqlCommand(sqlFive, c);
                    //xiaopiaos[index].ShangPinTypeNo = Convert.ToInt32(cmdFour.ExecuteScalar());
                    //#endregion

         
                    #region 添加Orders表数据
                  
         

 

                    //添加Orders表数据
                    string sqlSex = "insert into Orders(GoodsId,SaleCounts,OrderDate)values(" + bianHao + "," + num + ",default)";
                    SqlCommand cmdSex = new SqlCommand(sqlSex, c);
                    if (!(cmdSex.ExecuteNonQuery() > 0))
                    {
                        Console.WriteLine("系统故障,稍后再试..");
                    }
                    #endregion

                    //更新数组相应数据
                    #region 更新数组数据
                    string sqlTwo = "update Goods set ProdureCounts=ProdureCounts-" + num + " where  GoodsId=" + bianHao;
                    SqlCommand cmdTwo = new SqlCommand(sqlTwo, c);
                    #endregion

                    if (cmdTwo.ExecuteNonQuery() > 0)
                    {
                  
                    }
                    else
                    {
                        Console.WriteLine("sorry,系统出了问题!");
                    }
                    Console.WriteLine("");
                } while (!Console.ReadLine().ToLower().Equals("e"));
                #endregion

                //输出小票
                #region 输出小票
                Console.WriteLine("-----------------欢迎光临光辉超市------------------");
                Console.WriteLine("商品名称\t单价\t数量\t小计");
                Console.WriteLine("");

                #region MyRegion

                //double sum = 0;
                //foreach (XiaoPiao item in xiaopiaos)
                //{
                //    if (item != null)
                //    {

                //        Console.WriteLine("{0,-8}\t{1}\t{2}\t{3}", item.Name, item.DanJia, item.Num, (item.DanJia * item.Num));

 


                //        sum += (item.DanJia * item.Num);
                //        for (int i = 0; i < xiaoZongs.Length; i++)
                //        {
                //            if (xiaoZongs[i]!=null)
                //            {
                //            if (xiaoZongs[i].TypeNo == item.ShangPinTypeNo)
                //            {
                //                xiaoZongs[i].ZongMoney += (item.DanJia * item.Num);
                //                break;
                //            }
                //            }
                //        }
                //    }
                //}
                #endregion

                double sum = 0;
                string sqlSeven = "select GoodsName,Price,SaleCounts from Goods, Orders where Goods.GoodsId=Orders.GoodsId and OrderDate>convert(datetime,'" + cunTime+"') ";
                SqlCommand cmdSeven = new SqlCommand(sqlSeven,c);
                SqlDataReader rTwo = cmdSeven.ExecuteReader();
                if (rTwo!=null)
                {
                    if (rTwo.HasRows)
                    {
                        while (rTwo.Read())
                        {
                            string name = rTwo["GoodsName"].ToString();
                            double danJia = Convert.ToDouble(rTwo["Price"]);
                            int maiNum = Convert.ToInt32(rTwo["SaleCounts"]);


                            Console.WriteLine("{0}\t{1}\t{2}\t{3}",name,danJia,maiNum,(danJia*maiNum));
                            sum += (danJia * maiNum);
                        }
                        Console.WriteLine("总计:{0:C2}",sum);
                        rTwo.Close();
                    }
                }

 

                Console.WriteLine("");

                //Console.WriteLine("总计:{0:C2}", sum);
                #endregion
            }
            catch (Exception)
            {

                Console.WriteLine("异常.....可能连接有问题!");
            }
            finally
            {
                c.Close();
            }

            Console.WriteLine("");
            //返回bool类型的值
            bool flag = true;
            Console.WriteLine("返回Menu主菜单请输入'menu',其他字符退出程序!");
            if (!Console.ReadLine().ToLower().Equals("menu"))
            {
                flag = false;
            }
            return flag;
        }
        #endregion

  

 

这样就完成了。

 

功能2.销售统计:

分析可知

   :要按金额排倒序,然后输出商品类型编号,商品类型名称,还有总金额

所以,我以购物时得到的数据(商品数量,在Orders表,用其与Goods表两表联查的方法进行商品类型销售金额的统计)进行赋值给我再次创建的临时数组,然后用他冒泡排倒序,最后输出。

具体方法如下:

           

        #region 销售统计方法
        public bool ConsumeTongJi()
        {
            XiaoShouZongJi temp = new XiaoShouZongJi();
            Console.WriteLine("商品类型编号\t商品类型名称\t销售总金额");
            for (int i = 0; i < xiaoZongs.Length-1; i++)
            {
                for (int j = 0; j < xiaoZongs.Length-1-i; j++)
                {
                    if (xiaoZongs[i].ZongMoney<xiaoZongs[i+1].ZongMoney)
                    {
                        temp = xiaoZongs[i];
                        xiaoZongs[i] = xiaoZongs[i + 1];
                        xiaoZongs[i + 1] = temp;
                       
                    }
                }
            }
            foreach (XiaoShouZongJi item in xiaoZongs)
            {
                Console.WriteLine("{0,-8}\t{1,-8}\t{2,-8}",item.TypeNo,item.Name,item.ZongMoney);
            }

 

 

 

 

 

 

 


            //返回bool类型的值
            bool flag = true;
            Console.WriteLine("返回Menu主菜单请输入'menu',其他字符退出程序!");
            if (!Console.ReadLine().ToLower().Equals("menu"))
            {
                flag = false;
            }
            return flag;
        }
        #endregion

 

 

功能3最后是入库

 

  根据要求分析知道:

其方法和购物很像,只是他是打印库存不大于999的商品(多了一个限制),和不是购买而是入库,一样要打印输入的入库信息。所以都是修改,购买时是减购买数量,入库时是加入库数量,和最主要的一点是,购买不用确定,可以先执行数据库的数据修改,而入库时,如输入入库数量后,不确定入库,则不会更改数据库的数据,所以是先存在数组里输出要入库的信息,然后确定后在执行数据库的修改。

具体方法如下:

    


        public bool ShangPinInsert()
        {

 

            SqlConnection c = new SqlConnection(SqlHelper.str);
            string sql = "select *from Goods where ProdureCounts<999";
            SqlCommand cmd = new SqlCommand(sql,c);
            try
            {
                c.Open();
                #region 显示剩余商品小于999的商品
                SqlDataReader r = cmd.ExecuteReader();
                if (r != null)
                {
                    if (r.HasRows)
                    {
                        Console.WriteLine("商品编号\t商品名称\t商品剩余数量");
                        while (r.Read())
                        {
                            int shangPinNo = Convert.ToInt32(r["GoodsId"]);
                            string name = r["GoodsName"].ToString();
                            int num = Convert.ToInt32(r["ProdureCounts"]);
                            Console.WriteLine("{0,-8}\t{1,-8}\t{2,-8}", shangPinNo, name, num);
                        }
                        r.Close();
                    }
                }
                #endregion

 


            }
            catch (Exception)
            {

 

                throw;
            }
            finally
            {
                c.Close();
            }

 

 

 

 

 

 

总结:这个写的时候我一直很投入,非常有意思感觉,虽然自己写的不是很好,还有好些没完善,太多太多了,但还是很兴奋。

 

posted on 2015-07-06 00:57  上雨  阅读(971)  评论(0编辑  收藏  举报

导航