Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Math errors in C#

Tags:

c#

math

I am currently writing the code for a Shop program in C#. I am relatively new to C# and I am having difficulty in getting the math to work in the following piece of code:

//Add Basket
public void addBasket()
{
   //Add up the total of individual items
   double total = 0;
   if (shoppingCart.Count() == 0)
   {
      Console.WriteLine("ERROR - Basket is Empty");
   }
   else
   {
      foreach (Products tmp in shoppingCart)
      {
         total = (tmp.Price * tmp.BoughtStock);
         Console.WriteLine("The cost of the individual item  is: " + "\t" +total);
      }
   }
   //Calculate the products together
   double itemTotal = 0;
   if (shoppingCart.Count() == 0)
   {
      Console.WriteLine("ERROR - Basket is Empty");
   }
   else
   {
      foreach (Products tmp in shoppingCart)
      {
         itemTotal = (tmp.Price * tmp.BoughtStock);
         itemTotal = itemTotal + total;
         Console.WriteLine("The cost of the items together is: \t" +itemTotal);
      }
      //Calculate VAT 
      double vatPrice = total * .21;
      double netPriceBeforeDiscount = total + vatPrice;

      //calculate discount: if total cost of shop is over 25 give 10% discount.
      if (netPriceBeforeDiscount >= 25)
      {
         double reducedPrice = netPriceBeforeDiscount * .10;
         double netPrice = netPriceBeforeDiscount - reducedPrice;
         reducedPrice = Math.Round(reducedPrice, 2);
         netPrice = Math.Round(netPrice, 2);

         Console.WriteLine("Discount*:\t\t\t\t " + reducedPrice);
         Console.WriteLine("\nTotal Net Cost (including VAT and discounts):\t      Euro " + netPrice);
      }
      else
      {
         double netPrice = Math.Round(netPriceBeforeDiscount, 2);
      }
   }
}

The first part of the code works correctly, as in that it adds any products in the basket and displays there prices individually, the problem arises in the second part, adding the items in the baskets prices together. As you can see in the output http://gyazo.com/1656eecc689b7a9d0bfc47b8480169a6 (I have to link a screencap of the output as I don't know how to show output from C# on here) It displays the total of the first item, the second item and then correctly adds them two results together, although I don't know why it displays the cost of the second item multiplied by two. Finally as you may see at the bottom of the code, I have written what i believe to be a correct way of getting the VAT and displaying a bulk discount but from the link above when I use two items the code will not calculate or display the VAT or bulk discount yet with one item in the basket it will, see here > ( * Link Number 1 Below goes here *) . Again though ,from what I imagine is the error causing the other parts of the code to not work properly, when I do just one item despite calculating the vat and bulk discounts correctly and displaying the correct answer it multiplys the individual item cost by the amount of the product I bought, see here > ( * Link Number 2 Below goes here *)

As I said though I am new to this and well not exactly great at C# but any help would be greatly appreciated and if you require anything from me just ask, Thanks

Edit* : Just realised I need 10 reputation to post more than two links, I link the 2 missing links in the comments below.

like image 774
JasonL95 Avatar asked Apr 11 '26 12:04

JasonL95


1 Answers

foreach (Products tmp in shoppingCart)
        {
            total = (tmp.Price * tmp.BoughtStock);

You probably mean for it to be total +=, otherwise you are only keeping the last value.

like image 65
Rob G Avatar answered Apr 14 '26 07:04

Rob G



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!