1
\$\begingroup\$

I am posting this here because it was suggested that it is the best place by a seasoned user.

This is a project I already submitted, but I never received feedback. All my upcoming assignments will be based on this, so I wanted to make sure it is correct. I got this program to work as far as calculating the information, but I was hoping someone could please let me know if it meets the parameters/requirements?

I did attempt it. It works and adds the items exactly how the example showed in the video. However, I wanted to make sure my code is solid, and not just a mishmash or working because I got lucky, and if there is a better more solid way to do this I want to make sure I can. I do want feedback so I can learn and get better and I am trying my hardest. I always right out all the code I see here and try it and learn what it does piece by piece, because it helps me learn and improve. I also want to make sure it is going to work once I start the code for the next half of the requirements.

**Write a Console program, let users to enter prices calculate subtotal, tax, and total. - Delcare variable data type as decimal (6 variables needed) decimal apple;

Conver string to decimal from input apple = Convert.ToDecimal(Console.ReadLine()); Do addition for subtotal Multiplication for tax (0.065M) --- M or m stands for money value, read page 94. Addition for total You need to show only two digits after decimal point The class example:

`Console.WriteLine(" Subtotal: ${0}", String.Format("{0:0.00}", subTotal)); blank line: Console.WriteLine("") Console.Write(" Apple: $"); apple = Convert.ToDecimal(Console.ReadLine());`

My code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CalculatorRun
{
    class Calculator
    {
        static void Main(string[] args)
        {
            decimal FirstOperand, SecondOperand, Result, result;
            Console.Write("Addition Calculation");
            Console.Write("      \n\n");
            Console.Write("   Enter first operand:       ");
            FirstOperand = Convert.ToDecimal(Console.ReadLine());
            Console.Write("   Enter second operand:      ");
            SecondOperand = Convert.ToDecimal(Console.ReadLine());
            Console.Write("-------------------------\n\n");
            Result = FirstOperand + SecondOperand;
            result = Convert.ToDecimal(Result);
            Console.WriteLine("Addition Result: {0}", string.Format("{0}", result));
            Console.WriteLine("Press any key to continue.....");
            Console.ReadLine();
        }
    }
}

The next part of the assignment is this, which is what I need to make sure my code is correct so I can build towards:

Delcare variable data type as decimal (5 variables needed) count, price, subtotal, tax, total

Write a do while loop, if the price is not -1, loop continues show count number enter price get subtotal increase count

Calculate tax, total

Display total count, subtotal, tax, total

Feedback I received I am working on: What happens if the user enters an invalid number for either operand? (Thanks yo PaulF. I am not quit sure what this means, I think it means if someone enters a number such as -1. However, I will have to research a fix as this is a bit outside my realm).

This line is uncessary: result = Convert.ToDecimal(Result); because Result is already a decimal. You don't need two of them... (Thanks to Rufus L. I have to look into this and understand decimals more.)

This is the book I am using, and what I have to match so if some code seems oddly specific or old, that is why. https://cscnt.savannahstate.edu/StudentFiles/Data_Structure/Visual%20C%23%202012%20How%20to%20Program.pdf

\$\endgroup\$
1
\$\begingroup\$

This isn't bad for a beginner, but there is much room for improvement.

Organization

The app is small enough that everything can fit inside Main, but a more seasoned developer will have Main be more barebones, with calls to other methods.

You would also want to separate out the UI, that is the prompting for input values, from the other Logic, that is the addition and display of values.

Variable Declaration and Naming

The good news is that your variables have some meaning in their names. The bad news is you begin local variables with a capital letter (known as PascalCasing), when it should be camelCasing. Thus, FirstOperand should be firstOperand, etc.

Also, CodeReview prefer to see each variable declared on its own line. What I would expect you to end up with is something like:

decimal firstOperand;
decimal secondOperand;
decimal result;

Checking for Valid Input

You have no checks for valid input. What happens if someone enters "dog" instead of 123.45? You may want to read up on the Decimal.TryParse method to help out with that.

Things not needed

As mentioned, there is no need for Result and result since result is already a Decimal.

This line is too confusing:

Console.WriteLine("Addition Result: {0}", string.Format("{0}", result));

There is an overload for Console.WriteLine that accepts a string format. This would simplify the line to:

Console.WriteLine("Addition Result: {0}", result);

The link you provide is for C# 2012. Newer versions of C# support Interpolated Strings, prefaced with a $". So you could also do the above with:

Console.WriteLine($"Addition Result: {result}");

If you wanted to control formatting of the numbers, it could be done one of 2 ways:

Console.WriteLine($"Addition Result: {result.ToString("0.00")}");

Or

Console.WriteLine($"Addition Result: {result:0.00}");

More Reading

To learn more, I found a decent post here on CR 'Welcome to Buzzway Subs'. I recommend reading the original code, AND then follow up by reading the good advice in the answers.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.