Application is working, register and calculate

This commit is contained in:
2021-02-02 16:25:10 +01:00
parent 3f737a8010
commit 8839d7dff3
9 changed files with 448 additions and 7 deletions

View File

@ -41,6 +41,7 @@ namespace RepositoryPattern
this.chbShowBrowser = new System.Windows.Forms.CheckBox();
this.gbStockMgmnt = new System.Windows.Forms.GroupBox();
this.btnStockReg = new System.Windows.Forms.Button();
this.btnValueView = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit();
this.gB1.SuspendLayout();
this.gbStockMgmnt.SuspendLayout();
@ -151,6 +152,7 @@ namespace RepositoryPattern
//
// gbStockMgmnt
//
this.gbStockMgmnt.Controls.Add(this.btnValueView);
this.gbStockMgmnt.Controls.Add(this.btnStockReg);
this.gbStockMgmnt.Location = new System.Drawing.Point(411, 411);
this.gbStockMgmnt.Name = "gbStockMgmnt";
@ -168,6 +170,16 @@ namespace RepositoryPattern
this.btnStockReg.UseVisualStyleBackColor = true;
this.btnStockReg.Click += new System.EventHandler(this.btnStockReg_Click);
//
// btnValueView
//
this.btnValueView.Location = new System.Drawing.Point(7, 39);
this.btnValueView.Name = "btnValueView";
this.btnValueView.Size = new System.Drawing.Size(95, 23);
this.btnValueView.TabIndex = 1;
this.btnValueView.Text = "See values";
this.btnValueView.UseVisualStyleBackColor = true;
this.btnValueView.Click += new System.EventHandler(this.btnValueView_Click);
//
// frmInitial
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
@ -210,6 +222,7 @@ namespace RepositoryPattern
private System.Windows.Forms.CheckBox chbShowBrowser;
private System.Windows.Forms.GroupBox gbStockMgmnt;
private System.Windows.Forms.Button btnStockReg;
private System.Windows.Forms.Button btnValueView;
}
}

View File

@ -13,11 +13,12 @@ namespace RepositoryPattern
{
public partial class frmInitial : Form
{
IProductRepository _productRepository;
private readonly IProductRepository _productRepository;
private readonly IStockMemberRepository _stockMemberRepository;
private readonly IStockMarketRepository _stockMarketRepository;
private frmRegisterStock regWindow;
private frmMyStocks stockWindow;
public frmInitial(IProductRepository productRepository, IStockMemberRepository stockMemberRepository, IStockMarketRepository stockMarketRepository)
{
@ -80,13 +81,28 @@ namespace RepositoryPattern
{
if (rdbStock.Checked)
{
Cursor.Current = Cursors.WaitCursor;
_stockMarketRepository.LoadStockMarketList();
regWindow = new frmRegisterStock();
regWindow.Stocks = _stockMarketRepository.StockMarketList;
Cursor.Current = DefaultCursor;
regWindow.ShowDialog();
_stockMemberRepository.InsertMany(regWindow.RegisteredStocks);
}
}
private void btnValueView_Click(object sender, EventArgs e)
{
if (rdbStock.Checked)
{
Cursor.Current = Cursors.WaitCursor;
_stockMarketRepository.LoadStockMarketList();
stockWindow = new frmMyStocks(_productRepository,_stockMemberRepository,_stockMarketRepository);
stockWindow.Stocks = _stockMarketRepository.StockMarketList;
Cursor.Current = DefaultCursor;
stockWindow.ShowDialog();
}
}
}
}

230
RepositoryPattern/frmMyStocks.Designer.cs generated Normal file
View File

@ -0,0 +1,230 @@

namespace RepositoryPattern
{
partial class frmMyStocks
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.lvMyStocks = new System.Windows.Forms.ListView();
this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
this.columnHeader4 = new System.Windows.Forms.ColumnHeader();
this.columnHeader5 = new System.Windows.Forms.ColumnHeader();
this.columnHeader6 = new System.Windows.Forms.ColumnHeader();
this.columnHeader7 = new System.Windows.Forms.ColumnHeader();
this.columnHeader8 = new System.Windows.Forms.ColumnHeader();
this.columnHeader9 = new System.Windows.Forms.ColumnHeader();
this.columnHeader10 = new System.Windows.Forms.ColumnHeader();
this.txtBuyTotal = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.txtTotDiff = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.txtCurrValue = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// lvMyStocks
//
this.lvMyStocks.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.lvMyStocks.BackColor = System.Drawing.SystemColors.InactiveCaption;
this.lvMyStocks.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader1,
this.columnHeader2,
this.columnHeader3,
this.columnHeader4,
this.columnHeader5,
this.columnHeader6,
this.columnHeader7,
this.columnHeader8,
this.columnHeader9,
this.columnHeader10});
this.lvMyStocks.GridLines = true;
this.lvMyStocks.HideSelection = false;
this.lvMyStocks.Location = new System.Drawing.Point(12, 32);
this.lvMyStocks.Name = "lvMyStocks";
this.lvMyStocks.Size = new System.Drawing.Size(911, 278);
this.lvMyStocks.TabIndex = 0;
this.lvMyStocks.UseCompatibleStateImageBehavior = false;
this.lvMyStocks.View = System.Windows.Forms.View.Details;
this.lvMyStocks.DrawColumnHeader += new System.Windows.Forms.DrawListViewColumnHeaderEventHandler(this.lvMyStocks_DrawColumnHeader);
//
// columnHeader1
//
this.columnHeader1.Name = "columnHeader1";
this.columnHeader1.Text = "Stock Name";
this.columnHeader1.Width = 80;
//
// columnHeader2
//
this.columnHeader2.Name = "columnHeader2";
this.columnHeader2.Text = "Buy Price";
this.columnHeader2.Width = 80;
//
// columnHeader3
//
this.columnHeader3.Name = "columnHeader3";
this.columnHeader3.Text = "Amount Stk";
this.columnHeader3.Width = 80;
//
// columnHeader4
//
this.columnHeader4.Name = "columnHeader4";
this.columnHeader4.Text = "Buy Date";
this.columnHeader4.Width = 80;
//
// columnHeader5
//
this.columnHeader5.Name = "columnHeader5";
this.columnHeader5.Text = "Value diff";
//
// columnHeader6
//
this.columnHeader6.Name = "columnHeader6";
this.columnHeader6.Text = "Value diff %";
this.columnHeader6.Width = 80;
//
// columnHeader7
//
this.columnHeader7.Name = "columnHeader7";
this.columnHeader7.Text = "Market price";
this.columnHeader7.Width = 80;
//
// columnHeader8
//
this.columnHeader8.Name = "columnHeader8";
this.columnHeader8.Text = "Market Date";
this.columnHeader8.Width = 80;
//
// columnHeader9
//
this.columnHeader9.Name = "columnHeader9";
this.columnHeader9.Text = "Current value";
this.columnHeader9.Width = 80;
//
// columnHeader10
//
this.columnHeader10.Name = "columnHeader10";
this.columnHeader10.Text = "Owned days";
//
// txtBuyTotal
//
this.txtBuyTotal.Location = new System.Drawing.Point(163, 321);
this.txtBuyTotal.Name = "txtBuyTotal";
this.txtBuyTotal.ReadOnly = true;
this.txtBuyTotal.Size = new System.Drawing.Size(100, 23);
this.txtBuyTotal.TabIndex = 1;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("Segoe UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.label1.Location = new System.Drawing.Point(56, 319);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(101, 21);
this.label1.TabIndex = 2;
this.label1.Text = "Bought Totalt";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Font = new System.Drawing.Font("Segoe UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.label2.Location = new System.Drawing.Point(269, 319);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(55, 21);
this.label2.TabIndex = 4;
this.label2.Text = "TotDiff";
//
// txtTotDiff
//
this.txtTotDiff.Location = new System.Drawing.Point(330, 321);
this.txtTotDiff.Name = "txtTotDiff";
this.txtTotDiff.ReadOnly = true;
this.txtTotDiff.Size = new System.Drawing.Size(100, 23);
this.txtTotDiff.TabIndex = 3;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Font = new System.Drawing.Font("Segoe UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.label3.Location = new System.Drawing.Point(509, 319);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(105, 21);
this.label3.TabIndex = 6;
this.label3.Text = "Current Value";
//
// txtCurrValue
//
this.txtCurrValue.Location = new System.Drawing.Point(620, 321);
this.txtCurrValue.Name = "txtCurrValue";
this.txtCurrValue.ReadOnly = true;
this.txtCurrValue.Size = new System.Drawing.Size(100, 23);
this.txtCurrValue.TabIndex = 5;
//
// frmMyStocks
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(935, 481);
this.Controls.Add(this.label3);
this.Controls.Add(this.txtCurrValue);
this.Controls.Add(this.label2);
this.Controls.Add(this.txtTotDiff);
this.Controls.Add(this.label1);
this.Controls.Add(this.txtBuyTotal);
this.Controls.Add(this.lvMyStocks);
this.Name = "frmMyStocks";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "frmMyStocks";
this.Shown += new System.EventHandler(this.frmMyStocks_Shown);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.ListView lvMyStocks;
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.ColumnHeader columnHeader2;
private System.Windows.Forms.ColumnHeader columnHeader3;
private System.Windows.Forms.ColumnHeader columnHeader4;
private System.Windows.Forms.ColumnHeader columnHeader5;
private System.Windows.Forms.ColumnHeader columnHeader6;
private System.Windows.Forms.ColumnHeader columnHeader7;
private System.Windows.Forms.ColumnHeader columnHeader8;
private System.Windows.Forms.ColumnHeader columnHeader9;
private System.Windows.Forms.ColumnHeader columnHeader10;
private System.Windows.Forms.TextBox txtBuyTotal;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtTotDiff;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox txtCurrValue;
}
}

View 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();
}
}
}

View File

@ -0,0 +1,60 @@
<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@ -259,6 +259,7 @@ namespace RepositoryPattern
this.Number,
this.Comment});
this.lwRegBuffer.GridLines = true;
this.lwRegBuffer.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
this.lwRegBuffer.HideSelection = false;
this.lwRegBuffer.Location = new System.Drawing.Point(39, 427);
this.lwRegBuffer.Name = "lwRegBuffer";
@ -266,6 +267,7 @@ namespace RepositoryPattern
this.lwRegBuffer.TabIndex = 24;
this.lwRegBuffer.UseCompatibleStateImageBehavior = false;
this.lwRegBuffer.View = System.Windows.Forms.View.Details;
this.lwRegBuffer.DrawColumnHeader += new System.Windows.Forms.DrawListViewColumnHeaderEventHandler(this.lwRegBuffer_DrawColumnHeader);
//
// Stock
//

View File

@ -13,7 +13,7 @@ namespace RepositoryPattern
{
public partial class frmRegisterStock : Form
{
Color hdr = Color.Red;
public Dictionary<string, DiTraderStockRow> Stocks { get; set; }
public List<StockMember> RegisteredStocks { get; set; } = new List<StockMember>();
@ -73,6 +73,7 @@ namespace RepositoryPattern
lv.SubItems.Add(currStock.BuyValue.ToString());
lv.SubItems.Add(currStock.PostAmount.ToString());
lv.SubItems.Add(currStock.Comment);
//lv.BackColor = Color.Aquamarine;
}
private void AddValidateData()
@ -83,7 +84,7 @@ namespace RepositoryPattern
currentStock.BuyValue = decimal.Parse(string.IsNullOrEmpty(txtBuyPrice.Text) ? "0" : txtBuyPrice.Text);
currentStock.PostAmount = long.Parse(string.IsNullOrEmpty(txtBoughtAmount.Text) ? "0" : txtBoughtAmount.Text);
currentStock.ActDate = DateTime.Parse(txtActDate.Text);
currentStock.BuyDate = DateTime.Today;
currentStock.BuyDate = string.IsNullOrWhiteSpace(txtBuyDate.Text) ? DateTime.Today : DateTime.Parse(txtBuyDate.Text);
currentStock.ActValue = decimal.Parse(string.IsNullOrEmpty(txtActValue.Text) ? "0" : txtActValue.Text);
currentStock.SoldDate = null; //DateTime.MaxValue;
currentStock.SoldValue = decimal.Parse("0");
@ -115,5 +116,14 @@ namespace RepositoryPattern
{
txtActAmount.Text = txtBoughtAmount.Text;
}
private void lwRegBuffer_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
using (Brush hBr = new SolidBrush(hdr))
{
e.Graphics.FillRectangle(hBr, e.Bounds);
e.DrawText();
}
}
}
}

View File

@ -8,7 +8,7 @@ namespace StockDal.Interface
{
IEnumerable<StockMember> GetStocks();
//bool Insert(StockMember stockMember);
bool Update(StockMember stockMember);
void UpdateActPrice(int Id, decimal price);
bool Delete(string stockMemberId);
void InsertMany(List<StockMember> stockMembers);
}

View File

@ -47,9 +47,18 @@ namespace StockDal
", stockMembers);
}
public bool Update(StockMember stockMember)
public void UpdateActPrice(int Id, decimal price)
{
throw new NotImplementedException();
using IDbConnection db = new SqlConnection(StockDBConnection.ConnectionString);
if (db.State == ConnectionState.Closed)
db.Open();
db.Execute(
@"UPDATE [dbo].[StockMember]
SET ActValue = @val,
ActDate = @date
WHERE Id = @id
", new { val = price, date = DateTime.Today, id = Id });
}
IEnumerable<StockMember> IStockMemberRepository.GetStocks()