Question

Write a C# code for a Sales Calculator: Note: You cannot use the foreach loop in...

Write a C# code for a Sales Calculator:

Note: You cannot use the foreach loop in any part of this program!

sales.txt file: 1189.55, 1207.00, 1348.27, 1456.88, 1198.34, 1128.55, 1172.37, 1498.55, 1163.29

The application should have controls described as follows:

• A button that reads Get Sales. If the user clicks this button, the application should call a method named ProcesSalesFile. ProcessSalesFile Method: accepts no arguments and does not return anything The ProcessSalesFile Method will do the following: o Open the Sales.txt file o Read its contents into an array o Close the Sales.txt file o Displays the array’s contents in a list box control. o Calculate and display the total of the array’s values in an output label o Display an error message if the file cannot be opened o Variables must have appropriate data type and meaningful names o Exception handling must be included in this program: Use the try-catch

• A button that reads Clear. If the user clicks this button, the application should clear the list box and output label

• A button that reads Exit. If the user clicks this button, the application should close the program.

• A list box to display each sales value (one value per line) formatted to 2 decimal places.
• An output label that will display the total of the array’s values formatted to 2 decimal places.

• Additional label controls as needed for user prompts and information

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Dear Student ,

As per the requirement submitted above , kindly find the below solution.

Here a new Windows Forms Application in C# is created using Visual Studio 2017 with name "SalesCalculator".This application contains a form with name "Form1.cs".Below are the files associated with form1.

1.Form1.cs[Design]

2.Form1.Designer.cs

namespace SalesCalculator
{
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.btnGetSales = new System.Windows.Forms.Button();
this.btnClear = new System.Windows.Forms.Button();
this.lstSales = new System.Windows.Forms.ListBox();
this.lbltotal = new System.Windows.Forms.Label();
this.btnExit = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// btnGetSales
//
this.btnGetSales.Location = new System.Drawing.Point(80, 23);
this.btnGetSales.Name = "btnGetSales";
this.btnGetSales.Size = new System.Drawing.Size(115, 33);
this.btnGetSales.TabIndex = 0;
this.btnGetSales.Text = "Get Sales";
this.btnGetSales.UseVisualStyleBackColor = true;
this.btnGetSales.Click += new System.EventHandler(this.btnGetSales_Click);
//
// btnClear
//
this.btnClear.Location = new System.Drawing.Point(102, 279);
this.btnClear.Name = "btnClear";
this.btnClear.Size = new System.Drawing.Size(93, 34);
this.btnClear.TabIndex = 1;
this.btnClear.Text = "&Clear";
this.btnClear.UseVisualStyleBackColor = true;
this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
//
// lstSales
//
this.lstSales.FormattingEnabled = true;
this.lstSales.Location = new System.Drawing.Point(12, 77);
this.lstSales.Name = "lstSales";
this.lstSales.Size = new System.Drawing.Size(120, 173);
this.lstSales.TabIndex = 2;
//
// lbltotal
//
this.lbltotal.AutoSize = true;
this.lbltotal.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lbltotal.Location = new System.Drawing.Point(170, 137);
this.lbltotal.Name = "lbltotal";
this.lbltotal.Size = new System.Drawing.Size(0, 20);
this.lbltotal.TabIndex = 3;
//
// btnExit
//
this.btnExit.Location = new System.Drawing.Point(214, 279);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(70, 34);
this.btnExit.TabIndex = 4;
this.btnExit.Text = "E&xit";
this.btnExit.UseVisualStyleBackColor = true;
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(296, 325);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.lbltotal);
this.Controls.Add(this.lstSales);
this.Controls.Add(this.btnClear);
this.Controls.Add(this.btnGetSales);
this.Name = "Form1";
this.Text = "Sales Calculator";
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.Button btnGetSales;
private System.Windows.Forms.Button btnClear;
private System.Windows.Forms.ListBox lstSales;
private System.Windows.Forms.Label lbltotal;
private System.Windows.Forms.Button btnExit;
}
}

3.Form1.cs

