Re: Pizza Fridge
On 21 Apr 2007 09:53:25 -0700, FopZ <max2k@swissinfo.org> 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