Question

In: Computer Science

in visual C# Total Sales This is the design of the application. This image shows the...

in visual C#

Total Sales

This is the design of the application.

This image shows the application’s form when it starts running and processes the data in the file:

You are tasked with creating an application that reads a file’s contents (File: Sales) into an array, displays the array’s contents in a ListBox control (Chapter 4), and calculates and displays the total of the array’s values. You will use a loop (Chapter 5) to read in the data from the file (Chapter 5) and store the data into an array (Chapter 7). Place the code in the Load event so that it will process the data and calculate sales once the application starts.

Data in Sales.txt File

1245.67

1189.55

1098.72

1456.88

2109.34

1987.55

1872.36

NOTE: You can access the Form Load event by double-clicking on the gray area of your form, where there is no listbox or label control (also found on p. 330 in your textbook). Save the Sales.txt file in the /bin/Debug folder so that you can simply write "Sales.txt" for the file name when reading in the file. Once the file is stored in this folder, you won't have to include the file path for the application to find the file. Refer to Video: Ch5 Practice - Read to and Write From Files (34 min) as it covers how to work with files. Refer to p. 410-413 in your textbook, section 7.3 "Working with Files and Arrays" as it covers the code that you need to adapt for this assignment. Refer to p. 418 in your textbook which shows how to display the array's contents in a listbox - lines 16-20.Refer to p. 424 in your textbook shows how to total values in an array - lines 1-15. You could also total the values as you add them to the listbox.

Points Task
10 An array is created using a constant to store the size of the array.
10 The data file is opened and closed properly using StreamReader.
10 The data is read into the application from the file and stored into an array using a while loop.

Solutions

Expert Solution

//C# Code

//Form Interface

//Form1.Designer.cs

