A software company sells a package that retails for $99. Quantity discounts are
ID: 3672825 • Letter: A
Question
A software company sells a package that retails for $99. Quantity discounts are given according to the following table:
Quanty Discount
10-19 20%
20-49 30%
50-99 40%
100 or more 50%
Create an application that lets the user enter the number of packages purchased. The program should then display the amount of the discount (if any) and the total amount of the purchase after the discount.
I will need the Visual Studio window, along with the code in C#
Explanation / Answer
/* * Form2.cs [Design] contains the following controls * using System; using System.Windows.Forms; namespace EE.WinForms { public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void btnCalculate_Click(object sender, EventArgs e) { try { // initialise an int data type variable for the package quantity int packagesPurchased = 0; // attempt to parse the text input to an int // the out parameter returns 0 if unsuccessful int.TryParse(txtPackagesPurchased.Text, out packagesPurchased); // if more than 0 packages are purchased if (packagesPurchased > 0) { // caluclate the total without a discount decimal totalWithoutDiscount = 99 * packagesPurchased; // calculatge the discount percentage int discountPercentage = ReturnDiscount(packagesPurchased); // output the discount percentage txtDiscountPercentage.Text = string.Format("{0}%", discountPercentage); // calculate the total with discount // take the total without discount and multiply it by 0.n // discountPercentage needs to be cast to prevent the result being truncated decimal totalWithDiscount = totalWithoutDiscount * ((decimal) discountPercentage / 100); // output the total with discount txtTotalWithDiscount.Text = string.Format("{0:C}", totalWithDiscount); } // if there was an error with parsing the package quantity else { // provide some feedback MessageBox.Show("You need to purchase more than 0 packages", "Purchasing Error", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ex) { // exception handling } } private int ReturnDiscount(int packagesPurchased) { int discount = 0; // if statement break out at the first conditional that returns true // so testing your higher range first requires less typing as you just test for upper limits rather than bounds if (packagesPurchased >= 100) { discount = 50; } else if (packagesPurchased >= 50) { discount = 40; } else if (packagesPurchased >= 20) { discount = 30; } else if (packagesPurchased >= 10) { discount = 20; } return discount; } } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.