المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : OOP Advice



C# Programming
12-25-2011, 10:00 PM
Goal: I am wanting to write a C# program that will pull data from a website (http://goo.gl/K5Qw9) to help me exercise what I have learned and to further learn OOP and the .Net framework a little better.

Background: The website is a fantasy game that allows you to buy stocks of movies before they come out and the final stock price is the total amount the movie made in theaters.

What I Have: A class for each stock, a static class for the portfolio which holds a List containing each instance of stock, a static class called netConnection with a method for logging in and a method for pulling data from other pages, and a static class called dataProcessor that processes the data from netConnection.

Now I'm not brand new at programming but am having a little trouble figuring out how to setup my plan for my program with OOP in mind. I guess the easiest way to get advice would be to just post my code and let others give me ideas on ways to improve. I have seen other snippets that use their net connection in an OOP way but I'm not sure if I did it right since my connection doesn't need to be persistent.


Code I'm fairly sure would be considered good code
hsxStock.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace hsx { public class hsxStock { //Class for each stock option private string optionType; private string stockUrl; private string stockTitle; private string stockPhase; private string releaseDate; private string symbol; private int shares; private string held; private double avgPaid; private double currentValue; private double todaysChange; private double totalValue; private double netGainLoss; //Constructor public hsxStock(string optionType, string stockUrl, string stockTitle, string stockPhase, string releaseDate, string symbol, int shares, string held, double avgPaid, double currentValue, double todaysChange, double totalValue, double netGainLoss) { this.optionType = optionType; this.stockUrl = stockUrl; this.stockTitle = stockTitle; this.stockPhase = stockPhase; this.releaseDate = releaseDate; this.symbol = symbol; this.shares = shares; this.held = held; this.avgPaid = avgPaid; this.currentValue = currentValue; this.todaysChange = todaysChange; this.totalValue = totalValue; this.netGainLoss = netGainLoss; hsxPortfolio.Stock = this; } public string OptionType { get { return optionType; } //set { // optionType = value; //} } public string StockUrl { get { return stockUrl; } //set { // stockUrl = value; //} } public string StockTitle { get { return stockTitle; } //set { // stockTitle = value; //} } public string StockPhase { get { return stockPhase; } //set { // stockPhase = value; //} } public string ReleaseDate { get { return releaseDate; } //set { // releaseDate = value; //} } public string Symbol { get { return symbol; } //set { // symbol = value; //} } public int Shares { get { return shares; } //set { // shares = value; //} } public string Held { get { return held; } //set { // held = value; //} } public double AvgPaid { get { return avgPaid; } //set { // avgPaid = value; //} } public double CurrentValue { get { return currentValue; } //set { // currentValue = value; //} } public double TodaysChange { get { return todaysChange; } //set { // todaysChange = value; //} } public double TotalValue { get { return totalValue; } //set { // totalValue = value; //} } public double NetGainLoss { get { return netGainLoss; } //set { // netGainLoss = value; //} } } }
hsxPortfolio.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace hsx { static class hsxPortfolio { static private double netWorth; static private double cashOnHand; static private double investments; static private List stocks = new List(); static public List Stocks { get { return stocks; } } static public hsxStock Stock { set { stocks.Add(value); } } static public int StockCount { get { return stocks.Count; } } static public double NetWorth { get { return netWorth; } set { netWorth = value; } } static public double CashOnHand { get { return cashOnHand; } set { cashOnHand = value; } } static public double Investments { get { return investments; } set { investments = value; } } } } End Code I'm fairly sure would be considered good code

Code I'm not so sure about
hsxDataProcessor.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Windows.Forms; namespace hsx { public static class hsxDataProcessor { public static void portfolio(string data) { string optionType = ""; //Parse Net Worth, Cash, Investments if (data.IndexOf(@"<div class="/spanspan class="code-string""/spanspan class="code-string"net_worth"/spanspan class="code-string""/spanspan class="code-string">") != -1) { string netWorthData = Regex.Match(data, @"(
.*H\$)([0-9,.]*)()").Groups[2].Value; string cashOnHand = Regex.Match(data, @"(
.*H\$)([0-9,.]*)()").Groups[2].Value; string investments = Regex.Match(data, @"(
.*H\$)([0-9,.]*)()").Groups[2].Value; hsxPortfolio.NetWorth = netWorthData.Length != 0 ? Convert.ToDouble(netWorthData) : 0.00; hsxPortfolio.CashOnHand = cashOnHand.Length != 0 ? Convert.ToDouble(cashOnHand) : 0.00; hsxPortfolio.Investments = investments.Length != 0 ? Convert.ToDouble(investments) : 0.00; } //Parse stocks //4 tables - MovieStocks, StarBonds, Derivatives, MovieFunds Regex regexTable = new Regex(""(/security/view/[0-9A-Z]{3,6}).*? title=""(.*?)\((Production|Release|Development|Concept|Wrap)\).*?Release Date: (.*?)"">([0-9A-Z]{3,6})").Groups[1].Value; dataValue.Add(stockUrl); //dataValue[0] string stockTitle = Regex.Match(tdMatch.Value, @"([0-9A-Z]{3,6}) (http://www.barakasoft.com/vb/</span><span class=)").Groups[2].Value; dataValue.Add(stockTitle); //dataValue[1] string stockPhase = Regex.Match(tdMatch.Value, @"([0-9A-Z]{3,6}) (http://www.barakasoft.com/vb/</span><span class=)").Groups[3].Value; dataValue.Add(stockPhase); //dataValue[2] string releaseDate = Regex.Match(tdMatch.Value, @"([0-9A-Z]{3,6}) (http://www.barakasoft.com/vb/</span><span class=)").Groups[4].Value; dataValue.Add(releaseDate); //dataValue[3] string stockSymbol = Regex.Match(tdMatch.Value, @"([0-9A-Z]{3,6}) (http://www.barakasoft.com/vb/</span><span class=)").Groups[5].Value; dataValue.Add(stockSymbol); //dataValue[4] } else { string stockUrl = Regex.Match(tdMatch.Value, @"([0-9A-Z]{3,6}) (http://www.barakasoft.com/vb/</span><span class=)").Groups[1].Value; dataValue.Add(stockUrl); //dataValue[0] string stockTitle = Regex.Match(tdMatch.Value, @"([0-9A-Z]{3,6}) (http://www.barakasoft.com/vb/</span><span class=)").Groups[2].Value; dataValue.Add(stockTitle); //dataValue[1] string stockPhase = ""; dataValue.Add(stockPhase); //dataValue[2] string releaseDate = ""; dataValue.Add(releaseDate); //dataValue[3] string stockSymbol = Regex.Match(tdMatch.Value, @"([0-9A-Z]{3,6}) (http://www.barakasoft.com/vb/</span><span class=)").Groups[3].Value; dataValue.Add(stockSymbol); //dataValue[4] } } else if (tdMatch.Value.IndexOf("class=\"right\" sorttable_customkey") != -1) { //Capture AvgPaid, Current Price, Todays Dollar Change, Todays Percent Change, Total Value, Total Percent Change from AvgPaid //dataValue[7] - [11] string dollarValue = Regex.Match(tdMatch.Value, @"([0-9,]{1,})").Groups[1].Value; if(stockShares.IndexOf(',') != -1) { stockShares = stockShares.Replace(",", ""); } dataValue.Add(stockShares); } else if(tdMatch.Value.IndexOf("class=\"trade_button_cell right\"") != -1) { //We don't need the trade button continue; } else { //dataValue[6] string stockHeld = Regex.Match(tdMatch.Value, @"<span class="code-string">