Question

Murach's C# 2015 Training & Reference: I need help with both Exercise 4-1.

I figured out steps 1-4, but not understanding how to modify the code for 5 & 6.

Code is:

private void btnCalculate_Click(object sender, EventArgs e)
       {
           decimal subtotal = Convert.ToDecimal(txtSubtotal.Text);
           decimal discountPercent = .25m;
           decimal discountAmount = subtotal * discountPercent;
           decimal invoiceTotal = subtotal - discountAmount;

           txtDiscountPercent.Text = discountPercent.ToString("p1");
           txtDiscountAmount.Text = discountAmount.ToString("c");
           txtTotal.Text = invoiceTotal.ToString("c");

           txtSubtotal.Focus();
       }

5. Modify the first statement in the btnCalculate_Click method so it uses the Parse method of the Decimal class instead of the ToDecimal method of the Convert Class. Then, test the application to verify that it still works the same.

6. Round the values that are stored in the discountAmount and invoiceTotal variables to two decimal places, and delete the formatting codes for the statements that covert these variables to strings. Then, test the application to make sure that only two decimal places are displayed for the discount amount and total.

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

5.

private void btnCalculate_Click(object sender, EventArgs e)
       {
           decimal subtotal = Decimal.Parse(txtSubtotal.Text);
           decimal discountPercent = .25m;
           decimal discountAmount = subtotal * discountPercent;
           decimal invoiceTotal = subtotal - discountAmount;

           txtDiscountPercent.Text = discountPercent.ToString("p1");
           txtDiscountAmount.Text = discountAmount.ToString("c");
           txtTotal.Text = invoiceTotal.ToString("c");

           txtSubtotal.Focus();
       }

6.

private void btnCalculate_Click(object sender, EventArgs e)
       {
           decimal subtotal = Decimal.Parse(txtSubtotal.Text);
           decimal discountPercent = .25m;
           decimal discountAmount = Math.Round((subtotal * discountPercent),2);
           decimal invoiceTotal = Math.Round((subtotal - discountAmount),2);

           txtDiscountPercent.Text = discountPercent.ToString();
           txtDiscountAmount.Text = discountAmount.ToString();
           txtTotal.Text = invoiceTotal.ToString();

           txtSubtotal.Focus();
       }

Do ask if any doubt. Please upvote.

Add a comment
Know the answer?
Add Answer to:
Murach's C# 2015 Training & Reference: I need help with both Exercise 4-1. I figured out...
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
  • 6-1 Test and debug the Invoice Application i need help with this please.. all help is...

    6-1 Test and debug the Invoice Application i need help with this please.. all help is appreciated Source code Java: import java.util.Scanner; import java.text.NumberFormat; public class InvoiceApp { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String choice = "y"; while (!choice.equalsIgnoreCase("n")) { // get the input from the user System.out.print("Enter customer type (r/c): "); String customerType = sc.next(); System.out.print("Enter subtotal: "); double subtotal = sc.nextDouble(); // get the discount percent double discountPercent = 0.0; switch(customerType) {...

  • Visual Basic 2015: Extra 18-1 Use inheritance with the Inventory Maintenance Application Source Code: 1. frmNewItem.vb...

    Visual Basic 2015: Extra 18-1 Use inheritance with the Inventory Maintenance Application Source Code: 1. frmNewItem.vb Public Class frmNewItem Public InvItem As InvItem Private Sub frmNewItem_Load(sender As Object, e As EventArgs) Handles MyBase.Load Me.LoadComboBox() End Sub Private Sub LoadComboBox() cboSizeOrManufacturer.Items.Clear() If rdoPlant.Checked Then cboSizeOrManufacturer.Items.Add("1 gallon") cboSizeOrManufacturer.Items.Add("5 gallon") cboSizeOrManufacturer.Items.Add("15 gallon") cboSizeOrManufacturer.Items.Add("24-inch box") cboSizeOrManufacturer.Items.Add("36-inch box") Else cboSizeOrManufacturer.Items.Add("Bayer") cboSizeOrManufacturer.Items.Add("Jobe's") cboSizeOrManufacturer.Items.Add("Ortho") cboSizeOrManufacturer.Items.Add("Roundup") cboSizeOrManufacturer.Items.Add("Scotts") End If End Sub Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click If IsValidData() Then InvItem = New InvItem(CInt(txtItemNo.Text),...

  • C# - Inheritance exercise I could not firgure out why my code was not working. I...

    C# - Inheritance exercise I could not firgure out why my code was not working. I was hoping someone could do it so i can see where i went wrong. STEP 1: Start a new C# Console Application project and rename its main class Program to ZooPark. Along with the ZooPark class, you need to create an Animal class. The ZooPark class is where you will create the animal objects and print out the details to the console. Add the...

  • Posting this again because day limit has run out. Again I really need help with this....

    Posting this again because day limit has run out. Again I really need help with this. This is for my Advanced Java Programming class. The book we use is Murach's Java Servlet's and JSP 3rd Edition. The program used is NetBeans IDE 8.2. I need help modifying or adding some code. I will post the code I was told to open that needs to be modified below. Exercise 9-3     Use JSTL to add a table to the Future Value application. In...

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
Active Questions
ADVERTISEMENT