Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

Pizza Fridge

Thread view: 
FopZ - 21 Apr 2007 17:53 GMT
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)
Jim Korman - 22 Apr 2007 02:45 GMT
>Hi
>I have to writ an new class for a project named "PizzaFride" in this
[quoted text clipped - 87 lines]
>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
[quoted text clipped - 37 lines]
>    private PizzaFridge PizzaFridge;
>    private Pizza Pizza;

>    public Joe()
>    {
[quoted text clipped - 14 lines]
>        PizzaFridge.printContent();
>        PizzaFridge.whichPizzaShouldIEat();

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

>        PizzaFridge.printContent();
>
[quoted text clipped - 8 lines]
>
>(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
FopZ - 22 Apr 2007 12:11 GMT
Hi

Thank you very much Jim ! It works :)


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.