C# Windows Form based prototype of e-store

C# Windows Form based prototype of e-store
12/09/2018 Ernestas Konopliovas
In Back-end, C#

This application is prototype of e-store. Using Windows Form you can add new products to the store, choose from existing ones and add them to your cart. On your left, you will immediately see list and price of your cart.

How it looks like (GUI)
Code, I used
<pre>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication13
{
public partial class Form1 : Form
{
public List<Product> Products { get; set; }
Dictionary<int, string> ProductList { get; set; }
public double Quantity { get; set; }
public Product CurrentProduct { get; set; }
public List<CartItem> eCart { get; set; }
public double ProdTotalPrice { get; set; }
public FileInfo FileInfo { get; set; }

        public Form1()
{
InitializeComponent();

            GetAllProducts();
picProd.SizeMode = PictureBoxSizeMode.StretchImage;

            LoadDropDownList();

            Quantity = 0.0;
lblProdTotalPrice.Text = Quantity.ToString() + " Eur";
txtQuantity.Text = Quantity.ToString();
eCart = new List<CartItem>();
}
private void LoadDropDownList()
{
ProductList = new Dictionary<int, string>();

            for (int i = 0; i < Products.Count; i++)
{
ProductList.Add(Products[i].Id, Products[i].Name);
}
comboProducts.DataSource = new BindingSource(ProductList, null);
comboProducts.DisplayMember = "Value";
comboProducts.ValueMember = "Key";
}
public void GetAllProducts()
{
Products = new List<Product>();
foreach (string line in File.ReadAllLines(AppDomain.CurrentDomain.BaseDirectory + "Products.txt"))
{
string[] spec = line.Split(',');
Products.Add(new Product(int.Parse(spec[0]), double.Parse(spec[1].Replace('.', ',')), spec[2], spec[3], spec[4]));

            }

        }
private void Form1_Load(object sender, EventArgs e)
{
}
private void comboProducts_SelectedIndexChanged(object sender, EventArgs e)
{
CurrentProduct = Products.Where(x => x.Id == comboProducts.SelectedIndex + 1).FirstOrDefault();
picProd.ImageLocation = CurrentProduct.ImagePath;
lblUnit.Text = CurrentProduct.Unit;
lblPrice.Text = CurrentProduct.Price.ToString();
Quantity = 0;
txtQuantity.Text = "0";
}
private void txtQuantity_TextChanged(object sender, EventArgs e)
{
double quantity = 0;
string inputQuantity = txtQuantity.Text.Replace(".", ",");
if (!double.TryParse(inputQuantity, out quantity) && !string.IsNullOrWhiteSpace(inputQuantity))
{
MessageBox.Show("Iveskite kieki teisingai.");
btnAddToCart.Enabled = false;
}
else if (string.IsNullOrWhiteSpace(txtQuantity.Text) || txtQuantity.Text == "0") {
btnAddToCart.Enabled = false;
}
else
{
Quantity = quantity;
btnAddToCart.Enabled = true;
}
ProdTotalPrice = CurrentProduct.Price * Quantity;
lblProdTotalPrice.Text = string.Format("{0} Eur", ProdTotalPrice);
}
private void btnAddToCart_Click(object sender, EventArgs e)
{

            CartItem cartItem = new CartItem(CurrentProduct.Id, CurrentProduct.Name, CurrentProduct.Unit, Quantity, ProdTotalPrice);
if (!IsItemInCart())
{
eCart.Add(cartItem);
int index = eCart.IndexOf(cartItem);
eCart.Where(x => x.ID == -1).First().ID = index;
}

            DisplayCart();
label2.Text = TotalSum().ToString();
txtQuantity.Text = "0";
}
public bool IsItemInCart()
{
var item = eCart.Where(x => x.ProductID == CurrentProduct.Id).FirstOrDefault();
if (item != null)
{
item.Quantity += Quantity;
item.TotalPrice += ProdTotalPrice;
return true;
}
return false;
}

        public void DisplayCart()
{
listProductList.Items.Clear();
foreach (var cartItem in eCart)
{
listProductList.Items.Add(cartItem.DisplayProductString);
}

        }

        private void listProductList_SelectedIndexChanged(object sender, EventArgs e)
{

        }
private void btnDeleteFromCart_Click(object sender, EventArgs e)
{
var selectedItems = listProductList.SelectedItems;
for (int i = 0; i < selectedItems.Count; i++)
{
CartItem item = eCart.Where(x => x.DisplayProductString == selectedItems[i].ToString()).FirstOrDefault();
eCart.Remove(item);
}
for (int i = selectedItems.Count - 1; i >= 0; i--)
{
listProductList.Items.Remove(selectedItems[i]);
}
label2.Text = TotalSum().ToString();

        }
private double TotalSum()
{
double sum = 0;
for (int i = 0; i < eCart.Count; i++)
{
sum += eCart[i].TotalPrice;

            }
return sum;
}
private void btnAddNewProd_Click(object sender, EventArgs e)
{
if (AddNewProductValidation())
{
int lastID = Products.Select(x => x.Id).Last();
int newID = lastID + 1;
string photoPath = @"images" + txtProdPhoto.Text;
string stringToFile = string.Format("{0},{1},{2},{3},{4}", newID, txtProdPrice.Text.Replace(",", "."), txtProdName.Text, txtProdUnit.Text, photoPath);
File.AppendAllText(AppDomain.CurrentDomain.BaseDirectory + "Products.txt", Environment.NewLine + stringToFile);

                Products.Add(new Product(newID, double.Parse(txtProdPrice.Text.Replace(".", ",")), txtProdName.Text, txtProdUnit.Text, photoPath));

                File.Copy(FileInfo.SourceFile, FileInfo.DestinationFile, true);
LoadDropDownList();
ClearForm();

            }

        }
private void ClearForm()
{
txtProdName.Clear();
txtProdPrice.Clear();
txtProdPhoto.Clear();
txtProdUnit.Clear();
}
private bool AddNewProductValidation()
{

            double rezult;
if (!double.TryParse(txtProdPrice.Text.Replace(".", ","), out rezult))
{
MessageBox.Show("Klaidinga kaina!");
return false;
}
return true;
}
private void button2_Click(object sender, EventArgs e)
{

            OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = @"c:";

            if (ofd.ShowDialog() == DialogResult.OK)
{
MessageBox.Show(Path.GetFileName(ofd.FileName));
txtProdPhoto.Text = Path.GetFileName(ofd.FileName);
}

            FileInfo = new FileInfo();

            FileInfo.SourceFile = ofd.FileName;
FileInfo.DestinationFile = Path.GetFileName(ofd.FileName);

            }
}
}

Comments (0)

Leave a reply

Your email address will not be published. Required fields are marked *

*