
Implements a class hierarchy for a restaurant management system that models different types of dishes.
Abstract base class
You must create an abstract class called Plato with the following attributes:
name (String)
Preparation time (int) in minutes
calories (double)
price (double)
The Plato class must have:
A constructor that initializes all attributes
A concrete serve() method that returns a String with the message "Serving [dish name]"
A concrete method calculatePrice() that returns the base price plus an additional 10% (i.e. price * 1.10)
An abstract method prepare() that will return a String with the specific preparation instructions for each type of dish
Derived concrete classes
Then, create three concrete classes that inherit from Plato:
Pizza class with additional attributes:
massType (String)
MainIngredient (String)
size (int) in centimeters
This class must implement the prepare() method which must return: "Kneading pizza base of type [DoughType], adding tomato sauce, sprinkling cheese and adding [MainIngredient]. Baking pizza of [size] cm for [PreparationTime] minutes."
Salad class with additional attributes:
MainVegetable (String)
dressing (String)
This class must implement the prepare() method, which should return: "Washing and chopping [MainVegetable] and the rest of the fresh vegetables. Mixing the ingredients in a bowl and adding [Dressing] dressing. Prep time: [PrepTime] minutes."
Soup class with additional attributes:
temperature (int) in degrees Celsius
BrothBase (String)
This class must implement the prepare() method which should return: "Preparing base broth from [BrothBase]. Adding vegetables and spices. Simmering for [PreparationTime] minutes. Serve at [Temperature] degrees."
Creating objects
Finally, create a main method where:
Create an instance of each dish type
Call the prepare() method of each dish and display the result in the console
Call the serve() method of each dish and display the result in the console
Display the calculated price for each dish using the calculatePrice() method
package oop.restaurante;
// codificado por Javier Cachón Garrido en MX-Linux con IntelliJ IDEA Community Edition.
// Método main
public class Restaurante {
public static void main(String[] args) {
Plato pizza = new Pizza("Pizza Margarita", 20, 600, 8.0, "fina", "jamón", 40);
Plato ensalada = new Ensalada("Ensalada", 10, 350, 4.5, "lechuga", "Javier");
Plato sopa = new Sopa("Sopa de pollo", 30, 100, 3.2, 70, "pollo");
System.out.println(pizza.preparar());
System.out.println(pizza.servir());
System.out.println("Precio final: " + pizza.calcularPrecio() + "€.");
System.out.println();
System.out.println(ensalada.preparar());
System.out.println(ensalada.servir());
System.out.println("Precio final: " + ensalada.calcularPrecio() + "€.");
System.out.println();
System.out.println(sopa.preparar());
System.out.println(sopa.servir());
System.out.println("Precio final: " + sopa.calcularPrecio() + "€.");
}
}
abstract class Plato {
protected String nombre;
protected int tiempoPreparacion;
protected double calorias;
protected double precio;
public Plato(String nombre, int tiempoPreparacion, double calorias, double precio) {
this.nombre = nombre;
this.tiempoPreparacion = tiempoPreparacion;
this.calorias = calorias;
this.precio = precio;
}
// Metodos Concretos
public String servir() {
return "Sirviendo " + nombre;
}
public double calcularPrecio() {
return precio * 1.10;
}
public abstract String preparar();
}
// Clase Pizza
class Pizza extends Plato {
private String tipoDeMasa;
private String ingredientePrincipal;
private int tamano;
public Pizza(String nombre, int tiempoPreparacion, double calorias, double precio,
String tipoDeMasa, String ingredientePrincipal, int tamano) {
super(nombre, tiempoPreparacion, calorias, precio);
this.tipoDeMasa = tipoDeMasa;
this.ingredientePrincipal = ingredientePrincipal;
this.tamano = tamano;
}
@Override
public String preparar() {
return "Amasando la base de pizza de tipo " + tipoDeMasa +
", añadiendo salsa de tomate, espolvoreando queso y añadiendo " + ingredientePrincipal +
". Horneando pizza de " + tamano + " cm durante " + tiempoPreparacion + " minutos.";
}
}
// Clase Ensalada
class Ensalada extends Plato {
private String verduraPrincipal;
private String aderezo;
public Ensalada(String nombre, int tiempoPreparacion, double calorias, double precio,
String verduraPrincipal, String aderezo) {
super(nombre, tiempoPreparacion, calorias, precio);
this.verduraPrincipal = verduraPrincipal;
this.aderezo = aderezo;
}
@Override
public String preparar() {
return "Lavando y cortando " + verduraPrincipal +
" y el resto de vegetales frescos. Mezclando los ingredientes en un bol y añadiendo aderezo de " +
aderezo + ". Tiempo de preparación: " + tiempoPreparacion + " minutos.";
}
}
// Clase Sopa
class Sopa extends Plato {
private int temperatura;
private String baseDelCaldo;
public Sopa(String nombre, int tiempoPreparacion, double calorias, double precio,
int temperatura, String baseDelCaldo) {
super(nombre, tiempoPreparacion, calorias, precio);
this.temperatura = temperatura;
this.baseDelCaldo = baseDelCaldo;
}
@Override
public String preparar() {
return "Preparando caldo base de " + baseDelCaldo +
". Añadiendo verduras y especias. Cocinando a fuego lento durante " + tiempoPreparacion +
" minutos. Servir a " + temperatura + " grados.";
}
}
Result of execution and compilation:
Amasando la base de pizza de tipo fina, añadiendo salsa de tomate, espolvoreando queso y añadiendo jamón. Horneando pizza de 40 cm durante 20 minutos.
Sirviendo Pizza Margarita
Precio final: 8.8€.
Lavando y cortando lechuga y el resto de vegetales frescos. Mezclando los ingredientes en un bol y añadiendo aderezo de Javier. Tiempo de preparación: 10 minutos.
Sirviendo Ensalada
Precio final: 4.95€.
Preparando caldo base de pollo. Añadiendo verduras y especias. Cocinando a fuego lento durante 30 minutos. Servir a 70 grados.
Sirviendo Sopa de pollo
Precio final: 3.5200000000000005€.

Feedback and suggestions
The presented solution meets all the requirements of the statement. You have correctly implemented the class hierarchy with an abstract class, Dish, and concrete classes, Pizza, Salad, and Soup. Each concrete class properly implements the abstract method, prepare(), with specific preparation instructions. Furthermore, the main method creates instances of each type of dish, calls the methods, prepare(), serve(), and calculatePrice(), and displays the results on the console.
Strengths:
The class hierarchy structure is clear and follows good object-oriented programming practices.
The prepare(), serve() and calculatePrice() methods are correctly implemented and comply with the statement.
The code is readable and follows Java naming conventions.
Areas for improvement:
You might consider adding comments to your code to improve understanding, especially in methods and constructors.
Although it's not a requirement of the statement, you could implement basic validations in the constructors to ensure that attribute values are reasonable (for example, that the preparationTime and price are positive).
Overall, you've done an excellent job implementing the solution. Congratulations!
Open Source License: GPL: You can improve it, but it is not for commercial or business use, but for educational purposes.