Pizza Fridge
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=FChlschrank ist leer");
return;
}
System.out.println("Der K=FChlschrank enth=E4lt:");
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=E4chstes
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=FChlschrank 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)