Question
Please help me with this question. (This code I have here should be your start up file to make changes. This code is correct/ Please
Please help me with this question.
(This code I have here should be your start up file to make changes. This code is correct/ Please do not copy/paste the answer from other questions its not related to mine)
(Please mention where exactly in the code you made changes. You can use "//TODO" to point it out )
Thank you!
--->Modify your array declaration to be a rectangular array with three columns. The first column will represent a wholesale price, the second column with represent the markup percentage and the third column will represent the retail price.
Modify your source code to store the wholesale prices, markup percentages and retail prices in the rectangular array as each retail price is calculated.
Modify your GetAverage method to use the rectangular array when calculating the average.
---->Modify your source code to output the wholesale prices, markup percentages and retail prices in the Message Box. Each line of your display should consist of a wholesale price, markup percentage and retail price so that you end up with three columns of information displayed.
(Once you finished all of the above and tested it to make sure that it works properly, paste a copy of your C# source code showing your GetAverage method and your entire Calculate button and Exit button click event handlers. Also paste a screen print of the application running with the wholesale prices, markup percentages and retail prices displayed in the Message Box.)
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;
namespace Retail_Price_Calculator
{
public partial class Form1 : Form
{
// declare array to hold retail prices
private decimal[] retailPrices = new decimal[5];
// declare the array index
private int arrayIndex = 0;
public Form1()
{
InitializeComponent();
}
private void btnCalculate_Click(object sender, EventArgs e)
{
try
{
decimal wholesale, pMarkup;
if (IsValidData(txtWholesale, txtMarkup, out wholesale, out pMarkup))
{
this.CalculateRetail(wholesale, pMarkup);
this.GetAverage();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + " " +
ex.GetType().ToString() + " " +
ex.StackTrace, "Exception");
}
}
private void CalculateRetail(decimal wholesale, decimal pMarkup)
{
decimal markup = (pMarkup / 100);
decimal output = wholesale * (1 + markup);
txtOutput.Text = "$" + output.ToString("0.00");
// store retail price in array
for (int i = 0; i < retailPrices.Length; i++)
{
if (retailPrices[i] == 0)
{
retailPrices[i] = output;
arrayIndex++;
break;
}
}
}
// Adding the GetAverage method
private void GetAverage()
{
// calculate the average of all the elements in the array
decimal sum = 0;
// use arrayIndex to only add the elements that have values
for (int i = 0; i < arrayIndex; i++)
{
sum += retailPrices[i];
}
decimal average = sum / arrayIndex;
// display the average in the lblAverage label
lblAverage.Text = average.ToString("0.00");
}
private bool IsValidData(TextBox txtWholesale, TextBox txtMarkup, out decimal wholesale, out decimal pMarkup)
{
if (!IsPresent(txtWholesale, "Wholesale Cost") ||
!IsDecimal(txtWholesale, "Wholesale Cost") ||
!IsWithinRange(txtWholesale, "Wholesale Cost", 0, 100000) ||
!IsPresent(txtMarkup, "Markup Percentage") ||
!IsDecimal(txtMarkup, "Markup Percentage") ||
!IsWithinRange(txtMarkup, "Markup Percentage", 0, 100))
{
wholesale = 0m;
pMarkup = 0m;
return false;
}
wholesale = Convert.ToDecimal(txtWholesale.Text);
pMarkup = Convert.ToDecimal(txtMarkup.Text);
return true;
}
private bool IsPresent(TextBox textBox, string name)
{
if (textBox.Text == "")
{
MessageBox.Show(name + " is a required field.", "Entry Error");
textBox.Focus();
return false;
}
return true;
}
private bool IsDecimal(TextBox textBox, string name)
{
decimal number = 0m;
if (Decimal.TryParse(textBox.Text, out number))
{
return true;
}
else
{
MessageBox.Show(name + " must be a decimal number.", "Entry Error");
textBox.Focus();
return false;
}
}
private bool IsWithinRange(TextBox textBox, string name,
decimal min, decimal max)
{
decimal number = Convert.ToDecimal(textBox.Text);
if (number < min || number > max)
{
MessageBox.Show(name + " must be between " + min
+ " and " + max + ".", "Entry Error");
textBox.Focus();
return false;
}
return true;
}
private void btnExit_Click(object sender, EventArgs e)
{
// display contents of array in message box before closing
string output = "";
for (int i = 0; i < retailPrices.Length; i++)
{
if (retailPrices[i] != 0) ;
}
// Display the retail prices in a message box
string retailPricesText = "";
for (int i = 0; i < retailPrices.Length; i++)
{
if (retailPrices[i] != 0)
{
retailPricesText += $"Retail Price {i + 1}: ${retailPrices[i]:0.00} ";
}
}
MessageBox.Show(retailPricesText, "Retail Prices");
this.Close();
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started