In: Computer Science
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. |
//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