Write a program that reads data from a text file, “SalesData.txt” which contains
ID: 3714389 • Letter: W
Question
Write a program that reads data from a text file, “SalesData.txt” which contains records of sales at a department store. Create a parallel array containing the names of the salespeople {Carl Anderson, Eric Shoemacher, Mary Ricca, Lisa Braxton, Tammy Cross}. Upon clicking the button “Get Sales”, the program should read the sales amounts into an array and display the Salesperson’s name followed by their sales data in a ListBox. Determine and display the highest sales, the lowest sales, and the average sales. SAMPLE OUTPUT: Sales Data Carl Anderson: $2,345.00 Highest Sales $6325.00 Eric Shoemacher: $5,425.00 Mary Ricca: $6,325.00 Lowest Sales $2,345.00 Lisa Braxton: $3,670.00 Tammy Cross: $4,350.00 Average Sales $4,423.00 code in c#
Explanation / Answer
Here is the code for the problem statement
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;
using System.IO;
namespace SalseData
{
public partial class Form1 : Form
{
string[] names;
double[] sales;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
double Highestsales = 0;
double lowestsale = 9999999999;
double averagesale = 0;
double totalSale = 0;
var list1 = new List<string>();
var list = new List<double>();
var fileStream = new FileStream(@"C:WorkInternalProjectsMyResearchSalseDataSalseDataSalesData.txt", FileMode.Open, FileAccess.Read);
using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
{
string line;
string[] name;
string[] stringSeparators = new string[] {":$"};
while ((line = streamReader.ReadLine()) != null)
{
name = line.Split(stringSeparators, 2, StringSplitOptions.None);
list1.Add(name[0]);
double sale = Double.Parse(name[1]);
if(sale > Highestsales)
Highestsales = sale;
if(lowestsale > sale)
lowestsale = sale;
totalSale += sale;
list.Add(sale);
}
}
sales = list.ToArray();
names = list1.ToArray();
//CreateParallelArrays(sales);
for (int i = 0; i < names.Length; i++)
{
listBox1.Items.Add(names[i] + ": $" + sales[i]);
}
listBox1.Items.Add("Highest Sales" + ": $" + Highestsales);
listBox1.Items.Add("Lowest Sales" + ": $" + lowestsale);
listBox1.Items.Add("Average Sales" + ": $" + totalSale / names.Length);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.