Click here to Skip to main content
15,893,904 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to serialize this? Pin
Richard MacCutchan12-Aug-15 21:52
mveRichard MacCutchan12-Aug-15 21:52 
QuestionWPF TabControl content memory leak Pin
theonlyavenger12-Aug-15 11:17
theonlyavenger12-Aug-15 11:17 
AnswerRe: WPF TabControl content memory leak Pin
Pete O'Hanlon12-Aug-15 11:24
mvePete O'Hanlon12-Aug-15 11:24 
GeneralRe: WPF TabControl content memory leak Pin
theonlyavenger13-Aug-15 9:41
theonlyavenger13-Aug-15 9:41 
Questionprice calculator Pin
Member 1190452712-Aug-15 5:23
Member 1190452712-Aug-15 5:23 
AnswerRe: price calculator Pin
Eddy Vluggen12-Aug-15 5:31
professionalEddy Vluggen12-Aug-15 5:31 
GeneralRe: price calculator Pin
Member 1190452712-Aug-15 5:45
Member 1190452712-Aug-15 5:45 
GeneralRe: price calculator [warning, long post] Pin
Eddy Vluggen12-Aug-15 6:45
professionalEddy Vluggen12-Aug-15 6:45 
Ask and ye receive Laugh | :laugh:

The indentation is screwed up by the browser due to the formatting. I've cleaned it up and added it below in pre-tags;
C#
using System;

class PriceCalc
{
    static void Main(string[] args)
    {
        float costPrice = 0.0f;
        float profit;
        float tax;
        float totalCost = 0.0f;
        float totalProfit = 0.0f;
        float totalTax = 0.0f;
        bool finished = false;
        float salePrice;

        string priceString;
        string nameString;
        float taxRate = 0.2f;

        while (!finished)
        {
            Console.Write("Enter the name of the product : ");

            nameString = Console.ReadLine();
            Console.WriteLine("Product is " + nameString);
            if (nameString.Length != 0)
            {
                finished = true;
            }
            else
            {
                bool dataOK = false;
                while (!dataOK)
                {
                    Console.Write("Enter price of the product : ");
                    priceString = Console.ReadLine();
                    try
                    {
                        costPrice = float.Parse(priceString);
                        dataOK = true;
                    }
                    catch
                    {
                        Console.WriteLine("That is not a valid number");
                        Console.WriteLine("Enter a number, e.g. 15.5");
                    }
                }

                if (costPrice < 20.0f)
                {
                    profit = costPrice * 0.2f;
                }
                else
                {
                    if (costPrice < 40.0f)
                    {
                        profit = costPrice * 0.15f;
                    }
                    else
                    {
                        profit = costPrice * 0.1f;

                    }
                }
                tax = (costPrice + profit) * taxRate;
                totalTax = totalTax + tax;
                totalCost = totalCost + costPrice;
                totalProfit = totalProfit + profit;
                salePrice = costPrice + profit + tax;
            }
        }

        Console.ReadKey();
    }
}

At this moment, your entire program is a single method, and it would make it harder to reuse parts. If you would write a larger application like this, you'd end up with a lot of repeating and hard to maintain code.

The line "static void Main(string[] args)" is an example of a methods signature. It states the methods argument(s) and its name. Adding such a method to the code would give a different structure;
C#
class PriceCalc
{
    static int readInteger (string prompt) 
    {
        Console.WriteLine(prompt);
        // read an int or float from the console, preferably using TryParse.
        priceString = Console.ReadLine();
        return priceString;
    }

    static void Main(string[] args)
    {
        float costPrice = 0.0f;
        float profit;
        ..and so on

You'd then be able to call this new method from your main-method like below;
C#
// replace these in your main-method;
Console.Write("Enter price of the product : ");
priceString = Console.ReadLine();
// with this
  priceString = readInteger("Enter price of the product : ");
Now you have a new design-decision to make; will you implement the error-handling in the new method, or would you keep it in the main-method?

Some additional notes;
  • The signature states an "int", not a float. Might seem a minor detail, but in the case of calculating money, there's always a devil there.
  • I would go for Int.TryParse[^].
  • There's a small bug in there that causes the app to exit after entering a product-name. I'd recommend replacing the "nameString.Lengt != 0" with
    C#
    if (string.IsNullOrEmpty(nameString))
    

  • It would probably be helpfull to the end user if you also wrote the results to the screen before exiting Smile | :)


--edit;
Nearly forgot an important part, without which the code will not execute; you need to add the word "static" to the methods signature like below, or it won't work (updated the example above also);
C#
static int readInteger (string prompt) 
If you are not allowed to change the signature, then you'd additionally need to create a class, and create an object of that type before using it.
Bastard Programmer from Hell Suspicious | :suss:
If you can't read my code, try converting it here[^]

GeneralRe: price calculator [warning, long post] Pin
Member 1190452712-Aug-15 6:59
Member 1190452712-Aug-15 6:59 
GeneralRe: price calculator [warning, long post] Pin
Eddy Vluggen12-Aug-15 7:11
professionalEddy Vluggen12-Aug-15 7:11 
GeneralRe: price calculator [warning, long post] Pin
Matt T Heffron12-Aug-15 14:34
professionalMatt T Heffron12-Aug-15 14:34 
GeneralRe: price calculator [warning, long post] Pin
Eddy Vluggen12-Aug-15 20:05
professionalEddy Vluggen12-Aug-15 20:05 
GeneralRe: price calculator [warning, long post] Pin
Eddy Vluggen12-Aug-15 20:05
professionalEddy Vluggen12-Aug-15 20:05 
SuggestionRe: price calculator [warning, long post] Pin
Matt T Heffron13-Aug-15 6:24
professionalMatt T Heffron13-Aug-15 6:24 
GeneralRe: price calculator [warning, long post] Pin
Eddy Vluggen13-Aug-15 9:21
professionalEddy Vluggen13-Aug-15 9:21 
GeneralRe: price calculator [warning, long post] Pin
Matt T Heffron13-Aug-15 10:39
professionalMatt T Heffron13-Aug-15 10:39 
GeneralRe: price calculator [warning, long post] Pin
Eddy Vluggen14-Aug-15 3:28
professionalEddy Vluggen14-Aug-15 3:28 
GeneralRe: price calculator [warning, long post] Pin
Matt T Heffron14-Aug-15 7:08
professionalMatt T Heffron14-Aug-15 7:08 
GeneralRe: price calculator [warning, long post] Pin
Mycroft Holmes12-Aug-15 14:58
professionalMycroft Holmes12-Aug-15 14:58 
GeneralRe: price calculator [warning, long post] Pin
Eddy Vluggen12-Aug-15 20:07
professionalEddy Vluggen12-Aug-15 20:07 
Questionwhat can i use to create an installer Pin
jamesmc153511-Aug-15 22:36
jamesmc153511-Aug-15 22:36 
AnswerRe: what can i use to create an installer Pin
Richard MacCutchan12-Aug-15 2:00
mveRichard MacCutchan12-Aug-15 2:00 
AnswerRe: what can i use to create an installer Pin
dhaval.panchal512-Aug-15 3:02
dhaval.panchal512-Aug-15 3:02 
GeneralRe: what can i use to create an installer Pin
jamesmc153512-Aug-15 3:04
jamesmc153512-Aug-15 3:04 
AnswerRe: what can i use to create an installer Pin
Eddy Vluggen12-Aug-15 3:04
professionalEddy Vluggen12-Aug-15 3:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.