Application is working, register and calculate
This commit is contained in:
101
RepositoryPattern/frmMyStocks.cs
Normal file
101
RepositoryPattern/frmMyStocks.cs
Normal file
@ -0,0 +1,101 @@
|
||||
using StockDal.Interface;
|
||||
using StockDomain;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace RepositoryPattern
|
||||
{
|
||||
public partial class frmMyStocks : Form
|
||||
{
|
||||
Color hdrColor;
|
||||
private readonly IProductRepository _productRepository;
|
||||
private readonly IStockMemberRepository _stockMemberRepository;
|
||||
private readonly IStockMarketRepository _stockMarketRepository;
|
||||
public decimal BoughtSum { get; set; }
|
||||
public decimal TotalDiff { get; set; }
|
||||
public decimal CurrentSum { get; set; }
|
||||
|
||||
public Dictionary<string, DiTraderStockRow> Stocks { get; set; }
|
||||
public IEnumerable<StockMember> CurrentStocks { get; set; }
|
||||
|
||||
public frmMyStocks(IProductRepository productRepository, IStockMemberRepository stockMemberRepository, IStockMarketRepository stockMarketRepository)
|
||||
{
|
||||
hdrColor = Color.CadetBlue;
|
||||
InitializeComponent();
|
||||
_productRepository = productRepository;
|
||||
_stockMemberRepository = stockMemberRepository;
|
||||
_stockMarketRepository = stockMarketRepository;
|
||||
}
|
||||
|
||||
|
||||
private void ReloadData()
|
||||
{
|
||||
CurrentStocks = _stockMemberRepository.GetStocks();
|
||||
foreach(var stock in CurrentStocks)
|
||||
{
|
||||
stock.ActValue = Stocks[stock.StockId.Trim()].LatestPrice;
|
||||
stock.ActDate = DateTime.Today;
|
||||
_stockMemberRepository.UpdateActPrice(stock.Id, stock.ActValue);
|
||||
AddItemToListView(stock);
|
||||
}
|
||||
|
||||
txtBuyTotal.Text = BoughtSum.ToString();
|
||||
txtBuyTotal.Refresh();
|
||||
txtCurrValue.Text = CurrentSum.ToString();
|
||||
txtCurrValue.Refresh();
|
||||
txtTotDiff.Text = TotalDiff.ToString();
|
||||
txtTotDiff.Refresh();
|
||||
}
|
||||
|
||||
private void lvMyStocks_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
|
||||
{
|
||||
using (Brush hBr = new SolidBrush(hdrColor))
|
||||
{
|
||||
e.Graphics.FillRectangle(hBr, e.Bounds);
|
||||
e.DrawText();
|
||||
}
|
||||
}
|
||||
|
||||
private void AddItemToListView(StockMember currStock)
|
||||
{
|
||||
var lv = lvMyStocks.Items.Add(currStock.StockId);
|
||||
lv.SubItems.Add(currStock.BuyValue.ToString());
|
||||
lv.SubItems.Add(currStock.PostAmount.ToString());
|
||||
lv.SubItems.Add(currStock.BuyDate.ToString());
|
||||
var buyValue = currStock.PostAmount * currStock.BuyValue;
|
||||
var actValue = currStock.PostAmount * currStock.ActValue;
|
||||
var diffValue = actValue - buyValue;
|
||||
var diffproc = diffValue / buyValue * 100;
|
||||
BoughtSum += buyValue;
|
||||
TotalDiff += diffValue;
|
||||
CurrentSum += actValue;
|
||||
var lvs = lv.SubItems.Add(diffValue.ToString());
|
||||
lv.SubItems.Add(Math.Round(diffproc,2).ToString());
|
||||
lv.SubItems.Add(currStock.ActValue.ToString());
|
||||
lv.SubItems.Add(currStock.ActDate.ToString());
|
||||
lv.SubItems.Add(actValue.ToString());
|
||||
var owned = (DateTime.Today - currStock.BuyDate).TotalDays;
|
||||
lv.SubItems.Add(owned.ToString());
|
||||
if (diffValue < 0)
|
||||
{
|
||||
lv.BackColor = Color.Red;
|
||||
}
|
||||
else
|
||||
{
|
||||
lv.BackColor = Color.Green;
|
||||
}
|
||||
}
|
||||
|
||||
private void frmMyStocks_Shown(object sender, EventArgs e)
|
||||
{
|
||||
ReloadData();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user