2 Replies - 11421 Views - Last Post: 08 November 2009 - 01:24 PM Rate Topic: -----

#1 Kratos61  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 31-October 09

Pizza order Program

Post icon  Posted 05 November 2009 - 08:45 PM

sorry for being a noob but i nead to make a program that will allow the
user to enter in if they want a large or extra large pizza. They can also choose to have 1, 2, 3, or 4 toppings, not including cheese. The program will then calculate the cost of the pizza and the two taxes (provincial and federal). It will then display the cost, each tax and the final cost.
( i know the pathetic attempt at an if statement is very wrong)
I have no Idea how to continue.
my code so far is:

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace asignment_3
{
	public partial class frmPizzaOrder : Form
	{
		public frmPizzaOrder()
		{
			InitializeComponent();
		}

		private void numericUpDown1_ValueChanged(object sender, EventArgs e)
		{

		}

		private void btnCalculateCost_Click(object sender, EventArgs e)
		{
			const decimal LARGE_PIZZA_COST = 6.00M;
			const decimal EXTRA_LARGE_PIZZA_COST = 10.00M;
			const decimal ONE_TOPPING_COST = 1.00M;
			const decimal TWO_TOPPINGS_COST = 1.75M;
			const decimal THREE_TOPPINGS_COST = 2.50M;
			const decimal FOUR_TOPPINGS_COST = 3.25M;

			decimal pizza;
			decimal toppings;
			decimal total;

			if numudLargeOrExtra = 2





			this.lblFinalTotal.Text = "The cost is: " + string.Format("{0:c}", total);

		}
	}
}



I posted an example of the program (how it should be like)

Attached File(s)


This post has been edited by Kratos61: 05 November 2009 - 08:48 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Pizza order Program

#2 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 526
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: Pizza order Program

Posted 05 November 2009 - 11:49 PM

I was kind of bored and your little pizza project seemed fun.
I decided (as an exercise for myself) to implement the decorator pattern so there's quite a bit here.
However, adding new toppings is as easy as updating the enum and the AddTopping method in the factory.
It's really straight-forward.

Oh, but don't hand this in. Your instructor is definitely going to know you didn't write it because I'm covering concepts I'm fairly certain you have not gone over yet.

Regardless, save it and as your progress, look it over and study it. Try to make it better or add features/toppings. Whatever, just have fun with it :)

using System;
using System.Collections.Generic;

// Requirements
/*
- Allow the user to enter in if they want a large or extra large pizza. 
- They can also choose to have 1, 2, 3, or 4 toppings, not including cheese. 
- Calculate the cost of the pizza and the two taxes (provincial and federal). 
- Display the cost, each tax and the final cost.
*/

// TODO: Implement requirement that 4 is maximum number of toppings.

namespace PizzaShop {

	/// <summary>
	/// This is the main class. We set up our tests here and demo the final product.
	/// </summary>
	class Program {
		static void Main(string[] args) {

			// Ask the factory for a pizza. Based on the size, we get the right object back.
			IPizza pizza1 = PizzaFactory.OrderPizza(PizzaSizes.Large);

			// Add toppings to our pizza. Pass in the pizza so it can have a topping added to it. 
			pizza1 = PizzaFactory.AddTopping(pizza1, PizzaToppings.Ham);
			PizzaFactory.PrintRecipt(pizza1);

			Console.WriteLine();

			// Same stuff for these too... pretty simple at this level.
			IPizza pizza2 = PizzaFactory.OrderPizza(PizzaSizes.Large);
			pizza2 = PizzaFactory.AddTopping(pizza2, PizzaToppings.Olives);
			pizza2 = PizzaFactory.AddTopping(pizza2, PizzaToppings.Pepperoni);
			PizzaFactory.PrintRecipt(pizza2);

			Console.WriteLine();

			IPizza pizza3 = PizzaFactory.OrderPizza(PizzaSizes.ExtraLarge);
			pizza3 = PizzaFactory.AddTopping(pizza3, PizzaToppings.Ham);
			pizza3 = PizzaFactory.AddTopping(pizza3, PizzaToppings.Olives);
			pizza3 = PizzaFactory.AddTopping(pizza3, PizzaToppings.Pepperoni);
			PizzaFactory.PrintRecipt(pizza3);

			Console.WriteLine();

			IPizza pizza4 = PizzaFactory.OrderPizza(PizzaSizes.Large);
			PizzaFactory.PrintRecipt(pizza4);

			Console.ReadLine();

		}
	}

	/// <summary>
	/// The sizes of pizza we can have.
	/// </summary>
	enum PizzaSizes {
		Large,
		ExtraLarge
	}

	/// <summary>
	/// The various toppings we can add to our pizza.
	/// </summary>
	enum PizzaToppings
	{
		Pepperoni,
		Ham,
		Olives
		// etc...
	}

	/// <summary>
	/// Get a pizza
	/// </summary>
	static class PizzaFactory {

		// I just made these up. I don't know what they really are.
		private static decimal provincialTax = .6M;
		private static decimal federalTax = .7M;

		public static IPizza OrderPizza(PizzaSizes size) {

			switch (size) {
				case PizzaSizes.Large:
					return new LargePizza();
				case PizzaSizes.ExtraLarge:
					return new ExtraLargePizza();
				default:
					return null;
			}
		}

