Reputation: 25
https://i.sstatic.net/COOqs.png
Total Sales
Use the attached file named Sales.txt. Create an application that
reads the file’s content into an array of double or decimal displays the array’s content in a ListBox control, calculates the total of the array’s values, average sales, largest sales, smallest sales Display the Total Sales, Average sales, Highest Sales and Smallest Sales Form should look similar to the following:
How do I get the data to display the Total/Average/High/Low Sales part of the image to display properly by typing the corresponding code? I'd like to do this on my own so if you could provide an example that might relate to what I am doing that would really help. Here's what I've been able to type up so far:
namespace Total_Sales
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void displayButton_Click(object sender, EventArgs e)
{
//declaring array
const int SIZE = 100;
decimal[] sales = new decimal[SIZE];
//varible to hold amount stored in array
int count = 0;
decimal additionHolder = 0;
//declaring streamreader
StreamReader inputFile;
//opening the sales file
inputFile = File.OpenText("../../Sales.txt");
try
{
//pull contents from file into array while there is still items
//to pull and the array isnt full
while (!inputFile.EndOfStream && count < sales.Length)
{
sales[count] = decimal.Parse(inputFile.ReadLine());
count++;
}
//close the file
inputFile.Close();
//display contents in listbox
for (int index = 0; index < count; index++)
{
ListBox.Items.Add(sales[index]);
}
//add all the values
for (int index = 0; index < sales.Length; index++)
{
additionHolder += sales[index];
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Upvotes: 3
Views: 264
Reputation: 247018
You can get all the lines from a file using
var lines = System.IO.File.ReadAllLines("../../Sales.txt");
You can use Linq Select to project and parse the array of strings into to an array of decimals
decimal[] sales = lines.Select(line => decimal.Parse(line)).ToArray();
From there you can iterate the array and add them to the list box.
To find Total/Average/High/Low of an array of decimals, you can use linq extensions again.
var total = sales.Sum();
var average = sales.Average();
var high = sales.Max();
var low = sales.Min();
That should provide you with the data to display. Hope this helps.
Upvotes: 1