2013-03-19 182 views
0

嗨,我是C#的新手,我正在尝试构建一个非常简单的股票跟踪控制台应用程序,用户输入有关股票的一些信息,程序保存并使用输入。我一直在浏览在线教程,但似乎无法将它们放在一起。C#简单的控制台应用程序股票交易

我想要求用户选择他们是想卖出股票,买入股票还是退出程序。如果他们选择买入或卖出股票,该计划将要求他们输入股票报价,购买股票的数量和支付的价格。

在这种情况下,我希望用户能够继续购买股票或卖出股票,直到他们选择退出该应用程序。该程序应该跟踪用户对每个股票的总量。例如,用户可以输入他们在2012年2月1日购买了100股APPL股票,然后在2/15/12输入他们购买了另外50股APPL股票,然后卖出25股APPL股票, 1/12。

程序会跟踪信息,当用户选择退出时,程序将输出他们剩余的共享数量。在上面的例子中,他们剩下125个股票。

最终我想把它变成一个实时股票交易程序,但现在我只是采取婴儿的步骤。

我已经包含了我在下面写的代码。我不明白处理用户输入的最佳方式。

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

namespace StockTracker 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 

     while (true) 
     { 
      Console.WriteLine("Welcome to the Stock Tracker"); 
      Console.WriteLine("--------------------------------"); 
      Console.WriteLine("Please choose from the following options:"); 
      Console.WriteLine("1) Buy Stock, 2) Sell Stock, 0) Exit"); 

      ArrayList Array1 = new ArrayList(); 
      ArrayList Array2 = new ArrayList(); 

      string userValue = Console.ReadLine(); 
      string message = ""; 


      double price; 
      int amount; 
      string stockTicker; 


      switch (userValue) 
      { 

       case "1": 
        Console.WriteLine("Enter ticker to buy:"); 
        stockTicker = Console.ReadLine(); 
        Array1.Add(stockTicker); 
        Console.WriteLine("Enter stock price"); 
        price = double.Parse(Console.ReadLine()); 
        Array1.Add(price); 
        Console.WriteLine("Enter Quantity"); 
        amount = int.Parse(Console.ReadLine()); 
        Array1.Add(amount); 
        Console.WriteLine("***Stock Purchased***"); 
        break; 


       case "2": 
        //message = "Enter stock ticker to sell"; 
        Console.WriteLine("Enter stocker ticker to sell"); 
        stockTicker = Console.ReadLine(); 
        Array2.Add(stockTicker); 
        Console.WriteLine("Enter Quantity"); 
        amount = int.Parse(Console.ReadLine()); 
        Array2.Add(amount); 
        Console.WriteLine("***Stock Sold***"); 
        break; 


       case "0": 
        Console.WriteLine("Good Bye!"); 
        return; 

       default: 
        message = "Invalid entry"; 
        break; 

      } 

      Console.Write(message); 
      Console.ReadLine(); 
      Console.WriteLine(); 


     } //while (userValue != "0"); 
    } 
} 
} 
+0

这不是一个真正的问题 - 你要求模糊的建议,而不是示范问题领域。 – Dai 2013-03-19 00:36:30

+2

如果你想把它变成一个实时股票交易程序,你应该使用C/C++。 C#太慢了。 – 2013-03-19 02:39:46

+0

“我不明白处理用户输入的最佳方式。”有点模糊。也许你可以澄清一下你不明白的东西?谢谢。 – Kev 2013-03-20 01:46:01

回答

2

您需要仔细考虑您的要求是什么。你需要跟踪每笔交易吗?或者只是每只股票的总数?你想让这些信息持久吗?

如果您只是跟踪每只股票的总数,我建议您使用Dictionary而不是ArrayList s。事情是这样的:

Dictionary<string,int> myStocks = new Dictionary<string,int>(); 
//.... 
      case "1": 
       Console.WriteLine("Enter ticker to buy:"); 
       stockTicker = Console.ReadLine(); 
       Console.WriteLine("Enter stock price"); 
       price = double.Parse(Console.ReadLine()); 
       Console.WriteLine("Enter Quantity"); 
       amount = int.Parse(Console.ReadLine()); 
       int currAmount = 0; 
       myStocks.TryGetValue(stockTicker,out currAmount); 
       myStocks[stockTicker] = currAmount + amount; 
       Console.WriteLine("***Stock Purchased***"); 
       break; 

如果你真的需要跟踪的名称和单独金额,你应该创建一个类(称之为Trade)专门为股票代码,数量,价格,日期等,并存储属性那些在List<Trade>