C#

C# IF ListBox Arrays 2D Arrays Read File Example 1 Example Midterm 1 Example Midterm 2 Example 5 Example 7 Group Project 2 Example 9 Group Project 2
O O

Weather Controls

Time Of Day
Rain
Wind Speed
Wind Direction
Clouds

C# : Example 7

2017-01-01

Double Array

http://robertpowers.net/ref/csharp/examples/assignment7.exe read .txt

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 assignment7
{
    public partial class Form1 : Form
    {
        const int arrCount = 7;
        double[] salesFiguresArr = new double[arrCount];

        public Form1()
        {
            InitializeComponent();
            readFileContent();
        }

        private void readFileContent()
        {

            try
            {
                StreamReader inputFile;
                inputFile = File.OpenText("Sales.txt");

                int index = 0;
                while (!inputFile.EndOfStream)
                {
                    double salesOut;
                    if (double.TryParse(inputFile.ReadLine(), out salesOut))
                    {
                        salesFiguresArr[index] = salesOut;
                    }
                    index++;
                }

                inputFile.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            populateListBox();
            print_total.Text = calculateTotal().ToString();
        }

        private void populateListBox()
        {
            foreach (double arrItem in salesFiguresArr)
            {
                lb_sales.Items.Add(arrItem);
            }
        }

        private void validate()
        {
            bool data_ok = false;
            double temp;

            foreach (double arrItem in salesFiguresArr)
            {
                 if (double.TryParse(arrItem.ToString(), out temp))
                 {
                    data_ok = true;
                 }
                 else
                {
                    data_ok = false;
                    break;
                }
            }

            if (data_ok)
            {
                analyzeSales();
            }
            else
            {
                MessageBox.Show("There is a problem with your data is Sales.txt");
            }
        }

        private void analyzeSales()
        {
            print_smallest.Text = calculateSmallest().ToString();
            print_average.Text = calculateAverage().ToString();
            print_largest.Text = calculateLargest().ToString();
            print_total.Text = calculateTotal().ToString();
        }

        private double calculateTotal()
        {
            double total = 0;

            foreach (double arrItem in salesFiguresArr)
            {
                total = total + arrItem;
            }
            return total;
        }

        private double calculateSmallest()
        {
            double smallest = 1000000;

            foreach (double arrItem in salesFiguresArr)
            {
                if (arrItem < smallest)
                {
                    smallest = arrItem;
                }             
            }

            return smallest;
        }

        private double calculateLargest()
        {

            double largest = 0;

            foreach (double arrItem in salesFiguresArr)
            {
                if (arrItem > largest)
                {
                    largest = arrItem;
                }
            }

            return largest;
        }

        private double calculateAverage()
        {
            double avg = calculateTotal() / arrCount;
            return System.Math.Round(avg, 2);
        }

        private void clearSales()
        {
            print_smallest.Text = "";
            print_average.Text = "";
            print_largest.Text = "";
            print_total.Text = "";
        }

        private void button_close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button_clear_Click(object sender, EventArgs e)
        {
            clearSales();
        }

        private void button_analyze_Click(object sender, EventArgs e)
        {
            validate();
        }
    }
}