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

ONLY USE VISUAL STUDIO (NO JAVA CODING) VISUAL STUDIO -> C# -> CONSOLE APPLICATION In this...
ONLY USE VISUAL STUDIO (NO JAVA CODING) VISUAL STUDIO -> C# -> CONSOLE APPLICATION In this part of the assignment, you are required to create a C# Console Application project. The project name should be A3<FirstName><LastName>P2. For example, a student with first name John and Last name Smith would name the project A1JohnSmithP2. Write a C# (console) program to calculate the number of shipping labels that can be printed on a sheet of paper. This program will use a menu...
Must be in C# Create an application that determines the total due on purchases including sales...
Must be in C# Create an application that determines the total due on purchases including sales tax and shipping charges. Allow the user to input any number of item prices. Use a do-while loop such that in the first iteration, you ask the user to enter the price for an item. Keep a counter variable to track the number of items the user has entered by incrementing its value at every iteration. As the last statement for the do{} construct,...
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.
Using Visual Studio in C#; create a grading application for a class of ten students. The...
Using Visual Studio in C#; create a grading application for a class of ten students. The application should request the names of the students in the class. Students take three exams worth 100 points each in the class. The application should receive the grades for each student and calculate the student’s average exam grade. According to the average, the application should display the student’s name and the letter grade for the class using the grading scheme below. Grading Scheme: •...
answer the following using C# Design and program a Visual Studio Console project in C# that...
answer the following using C# Design and program a Visual Studio Console project in C# that allows your user to enter a number. The program will examine the number to see if it is prime. If it is prime, it will print the next higher prime and the next lower primes to the console. If the number entered by the user is not prime, display a message to that effect. All code should be written by you. Do not copy/paste...
Program is to be written in C++ using Visual studios Community You are to design a...
Program is to be written in C++ using Visual studios Community You are to design a system to keep track of either a CD or DVD/Blu-ray collection. The program will only work exclusively with either CDs or DVDs/Blu-rays since some of the data is different. Which item your program will work with will be up to you. Each CD/DVD/Blu-ray in the collection will be represented as a class, so there will be one class that is the CD/DVD/Blu-ray. The CD...
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?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT