/// <summary> /// Method checks if the value input for monthly sales is a valid number. If is is the number /// is converted to a dollar figure else a message is shown that the information /// is not valid and the field is cleared. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void TotalMonthlySalesTextBox_Leave(object sender, EventArgs e) { double TotalSales; if (TotalMonthlySalesTextBox.Text != "") { try { TotalSales = Convert.ToDouble(TotalMonthlySalesTextBox.Text); TotalMonthlySalesTextBox.Text = TotalSales.ToString("C2"); } catch (Exception exception) { try { TotalSales = double.Parse(TotalMonthlySalesTextBox.Text, NumberStyles.Currency); TotalMonthlySalesTextBox.Text = TotalSales.ToString("C2"); } catch { MessageBox.Show("Invalid Data Entered", "Input Error"); Debug.WriteLine(exception.Message); TotalMonthlySalesTextBox.Focus(); TotalMonthlySalesTextBox.Text = ""; TotalMonthlySalesTextBox.SelectAll(); } } } }
/// <summary> /// reads in present sales report, if exists, then updates /// sales report dictionary values, adding previous total sales /// to total sales after each user transaction /// finally, writes new updated values to the sales report /// by overwriting existing data /// </summary> private void GenerateSalesReport() { if (File.Exists(_reportPath)) { using (StreamReader sr = new StreamReader(_reportPath)) { while (!sr.EndOfStream) { string line = sr.ReadLine(); if (line.Contains("$")) { string[] totalLine = line.Split('$'); TotalSales += decimal.Parse(totalLine[1]); } else if (line != "") { string[] dictString = line.Split('|'); string itemName = dictString[0]; int lastQtySold = int.Parse(dictString[1]); foreach (Item item in _itemList) { if (item.Name == itemName) { _salesReport[itemName] = lastQtySold + item.QtySold; } } } } } using (StreamWriter sw = new StreamWriter(_reportPath, false)) { foreach (KeyValuePair <string, int> entry in _salesReport) { sw.WriteLine($"{entry.Key}|{entry.Value}"); } sw.WriteLine(); sw.WriteLine($"**TOTAL SALES** {TotalSales.ToString("C")}"); TotalSales = 0; } } else { using (StreamWriter sw = new StreamWriter(_reportPath, false)) { foreach (KeyValuePair <string, int> line in _salesReport) { string key = line.Key; int qtySold = 0; foreach (Item item in _itemList) { if (item.Name == key) { qtySold = item.QtySold; } } sw.WriteLine($"{line.Key}|{line.Value + qtySold}"); } sw.WriteLine(); sw.WriteLine($"**TOTAL SALES** {TotalSales.ToString("C")}"); } } }