SoFunction
Updated on 2025-03-07

C# object-oriented simulation realizes shopping function in malls

This article shares the specific code for C# to implement shopping function in malls for your reference. The specific content is as follows

Product category

namespace ShoppingSystem
{
    /*
 * Product information includes: product name, product price, product model, product description, etc.
 */

    /// <summary>
    /// Product category    /// </summary>
    class Goods
    {
        /// <summary>
        /// Product Name        /// </summary>
        private string goodName;

        /// <summary>
        /// Product price        /// </summary>
        private float goodPrice;

        /// <summary>
        /// Product model        /// </summary>
        private string[] goodModel = new string[2];

        /// <summary>
        /// Product category        /// </summary>
        private string goodType;

        /// <summary>
        /// Product Description        /// </summary>
        private string goodDescribe;


        /// <summary>
        /// Seller        /// </summary>
        private Seller seller;

        public Seller Seller
        {
            get
            {
                return seller;
            }
            set
            {
                seller = value;
            }
        }


        public string GoodName
        {
            get
            {
                return goodName;
            }
            set
            {
                goodName = value;
            }
        }

        public float GoodPrice
        {
            get
            {
                return goodPrice;
            }
            set
            {
                goodPrice = value;
            }
        }

        public string[] GoodModel
        {
            get
            {
                return goodModel;
            }
            set
            {
                goodModel = value;
            }
        }

        public string GoodType
        {
            get
            {
                return goodType;
            }
            set
            {
                goodType = value;
            }
        }

        public string GoodDescribe
        {
            get
            {
                return goodDescribe;
            }
            set
            {
                goodDescribe = value;
            }
        }

        /// <summary>
        /// Constructor, assign values ​​to each variable and add product description        /// </summary>
        /// <param name="goodName">Product name</param>        /// <param name="goodPrice">Product Price</param>        /// <param name="goodId">Product number</param>        /// <param name="goodModel">Product model</param>        /// <param name="goodType">Product Category</param>        public Goods(string goodName, float goodPrice, string[] goodModel, string goodType)
        {
             = goodName;
             = goodPrice;
             = goodModel;
             = goodType;

            goodDescribe = goodName + goodModel[0] + "|" + goodModel[1] + "|" + goodPrice + "|";

        }
    }
}

Product Library

namespace ShoppingSystem
{
    class GoodsSql
    {
        /// &lt;summary&gt;
        /// Total product library        /// &lt;/summary&gt;
        private Goods[] good = new Goods[20];

        public Goods[] Good
        {
            get
            {
                return good;
            }
            set
            {
                good = value;
            }
        }
    }
}

User class

using System;
using ;
using ;
using ;
using ;

namespace ShoppingSystem
{
    /// &lt;summary&gt;
    /// User class    /// &lt;/summary&gt;
    class User
    {
        /// &lt;summary&gt;
        /// username        /// &lt;/summary&gt;
        private string username;

        /// &lt;summary&gt;
        /// User balance        /// &lt;/summary&gt;
        private float userBalance;

        /// &lt;summary&gt;
        /// Shopping cart        /// &lt;/summary&gt;
        private ShoppingCart cart = new ShoppingCart();

        public User(string username, float userBalance)
        {
             = username;
             = userBalance;
             = this;
        }

        public string Username
        {
            get
            {
                return username;
            }
            set
            {
                username = value;
            }
        }

        public float UserBalance
        {
            get
            {
                return userBalance;
            }
            set
            {
                userBalance = value;
            }
        }

        public ShoppingCart Cart
        {
            get
            {
                return cart;
            }
            set
            {
                cart = value;
            }
        }


        /// &lt;summary&gt;
        /// add to the cart        /// &lt;/summary&gt;
        /// <param name="good">Products to be added</param>        /// <param name="goodsNum">Quantity to buy</param>        public void BuyGoods(Goods good, int goodsNum)
        {
            
            (good, goodsNum);

            //TODO

        }

        /// &lt;summary&gt;
        /// Check and clear the shopping cart        /// &lt;/summary&gt;
        public void CheckoutCart()
        {
            ();
        }


    }
}

Seller category

using System;

namespace ShoppingSystem
{
    /// &lt;summary&gt;
    /// Seller category    /// &lt;/summary&gt;
    class Seller
    {
        /// &lt;summary&gt;
        /// Seller's name        /// &lt;/summary&gt;
        private string sellerName;

        /// &lt;summary&gt;
        /// Seller's balance        /// &lt;/summary&gt;
        private float sellerBalance;

        /// &lt;summary&gt;
        /// Seller's product array        /// &lt;/summary&gt;
        private Goods[] sellerGoods = new Goods[5]; 

        public Seller(string sellerName, float sellerBalance)
        {
             = sellerName;
             = sellerBalance;
        }
        public string SellerName
        {
            get
            {
                return sellerName;
            }
            set
            {
                sellerName = value;
            }
        }

        public float SellerBalance
        {
            get
            {
                return sellerBalance;
            }
            set
            {
                sellerBalance = value;
            }
        }

        public Goods[] SellerGoods
        {
            get
            {
                return sellerGoods;
            }
            set
            {
                sellerGoods = value;
            }
        }