//namespace
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;
//application namespace
namespace SalesCalculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//Sales button click
private void btnGetSales_Click(object sender, EventArgs e)
{
//try catch block
try
{
//calling method procee file
ProcesSalesFile();
}
//catch exception
catch (Exception ex)
{ //disply exception if any
MessageBox.Show(ex.Message);
}
}
//method to read the file
public void ProcesSalesFile()
{
//declaring array
decimal[] salesArray = new decimal[9];
//declaring variable
string line = "";
int i = 0;
decimal total =0;
//creating object of StramReader class
StreamReader streamReader = new StreamReader("sales.txt");
//while is used to read each line from the file
while((line=streamReader.ReadLine())!=null)
{
//add the line from the file into listbox
lstSales.Items.Add(line);
//parsing each line and add it into the array
salesArray[i] = decimal.Parse(line);
//calculating total of each sales item
total = total + salesArray[i];
//increment value of i
i++;
}
//display total on the lable
lbltotal.Text = total.ToString("0.00");
streamReader.Close();//close the file

}
//clear button click
private void btnClear_Click(object sender, EventArgs e)
{
//clear listbox and output label
lbltotal.Text = "";
lstSales.Items.Clear();
}
//Exit button click
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();//close application
}
}
}

======================================================

Output : Run application using F5 and will get the screen as shown below

Screen 1 :

Screen 2 :Click on button to get sales total

NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.

Add a comment
Know the answer?
Add Answer to:
Write a C# code for a Sales Calculator: Note: You cannot use the foreach loop in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C# code for a Sales Calculator: Note: You cannot use the foreach loop in any part of this...

    Write a C# code for a Sales Calculator: Note: You cannot use the foreach loop in any part of this program! sales.txt file: 1189.55, 1207.00, 1348.27, 1456.88, 1198.34, 1128.55, 1172.37, 1498.55, 1163.29 The application should have controls described as follows: • A button that reads Get Sales. If the user clicks this button, the application should call a method named ProcesSalesFile. ProcessSalesFile Method: accepts no arguments and does not return anything The ProcessSalesFile Method will do the following: o Open...

  • How do you do this using visual studio on a Mac? You are tasked with creating...

    How do you do this using visual studio on a Mac? You are tasked with creating an application with five PictureBox controls. In the Poker Large folder, you will find JPEG image files for a complete deck of poker cards. Feel free to choose any card image that you like or be creative with this application. Each PictureBox should display a different card from the set of images. When the user clicks any of the PictureBox controls, the name of...

  • *Use Java to create this program* For this assignment, you will be building a Favorite Songs...

    *Use Java to create this program* For this assignment, you will be building a Favorite Songs application. The application should have a list with the items displayed, a textbox for adding new items to the list, and four buttons: Add, Remove, Load, and Save. The Add button takes the contents of the text field (textbox) and adds the item in it to the list. Your code must trim the whitespace in front of or at the end of the input...

  • C# Visual Studio Problem You are given a file named USPopulation.txt. The file contains the midyear...

    C# Visual Studio Problem You are given a file named USPopulation.txt. The file contains the midyear population of the United States, in thousands, during the years 1950 through 1990. The first line in the file contains the population for 1950, the second line contains the population for 1951, and so forth. Create an application that reads the file’s contents into an array or a List. The application should display the following data (statistics) in read-only textboxes. The average population during...

  • Can you solve this proble by using C# application? C# is one of programming languages supported...

    Can you solve this proble by using C# application? C# is one of programming languages supported by Visual Studio 2015. It combines the features of Java and C ++ and is suitbale for rapid application development. A retail company must file a monthly sales tax report listing the total sales for the month, and the amount of state and county sales tax collected. The state sales tax rate is 4% and the county sales tax rate is 2%. Create an...

  • the code is in visual basic 376 Chapter 6 Loop Structures Case Programming Assignments con Most...

    the code is in visual basic 376 Chapter 6 Loop Structures Case Programming Assignments con Most Rainfall in the USA USE CASE DEFINITION e Windows application opens with the heading "Most Rainfall in US - Kauai," a ListBox object that displays the monthly rainfall amounts, an i amounts in inches mage, and a Button object that allows the user to begin entering their rainfall 2. A menu bar displays the File menu, which has two menu items: Clear and Exit....

  • NEED HELP DIRECTIONS: Notice that there is one input area and two buttons There is a...

    NEED HELP DIRECTIONS: Notice that there is one input area and two buttons There is a place in the HTML reserved for < li > elements under the heading “Shopping List” Write a javascript program that will cause whatever the user inputs to be placed in a newly -created < li > tag and the tag placed inside the < ul > element whenever the user clicks the button Your program must not create any < li > tag is...

  • answer in c# console application. File l/O Create a Person class (firstName, lastName, phoneNumber, gender (Use...

    answer in c# console application. File l/O Create a Person class (firstName, lastName, phoneNumber, gender (Use radioButtons for gender in the GUI) ) Create a List<Person> Create a GUI that will collect the Person information from the user and have 3 buttons: Add, Display, Read. Add button Add the just created person to List<Person>, and append to a text file Display button > print out all persons currently in the List<Person> in a label in a visually attractive way Read...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT