#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0

    Need guidance with pizza ordering system project - urgently!


    Hi all,

    I am a complete beginner in Java and things are moving quite quickly at university and I'm having difficulty keeping up. I have a project and I will share the specifications and show you what I've done so far. It may be completely awful, and the specification is quite long but I'd be very happy if you'd just bear with me and give me a hand, I've got until this Friday - the 14th of December to finish this and not sure how I'm going to do it!

    Background

    As part of a larger project, building an online ordering system for a Pizza company, you need to design and implement a system for calculating the costs and some other features about a pizza. The system will allow you to build a pizza from a base and some toppings.

    Task

    Your task is to create a prototype which allows cost calculations for pizzas.

    You will need at least three classes.

    Pizza
    This should carry all the essential information about a pizza, including a single base and some toppings. This class should know what toppings and which base the pizza has, so it should be the only class that has the information to calculate the cost of the pizza.

    PizzaBase
    This class should model a pizza base. You will need at least two, one thick and one thin. This needs to have information about different bases & prices.

    PizzaTopping
    This class should model a pizza topping. You need at least four toppings, but you may provide as many as you choose. Again this needs to have information about toppings & their prices.

    For later stages of this coursework, you will build a simple user interface. This is a prototype system, so the user interface does NOT need to be complex; a simple command-line interface using Scanner and println statements is entirely sufficient. However, as your classes may be used in several different situations, none of Pizza, PizzaBase nor PizzaTopping should require any user interaction. None of the above classes need main methods.

    There are a number of features that you should attempt to achieve; please complete these in the order given.

    Build a Pizza
    The initial version of your system should allow enable you to create objects for a pizza, one base and several toppings. Each of the ingredients should have a cost associated with it. You should be able to combine these to create a pizza which should have one and only one base, but many toppings. The finished pizza should calculate itís cost. You should write a simple class called PizzaBuild which contains a main method which demonstrates this working.

    Here is what I have so far:

    Code:
     
    import java.util.Map;
    import java.util.HashMap;
    
    public class Pizza {
    
    	PizzaBase base;
    	Map<String,PizzaTopping> toppings = new HashMap<String,PizzaTopping>(4);
    	
    	Pizza(PizzaBase b, HashMap<String,PizzaTopping> t){
    		base = b;
    		toppings = t;
    	}
    	
    	public void putBase(String b){
    		PizzaBase base = new PizzaBase("thick");
    		base.getCost();
    	}
    	
    	public void addTopping(String t){
    		PizzaTopping topping = new PizzaTopping(t);
    	    //...
    		
    	}
    	
    	public double pizzaCost(){	
    		
    			
    		}
    	}
    ----
    Code:
    	
    public class PizzaBase {
    
    	private String base;
    	private double cost;
    	
    	PizzaBase(String b){
    		base = b;
    	
    	if(base.equals("thin")){
    		setCost(1.8);
    	}
    	
    	else if(base.equals("thick"))
    		setCost(2.0);
    	}
    
    	public double getCost() {
    		return cost;
    	}
    
    	private void setCost(double cost) {
    		this.cost = cost;
    	}
      }
    ---
    Code:
    import java.util.Map;
    import java.util.HashMap;
    
    public class PizzaTopping {
    	
    	private String topping;
    	private double cost;
    	
    	PizzaTopping(String t){
    		topping = t;
    
    	if(topping.equals("tomato sauce")){ 
    		setCost(0.1);
    	}
    		
    	else if(topping.equals("olives")){
    		setCost(0.1);
    	}
    	
    	else if(topping.equals("corn")){
    		setCost(0.15);
    	}
    	
    	else if(topping.equals("mushrooms")){
    		setCost(0.2);
    	}
    	
    	else if(topping.equals("pineapple")){
    		setCost(0.25);
    	}}
    
    	public double getCost(){
    		return cost;
    	}
    	
    	private void setCost(double cost) {
    		this.cost = cost;
    	}
    }
    ---
    Code:
    import java.util.ArrayList;
    
    
    public class PizzaBuild {
    
    	public static void main(String[] args) {
    		
    		Pizza p = new Pizza();
    
    	}
    
    }
    As you can see I've left some parts empty, and in the PizzaBuild class I've written hardly anything at all so far. I just hope that someone can have a look at what I've done so far and tell me if I'm on the right track, if I'm overcomplicating things, if I'm making any errors in any sense etc. I need someone to point me in the right direction basically. Thanks in advance!
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    What sort of course is this? Beginning or advanced? The reason I ask is because your problem is a perfect case for using the decorator design pattern. Of course, for beginning programming classes, you are not expected to know anything about design patterns and I'm not sure what your teacher is expecting here. So is this a beginner class or an advanced class?
    Last edited by Scorpions4ever; December 10th, 2012 at 04:04 PM.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,580
    Rep Power
    1906
    It looks quite good, but missing some functionality:

    In Pizza class, you have the function putBase; when you create the PizzaBase you have it set to only create a "thick" pizza.
    You should use the parameter b as input for you PizzaBase.

    In Pizza class, you have the funtion addTopping.
    You will have to save/add/put the new topping into the HashMap toppings.
    Any specific reason you use a HashMap? my first thought was to use an ArrayList?
    It is not wrong to HashMap as it does have some feature that can be useful, but in the beginning the ArrayList can be easier to work with, IMO.

    I suggest you take a look at http://www.anyexample.com/programming/java/java_hashmap_example.xml
    It have example of how to use the hashmap

    For pizzaCost, It also have an example how to run through the list of added object, which you will need to calculate the cost.
    Where the example have Integer, you will need to use PizzaTopping.

    One thing you can/should think about is how the user know which Base and Toppings are available.
    But first, you should properly try to create a specific pizza using the defined functions to specify a base and toppings and then get the total cost.

    For easier code reading, I will recommend you have more intending (sp?) for the code inside a function.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0
    Originally Posted by Scorpions4ever
    What sort of course is this? Beginning or advanced? The reason I ask is because your problem is a perfect case for using the decorator design pattern. Of course, for beginning programming classes, you are not expected to know anything about design patterns and I'm not sure what your teacher is expecting here.
    It is a first year undergraduate Computer Science degree, no prior programming experience or knowledge is expected. And I have no previous programming experience. I can tell you what we've done so far in class if that helps. We've done constructors, variables, objects, lists, arraylists, hashmaps etc. I'm trying my best to work things out and I've changed the code around a bit since the time I posted this thread, but I need someone to give me some feedback and guide me, I'm not entirely sure if I'm on the right track at all...
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0
    Originally Posted by MrFujin
    It looks quite good, but missing some functionality:

    In Pizza class, you have the function putBase; when you create the PizzaBase you have it set to only create a "thick" pizza.
    You should use the parameter b as input for you PizzaBase.

    In Pizza class, you have the funtion addTopping.
    You will have to save/add/put the new topping into the HashMap toppings.
    Any specific reason you use a HashMap? my first thought was to use an ArrayList?
    It is not wrong to HashMap as it does have some feature that can be useful, but in the beginning the ArrayList can be easier to work with, IMO.

    I suggest you take a look at (URL)
    It have example of how to use the hashmap

    For pizzaCost, It also have an example how to run through the list of added object, which you will need to calculate the cost.
    Where the example have Integer, you will need to use PizzaTopping.

    One thing you can/should think about is how the user know which Base and Toppings are available.
    But first, you should properly try to create a specific pizza using the defined functions to specify a base and toppings and then get the total cost.

    For easier code reading, I will recommend you have more intending (sp?) for the code inside a function.
    Hey - thank you for the in depth reply. The reason I thought I should use HashMap is because it can store both the name of the topping AND the corresponding price, but I suppose I could use an ArrayList. I'm not sure how I would use the ArrayList though to be honest. Could you explain this in a bit more detail if you have some time?

    For the time being, all I need to do is to have the three classes and to create a pizza with either a thick or thin base, and a few toppings, and make sure it can calculate the cost of this pizza. After I do that, the next step will be to make a simple user interface with a scanner to ask the user what base and toppings they would like...
  10. #6
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    In that case, I'd do something like this. First we declare a base PizzaBase class.
    Code:
    public class PizzaBase {
    
    	private String base;
    	private double cost;
    
        public PizzaBase(String base, double cost) {
            this.base = base;
            this.cost = cost;
        }
    
        public String getBase() {
            return base;
        }
    
         public double getCost() {
            return cost;
         }
    
        /* add setBase() and setCost() here as needed */
    }
    Then we declare a couple of derived classes (I hope you learned how to do this in the course) from PizzaBase:
    Code:
    public class ThinPizzaBase extends PizzaBase {
        public ThinPizzaBase() {
            super("thin", 1.8);
        }
    }
    Code:
    public class ThickPizzaBase extends PizzaBase {
        public ThickPizzaBase() {
            super("thick", 2.0);
        }
    }
    Similarly, you can do the same thing for the toppings by declaring a base Topping class and individual toppings that set the cost of each topping.

    Then, in the Pizza class, I'd declare an ArrayList<Topping> instead of a HashMap. You can then code the addTopping() method as:
    Code:
        ArrayList<Topping>toppingList = new ArrayList<Topping>();
    ...
            public void addTopping(Topping topping) {
                toppingList.add(topping)
            }
    and compute the total like this:
    Code:
    	public double pizzaCost(){	
    	    double cost = base.getCost();
    	    for (Topping topping : toppingList) {
                    cost += topping.getCost();
                }
                return cost;
            }
    The key here is that the base classes (PizzaBase and Topping) have the code to return the cost, but the derived classes actually set the cost values. You can then call the code like this:
    Code:
    pizza.setBase(new ThickPizzaBase());
    pizza.addTopping(new CornTopping());
    pizza.addTopping(new TomatoTopping());
    double cost = pizza.getCost();
    The code will then call the appropriate derived classes to get the individual cost of items. Hope this helps you out somewhat.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  12. #7
  13. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,580
    Rep Power
    1906
    Just thinking, wouldn't this approach require some sort of factory pattern to get/select the base and/or toppings, when you start with the user input?
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0
    Originally Posted by Scorpions4ever
    In that case, I'd do something like this. First we declare a base PizzaBase class.
    Code:
    public class PizzaBase {
    
    	private String base;
    	private double cost;
    
        public PizzaBase(String base, double cost) {
            this.base = base;
            this.cost = cost;
        }
    
        public String getBase() {
            return base;
        }
    
         public double getCost() {
            return cost;
         }
    
        /* add setBase() and setCost() here as needed */
    }
    Then we declare a couple of derived classes (I hope you learned how to do this in the course) from PizzaBase:
    Code:
    public class ThinPizzaBase extends PizzaBase {
        public ThinPizzaBase() {
            super("thin", 1.8);
        }
    }
    Code:
    public class ThickPizzaBase extends PizzaBase {
        public ThickPizzaBase() {
            super("thick", 2.0);
        }
    }
    Similarly, you can do the same thing for the toppings by declaring a base Topping class and individual toppings that set the cost of each topping.

    Then, in the Pizza class, I'd declare an ArrayList<Topping> instead of a HashMap. You can then code the addTopping() method as:
    Code:
        ArrayList<Topping>toppingList = new ArrayList<Topping>();
    ...
            public void addTopping(Topping topping) {
                toppingList.add(topping)
            }
    and compute the total like this:
    Code:
    	public double pizzaCost(){	
    	    double cost = base.getCost();
    	    for (Topping topping : toppingList) {
                    cost += topping.getCost();
                }
                return cost;
            }
    The key here is that the base classes (PizzaBase and Topping) have the code to return the cost, but the derived classes actually set the cost values. You can then call the code like this:
    Code:
    pizza.setBase(new ThickPizzaBase());
    pizza.addTopping(new CornTopping());
    pizza.addTopping(new TomatoTopping());
    double cost = pizza.getCost();
    The code will then call the appropriate derived classes to get the individual cost of items. Hope this helps you out somewhat.
    Wow, thanks. That surely helps a lot. One thing I am wondering however is if there is an alternative to creating the derived classes..In the specs I think it mentions that I can have as many classes as I want in my project, but just for the sake of simplicity, I'm wondering if there is an alternative where I don't need to use the derived classes..in other words an alternative where I only have the Pizza, PizzaBase and PizzaTopping classes along with the PizzaBuild class
  16. #9
  17. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,580
    Rep Power
    1906
    The "simple method" is to use the classes you have have now, and then create it with topping and base name as parameter:
    Code:
    pizza.setBase(new PizzaBase("Thick"));
    pizza.addTopping(new PizzaTopping("Corn"));
    pizza.addTopping(new PizzaTopping("Tomato"));
    double cost = pizza.getCost();
    You will have to create the setBase (or putBase) function to accept PizzaBase as parameter.

    But it is better to have the class constructor accepting name and cost at the creation, instead of have the name and priced fixed in the class, similar to how Scorpions4ever have with the PizzaBase.
    then you should be able to create it like this:
    Code:
    pizza.setBase(new PizzaBase("Thick",1.0));
    pizza.addTopping(new PizzaTopping("Corn",2.0));
    pizza.addTopping(new PizzaTopping("Tomato",1.5));
    double cost = pizza.getCost();
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0
    Originally Posted by MrFujin
    The "simple method" is to use the classes you have have now, and then create it with topping and base name as parameter:
    Code:
    pizza.setBase(new PizzaBase("Thick"));
    pizza.addTopping(new PizzaTopping("Corn"));
    pizza.addTopping(new PizzaTopping("Tomato"));
    double cost = pizza.getCost();
    You will have to create the setBase (or putBase) function to accept PizzaBase as parameter.

    But it is better to have the class constructor accepting name and cost at the creation, instead of have the name and priced fixed in the class, similar to how Scorpions4ever have with the PizzaBase.
    then you should be able to create it like this:
    Code:
    pizza.setBase(new PizzaBase("Thick",1.0));
    pizza.addTopping(new PizzaTopping("Corn",2.0));
    pizza.addTopping(new PizzaTopping("Tomato",1.5));
    double cost = pizza.getCost();
    I'm not entirely sure about what you are suggesting. Could you demonstrate with the code? Here is what I have right now:

    The Pizza class:

    Code:
    import java.util.ArrayList;
    
    public class Pizza {
    
    	PizzaBase base;
    	ArrayList<PizzaTopping> toppingList = new ArrayList<PizzaTopping>();
    	
    	Pizza(PizzaBase b){
    		base = b;
    	}
    	
    	public void putBase(PizzaBase b){
    //		PizzaBase base = new PizzaBase(b);
    //		base.getCost();
    	}
    	
    	public void addTopping(PizzaTopping topping){
    		toppingList.add(topping);
    	}
    	
    	public double pizzaCost(){	
    		return base.getCost();
    			
    		}
    	}
    The PizzaBase class - I've changed this according to what Scropions4ever told me to do
    Code:
    	
    public class PizzaBase {
    
    	private String base;
    	private double cost;
    	
    	public PizzaBase(String base, double cost){
    		this.base = base;
    		this.cost = cost;
    	}
    
    	public String getBase() {
    		return base;
    	}
    		
    	public double getCost() {
    		return cost;
    	}
    
    	public void setBase(String base) {
    		this.base = base;
    	}
    	
    	public void setCost(double cost) {
    		this.cost = cost;
    	}
    	
      }
    The PizzaTopping class - Need a bit of extra help with this one.
    Code:
    public class PizzaTopping {
    	
    	private String topping; 
    	private double cost;
    	
    	{
    	if(topping.equals("tomato sauce")){ 
    		setCost(0.1);
    	}
    		
    	else if(topping.equals("olives")){
    		setCost(0.1);
    	}
    	
    	else if(topping.equals("corn")){
    		setCost(0.15);
    	}
    	
    	else if(topping.equals("mushrooms")){
    		setCost(0.2);
    	}
    	
    	else if(topping.equals("pineapple")){
    		setCost(0.25);
    	}}
    
    	public double getCost(){
    		return cost;
    	}
    	
    	private void setCost(double cost) {
    		this.cost = cost;
    	}
    }
    As I said I am a beginner so I'm sorry if what may seem obvious to you appears to puzzle me at times. I'd be glad if you can just bear with me, I'm sure you've all been there! Thanks a lot.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    74
    Rep Power
    18
    You are very close to a solution. (There are a lot of ways to tackle a problem). I would come up with this, simple, approach:

    Pizza.java:
    Code:
    import java.util.ArrayList;
    
    public class Pizza {
    
    	private PizzaBase base;
    	private ArrayList<PizzaTopping> toppingList;
    	
    	public Pizza(PizzaBase b){
    		this.setBase(b);
    		this.toppingList = new ArrayList<PizzaTopping>();
    	}
    	
    	public void setBase(PizzaBase b){
    		base = b;
    	}
    	
    	public void addTopping(PizzaTopping topping){
    		toppingList.add(topping);
    	}
    	
    	public double pizzaCost(){	
    		double cost = base.getCost();
    		for(PizzaTopping topping : toppingList)
    		{
    			cost += topping.getCost();
    		}
    		return cost;
    	}
    	
    	public void completeOrder()
    	{
    		System.out.println("*****************");
    		System.out.println(base.getName() + ": \t" + base.getCost());
    		for(PizzaTopping topping : toppingList)
    		{
    			System.out.println(topping.getName() + ": " + topping.getCost());
    		}
    		System.out.println();
    		System.out.println("total: "+ pizzaCost());
    	}
    }
    PizzaBase.java:
    Code:
    public class PizzaBase {
    
    	private String baseName;
    	private double cost;
    	
    	public PizzaBase(String base, double cost){
    		this.baseName = base;
    		this.cost = cost;
    	}
    
    	public String getName() {
    		return baseName;
    	}
    		
    	public double getCost() {
    		return cost;
    	}
    
    	
    	
      }
    PizzaTopping.java:
    Code:
    public class PizzaTopping {
    	
    	private String toppingName; 
    	private double cost;
    	
    	public PizzaTopping(String tn, double c)
    	{
    	
    		this.toppingName = tn;
    		this.cost = c;
    	}
    	
    	public String getName()
    	{
    		return toppingName;
    	}
    	
    	public double getCost(){
    		return cost;
    	}
    	
    }
    PizzaMain.java:
    Code:
    public class PizzaMain
    {
    
    	public static void main(String[] args)
    	{
    		Pizza pizza = new Pizza(new PizzaBase("thin", 3.0));
    		pizza.addTopping(new PizzaTopping("Tomato sauce", 0));
    		pizza.addTopping(new PizzaTopping("Mozarella",1.50));
    		pizza.addTopping(new PizzaTopping("Ham", 2.00));
    		pizza.completeOrder();
    	
    	
    	}
    
    
    }

    Another nice addition would be to add the PizzaTopping and PizzaBase entries to a file. For example:

    PizzaTopping.txt:
    pineapple 1.00
    mushrooms 1.25
    chilis 2.50

    PizzaBase.txt:
    thin 4.00
    thick 4.50
    italian 4.95

    Then , you create two classes: for example a PizzaToppingProvider and a PizzaBase provider, that read those files and which can only return instances of the entries in those files.

    Comments on this post

    • MrFujin agrees

IMN logo majestic logo threadwatch logo seochat tools logo