C#从用户输入获取类名

时间:2015-09-24 14:41:17

标签: c# .net oop

在C#中,如何使用用户输入或流来决定使用哪个类?此示例使用Console.Readline()但是,实际程序将根据循环中读取的流中的数据来决定使用哪个类。这只是简化了问题的一个例子:

static void Main(string[] args)
        {
            stock aapl = new stock(); //instantiate a class for Apple Stock
            stock fb = new stock();   //instantiate a class for Facebook Stock

            Console.WriteLine("Please enter a symbol for Apple or Facebook");
            string symbol = Console.ReadLine(); //this should get the class to work on

            Console.WriteLine("Please enter yesterdays price for the symbol");
            double yestPrice = Convert.ToDouble(Console.ReadLine());

            Console.WriteLine("Please enter Todays Price for the symbol");
            double currPrice = Convert.ToDouble(Console.ReadLine());

            //Assuming aapl was entered, how do I
            //set values for the appl member using
            //the symbol variable like this:
            symbol.YesterdaysPrice = yestPrice; 
            symbol.CurrentPrice = currPrice; 

        }
        class stock
        {
            private double yesterdayPrice;
            private double currentPrice;
            private double dailyGain;

            public double YesterdaysPrice
            {
                get { return yesterdayPrice; }
                set { yesterdayPrice = value; }
            }
            public double CurrentPrice
            {
                get { return currentPrice; }
                set { currentPrice = value; }
            }
            public double DailyGain
            {
                get { return currentPrice - yesterdayPrice; }
                // No need to ever set directly
            }
        }

2 个答案:

答案 0 :(得分:2)

以下是使用字典解决此问题的示例:

    static void Main(string[] args)
    {
        Dictionary<string, stock> stocks = new Dictionary<string, stock>(StringComparer.CurrentCultureIgnoreCase);

        //Add the initial stocks here if desired.

        Console.WriteLine("Please enter a symbol");
        string symbol = Console.ReadLine();

        Console.WriteLine("Please enter yesterdays price for the symbol");
        double yestPrice = Convert.ToDouble(Console.ReadLine());

        Console.WriteLine("Please enter Todays Price for the symbol");
        double currPrice = Convert.ToDouble(Console.ReadLine());

        if (stocks.ContainsKey(symbol))     //The dictionary contains the stock
        {
            stocks[symbol].YesterdaysPrice = yestPrice;
            stocks[symbol].CurrentPrice = currPrice;
        }
        else
        {
            //The stock wasn't found, we can either say invalid stock, or add one like this:
            stocks[symbol] = new stock()
            {
                YesterdaysPrice = yestPrice,
                CurrentPrice = currPrice;
            };
        }
    }

字典在顶部声明,并将保存所有输入的符号。用户输入符号和数据后,if语句将检查具有该符号的库存是否已存在,如果存在,则更新其值。简单来说,我创建了一个带有StringComparer.CurrentCultureIgnoreCase参数的字典,以便用户可以输入AAPL或aapl或aApL,它们都匹配相同的股票,默认情况下它区分大小写并会创建不同的股票对那些人来说。

如果股票不存在,您可以告诉用户其库存无效,或者您可以添加库存,因为您拥有所需的所有数据。我展示了为价格属性添加带有内联初始化程序的新库存的示例。

答案 1 :(得分:0)

Ron建议如下操作

        static readonly IDictionary<string, Stock> Stocks = new Dictionary<string, Stock>();
        static void Main(string[] args)
        {

            Console.WriteLine("Please enter a symbol for Apple or Facebook");
            var symbol = Console.ReadLine(); //this should get the class to work on

            Console.WriteLine("Please enter yesterdays price for the symbol");
            var yestPrice = Convert.ToDouble(Console.ReadLine());

            Console.WriteLine("Please enter Todays Price for the symbol");
            var currPrice = Convert.ToDouble(Console.ReadLine());


            Stock stock;
            if (Stocks.ContainsKey(symbol))
                stock = Stocks[symbol];
            else
            {
                stock=new Stock();
                Stocks[symbol] = stock;
            }


            stock.YesterdaysPrice = yestPrice;
            stock.CurrentPrice = currPrice;

        }