r/programmingrequests • • Apr 22 '17

Do-While Statement for Receipt (C#)

Sorry in advance, this is going to be a long question.

So, as the title suggests, I am trying to use a Do-While statement in C# to make a receipt. What I wish to happen is, when an order is placed, the Do-While statement will be able to see the items purchased, and the total that the purchased items should come to.

So, for example, I have two espressos that cost £2.20, a hot chocolate that costs £2.75 and a mocha that also costs £2.75.

I want the receipt to look something like:

Espresso x2 - £4.40 Hot Chocolate x1 - £2.75 Mocha x1 - £2.75 Of course, if say the order had two mocha instead of one, and maybe an extra item, like three americano for £2.50 it would look like:

Espresso x2 - £4.40 Hot Chocolate x1 - £2.75 Mocha x2 - £5.50 Americano x3 - £7.50

I understand how to do a simple Do-While code, but have no real experience of doing a Do-While statement like this. It has to be included in my assignment and the only way I can think of using a Do-While statement in my assignment is with it doing a receipt creation.

Any help would be appreciated.

My Code Currently looks like this:

namespace While_Receipt { public partial class Form1 : Form { decimal priceespresso, totalespresso, pricefilter, totalfilter, priceCappuccino, totalCappuccino, priceAmericano, totalAmericano, priceLatte, totalLatte, priceMocha, totalMocha, priceHotChocolate, totalHotChocolate, priceIceCoffee, totalIceCoffee, priceExtra, totalExtra, TotalOverall; int quantityespresso, quantityfilter, quantitiyCappuccino, quantityAmericano, quantityLatte, quantitiyMocha, quantitiyHotChocolate, quantityIceCoffee, quantityExtra;

public Form1()
{
    InitializeComponent();
}

private void btnTotal_Click(object sender, EventArgs e) { priceespresso = Convert.ToDecimal(txtEspressoPrice.Text); quantityespresso = Convert.ToInt32(txtEspressoMultiply.Text); totalespresso = priceespresso * quantityespresso; lblResults.Text = Convert.ToString(totalespresso);

pricefilter = Convert.ToDecimal(txtFilterPrice.Text); quantityfilter = Convert.ToInt32(txtFilterMultiply.Text); totalfilter = pricefilter * quantityfilter; lblFilterResults.Text = Convert.ToString(totalfilter);

    priceCappuccino = Convert.ToDecimal(txtCappuccinoPrice.Text);
    quantitiyCappuccino = Convert.ToInt32(txtAmericanoMultiply.Text);
    totalCappuccino = priceCappuccino * quantitiyCappuccino;
    lblCappuccinoResults.Text = Convert.ToString(totalCappuccino);

    priceAmericano = Convert.ToDecimal(txtAmericanoPrice.Text);
    quantityAmericano = Convert.ToInt32(txtCappuccinoMultiply.Text);
    totalAmericano = priceAmericano * quantityAmericano;
    lblAmericanoResults.Text = Convert.ToString(totalAmericano);

    priceLatte = Convert.ToDecimal(txtLattePrice.Text);
    quantityLatte = Convert.ToInt32(txtLatteMultiply.Text);
    totalLatte = priceLatte * quantityLatte;
    lblLatteResults.Text = Convert.ToString(totalLatte);

    priceMocha = Convert.ToDecimal(txtMochaPrice.Text);
    quantitiyMocha = Convert.ToInt32(txtMochaMultiply.Text);
    totalMocha = priceMocha * quantitiyMocha;
    lblMochaResults.Text = Convert.ToString(totalMocha);

    priceHotChocolate = Convert.ToDecimal(txtHotChocolatePrice.Text);
    quantitiyHotChocolate = Convert.ToInt32(txtHotChocolateMultiply.Text);
    totalHotChocolate = priceHotChocolate * quantitiyHotChocolate;
    lblHotChocolateResults.Text = Convert.ToString(totalHotChocolate);

    priceIceCoffee = Convert.ToDecimal(txtIceCoffeePrice.Text);
    quantityIceCoffee = Convert.ToInt32(txtIcedCoffeeMultiply.Text);
    totalIceCoffee = priceIceCoffee * quantityIceCoffee;
    lblIcedCoffeeResults.Text = Convert.ToString(totalIceCoffee);

    priceExtra = Convert.ToDecimal(txtExtra.Text);
    quantityExtra = Convert.ToInt32(txtExtraMultiply.Text);
    totalExtra = priceExtra * quantityExtra;
    txtExtraResults.Text = Convert.ToString(totalExtra);

    TotalOverall = totalespresso + totalfilter + totalCappuccino + totalAmericano + totalLatte
    + totalMocha + totalHotChocolate + totalIceCoffee + totalExtra;
    lblTotalOverall.Text = Convert.ToString(TotalOverall);
  }

private void lstResults_SelectedIndexChanged(object sender, EventArgs e)
{

}

} }

2 Upvotes

1 comment sorted by

2

u/wischichr Apr 22 '17

First of all - change all decimals to int and calculate your sums in cents (thank me later)

After that create a new class ReceiptItem with properties amount, name and price.

Iterate through items with foreach. I'm on mobile now so sadly I can't write code for you right now.