在c#console应用程序中按下按钮时添加项目

时间:2016-12-27 16:55:59

标签: c# .net

我一直在研究一个小型控制台应用程序,它有一个项目列表,当按下一个数字时,相关项目应该计算为总数(已完成)。

问题在于如何将这些项添加到列表视图并在控制台应用程序中显示它们。这就是我到目前为止所拥有的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SuperMarcado
{
    class MainClass
    {
        public static void Main(string[] args)
        {

            ShoppingCart myCart = new ShoppingCart();

            Product[] shopProducts = new Product[]
            {
                new Product("Cheese", 6, "Milk Cheese", "3/12/2016", 4),
                new Product("Bread", 2, "Grain Bread", "27/11/2016", 8),
                new Product("Ice Cream", 10, "Ice Cream", "09/11/2001", 1),
                new Product("Cookies", 100, " Chocolate Cookies", "00/00/0000", 5),
                new Product("Biscuits", 0.25f, "Vanila ", "08/01/2006", 6)
            };

            Shop shop = new Shop(shopProducts);
            shop.DisplayProducts();
            Console.WriteLine("------------------------------------------------------------");

            myCart.printY = Console.CursorTop;
            myCart.Display();
            ConsoleKeyInfo input;


            while ((input = Console.ReadKey(true)).Key != ConsoleKey.Escape)
            {
                if (!Char.IsDigit(input.KeyChar))
                    return;
                int index = Convert.ToInt16(input.KeyChar.ToString()) - 1;

                if (index < 1 || index > shop.products.Length)
                    return;

                myCart.AddProduct(shop.products[index]);

                shop.DecreaseAmount(shop.products[index]);

                shop.DisplayProducts();

                myCart.Display();



                int userInput = 0;
                do
                {

                    userInput = ShoppingCart();

                } while (userInput != 5);
            }

        }
        static public int ShoppingCart()
        {
            Console.WriteLine("Your cart");
            Console.WriteLine();
            var result = Console.ReadLine();
            return Convert.ToInt32(result);
        }


    }
}

1 个答案:

答案 0 :(得分:0)

首先,你的问题是第二个循环,删除它,它应该按预期工作

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

ShoppingCart Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SuperMarcado
{
class MainClass
{
    public static void Main(string[] args)
    {

        ShoppingCart myCart = new ShoppingCart();

        Product[] shopProducts = new Product[]
        {
            new Product("Cheese", 6, "Milk Cheese", "3/12/2016", 4),
            new Product("Bread", 2, "Grain Bread", "27/11/2016", 8),
            new Product("Ice Cream", 10, "Ice Cream", "09/11/2001", 1),
            new Product("Cookies", 100, " Chocolate Cookies", "00/00/0000", 5),
            new Product("Biscuits", 0.25f, "Vanila ", "08/01/2006", 6)
        };

        Shop shop = new Shop(shopProducts);
        shop.DisplayProducts();
        Console.WriteLine("------------------------------------------------------------");

        myCart.printY = Console.CursorTop;
        myCart.Display();
        ConsoleKeyInfo input;


        while ((input = Console.ReadKey(true)).Key != ConsoleKey.Escape)
        {
            if (!Char.IsDigit(input.KeyChar))
                return;
            int index = Convert.ToInt16(input.KeyChar.ToString()) - 1;

            if (index < 1 || index > shop.products.Length)
                return;

            myCart.AddProduct(shop.products[index]);

            shop.DecreaseAmount(shop.products[index]);

            shop.DisplayProducts();

            myCart.Display();



            //int userInput = 0;
            //do
            //{

            //    userInput = ShoppingCart();

            //} while (userInput != 5);
        }

    }

    //static public int ShoppingCart()
    //{
    //    Console.WriteLine("Your cart");
    //    Console.WriteLine();
    //    var result = Console.ReadLine();
    //    return Convert.ToInt32(result);
    //}
}
}