Dẫn nhập
Bạn đã học qua LẬP TRÌNH C# CƠ BẢN ? Xong nốt cả LẬP TRÌNH WINFORM lẫn SQL ?
Bạn đã chán các bài tập căn bản, muốn thực hành các kiến thức đã học vào một dự án thực tế?
Hay đơn giản bạn là chủ quán café, mong muốn tự tạo nên phần mềm dành cho chính mình sử dụng?
Sẽ không có gì tuyệt vời hơn là luyện tập với ví dụ thực tế. Nào! Cùng nhau thử thách bản thân với PHẦN MỀM QUÁN CAFÉ VỚI C# WINFORM
Ở bài trước, Kteam đã hướng dẫn bạn cách HIỂN THỊ HÓA ĐƠN THEO BÀN CHO PHẦN MỀM
QUẢN LÝ QUÁN CAFE . Trong bài này, chúng ta sẽ tìm hiểu cách Hiển thị tổng tiền cho phần mềm Quản lý quán cafe với C# Winform
Nội dung
Nội dung bao gồm Source code & các lưu ý chính về quá trình thực hiện phần mềm. Kteam khuyến khích bạn cập nhập thêm nhiều kinh nghiệm cũng như hiểu chi tiết hơn về các kỹ thuật được đề cập trong bài học thông qua các video đính kèm.
Đừng quên Like Facebook hoặc +1 Google để ủng hộ Kteam và tác giả.
Để theo dõi tốt khóa học này, hãy đảm bảo bạn đã xem qua kiến thức về:
Project tham khảo
Code fTableManager.cs
using QuanLyQuanCafe.DAO;
using QuanLyQuanCafe.DTO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace QuanLyQuanCafe
{
public partial class fTableManager : Form
{
public fTableManager ()
{
InitializeComponent();
LoadTable();
}
#region Method
void LoadTable()
{
List<Table> tableList = TableDAO.Instance.LoadTableList();
foreach (Table item in tableList)
{
Button btn = new Button() { Width = TableDAO.TableWidth, Height = TableDAO.TableHeight};
btn.Text = item.Name + Environment.NewLine + item.Status;
btn.Click += btn_Click;
btn.Tag = item;
switch (item.Status)
{
case "Trống" :
btn.BackColor = Color.Aqua;
break ;
default :
btn.BackColor = Color.LightPink;
break ;
}
flpTable.Controls.Add(btn);
}
}
void ShowBill(int id)
{
lsvBill.Items.Clear();
List<QuanLyQuanCafe.DTO.Menu> listBillInfo = MenuDAO.Instance.GetListMenuByTable(id);
float totalPrice = 0 ;
foreach (QuanLyQuanCafe.DTO.Menu item in listBillInfo)
{
ListViewItem lsvItem = new ListViewItem(item.FoodName.ToString());
lsvItem.SubItems.Add(item.Count.ToString());
lsvItem.SubItems.Add(item.Price.ToString());
lsvItem.SubItems.Add(item.TotalPrice.ToString());
totalPrice += item.TotalPrice;
lsvBill.Items.Add(lsvItem);
}
CultureInfo culture = new CultureInfo("vi-VN" );
txbTotalPrice.Text = totalPrice.ToString("c" , culture);
}
#endregion
#region Events
void btn_Click(object sender, EventArgs e)
{
int tableID = ((sender as Button).Tag as Table).ID;
ShowBill(tableID);
}
private void đăngXuấtToolStripMenuItem_Click (object sender, EventArgs e)
{
this .Close();
}
private void thôngTinCáNhânToolStripMenuItem_Click (object sender, EventArgs e)
{
fAccountProfile f = new fAccountProfile();
f.ShowDialog();
}
private void adminToolStripMenuItem_Click (object sender, EventArgs e)
{
fAdmin f = new fAdmin();
f.ShowDialog();
}
#endregion
}
}
Code fTableManager.Designer.cs
namespace QuanLyQuanCafe
{
partial class fTableManager
{
private System.ComponentModel.IContainer components = null ;
protected override void Dispose (bool disposing)
{
if (disposing && (components != null ))
{
components.Dispose();
}
base .Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent ()
{
this .menuStrip1 = new System.Windows.Forms.MenuStrip();
this .adminToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this .thôngTinTàiKhoảnToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this .thôngTinCáNhânToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this .đăngXuấtToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this .panel2 = new System.Windows.Forms.Panel();
this .lsvBill = new System.Windows.Forms.ListView();
this .columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this .columnHeader2 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this .columnHeader3 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this .columnHeader4 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this .panel3 = new System.Windows.Forms.Panel();
this .cbSwitchTable = new System.Windows.Forms.ComboBox();
this .btnSwitchTable = new System.Windows.Forms.Button();
this .nmDisCount = new System.Windows.Forms.NumericUpDown();
this .btnDiscount = new System.Windows.Forms.Button();
this .btnCheckOut = new System.Windows.Forms.Button();
this .panel4 = new System.Windows.Forms.Panel();
this .nmFoodCount = new System.Windows.Forms.NumericUpDown();
this .btnAddFood = new System.Windows.Forms.Button();
this .cbFood = new System.Windows.Forms.ComboBox();
this .cbCategory = new System.Windows.Forms.ComboBox();
this .flpTable = new System.Windows.Forms.FlowLayoutPanel();
this .txbTotalPrice = new System.Windows.Forms.TextBox();
this .menuStrip1.SuspendLayout();
this .panel2.SuspendLayout();
this .panel3.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this .nmDisCount)).BeginInit();
this .panel4.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this .nmFoodCount)).BeginInit();
this .SuspendLayout();
this .menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this .adminToolStripMenuItem,
this .thôngTinTàiKhoảnToolStripMenuItem});
this .menuStrip1.Location = new System.Drawing.Point(0 , 0 );
this .menuStrip1.Name = "menuStrip1" ;
this .menuStrip1.Size = new System.Drawing.Size(797 , 24 );
this .menuStrip1.TabIndex = 1 ;
this .menuStrip1.Text = "menuStrip1" ;
this .adminToolStripMenuItem.Name = "adminToolStripMenuItem" ;
this .adminToolStripMenuItem.Size = new System.Drawing.Size(55 , 20 );
this .adminToolStripMenuItem.Text = "Admin" ;
this .adminToolStripMenuItem.Click += new System.EventHandler(this .adminToolStripMenuItem_Click);
this .thôngTinTàiKhoảnToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this .thôngTinCáNhânToolStripMenuItem,
this .đăngXuấtToolStripMenuItem});
this .thôngTinTàiKhoảnToolStripMenuItem.Name = "thôngTinTàiKhoảnToolStripMenuItem" ;
this .thôngTinTàiKhoảnToolStripMenuItem.Size = new System.Drawing.Size(123 , 20 );
this .thôngTinTàiKhoảnToolStripMenuItem.Text = "Thông tin tài khoản" ;
this .thôngTinCáNhânToolStripMenuItem.Name = "thôngTinCáNhânToolStripMenuItem" ;
this .thôngTinCáNhânToolStripMenuItem.Size = new System.Drawing.Size(171 , 22 );
this .thôngTinCáNhânToolStripMenuItem.Text = "Thông tin cá nhân" ;
this .thôngTinCáNhânToolStripMenuItem.Click += new System.EventHandler(this .thôngTinCáNhânToolStripMenuItem_Click);
this .đăngXuấtToolStripMenuItem.Name = "đăngXuấtToolStripMenuItem" ;
this .đăngXuấtToolStripMenuItem.Size = new System.Drawing.Size(171 , 22 );
this .đăngXuấtToolStripMenuItem.Text = "Đăng xuất" ;
this .đăngXuấtToolStripMenuItem.Click += new System.EventHandler(this .đăngXuấtToolStripMenuItem_Click);
this .panel2.Controls.Add(this .lsvBill);
this .panel2.Location = new System.Drawing.Point(447 , 85 );
this .panel2.Name = "panel2" ;
this .panel2.Size = new System.Drawing.Size(338 , 315 );
this .panel2.TabIndex = 2 ;
this .lsvBill.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this .columnHeader1,
this .columnHeader2,
this .columnHeader3,
this .columnHeader4});
this .lsvBill.GridLines = true ;
this .lsvBill.Location = new System.Drawing.Point(3 , 3 );
this .lsvBill.Name = "lsvBill" ;
this .lsvBill.Size = new System.Drawing.Size(332 , 309 );
this .lsvBill.TabIndex = 0 ;
this .lsvBill.UseCompatibleStateImageBehavior = false ;
this .lsvBill.View = System.Windows.Forms.View.Details;
this .columnHeader1.Text = "Tên món" ;
this .columnHeader1.Width = 133 ;
this .columnHeader2.Text = "Số lượng" ;
this .columnHeader3.Text = "Đơn giá" ;
this .columnHeader3.Width = 54 ;
this .columnHeader4.Text = "Thành tiền" ;
this .columnHeader4.Width = 81 ;
this .panel3.Controls.Add(this .txbTotalPrice);
this .panel3.Controls.Add(this .cbSwitchTable);
this .panel3.Controls.Add(this .btnSwitchTable);
this .panel3.Controls.Add(this .nmDisCount);
this .panel3.Controls.Add(this .btnDiscount);
this .panel3.Controls.Add(this .btnCheckOut);
this .panel3.Location = new System.Drawing.Point(447 , 406 );
this .panel3.Name = "panel3" ;
this .panel3.Size = new System.Drawing.Size(338 , 52 );
this .panel3.TabIndex = 3 ;
this .cbSwitchTable.FormattingEnabled = true ;
this .cbSwitchTable.Location = new System.Drawing.Point(3 , 28 );
this .cbSwitchTable.Name = "cbSwitchTable" ;
this .cbSwitchTable.Size = new System.Drawing.Size(75 , 21 );
this .cbSwitchTable.TabIndex = 6 ;
this .btnSwitchTable.Location = new System.Drawing.Point(3 , 3 );
this .btnSwitchTable.Name = "btnSwitchTable" ;
this .btnSwitchTable.Size = new System.Drawing.Size(75 , 26 );
this .btnSwitchTable.TabIndex = 5 ;
this .btnSwitchTable.Text = "Chuyển bàn" ;
this .btnSwitchTable.UseVisualStyleBackColor = true ;
this .nmDisCount.Location = new System.Drawing.Point(84 , 29 );
this .nmDisCount.Name = "nmDisCount" ;
this .nmDisCount.Size = new System.Drawing.Size(74 , 20 );
this .nmDisCount.TabIndex = 4 ;
this .nmDisCount.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
this .btnDiscount.Location = new System.Drawing.Point(84 , 3 );
this .btnDiscount.Name = "btnDiscount" ;
this .btnDiscount.Size = new System.Drawing.Size(75 , 26 );
this .btnDiscount.TabIndex = 4 ;
this .btnDiscount.Text = "Giảm giá" ;
this .btnDiscount.UseVisualStyleBackColor = true ;
this .btnCheckOut.Location = new System.Drawing.Point(260 , 3 );
this .btnCheckOut.Name = "btnCheckOut" ;
this .btnCheckOut.Size = new System.Drawing.Size(75 , 46 );
this .btnCheckOut.TabIndex = 3 ;
this .btnCheckOut.Text = "Thanh toán" ;
this .btnCheckOut.UseVisualStyleBackColor = true ;
this .panel4.Controls.Add(this .nmFoodCount);
this .panel4.Controls.Add(this .btnAddFood);
this .panel4.Controls.Add(this .cbFood);
this .panel4.Controls.Add(this .cbCategory);
this .panel4.Location = new System.Drawing.Point(447 , 27 );
this .panel4.Name = "panel4" ;
this .panel4.Size = new System.Drawing.Size(338 , 52 );
this .panel4.TabIndex = 4 ;
this .nmFoodCount.Location = new System.Drawing.Point(296 , 19 );
this .nmFoodCount.Minimum = new decimal (new int [] {
100 ,
0 ,
0 ,
-2147483648 });
this .nmFoodCount.Name = "nmFoodCount" ;
this .nmFoodCount.Size = new System.Drawing.Size(39 , 20 );
this .nmFoodCount.TabIndex = 3 ;
this .nmFoodCount.Value = new decimal (new int [] {
1 ,
0 ,
0 ,
0 });
this .btnAddFood.Location = new System.Drawing.Point(214 , 3 );
this .btnAddFood.Name = "btnAddFood" ;
this .btnAddFood.Size = new System.Drawing.Size(75 , 46 );
this .btnAddFood.TabIndex = 2 ;
this .btnAddFood.Text = "Thêm món" ;
this .btnAddFood.UseVisualStyleBackColor = true ;
this .cbFood.FormattingEnabled = true ;
this .cbFood.Location = new System.Drawing.Point(3 , 28 );
this .cbFood.Name = "cbFood" ;
this .cbFood.Size = new System.Drawing.Size(205 , 21 );
this .cbFood.TabIndex = 1 ;
this .cbCategory.FormattingEnabled = true ;
this .cbCategory.Location = new System.Drawing.Point(3 , 3 );
this .cbCategory.Name = "cbCategory" ;
this .cbCategory.Size = new System.Drawing.Size(205 , 21 );
this .cbCategory.TabIndex = 0 ;
this .flpTable.AutoScroll = true ;
this .flpTable.Location = new System.Drawing.Point(12 , 30 );
this .flpTable.Name = "flpTable" ;
this .flpTable.Size = new System.Drawing.Size(429 , 428 );
this .flpTable.TabIndex = 5 ;
this .txbTotalPrice.Font = new System.Drawing.Font("Arial" , 11.25 F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte )(163 )));
this .txbTotalPrice.ForeColor = System.Drawing.Color.OrangeRed;
this .txbTotalPrice.Location = new System.Drawing.Point(164 , 17 );
this .txbTotalPrice.Name = "txbTotalPrice" ;
this .txbTotalPrice.ReadOnly = true ;
this .txbTotalPrice.Size = new System.Drawing.Size(90 , 25 );
this .txbTotalPrice.TabIndex = 7 ;
this .txbTotalPrice.Text = "0" ;
this .txbTotalPrice.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
this .AutoScaleDimensions = new System.Drawing.SizeF(6 F, 13 F);
this .AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this .ClientSize = new System.Drawing.Size(797 , 470 );
this .Controls.Add(this .flpTable);
this .Controls.Add(this .panel4);
this .Controls.Add(this .panel3);
this .Controls.Add(this .panel2);
this .Controls.Add(this .menuStrip1);
this .MainMenuStrip = this .menuStrip1;
this .Name = "fTableManager" ;
this .StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this .Text = "Phần mềm quản lý quán cafe" ;
this .menuStrip1.ResumeLayout(false );
this .menuStrip1.PerformLayout();
this .panel2.ResumeLayout(false );
this .panel3.ResumeLayout(false );
this .panel3.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this .nmDisCount)).EndInit();
this .panel4.ResumeLayout(false );
((System.ComponentModel.ISupportInitialize)(this .nmFoodCount)).EndInit();
this .ResumeLayout(false );
this .PerformLayout();
}
#endregion
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem adminToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem thôngTinTàiKhoảnToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem thôngTinCáNhânToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem đăngXuấtToolStripMenuItem;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.ListView lsvBill;
private System.Windows.Forms.Panel panel3;
private System.Windows.Forms.Panel panel4;
private System.Windows.Forms.NumericUpDown nmFoodCount;
private System.Windows.Forms.Button btnAddFood;
private System.Windows.Forms.ComboBox cbFood;
private System.Windows.Forms.ComboBox cbCategory;
private System.Windows.Forms.ComboBox cbSwitchTable;
private System.Windows.Forms.Button btnSwitchTable;
private System.Windows.Forms.NumericUpDown nmDisCount;
private System.Windows.Forms.Button btnDiscount;
private System.Windows.Forms.Button btnCheckOut;
private System.Windows.Forms.FlowLayoutPanel flpTable;
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.ColumnHeader columnHeader2;
private System.Windows.Forms.ColumnHeader columnHeader3;
private System.Windows.Forms.ColumnHeader columnHeader4;
private System.Windows.Forms.TextBox txbTotalPrice;
}
}
Code MenuDAO.cs
using QuanLyQuanCafe.DTO;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace QuanLyQuanCafe.DAO
{
public class MenuDAO
{
private static MenuDAO instance;
public static MenuDAO Instance
{
get { if (instance == null )instance = new MenuDAO(); return MenuDAO.instance; }
private set { MenuDAO.instance = value ; }
}
private MenuDAO () { }
public List<Menu> GetListMenuByTable (int id)
{
List<Menu> listMenu = new List<Menu>();
string query = "SELECT f.name, bi.count, f.price, f.price*bi.count AS totalPrice FROM dbo.BillInfo AS bi, dbo.Bill AS b, dbo.Food AS f WHERE bi.idBill = b.id AND bi.idFood = f.id AND b.status = 0 AND b.idTable = " + id;
DataTable data = DataProvider.Instance.ExecuteQuery(query);
foreach (DataRow item in data.Rows)
{
Menu menu = new Menu(item);
listMenu.Add(menu);
}
return listMenu;
}
}
}
Kết
Trong bài này, chúng ta đã tìm hiểu cách hiển thị tổng tiền cho phần mềm Quản lý quán cafe với C# Winform
Ở bài sau, Kteam sẽ giới thiệu đến bạn về THÊM BỚT MÓN HÓA ĐƠN TRONG PHẦN MỀM QUẢN
LÝ QUÁN CAFE
Cảm ơn các bạn đã theo dõi bài viết. Hãy để lại bình luận hoặc góp ý của mình để phát triển bài viết tốt hơn. Và đừng quên “Luyện tập – Thử Thách – Không ngại khó ”
Tải xuống
Tài liệu
Nhằm phục vụ mục đích học tập Offline của cộng đồng, Kteam hỗ trợ tính năng lưu trữ nội dung bài học Hiển thị tổng tiền cho phần mềm Quản lý quán cafe với C# Winform dưới dạng file PDF trong link bên dưới.
Ngoài ra, bạn cũng có thể tìm thấy các tài liệu được đóng góp từ cộng đồng ở mục TÀI LIỆU trên thư viện Howkteam.com
Đừng quên like và share để ủng hộ Kteam và tác giả nhé!
Project
Nếu việc thực hành theo hướng dẫn không diễn ra suôn sẻ như mong muốn. Bạn cũng có thể tải xuống PROJECT THAM KHẢO ở link bên dưới!
Thảo luận
Nếu bạn có bất kỳ khó khăn hay thắc mắc gì về khóa học, đừng ngần ngại đặt câu hỏi trong phần BÌNH LUẬN bên dưới hoặc trong mục HỎI & ĐÁP trên thư viện Howkteam.com để nhận được sự hỗ trợ từ cộng đồng.
mn muốn hiển thị số như kiểu giá tiền VN thì dùng format "N0" nha.
Cứ nhìn theo danh sách bài học và mò mẫm làm theo sườn đó. nhìn vào yêu cầu và tự tìm hướng giải quyết, có cái làm được có cái làm mãi không thấy chạy :D bó tay rồi thì xem video rồi làm theo, làm xong cái này nắm thêm nhiều về mô hình 3 lớp và luồng đi của dữ liệu. cảm thấy khai sáng :))
Thanks bạn nhiều