Pizza Fridge

Discussion in 'Java' started by FopZ, Apr 21, 2007.

  1. FopZ

    FopZ Guest

    Hi
    I have to writ an new class for a project named "PizzaFride" in this
    project are the 2 classes "Pizza" and PizzaFridg"
    already given. Here are they:

    /**
    * Class that represents a Pizza.
    *
    * @author David Schuler
    *
    */
    public class Pizza {

    /**
    * Number of days the pizza is durable.
    */
    private int durableDays;

    /**


    {
    return String.format("Pizza %s (noch %d Tage haltbar)", description,
    durableDays);
    }

    /**
    * Returns the number of days the pizza is durable.
    *
    * @return The number of days the pizza is durable
    */
    public int getDurableDays() {
    return durableDays;
    }

    }




    And the other class:




    import java.util.ArrayList;
    import java.util.List;

    /**
    * Class that represents an intelligent pizza fridge, which keeps
    track of the
    * durability of the contained pizzas.
    *
    * @author David Schuler
    *
    */
    public class PizzaFridge {

    private List<Pizza> contents = new ArrayList<Pizza>();


    /**
    * Add a pizza to the fridge.
    */
    public void addPizza(Pizza item) {
    contents.add(item);
    }

    /**
    * Prints the contents of the fridge to the System.out.
    */
    public void printContent() {
    if (contents.size() == 0) {
    System.out.println("Der Kühlschrank ist leer");
    return;
    }
    System.out.println("Der Kühlschrank enthält:");
    for (Pizza item : contents) {
    System.out.println("\t" + item);
    }
    }

    /**
    * @return The pizza with the lowest number of durable days.
    */
    public Pizza whichPizzaShouldIEat() {
    if (contents.size() == 0) {
    return null;
    }
    Pizza pizzaToConsume = contents.get(0);
    int min = pizzaToConsume.getDurableDays();
    for (Pizza item : contents) {
    if (min > item.getDurableDays())
    {
    pizzaToConsume = item;
    min = pizzaToConsume.getDurableDays();
    System.out.println("Diese Pizza sollte als nächstes
    gegessen werden:");
    System.out.println(pizzaToConsume);
    preparePizza(pizzaToConsume); // i have paste this
    line
    }

    }
    return pizzaToConsume;

    }






    /**
    * If the pizza is contained in the fridge it automatically
    removed from the
    * fridge and then prepared. Otherwise an error message is given.
    *
    * @param pizza
    * The Pizza to prepare.
    */
    public void preparePizza(Pizza Pizza) {
    if (contents.contains(Pizza)) {
    contents.remove(Pizza);
    System.out.println("Diese Pizza wurde zubereitet:");
    System.out.println(Pizza);
    } else {
    System.out
    .println("Diese Pizza ist nicht (mehr) im
    Kühlschrank enthalten");
    }
    }

    }



    My task was it to write a new class with the method usePizzaFridge().
    This class should add 3 new pizzas and find out witch of this pizzas
    should eat first. This pizza have to prepare.
    My new class works only with th following text:

    public class Joe
    {

    private PizzaFridge PizzaFridge;
    private Pizza Pizza;

    public Joe()
    {

    }

    public void usePizzaFridge()
    {

    PizzaFridge = new PizzaFridge();
    Pizza = new Pizza (20, "Quattro Stagioni");
    PizzaFridge.addPizza(Pizza);
    Pizza = new Pizza (3, "Margherita");
    PizzaFridge.addPizza(Pizza);
    Pizza = new Pizza (17, "Pizza Napoli");
    PizzaFridge.addPizza(Pizza);
    PizzaFridge.printContent();
    PizzaFridge.whichPizzaShouldIEat();
    PizzaFridge.printContent();



    }

    }


    But if i change the durable days of the margherita pizza, for example
    to 30 days the programm doesn't react and
    prepare again the margherita pizza.
    I hope someone can help me! thx!

    (and sorry for my bad english)
    FopZ, Apr 21, 2007
    #1
    1. Advertising

  2. FopZ

    Jim Korman Guest

    On 21 Apr 2007 09:53:25 -0700, FopZ <> wrote:

    >Hi
    >I have to writ an new class for a project named "PizzaFride" in this
    >project are the 2 classes "Pizza" and PizzaFridg"
    >already given. Here are they:
    >
    >/**
    > * Class that represents a Pizza.
    > *
    > * @author David Schuler
    > *
    > */
    >public class Pizza {
    >
    > /**
    > * Number of days the pizza is durable.
    > */
    > private int durableDays;
    >
    > /**
    >
    >
    >{
    > return String.format("Pizza %s (noch %d Tage haltbar)", description,
    > durableDays);
    > }
    >
    > /**
    > * Returns the number of days the pizza is durable.
    > *
    > * @return The number of days the pizza is durable
    > */
    > public int getDurableDays() {
    > return durableDays;
    > }
    >
    >}
    >
    >
    >
    >
    >And the other class:
    >
    >
    >
    >
    >import java.util.ArrayList;
    >import java.util.List;
    >
    >/**
    > * Class that represents an intelligent pizza fridge, which keeps
    >track of the
    > * durability of the contained pizzas.
    > *
    > * @author David Schuler
    > *
    > */
    >public class PizzaFridge {
    >
    > private List<Pizza> contents = new ArrayList<Pizza>();
    >
    >
    > /**
    > * Add a pizza to the fridge.
    > */
    > public void addPizza(Pizza item) {
    > contents.add(item);
    > }
    >
    > /**
    > * Prints the contents of the fridge to the System.out.
    > */
    > public void printContent() {
    > if (contents.size() == 0) {
    > System.out.println("Der Kühlschrank ist leer");
    > return;
    > }
    > System.out.println("Der Kühlschrank enthält:");
    > for (Pizza item : contents) {
    > System.out.println("\t" + item);
    > }
    > }
    >
    > /**
    > * @return The pizza with the lowest number of durable days.
    > */
    > public Pizza whichPizzaShouldIEat() {
    > if (contents.size() == 0) {
    > return null;
    > }
    > Pizza pizzaToConsume = contents.get(0);
    > int min = pizzaToConsume.getDurableDays();
    > for (Pizza item : contents) {
    > if (min > item.getDurableDays())
    > {
    > pizzaToConsume = item;
    > min = pizzaToConsume.getDurableDays();
    > System.out.println("Diese Pizza sollte als nächstes
    >gegessen werden:");
    > System.out.println(pizzaToConsume);


    -----------------------------------------------------------------------
    Do not perform this here. preparePizza will attempt to remove the
    current pizzaToConsume for each time through the loop! This will
    cause an exception in preparePizza when the remove is attempted.
    > preparePizza(pizzaToConsume); // i have paste this
    >line
    > }
    >
    > }
    > return pizzaToConsume;
    >
    > }
    >
    >
    >
    >
    >
    >
    > /**
    > * If the pizza is contained in the fridge it automatically
    >removed from the
    > * fridge and then prepared. Otherwise an error message is given.
    > *
    > * @param pizza
    > * The Pizza to prepare.
    > */
    > public void preparePizza(Pizza Pizza) {
    > if (contents.contains(Pizza)) {
    > contents.remove(Pizza);
    > System.out.println("Diese Pizza wurde zubereitet:");
    > System.out.println(Pizza);
    > } else {
    > System.out
    > .println("Diese Pizza ist nicht (mehr) im
    >Kühlschrank enthalten");
    > }
    > }
    >
    >}
    >
    >
    >
    >My task was it to write a new class with the method usePizzaFridge().
    >This class should add 3 new pizzas and find out witch of this pizzas
    >should eat first. This pizza have to prepare.
    >My new class works only with th following text:
    >
    >public class Joe
    >{
    >
    > private PizzaFridge PizzaFridge;
    > private Pizza Pizza;



    >
    > public Joe()
    > {
    >
    > }
    >
    > public void usePizzaFridge()
    > {
    >
    > PizzaFridge = new PizzaFridge();
    > Pizza = new Pizza (20, "Quattro Stagioni");
    > Pizza = new Pizza (20, "Quattro Stagioni");
    > PizzaFridge.addPizza(Pizza);
    > Pizza = new Pizza (3, "Margherita");
    > PizzaFridge.addPizza(Pizza);
    > Pizza = new Pizza (17, "Pizza Napoli");
    > PizzaFridge.addPizza(Pizza);
    > PizzaFridge.printContent();
    > PizzaFridge.whichPizzaShouldIEat();


    -------> Determine which pizza to eat.
    -------> pizza = PizzaFridge.whichPizzaShouldIEat();
    -------> PizzaFridge.preparePizza(pizza);

    > PizzaFridge.printContent();
    >
    >
    >
    > }
    >
    >}
    >
    >
    >But if i change the durable days of the margherita pizza, for example
    >to 30 days the programm doesn't react and
    >prepare again the margherita pizza.
    >I hope someone can help me! thx!
    >
    >(and sorry for my bad english)


    David, Guten Tag...

    I see a couple of items that I have made comments on
    in your code above.

    May I add.

    You should never use a variable name which is the
    same as the class name. Example Pizza Pizza. This
    will become very confussing.

    Jim
    Jim Korman, Apr 22, 2007
    #2
    1. Advertising

  3. FopZ

    FopZ Guest

    Hi

    Thank you very much Jim ! It works :)
    FopZ, Apr 22, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    6
    Views:
    362
    Ken Cox - Microsoft MVP
    May 7, 2006
  2. n1kk1

    HI PIZZA LOVERS :D

    n1kk1, Jan 1, 2010, in forum: Java
    Replies:
    0
    Views:
    390
    n1kk1
    Jan 1, 2010
  3. emma jacob
    Replies:
    0
    Views:
    158
    emma jacob
    Jun 22, 2011
  4. Replies:
    4
    Views:
    92
    Ken Cox - Microsoft MVP
    May 7, 2006
  5. Rich Morin
    Replies:
    0
    Views:
    85
    Rich Morin
    Oct 3, 2005
Loading...

Share This Page