        /// &lt;summary&gt;
        /// New products are on the shelves        /// &lt;/summary&gt;
        /// &lt;param name="good"&gt;&lt;/param&gt;
        public void AddGood(Goods good,GoodsSql goods)
        {
            for (int i = 0; i &lt; ; i++)
            {
                if (sellerGoods[i] == null)
                {
                    sellerGoods[i] = good;
                     = this;
                    for (int j = 0; j &lt; ; j++)
                    {
                        if ([j] == null)
                        {
                            [j] = good;
                            break;
                        }
                    }
                    ("Add product successfully!");
                    break;
                }
                if (i ==  - 1)
                {
                    ("Add product failed! The maximum number of items available for listing has been reached!");
                    return;
                }
            }
        }

        /// &lt;summary&gt;
        /// Update product information        /// &lt;/summary&gt;
        /// <param name="good">Products to be updated</param>        /// <param name="goodName">Product name</param>        /// <param name="goodPrice">Product Price</param>        /// <param name="goodId">Product number</param>        /// <param name="goodModel">Product model</param>        /// <param name="goodType">Product Type</param>        public void UpdateGoodInfo(Goods good, string goodName, float goodPrice, string[] goodModel, string goodType)
        {
            if (good != null)
            {
                 = goodName;
                 = goodModel;
                 = goodType;
                 = goodName + goodModel[0] + "|" + goodModel[1] + "|" + goodPrice + "|";
                ("Updated product information successfully!");
                return;
            }
            ("Update product information failed!");

        }
    }
}

Service category

namespace ShoppingSystem
{
    class Service
    {

        private Goods[] goods = new Goods[20];

        public Goods[] Goods
        {
            get
            {
                return goods;
            }
            set
            {
                goods = value;
            }
        }

        /// &lt;summary&gt;
        /// Search for products by type        /// &lt;/summary&gt;
        /// &lt;param name="goodType"&gt;&lt;/param&gt;
        /// &lt;param name="goods"&gt;&lt;/param&gt;
        public void Search(string goodType, GoodsSql goods)
        {
             = new Goods[20];
            int count = 0;

            for (int i = 0; i &lt; ; i++)
            {

                if ([i] != null &amp;&amp; [i].(goodType))
                {
                    [count++] = [i];
                }
            }

        }
    }
}

Shopping cart category

using System;
using ;
using ;
using ;
using ;

namespace ShoppingSystem
{
    /// &lt;summary&gt;
    /// Shopping cart category    /// &lt;/summary&gt;
    class ShoppingCart
    {
        /// &lt;summary&gt;
        /// Array of shopping entries        /// &lt;/summary&gt;
        private ShoppingItems[] items;

        /// &lt;summary&gt;
        /// Total shopping fee        /// &lt;/summary&gt;
        private float totalPrice = 0.00f;

        /// &lt;summary&gt;
        /// Users to which the shopping cart belongs        /// &lt;/summary&gt;
        private User user;

        public ShoppingItems[] Items
        {
            get
            {
                return items;
            }
            set
            {
                items = value;
            }
        }

        public float TotalPrice
        {
            get
            {
                return totalPrice;
            }
            set
            {
                totalPrice = value;
            }
        }

        public User User
        {
            get
            {
                return user;
            }
            set
            {
                user = value;
            }
        }

        /// &lt;summary&gt;
        /// Add items to cart        /// &lt;/summary&gt;
        /// <param name="good">Products to be added</param>        /// <param name="goodsNum">Quantity to buy</param>        public void AddGoods(Goods good, int goodsNum)
        {
            //If the shopping cart entry is empty, instantiate the shopping cart entry array            if (items == null)
            {
                items = new ShoppingItems[10];
            }

            //Add product entries to the shopping entries array            for (int i = 0; i &lt; ; i++)
            {
                if (items[i] == null)
                {
                    items[i] = new ShoppingItems();
                    items[i].Good = good;
                    items[i].GoodsNum = goodsNum;
                    totalPrice +=  * goodsNum;
                    ($"Already{}quantity:{goodsNum},add to the cart");
                    break;
                }

                if (i ==  - 1)
                {
                    ("The shopping cart is full!");
                }
            }
        }
        
        /// &lt;summary&gt;
        /// Check and clear the shopping cart        /// &lt;/summary&gt;
        public void CheckoutCart()
        {
            //Judge whether the shopping cart is empty            if (items == null)
            {
                ("There is no item in your shopping cart!");
                return;
            }

            foreach (var item in items)
            {
                if (item != null)
                {
                     +=  * ;
                    ($"Product Name:{}");
                }
            }

            ($"{}The shopping cart has been cleared,Total expenses{totalPrice}Yuan");
             -= totalPrice;
            items = null;

            //TODO

        }


        public void selectCart()
        {
            if (items == null)
            {
                ("Your cart is empty!");
                return;
            }

            foreach (var item in items)
            {
                ($"{}quantity:{}");
            }
        }
    }
}

Shopping cart entry category

using System;
using ;
using ;
using ;
using ;