namespace sales_report
{
    partial class Form1
    {
        /// <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.label1 = new System.Windows.Forms.Label();
            this.lstSales = new System.Windows.Forms.ListBox();
            this.lblTotal = new System.Windows.Forms.Label();
            this.btnTotalSales = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.Location = new System.Drawing.Point(44, 27);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(87, 17);
            this.label1.TabIndex = 0;
            this.label1.Text = "Sales Data";
            // 
            // lstSales
            // 
            this.lstSales.FormattingEnabled = true;
            this.lstSales.Location = new System.Drawing.Point(43, 47);
            this.lstSales.Name = "lstSales";
            this.lstSales.Size = new System.Drawing.Size(99, 121);
            this.lstSales.TabIndex = 1;
            // 
            // lblTotal
            // 
            this.lblTotal.AutoSize = true;
            this.lblTotal.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.lblTotal.Location = new System.Drawing.Point(30, 215);
            this.lblTotal.MinimumSize = new System.Drawing.Size(150, 30);
            this.lblTotal.Name = "lblTotal";
            this.lblTotal.Size = new System.Drawing.Size(150, 30);
            this.lblTotal.TabIndex = 2;
            this.lblTotal.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // btnTotalSales
            // 
            this.btnTotalSales.Location = new System.Drawing.Point(43, 174);
            this.btnTotalSales.Name = "btnTotalSales";
            this.btnTotalSales.Size = new System.Drawing.Size(99, 38);
            this.btnTotalSales.TabIndex = 3;
            this.btnTotalSales.Text = "Get Total Sales";
            this.btnTotalSales.UseVisualStyleBackColor = true;
            this.btnTotalSales.Click += new System.EventHandler(this.btnTotalSales_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(258, 266);
            this.Controls.Add(this.btnTotalSales);
            this.Controls.Add(this.lblTotal);
            this.Controls.Add(this.lstSales);
            this.Controls.Add(this.label1);
            this.Name = "Form1";
            this.Text = "Sales Report";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.ListBox lstSales;
        protected System.Windows.Forms.Label lblTotal;
        private System.Windows.Forms.Button btnTotalSales;
    }
}

//Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sales_report
{
    public partial class Form1 : Form
    {
        //Constant size of array
        const int SIZE = 100;
        //Create array of max size
        double[] sales = new double[SIZE];
        //Count number of sales to be added
        int counter = 0;
        //get Total sales
        double salesTotal = 0;
        public Form1()
        {
            InitializeComponent();
        }

        //Button Click event
        private void btnTotalSales_Click(object sender, EventArgs e)
        {
            lblTotal.Text = "Total Sales: " + salesTotal;
        }

        //Form load event
        private void Form1_Load(object sender, EventArgs e)
        {
            //Open file to read
            try
            {
                StreamReader reader = new StreamReader("Sales.txt");
                //read file content using while loop
                while(!reader.EndOfStream)
                {
                    sales[counter] = double.Parse(reader.ReadLine());
                    counter++;
                }
                //close stream
                reader.Close();
                //Add data to listBox
                for(int i = 0; i < counter; i++)
                {
                    lstSales.Items.Add(sales[i]);
                    //Calculate total
                    salesTotal += sales[i];
                }

            }
            catch(FileNotFoundException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

//Data File

//Output

//If you need any help regarding this solution....... please leave a comment...... thanks


Related Solutions

Must be in Visual C# using windows forms : Create an application that opens a specified...
Must be in Visual C# using windows forms : Create an application that opens a specified text file and then displays a list of all the unique words found in the file. Hint: Use a LINQ method to remove all duplicate words.
You are tasked to design an application to keep track of sales for your company. Sales...
You are tasked to design an application to keep track of sales for your company. Sales should be tracked for two types of accounts: supplies and services. Complete the following:     Create a UML class diagram for the Account inheritance hierarchy. Your subclasses should be Supplies and Services.     All sales accounts will have an account ID (accountId).     You will need attributes to keep track of the number of hours (numberOfHours) and rate per hour of services provided (ratePerHour)....
C# Programming language!!! Using visual studios if possible!! PrimeHealth Suite You will create an application that...
C# Programming language!!! Using visual studios if possible!! PrimeHealth Suite You will create an application that serves as a healthcare billing management system. This application is a multiform project (Chapter 9) with three buttons. The "All Accounts" button allows the user to see all the account information for each patient which is stored in an array of class type objects (Chapter 9, section 9.4).The "Charge Service" button calls the Debit method which charges the selected service to the patient's account...
C++ program that runs on Visual Basic that computes the total cost of books you want...
C++ program that runs on Visual Basic that computes the total cost of books you want to order from an online bookstore. It does so by first asking how many books are in your shopping cart and then based on that number, it repeatedly asks you to enter the cost of each item. It then calls two functions: one for computing the taxes and another for computing the delivery charges. The function which computes delivery charges works as follows: It...
How is an image organized in the lateral geniculate nucleus, the primary visual cortex?
How is an image organized in the lateral geniculate nucleus, the primary visual cortex?
Is the visual appearance of an image a reliable indicator of overexposure? Why/why not ? How...
Is the visual appearance of an image a reliable indicator of overexposure? Why/why not ? How could this negatively affect the patient? -How images suffer from a loss of diagnostic quality due to excessive exposure? Explain. (Radiologic question)
In C# Create a GUI application that calculates and displays the total travel expenses of a...
In C# Create a GUI application that calculates and displays the total travel expenses of a business person on a trip. Here is the information that the user must provide: Number of days on the trip Amount of airfare, if any Amount of car rental fees, if any Number of miles driven, if a private vehicle was used Amount of parking fees, if any Amount of taxi charges, if any Conference or seminar registration fees, if any Lodging charges, per...
A, B:   Design and Implement a C# windows form application to ask the user for 10...
A, B:   Design and Implement a C# windows form application to ask the user for 10 integer numbers, sort them in ascending order and display the sorted list. Use bubble sort technique to sort the array elements and do not use any built-in sort method to sort the array elements.                                                        [02] C:    Test and evaluate your program by inputting variety of values.
A, B:    Design and Implement a C# windows form application to encrypt and decrypt text....
A, B:    Design and Implement a C# windows form application to encrypt and decrypt text. The application use to receive a string and display another encrypted string. The application also decrypt the encrypted string. The approach for encryption/decryption is simple one i.e. to encrypt we will add 1 to each character, so that "hello" would become "ifmmp", and to decrypt we would subtract 1 from each character.    C:   Test and evaluate application by applying different strings.      ...
Write a Windows Form application named SumFiveInts. Microsoft Visual C#: An Introduction to Object-Oriented Programming,7th Edition....
Write a Windows Form application named SumFiveInts. Microsoft Visual C#: An Introduction to Object-Oriented Programming,7th Edition. Ch. 5, page 220. Take snip of program results.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT