How can I send to print in a class, several variables that I enter and have in another class? [closed]

-1

The program is for a rental of bicycles, I enter the time desired by the client and his name to authorize the rent, and in the other class I send to print the variables of the time, the name of the client and the total amount of income The main class is this:

package bicicletas;

public class Bicicletas {

    public static void main (String[] args ) {

        System.out.println("Bienvenido a rentas Bayka");

        Renta R = new Renta();
        R.Datos();
    }

    public static void despliegue () {

        Renta instancia1 = new Renta();
        Renta instancia2 = new Renta();
        Renta instancia3 = new Renta();



    System.out.println("El tiempo de renta es: "+instancia1.tiempo);
    System.out.println("Su nombre es: "+instancia2.nombre);
    System.out.println("El precio de la renta es: "+instancia3.total);
    System.out.println("Gracias por su renta :) ");
    }
}

The second class is this:

package bicicletas;

import java.util.Scanner;

public class Renta {

        int total;
        int tiempo;
        String nombre;

    public static void Datos() {

        int precio=30;

        Scanner teclado = new Scanner (System.in);
    System.out.println("Ingrese el tiempo que le gustaria rentar (en horas): ");
        tiempo = teclado.nextInt();
        System.out.println("Ingrese su nombre: ");
        nombre = teclado.next();

        total = tiempo * precio;

        Bicicletas D = new Bicicletas();
        D.despliegue();
    }

}
    
asked by JCarlos25 08.09.2017 в 20:07
source

1 answer

2

Let's make a quick analysis of your code.

Start with the main.
There you define a Renta class. and you call your Data method. And that's when I realize that you do not know what to program with classes.

You are using the class as if it were a procedure, without understanding the concept of classes.

Your rental class, after doing what you have to do, calls the bicycle class again (which is where the main is), and calls the deployment function.

(Separate note: there is a misuse of static functions everywhere, you should read about that too).

The deployment function generates 3 empty rental classes, and then tries to print a data from each of the 3 different classes that do not exist. I do not know how it was the original exercise of your task, and if you are studying classes or structured programming, but you would have to do all your code back and it does not make much sense for us to write it for you.

As a side note, to print the value of a variable in a class, what you do is fine.

Renta instancia1 = new Renta();
System.out.println("El tiempo de renta es: "+instancia1.tiempo);

Except that the class that contains instance1 is empty, and will not print anything.

You have a misconception about scope of variables (scope in English, the life cycle of them).

For your case to work, it should be something like this:

Renta instancia1 = new Renta();
instancia1.Datos();
System.out.println("El tiempo de renta es: "+instancia1.tiempo);
    
answered by 08.09.2017 в 20:28