namespace ShoppingSystem
{
    /// &lt;summary&gt;
    /// Product entry category in the shopping cart    /// &lt;/summary&gt;
    class ShoppingItems
    {
        /// &lt;summary&gt;
        /// Product        /// &lt;/summary&gt;
        private Goods good;

        /// &lt;summary&gt;
        /// The quantity to buy        /// &lt;/summary&gt;
        private int goodsNum;


        public Goods Good
        {
            get
            {
                return good;
            }
            set
            {
                good = value;
            }
        }

        public int GoodsNum
        {
            get
            {
                return goodsNum;
            }
            set
            {
                goodsNum = value;
            }
        }

    }
}

desk

Actually, there should be a lot of things here. I'm lazy and don't do it anymore

using System;

namespace ShoppingSystem
{
    /// &lt;summary&gt;
    /// Software usage class    /// &lt;/summary&gt;
    class SoftwareUsage
    {
        /// &lt;summary&gt;
        /// Get user instructions        /// &lt;/summary&gt;
        /// &lt;returns&gt;&lt;/returns&gt;
        public string Order()
        {
            ("Please enter the command first:");
            ("0-Exit, 1-Search, 2-Purchase, 3-Empty and settle the shopping cart, 4-Inquiry shopping cart");
            return ();
        }
    }
}

Program entry main function:

using System;

namespace ShoppingSystem
{
    class Program
    {
        static void Main(string[] args)
        {
            GoodsSql goodsSql = new GoodsSql();

            Service service = new Service();

            User user = new User("Yue Xiang", 10000000.00f);

            SoftwareUsage use = new SoftwareUsage();

            Seller seller01 = new Seller("Seller 1", 10000.00f);
            Seller seller02 = new Seller("Seller 2", 10000.00f);
            Seller seller03 = new Seller("Seller 3", 10000.00f);

            Goods good01 = new Goods("The beauty of programming(books)", 120.00f, new string[]{ "quality", "Normal Edition" }, "books");
            Goods good02 = new Goods("The beauty of programming(books)", 145.00f, new string[]{ "quality", "Hardcover" }, "books");
            Goods good03 = new Goods("Sanmao Wandering(books)", 20.00f, new string[]{ "quality", "Normal Edition" }, "books");
            Goods good04 = new Goods("Sanmao Wandering(books)", 25.00f, new string[]{ "quality", "Hardcover" }, "books");

            Goods good05 = new Goods("iPhone 100(cell phone)", 6000.00f, new string[]{ "RAM", "64GB" }, "cell phone");
            Goods good06 = new Goods("iPhone 100(cell phone)", 7000.00f, new string[]{ "RAM", "128GB" }, "cell phone");
            Goods good07 = new Goods("iPhone 100(cell phone)", 9000.00f, new string[]{ "RAM", "512GB" }, "cell phone");
            Goods good08 = new Goods("Nokia(cell phone)", 1000.00f, new string[]{ "model", "E63" }, "cell phone");
            Goods good09 = new Goods("Nokia(cell phone)", 2000.00f, new string[]{ "model", "N95" }, "cell phone");
            Goods good10 = new Goods("Nokia(cell phone)", 2300.00f, new string[]{ "model", "N97" }, "cell phone");

            Goods good11 = new Goods("Mac Book Pro(computer)", 18000.00f, new string[]{ "Configuration", "Low version" }, "computer");
            Goods good12 = new Goods("Mac Book Pro(computer)", 20000.00f, new string[]{ "Configuration", "Middle version" }, "computer");
            Goods good13 = new Goods("Mac Book Pro(computer)", 22000.00f, new string[]{ "Configuration", "High-end version" }, "computer");

            (good01, goodsSql);
            (good02, goodsSql);
            (good03, goodsSql);
            (good04, goodsSql);
            (good05, goodsSql);

            (good06, goodsSql);
            (good07, goodsSql);
            (good08, goodsSql);
            (good09, goodsSql);
            (good10, goodsSql);

            (good11, goodsSql);
            (good12, goodsSql);
            (good13, goodsSql);

            ();

            while (true)
            {
                string order = ();

                switch (order)
                {
                    case "0":
                        ("Shopping is over!");
                        return;

                    case "1":
                        ("Please enter the search keyword:");
                        string goodType = ();
                        (goodType, goodsSql);
                        Goods[] goods = ;
                        ($"Current buyer{}Searching for products:{goodType}");
                        ("-----------------------");
                        foreach (var item in goods)
                        {
                            if (item != null)
                            {
                                ($"Product Name:{},Product Type({[0]})" +
                                    $"{[1]},{}Yuan");
                            }
                        }
                        ("-----------------------");

                        break;

                    case "2":

                        if ([0] == null)
                        {
                            ("Please search and purchase first!");
                            break;
                        }

                        ("Please enter the product number first:");
                        int goodId = (());
                        ("Please enter the quantity of the product first:");
                        int goodsNum = (());

                        ([goodId - 1], goodsNum);
                        ("-----------------------");
                        break;

                    case "3":
                        ();
                        ($"Account balance:{}");
                        break;

                    case "4":
                        ();
                        break;

                    default:
                        ("The command you entered is incorrect!");
                        break;
                }

            }

        }
    }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.