Factory Design Patten Application : Adding Items to Cart List and calculating the total price in Windows Form UI


Form Design:
------------------------------------------------------------------------------------------------
namespace ShoppingCart
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.button3 = new System.Windows.Forms.Button();
            this.button4 = new System.Windows.Forms.Button();
            this.listBox1 = new System.Windows.Forms.ListBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.listBox2 = new System.Windows.Forms.ListBox();
            this.label3 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(315, 21);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 7;
            this.button1.Text = "Add to Item List";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // button2
            //
            this.button2.Location = new System.Drawing.Point(406, 21);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 8;
            this.button2.Text = "Add to Cart";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            //
            // button3
            //
            this.button3.Location = new System.Drawing.Point(499, 21);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(75, 23);
            this.button3.TabIndex = 9;
            this.button3.Text = "Remove";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new System.EventHandler(this.button3_Click);
            //
            // button4
            //
            this.button4.Location = new System.Drawing.Point(593, 21);
            this.button4.Name = "button4";
            this.button4.Size = new System.Drawing.Size(75, 23);
            this.button4.TabIndex = 10;
            this.button4.Text = "Calculate";
            this.button4.UseVisualStyleBackColor = true;
            this.button4.Click += new System.EventHandler(this.button4_Click);
            //
            // listBox1
            //
            this.listBox1.FormattingEnabled = true;
            this.listBox1.Location = new System.Drawing.Point(331, 86);
            this.listBox1.Name = "listBox1";
            this.listBox1.Size = new System.Drawing.Size(134, 199);
            this.listBox1.TabIndex = 11;
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(487, 86);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(111, 13);
            this.label1.TabIndex = 12;
            this.label1.Text = "Total Amount Payable";
            //
            // label2
            //
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(332, 69);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(81, 13);
            this.label2.TabIndex = 13;
            this.label2.Text = "Item Purchased";
            //
            // listBox2
            //
            this.listBox2.FormattingEnabled = true;
            this.listBox2.Location = new System.Drawing.Point(23, 39);
            this.listBox2.Name = "listBox2";
            this.listBox2.Size = new System.Drawing.Size(167, 238);
            this.listBox2.TabIndex = 14;
            //
            // label3
            //
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(490, 103);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(0, 13);
            this.label3.TabIndex = 15;
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(707, 350);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.listBox2);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.listBox1);
            this.Controls.Add(this.button4);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Button button3;
        private System.Windows.Forms.Button button4;
        private System.Windows.Forms.ListBox listBox1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.ListBox listBox2;
        private System.Windows.Forms.Label label3;

    }
}


Class Design
-------------------------------------------------------------------------------------------------------
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;

namespace ShoppingCart
{
    public partial class Form1 : Form
    {
        List<string> l1;
        Factory ItemCtrl ;
        public Form1()
        {
             l1= new List<string>();
             ItemCtrl = new Factory();
            InitializeComponent();
           

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
           
            var items = from n in ItemCtrl.getItemList()
                        select n;
            List<string> items_name= new List<string>();

            foreach( IItems n in items)
            {
                items_name.Add(n.Name);
            }


            listBox2.DataSource = items_name; 
           
        }

        private void button2_Click(object sender, EventArgs e)
        {
           
            l1.Add(listBox2.SelectedItem.ToString());

            listBox1.DataSource = null;
            listBox1.DataSource = l1;
        }

        private void button3_Click(object sender, EventArgs e)
        {

            l1.Remove(listBox1.SelectedItem.ToString());


            listBox1.DataSource = null;
            listBox1.DataSource = l1;

        }

        private void button4_Click(object sender, EventArgs e)
        {

            int price = 0;
           
            foreach(string s in l1)
            {
                price = price + ItemCtrl.getPrice(s);
                
            }

            label3.Text =  price.ToString();
        }

    }


    abstract class IItems
    {
        public String Name { get; set; }
        public String Id { get; set; }
        public int Price { get; set; }

        public abstract IItems getItem() ;
    }

    class Torch : IItems
    {

        Torch t;

        public Torch()
        {
            Name = "Torch";
            Id = "ITM1";
            Price = 25;
        }

        public override IItems getItem()
        {
            return t;
        }
    }


    class Copy : IItems
    {

        Copy c;

        public Copy()
        {
            Name = "Copy";
            Id = "ITM2";
            Price = 26;
        }

        public override IItems getItem()
        {
            return c;
        }
    }


    class Book : IItems
    {

        Book b;
        public Book()
        {
            Name = "Book";
            Id = "ITM3";
            Price = 30;
        }

        public override IItems getItem()
        {
            return b;
        }
    }

    class NailCutter : IItems
    {

        NailCutter NC;
        public NailCutter()
        {
            Name = "NailCutter";
            Id = "ITM4";
            Price = 20;
        }

        public override IItems getItem()
        {
            return NC;
        }
    }

    class PenStand : IItems
    {

        PenStand PS;
        public PenStand()
        {
            Name = "PenStand";
            Id = "ITM5";
            Price = 20;
        }

        public override IItems getItem()
        {
            return PS;
        }
    }


    class MobileCover : IItems
    {

        MobileCover MC;
        public MobileCover()
        {
            Name = "MobileCover";
            Id = "ITM6";
            Price = 50;
        }

        public override IItems getItem()
        {
            return MC;
        }
    }



    class Factory
    {
        List<IItems> l1 = new List<IItems>();
        public Factory()
        {
            l1.Add(new Torch());
            l1.Add(new Copy());
            l1.Add(new Book());
            l1.Add(new PenStand());
            l1.Add(new MobileCover());
            l1.Add(new NailCutter());
        }

        public List<IItems> getItemList()
        {
            return l1;
        }

        public int getPrice(string name)
        {
            int price = 0;
            foreach (IItems it in l1)
            {
                if (it.Name == name)
                {
                    price = it.Price;
                }
            }

            return price;

        }



    }

   
}


Comments

Popular posts from this blog

Authentication and Authorization in Web API -Part1

My Gardening Journey 6