EDN Admin
Well-known member
I am trying to create a simple order entry program using winforms. The way I want it to work is that the user input the number of food items (ex. 5 pizzas)in a text box and then the number of drinks (ex. 4 cokes) in a text box. When the user
clicks the order button, the total Amount due should show up in a text box called Amount Due and then the user would input a payment amount and click the payment button. From there a multiline text box should show the entire order, to include any change
due.
The problem is that it doesnt work the way I am wanting it to. It doesnt wait for the user to input a payment amount. The reason is because once it leaves the forms class and moves to the Order class, it stays in the order class, following
the items in the constructor line by line. So it seems to me that I need to insert a line within the constructor that will send it back to the forms class so that I can insert the payment amount.
That is where the problem comes in. I cannot figure out how to do that. Here is the code I have:
The Form class:
<pre class="prettyprint public partial class OrderForm : Form
{
int numberOfPizzas = 0,
numberOfCokes = 0;
const double PIZZA_PRICE = 0.0,
COKE_PRICE = 0.0;
double
totalCostOfPizza = 0.0,
totalCostOfCoke = 0.0,
foodAndDrinkTotal = 0.0,
totalSalesTax = 0.0,
amountDue = 0.0,
amountPaid = 0.0,
totalAmountDue = 0.0,
changeDue = 0.0;
public OrderForm()
{
InitializeComponent();
}
private void btnOrderButton_Click(object sender, EventArgs e)
{
numberOfPizzas = int.Parse(this.txtNumberOfPizzaOrdered.Text);
numberOfCokes = int.Parse(this.txtNumberOfCokesOrdered.Text);
PizzaOrder orderObject = new PizzaOrder(numberOfPizzas, numberOfCokes, totalAmountDue, amountPaid);
this.txtAmountDue.Text = PizzaOrder.TotalAmountDue.ToString("C");
amountPaid = int.Parse(this.txtAmountPaid.Text);
}
private void btnPaymentButton_Click(object sender, EventArgs e)
{
this.txtYourOrder.Text = PizzaOrder.DisplayResult(numberOfPizzas, PIZZA_PRICE,
ref totalCostOfPizza, numberOfCokes, COKE_PRICE,
ref totalCostOfCoke, foodAndDrinkTotal, totalSalesTax,
totalAmountDue, amountPaid, changeDue);
} [/code]
The order class:
<pre class="prettyprint public class PizzaOrder
{
private const double PIZZA_PRICE = 7.99,
COKE_PRICE = 1.49,
TAX_RATE = 0.065;
private static int numberOfCokes = 0,
numberOfPizzas = 0;
private static double totalCostOfPizza = 0.0,
totalCostOfCoke = 0.0,
totalSalesTax = 0.0,
foodAndDrinkTotal = 0.0,
totalAmountDue = 0.0,
amountPaid = 0.0,
changeDue = 0.0;
public PizzaOrder()
{
}
public PizzaOrder(int pizza, int coke, double amountDue, double payment)
{
numberOfPizzas = pizza;
numberOfCokes = coke;
InputOrder(numberOfPizzas, numberOfCokes,
ref totalCostOfPizza, ref totalCostOfCoke);
SalesTax(foodAndDrinkTotal);
GetAmountDue(foodAndDrinkTotal, totalSalesTax);
GetChangeDue(amountPaid, totalAmountDue);
DisplayResult(numberOfPizzas, PIZZA_PRICE, ref totalCostOfPizza,
numberOfCokes, COKE_PRICE, ref totalCostOfCoke,
foodAndDrinkTotal, totalSalesTax, totalAmountDue, amountPaid,
changeDue);
}
//property of the numberOfCokes attribute
public int NumberOfCokes
{
get
{
return numberOfCokes;
}
set
{
numberOfCokes = value;
}
}
//property of the numberOfPizzas atrribute
public int NumberOfPizzas
{
get
{
return numberOfPizzas;
}
set
{
numberOfPizzas = value;
}
}
public static double TotalAmountDue
{
get
{
return totalAmountDue;
}
}
public double ChangeDue
{
get
{
return changeDue;
}
}
public static double InputOrder(int numberOfPizzas, int numberOfCokes,
ref double totalCostOfPizza, ref double totalCostOfCoke)
{
totalCostOfPizza = numberOfPizzas * PIZZA_PRICE;
totalCostOfCoke = numberOfCokes * COKE_PRICE;
foodAndDrinkTotal = totalCostOfPizza + totalCostOfCoke;
return foodAndDrinkTotal;
}
private static double SalesTax(double foodAndDrinkTotal)
{
totalSalesTax = foodAndDrinkTotal * TAX_RATE;
return totalSalesTax;
}
public static void GetAmountDue(double foodAndDrinkTotal,
double totalSalesTax)
{
totalAmountDue = foodAndDrinkTotal + totalSalesTax;
//return totalAmountDue;
}
//calculate amount of change due customer
public static double GetChangeDue(double amountPaid, double totalAmountDue)
{
changeDue = amountPaid - totalAmountDue;
return changeDue;
}
public static string DisplayResult(int numberOfPizzas, double PIZZA_PRICE,
ref double totalCostOfPizza, int numberOfCokes, double COKE_PRICE,
ref double totalCostOfCoke, double foodAndDrinkTotal,
double totalSalesTax, double totalAmountDue, double amountPaid,
double changeDue)
{
return string.Format("{0} Pizzas @ {1:C}: {2:C}n" +
"{3} Cokes @ {4:C}: {5:C}n" +
"Order Amount: {6:C}n" +
"Sales Tax: {7:C}n" +
"Amount Due: {8:C}n" +
"Amount Paid: {9:C}n" +
"Change Due: {10:C}", numberOfPizzas, PIZZA_PRICE +
totalCostOfPizza, numberOfCokes, COKE_PRICE, totalCostOfCoke +
foodAndDrinkTotal, totalSalesTax, totalAmountDue, amountPaid, +
changeDue);
} [/code]
So, I tired this:
<pre class="prettyprint public PizzaOrder(int pizza, int coke, double amountDue, double payment)
{
numberOfPizzas = pizza;
numberOfCokes = coke;
InputOrder(numberOfPizzas, numberOfCokes,
ref totalCostOfPizza, ref totalCostOfCoke);
SalesTax(foodAndDrinkTotal);
GetAmountDue(foodAndDrinkTotal, totalSalesTax);
--> amountPaid = OrderForm.txtAmountPaid.Text; <--
GetChangeDue(amountPaid, totalAmountDue);
DisplayResult(numberOfPizzas, PIZZA_PRICE, ref totalCostOfPizza,
numberOfCokes, COKE_PRICE, ref totalCostOfCoke,
foodAndDrinkTotal, totalSalesTax, totalAmountDue, amountPaid,
changeDue);
}[/code]
But that doesnt work, and besides, Im not sure that would move the program back to the form class so that the form will pop back up and allow me enter the payment amount
Then I tried instantiating an object from the forms class and doing this:
<pre class="prettyprint OrderForm newOrderObject = new OrderForm();
public PizzaOrder()
{
}
public PizzaOrder(int pizza, int coke, double amountDue, double payment)
{
numberOfPizzas = pizza;
numberOfCokes = coke;
InputOrder(numberOfPizzas, numberOfCokes,
ref totalCostOfPizza, ref totalCostOfCoke);
SalesTax(foodAndDrinkTotal);
GetAmountDue(foodAndDrinkTotal, totalSalesTax);
--> amountPaid = newOrderObject.txtAmountPaid.Text; <--
GetChangeDue(amountPaid, totalAmountDue);
DisplayResult(numberOfPizzas, PIZZA_PRICE, ref totalCostOfPizza,
numberOfCokes, COKE_PRICE, ref totalCostOfCoke,
foodAndDrinkTotal, totalSalesTax, totalAmountDue, amountPaid,
changeDue);
}[/code]
The compiler seemed to like that a little better, but I still get an error message telling me that .txtAmountPaid is inaccessible due to its protection level. So I went back and changed the private void btnOrderButton_Click(object sender, EventArgs
e) to public, but that didnt change anything. The constructor is already public, as are the classes so Im not sure what it wants.
So at this point I am at a loss as to what I should be doing in order to allow the form to pop back up so that I can enter a payment amount into the text box.
View the full article
clicks the order button, the total Amount due should show up in a text box called Amount Due and then the user would input a payment amount and click the payment button. From there a multiline text box should show the entire order, to include any change
due.
The problem is that it doesnt work the way I am wanting it to. It doesnt wait for the user to input a payment amount. The reason is because once it leaves the forms class and moves to the Order class, it stays in the order class, following
the items in the constructor line by line. So it seems to me that I need to insert a line within the constructor that will send it back to the forms class so that I can insert the payment amount.
That is where the problem comes in. I cannot figure out how to do that. Here is the code I have:
The Form class:
<pre class="prettyprint public partial class OrderForm : Form
{
int numberOfPizzas = 0,
numberOfCokes = 0;
const double PIZZA_PRICE = 0.0,
COKE_PRICE = 0.0;
double
totalCostOfPizza = 0.0,
totalCostOfCoke = 0.0,
foodAndDrinkTotal = 0.0,
totalSalesTax = 0.0,
amountDue = 0.0,
amountPaid = 0.0,
totalAmountDue = 0.0,
changeDue = 0.0;
public OrderForm()
{
InitializeComponent();
}
private void btnOrderButton_Click(object sender, EventArgs e)
{
numberOfPizzas = int.Parse(this.txtNumberOfPizzaOrdered.Text);
numberOfCokes = int.Parse(this.txtNumberOfCokesOrdered.Text);
PizzaOrder orderObject = new PizzaOrder(numberOfPizzas, numberOfCokes, totalAmountDue, amountPaid);
this.txtAmountDue.Text = PizzaOrder.TotalAmountDue.ToString("C");
amountPaid = int.Parse(this.txtAmountPaid.Text);
}
private void btnPaymentButton_Click(object sender, EventArgs e)
{
this.txtYourOrder.Text = PizzaOrder.DisplayResult(numberOfPizzas, PIZZA_PRICE,
ref totalCostOfPizza, numberOfCokes, COKE_PRICE,
ref totalCostOfCoke, foodAndDrinkTotal, totalSalesTax,
totalAmountDue, amountPaid, changeDue);
} [/code]
The order class:
<pre class="prettyprint public class PizzaOrder
{
private const double PIZZA_PRICE = 7.99,
COKE_PRICE = 1.49,
TAX_RATE = 0.065;
private static int numberOfCokes = 0,
numberOfPizzas = 0;
private static double totalCostOfPizza = 0.0,
totalCostOfCoke = 0.0,
totalSalesTax = 0.0,
foodAndDrinkTotal = 0.0,
totalAmountDue = 0.0,
amountPaid = 0.0,
changeDue = 0.0;
public PizzaOrder()
{
}
public PizzaOrder(int pizza, int coke, double amountDue, double payment)
{
numberOfPizzas = pizza;
numberOfCokes = coke;
InputOrder(numberOfPizzas, numberOfCokes,
ref totalCostOfPizza, ref totalCostOfCoke);
SalesTax(foodAndDrinkTotal);
GetAmountDue(foodAndDrinkTotal, totalSalesTax);
GetChangeDue(amountPaid, totalAmountDue);
DisplayResult(numberOfPizzas, PIZZA_PRICE, ref totalCostOfPizza,
numberOfCokes, COKE_PRICE, ref totalCostOfCoke,
foodAndDrinkTotal, totalSalesTax, totalAmountDue, amountPaid,
changeDue);
}
//property of the numberOfCokes attribute
public int NumberOfCokes
{
get
{
return numberOfCokes;
}
set
{
numberOfCokes = value;
}
}
//property of the numberOfPizzas atrribute
public int NumberOfPizzas
{
get
{
return numberOfPizzas;
}
set
{
numberOfPizzas = value;
}
}
public static double TotalAmountDue
{
get
{
return totalAmountDue;
}
}
public double ChangeDue
{
get
{
return changeDue;
}
}
public static double InputOrder(int numberOfPizzas, int numberOfCokes,
ref double totalCostOfPizza, ref double totalCostOfCoke)
{
totalCostOfPizza = numberOfPizzas * PIZZA_PRICE;
totalCostOfCoke = numberOfCokes * COKE_PRICE;
foodAndDrinkTotal = totalCostOfPizza + totalCostOfCoke;
return foodAndDrinkTotal;
}
private static double SalesTax(double foodAndDrinkTotal)
{
totalSalesTax = foodAndDrinkTotal * TAX_RATE;
return totalSalesTax;
}
public static void GetAmountDue(double foodAndDrinkTotal,
double totalSalesTax)
{
totalAmountDue = foodAndDrinkTotal + totalSalesTax;
//return totalAmountDue;
}
//calculate amount of change due customer
public static double GetChangeDue(double amountPaid, double totalAmountDue)
{
changeDue = amountPaid - totalAmountDue;
return changeDue;
}
public static string DisplayResult(int numberOfPizzas, double PIZZA_PRICE,
ref double totalCostOfPizza, int numberOfCokes, double COKE_PRICE,
ref double totalCostOfCoke, double foodAndDrinkTotal,
double totalSalesTax, double totalAmountDue, double amountPaid,
double changeDue)
{
return string.Format("{0} Pizzas @ {1:C}: {2:C}n" +
"{3} Cokes @ {4:C}: {5:C}n" +
"Order Amount: {6:C}n" +
"Sales Tax: {7:C}n" +
"Amount Due: {8:C}n" +
"Amount Paid: {9:C}n" +
"Change Due: {10:C}", numberOfPizzas, PIZZA_PRICE +
totalCostOfPizza, numberOfCokes, COKE_PRICE, totalCostOfCoke +
foodAndDrinkTotal, totalSalesTax, totalAmountDue, amountPaid, +
changeDue);
} [/code]
So, I tired this:
<pre class="prettyprint public PizzaOrder(int pizza, int coke, double amountDue, double payment)
{
numberOfPizzas = pizza;
numberOfCokes = coke;
InputOrder(numberOfPizzas, numberOfCokes,
ref totalCostOfPizza, ref totalCostOfCoke);
SalesTax(foodAndDrinkTotal);
GetAmountDue(foodAndDrinkTotal, totalSalesTax);
--> amountPaid = OrderForm.txtAmountPaid.Text; <--
GetChangeDue(amountPaid, totalAmountDue);
DisplayResult(numberOfPizzas, PIZZA_PRICE, ref totalCostOfPizza,
numberOfCokes, COKE_PRICE, ref totalCostOfCoke,
foodAndDrinkTotal, totalSalesTax, totalAmountDue, amountPaid,
changeDue);
}[/code]
But that doesnt work, and besides, Im not sure that would move the program back to the form class so that the form will pop back up and allow me enter the payment amount
Then I tried instantiating an object from the forms class and doing this:
<pre class="prettyprint OrderForm newOrderObject = new OrderForm();
public PizzaOrder()
{
}
public PizzaOrder(int pizza, int coke, double amountDue, double payment)
{
numberOfPizzas = pizza;
numberOfCokes = coke;
InputOrder(numberOfPizzas, numberOfCokes,
ref totalCostOfPizza, ref totalCostOfCoke);
SalesTax(foodAndDrinkTotal);
GetAmountDue(foodAndDrinkTotal, totalSalesTax);
--> amountPaid = newOrderObject.txtAmountPaid.Text; <--
GetChangeDue(amountPaid, totalAmountDue);
DisplayResult(numberOfPizzas, PIZZA_PRICE, ref totalCostOfPizza,
numberOfCokes, COKE_PRICE, ref totalCostOfCoke,
foodAndDrinkTotal, totalSalesTax, totalAmountDue, amountPaid,
changeDue);
}[/code]
The compiler seemed to like that a little better, but I still get an error message telling me that .txtAmountPaid is inaccessible due to its protection level. So I went back and changed the private void btnOrderButton_Click(object sender, EventArgs
e) to public, but that didnt change anything. The constructor is already public, as are the classes so Im not sure what it wants.
So at this point I am at a loss as to what I should be doing in order to allow the form to pop back up so that I can enter a payment amount into the text box.
View the full article