user3163154
user3163154

Reputation: 1

Calculation of change money placed into a label

I have to place multiple ints into a label. Sometimes one, sometimes 2 or more, depending on the calculation. It's the calculation of changing money and it's the number of the coin values that has to be shown in the label. example: 1 x € 1,00 + 2 x € 0,20 + 1 x € 0,05 where 1,2 and 1 are the ints from the calculation of money change. The change money has to be in as few coins as possible. Right now I have the code with a while-loop and a label.content = ---------- each time but I know that if the second or thirth one is also given it always shows the last result into the label. How can I fix this in the most simple way? Thanks in advance!

    private void btnBuy_Click(object sender, RoutedEventArgs e)
    {


        if (given < cost)
            lblAction.Content = "Not enough money";
        else
        {
            change = (given - cost);
            lblChange.Content = "Terug: € " + change;
            lblReport.Content = "Take";
            lblAction.Content = "Take your beverage";
            btnTakeBeverage.IsEnabled = true;
        }


        while (change >= 1M)
        {
            oneEuro++;
            change -= 1M;
            if (oneEuro > 0)
               lblCalculatingchange.Content = oneEuro + " x € 1,00";
        }
        while (change >= 0.50M)
        {
            fiftyEurocent++;
            change -= 0.50M;
            if (fiftyEurocent > 0)
                 lblCalculatingchange.Content = fiftyEurocent + " x € 0,50";
        }
        while (change >= 0.20M)
        {
            twentyEurocent++;
            change -= 0.20M;
            if (twentyEurocent > 0)
                 lblCalculatingchange.Content = twentyEurocent + " x € 0,20";
        }
        while (change >= 0.10M)
        {
            tenEurocent++;
            change -= 0.10M;
            if (tenEurocent > 0)
                lblCalculatingchange.Content = tenEurocent + " x € 0,10";
        }
        while (change >= 0.05M)
        {
            fiveEurocent++;
            change -= 0.05M;
            if (fiveEurocent > 0)
                 lblCalculatingchange.Content = fiveEurocent + " x € 0,05";
        }

Upvotes: 0

Views: 156

Answers (1)

Andrei
Andrei

Reputation: 56716

You are overriding the previous value of the label every time a new portion is calculated. Proper way would be adding new part to the label's text:

lblCalculatingchange.Content += fiftyEurocent + " x € 0,50";

Or even better - you can use StringBuilder for this:

StringBuilder text = new StringBuilder();
...
text.Append(oneEuro + " x € 1,00");
...
text.Append(fiftyEurocent + " x € 0,50");
...

// all calculations done
lblCalculatingchange.Content = text.ToString();

Update. Another, suggested in comments - this allows you to add "+" signs as well:

List<string> tokens = new List<string>();
...
tokens.Add(oneEuro + " x € 1,00");
...
tokens.Add(fiftyEurocent + " x € 0,50");
...

// all calculations done
lblCalculatingchange.Content = string.Join(" + ", tokens);

Upvotes: 2

Related Questions