		public static IPizza AddTopping(IPizza pizza, PizzaToppings topping) 
		{
			switch (topping) {
				case PizzaToppings.Pepperoni:
					return new PeperoniTopping(pizza);
				case PizzaToppings.Ham:
					return new HamTopping(pizza);
				case PizzaToppings.Olives:
					return new OliveTopping(pizza);
				default:
					return pizza;
			}
		}

		// Just print out pizza info that is required by spec.
		public static void PrintRecipt(IPizza pizza) {
			Console.WriteLine("Pizza: " + pizza.Name);
			Console.WriteLine("Sub-Total: " + pizza.Price);
			Console.WriteLine("Total After Federal: " + (pizza.Price + federalTax));
			Console.WriteLine("Total After Provincial: " + (pizza.Price + provincialTax));
			Console.WriteLine("Final Cost: " + (pizza.Price + provincialTax + federalTax));
		}
	}

	/// <summary>
	/// This is the base topping. It's not an actual topping. It's to be extended for all of our concrete toppings.
	/// Note that it contains a reference to the passed in IPizza in the constructor.
	/// </summary>
	abstract class Toppings : IPizza {

		private IPizza pizza;

		public Toppings(IPizza pizza) {
			this.pizza = pizza;
		}

		public virtual string Name {
			get {
				return pizza.Name;
			}
		}

		public virtual decimal Price {
			get {
				return pizza.Price;
			}
		}
	}

	/// <summary>
	/// And this is our actual topping. It can call its base constructor and when it does it passes in the IPizza.
	/// This is the same for all toppings that you would implement. With this (It's called the decorator pattern)
	/// we can unroll our pizza for output and all toppings will be calculated plus cost of pizza.
	/// </summary>
	class PeperoniTopping : Toppings {

		public PeperoniTopping(IPizza pizza) : base(pizza) {}

		public override string  Name
		{
			get 
			{ 
				 return base.Name + " with pepperoni";
			}
		}

		public override decimal  Price
		{
			get 
			{ 
				 return base.Price + .75M;
			}
		}
	}

	class HamTopping : Toppings {

		public HamTopping(IPizza pizza) : base(pizza) {}

		public override string  Name
		{
			get 
			{
				return base.Name + " with ham";
			}
		}

		public override decimal  Price
		{
			get 
			{ 
				 return base.Price + .75M;
			}
		}
	}

	class OliveTopping : Toppings {

		public OliveTopping(IPizza pizza) : base(pizza) { }

		public override string Name {
			get {
				return base.Name + " with olives";
			}
		}

		public override decimal Price {
			get {
				return base.Price + .75M;
			}
		}
	}

	/// <summary>
	/// Abstraction of pizza concept.
	/// </summary>
	interface IPizza {
		string Name { get; }
		decimal Price { get; }
	}

	/// <summary>
	/// implemented behavior of a regular old Pizza
	/// </summary>
	class LargePizza : IPizza {

		public string Name {
			get {
				return "Large pizza";
			}
		}

		public decimal Price {
			get {
				return 6.00M;
			}
		}
	}

	/// <summary>
	/// imeplemented behavior of a regular old Pizza
	/// </summary>
	class ExtraLargePizza : IPizza {

		public string Name {
			get {
				return "Extra large pizza";
			}
		}

		public decimal Price {
			get {
				return 10.00M;
			}
		}
	}
}



Enjoy.

Edit:
Here it is in action -
Posted Image

This post has been edited by MentalFloss: 05 November 2009 - 11:50 PM

Was This Post Helpful? 0
  • +
  • -

#3 Kratos61  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 31-October 09

Re: Pizza order Program

Posted 08 November 2009 - 01:24 PM

HI i need to know how to use a boolean expression for this program so that if the user chooses 2 on the numeric up and down, the computer puts in the price for a large pizza in the subtotals and the final total. my incorrect code that i have for my program is:

 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace asignment_3
{
	public partial class frmPizzaOrder : Form
	{
		public frmPizzaOrder()
		{
			InitializeComponent();
		}


		private void btnCalculateCost_Click(object sender, EventArgs e)
		{
			const decimal LARGE_PIZZA_COST = 6.00M;
			const decimal EXTRA_LARGE_PIZZA_COST = 10.00M;
			const decimal ONE_TOPPING_COST = 1.00M;
			const decimal TWO_TOPPINGS_COST = 1.75M;
			const decimal THREE_TOPPINGS_COST = 2.50M;
			const decimal FOUR_TOPPINGS_COST = 3.25M;
			
			
			decimal pizza;
			decimal toppings;
			decimal total;
			 


			if (numudLargeOrExtra = 1)
			{
				total = LARGE_PIZZA_COST + ONE_TOPPING_COST;
				if (numudLargeOrExtra = 2)
				{
					total = EXTRA_LARGE_PIZZA_COST + ONE_TOPPING_COST;




					this.lblFinalTotal.Text = "Final Total: " + string.Format("{0:c}", total);

				}
			}
		}
	}
}



I need to know what is wrong with my code so far

This post has been edited by Kratos61: 08 November 2009 - 02:00 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1