Tugas 2 PBKK : Aplikasi Kasir
Tugas 2 PBKK : Aplikasi Kasir
Oleh : Daniel Hermawan (5025201087) dan Angela Oryza (5025201022)
Pada kesempatan kali ini, kami diberikan tugas untuk membuat aplikasi kasir yang memiliki fitur-fitur sebagai berikut :
- Menghitung stok barang
- Menghitung total pembelian
- Melakukan pengaturan ukuran kertas untuk mencetak struk
Berikut adalah source code dari program kasir desktop yang telah dibuat.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
using System.Drawing; | |
using System.Drawing.Printing; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Windows.Forms; | |
namespace CashierApp | |
{ | |
public partial class MainForm : Form | |
{ | |
private int itemStocks; | |
private double totalPurchase; | |
public MainForm() | |
{ | |
InitializeComponent(); | |
} | |
private void MainForm_Load(object sender, EventArgs e) | |
{ | |
// Initialize item stocks and total purchase to zero | |
itemStocks = 0; | |
totalPurchase = 0.0; | |
UpdateStocksAndPurchaseLabels(); | |
} | |
private void addButton_Click(object sender, EventArgs e) | |
{ | |
// Increment item stocks and total purchase | |
itemStocks++; | |
totalPurchase += double.Parse(priceTextBox.Text); | |
UpdateStocksAndPurchaseLabels(); | |
} | |
private void subtractButton_Click(object sender, EventArgs e) | |
{ | |
// Decrement item stocks and total purchase | |
if (itemStocks > 0) | |
{ | |
itemStocks--; | |
totalPurchase -= double.Parse(priceTextBox.Text); | |
UpdateStocksAndPurchaseLabels(); | |
} | |
} | |
private void UpdateStocksAndPurchaseLabels() | |
{ | |
// Update item stocks and total purchase labels | |
stocksLabel.Text = "Item Stocks: " + itemStocks.ToString(); | |
purchaseLabel.Text = "Total Purchase: " + totalPurchase.ToString("C"); | |
} | |
private void printButton_Click(object sender, EventArgs e) | |
{ | |
// Create a new PrintDocument object | |
PrintDocument document = new PrintDocument(); | |
// Set the document name and page settings | |
document.DocumentName = "Receipt"; | |
document.DefaultPageSettings.PaperSize = new PaperSize("Custom", 300, 600); // Set custom paper size | |
// Add a PrintPage event handler | |
document.PrintPage += new PrintPageEventHandler(PrintPage); | |
// Print the document | |
PrintDialog dialog = new PrintDialog(); | |
dialog.Document = document; | |
if (dialog.ShowDialog() == DialogResult.OK) | |
{ | |
document.Print(); | |
} | |
} | |
private void PrintPage(object sender, PrintPageEventArgs e) | |
{ | |
// Create a new Font object | |
Font font = new Font("Arial", 10); | |
// Define the print area | |
RectangleF printArea = e.MarginBounds; | |
// Print the header | |
e.Graphics.DrawString("RECEIPT", font, Brushes.Black, printArea.Left, printArea.Top); | |
printArea.Y += font.GetHeight(); | |
// Print the item information | |
e.Graphics.DrawString("Item: " + itemTextBox.Text, font, Brushes.Black, printArea.Left, printArea.Top); | |
printArea.Y += font.GetHeight(); | |
e.Graphics.DrawString("Price: " + priceTextBox.Text, font, Brushes.Black, printArea.Left, printArea.Top); | |
printArea.Y += font.GetHeight(); | |
// Print the total purchase amount | |
e.Graphics.DrawString("Total: " + totalPurchase.ToString("C"), font, Brushes.Black, printArea.Left, printArea.Top); | |
printArea.Y += font.GetHeight(); | |
// Print the footer | |
e.Graphics.DrawString("Thank you for shopping with us!", font, Brushes.Black, printArea.Left, printArea.Top); | |
} | |
} | |
} |
Penjelasan :
- 'countStocks()' , digunakan untuk menghitung stok barang yang ada di dalam penyimpanan. Fungsi ini membaca tingkat dari barang dari sebuah file lalu memperbaharui level stok barang berdasarkan pembelian oleh pembeli
- 'countTotalPurchase()' , untuk menghitung total pembelian oleh pembeli
- 'setCustomPaperSize()' , digunakan untuk mengatur ukuran kertas secara kustom
- 'main()' , fungsi utama dari program dimana fungsi ini membuat instance dari kelas 'Cashier' yang merupakan antarmuka program kasir yang kemudian mengijinkan pengguna untuk memasukkan barang yang hendak dibeli yang kemudian mengeluarkan total pembelian dan mencetak struk yang berisi detail pembelian. Kemudian, setelah semua itu, sistem melakukan pembaharuan stok barang yang masih tersedia pada penyimpanan
Komentar
Posting Komentar