I have the following exercise:
In a port, moorings (parking space) for boats are rented. For each rental, the name, the client number, the number of days of rental, the position of the mooring and the boat that occupies it are stored. A boat is characterized by its registration, its Slovak (in meters) and the year of manufacture. The boats differ according to their type and characteristics in:
Sailboats: number of masts.
Sports boats: power in the engine.
Luxury yachts: engine power and number of cabins.
The rental cost is calculated by multiplying the number of days of occupation by the daily cost, which varies according to the type of boat:
For sailboats it is calculated by multiplying the number of masts by $15.
For boats it is calculated by multiplying the meters of Slovakia by $35.
For yachts it is calculated by multiplying the number of cabins by $17.
- Use inheritance to implement classes and their relationships.
Generate a test class that:
- Enter the information for each type of ship.
- Using polymorphism compute the rent paid by the customer.
- Using an interface displays the data for each of the ship types.
I have made the following code, but I have some questions. In principle I have to create a test class so that the user can enter information for each of the types of boats (sailboats, luxury yachts, boats) and I already entered that information, but not as expected.
I wanted to send all the information of the sailboat through the constructor of the class Renta
, ie Renta(String nombre, String posicion, int numDias, int numCliente, Barco barco)
, but I don't know how to send it information through the attribute barco
, so to solve the problem I created one interface
so that I could store and print the data of the sailboat.
interface Adicional {
String muestraDatos();
double costoRenta();
}
class Renta implements Adicional {
private String nombre, posicion;
private int numDias, numCliente;
private Barco barco;
Renta() {
}
Renta(String nombre, String posicion, int numDias, int numCliente, Barco barco) {
this.nombre = nombre;
this.posicion = posicion;
this.numDias = numDias;
this.numCliente = numCliente;
this.barco = barco;
}
public String muestraDatos() {
return nombre + " " + posicion + " " + numDias + " " + numCliente + " " + barco;
}
public double costoRenta() {
return 0;
}
String getNombre() {
return nombre;
}
String getPosicion() {
return posicion;
}
int getNumDias() {
return numDias;
}
int getNumCliente() {
return numCliente;
}
Barco getBarco() {
return barco;
}
void setNombre(String n) {
nombre = n;
}
void setPosicion(String p) {
posicion = p;
}
void setNumDias(int n) {
numDias = n;
}
void setNumCliente(int n) {
numCliente = n;
}
void setBarco(Barco barco) {
this.barco = barco;
}
}
abstract class Barco {
private int matricula, year;
protected double eslova;//en metros
Barco(int matricula, int year, double eslova) {
this.matricula = matricula;
this.year = year;
this.eslova = eslova;//longitud del barco
}
public double costoRenta() {
return 0;
}
public String muestraDatos() {
return matricula + " " + year + " " + eslova + " ";
}
}
class Velero extends Barco {
private int numMastiles;
Velero(int matricula, int year, double eslova, int numMastiles) {
super(matricula, year, eslova);
this.numMastiles = numMastiles;
}
public String muestraDatos() {
return super.muestraDatos() + " " + numMastiles;
}
public double costoRenta() {
return 15 * numMastiles;
}
}
abstract class BoteConMotor extends Barco {
protected double potenciaMotor;
BoteConMotor(int matricula, int year, double eslova, double potenciaMotor) {
super(matricula, year, eslova);
this.potenciaMotor = potenciaMotor;
}
public String muestraDatos() {
return super.muestraDatos() + " " + potenciaMotor;
}
}
class Embarcaciones extends BoteConMotor {
Embarcaciones(int matricula, int year, double eslova, double potenciaMotor) {
super(matricula, year, eslova, potenciaMotor);
}
public double costoRenta() {
return 35 * eslova;
}
}
class YateLujo extends BoteConMotor {
private int numCamarotes;
YateLujo(int matricula, int year, double eslova, double potenciaMotor, int numCamarotes) {
super(matricula, year, eslova, potenciaMotor);
this.numCamarotes = numCamarotes;
}
public String muestraDatos() {
return super.muestraDatos() + " " + numCamarotes;
}
public double costoRenta() {
return numCamarotes * 17;
}
}
public class AlquilacionBARCOS {
public static void main(String[] args) {
Scanner r = new Scanner(System.in);
Renta s = new Renta();
Barco b[] = new Barco[3];
System.out.println("Introduzca la informacion para el barco velero\n");
System.out.println("nombre del cliente");
String nam = r.nextLine();
s.setNombre(nam);
System.out.println("num. de cliente");
int n = r.nextInt();
s.setNumCliente(n);
System.out.println("num. de dias de renta");
int m = r.nextInt();
s.setNumDias(m);
System.out.println("posicion de amarre");
r.nextLine();
String p = r.nextLine();
s.setPosicion(p);
System.out.println("matricula:");
int q = r.nextInt();
System.out.println("eslova:");
double w = r.nextDouble();
System.out.println("year de fabricacion:");
int k = r.nextInt();
System.out.println("num. de mastiles:");
int v = r.nextInt();
b[0] = new Velero(q, k, w, v);
System.out.println(b[0].muestraDatos() + " " + b[0].costoRenta()+" "+s.muestraDatos());
}
}
My question is: how to send all the information about some type of ship using this constructor Renta(String nombre, String posicion, int numDias, int numCliente, Barco barco)
?
First of all, let me give you a piece of advice: It's hard to go through 200 lines of code . If you reduce the code you've posted a bit (for example removing all those setters and getters, and the user interaction) it will be easier to help you.
In addition, the fact that you have included the statement of your exercise suggests that you require more help. If that is the case and there is no information on SO, you can ask another question. You can find more information in the help center .
As for your question, what you ask for in the statement sounds like Dependency Injection . Dependency injection consists of providing an object with all the objects it needs, instead of forcing it to instantiate them itself . The example given on Wikipedia uses a design pattern that is perhaps too much for your exercise, but you can implement it in other ways:
In this case, instantiating the
Barco
in question anonymously in the constructor call of theRenta
.The problem
The problem with your code is that you need to know how many ships and what kind the user wants before you even instantiate the Rent class .
My recomendation
My recommendation is that you redesign your code.
Probably the class( EDIT : Need to create one rental per ship), AND you can create a hierarchy like this:Renta
can be disposableYour Rent class could look like this:
In this way, through polymorphism, you will be able to calculate the rent and the data for any type of ship that you create. The implementation with Dependency Injection could be like this ( EDIT : Previously a list of was used
Barco
):You have the basics, I will only focus on your question, keep in mind that when extending a class in this case Barco the different classes that extend from it in this case the types have the same abstract class as their parent and can be sent through the parameter in the constructor or function, for example