I'm starting with this programming with Java and in one part of an exercise I got stuck, I have a Patient class and two methods, one method will ask me for all the patient's data and it has to store them in an array so that the other method show it, how could I do it? Is it necessary to be able to store them using Getters and Setters or is there some other way? EDIT: I just made everything private, maybe it's easier that way, still I don't know how I can display the data in the ShowData() method; I don't know if what I've done is right
import java.util.Scanner;
public class Paciente {
private float alturaCuadrado;
private int x = 0;
private int contador = 0;
private String nombre = null;
private int n_pacientes = 0;
private int edad = 0;
private double peso;
private float altura;
private double imc;
public float getAlturaCuadrado() {return alturaCuadrado;}
public int getX() { return x; }
public int getContador() {return contador;}
public String getNombre() {return nombre;}
public int getN_pacientes() {return n_pacientes;}
public int getEdad () {return edad;}
public double getPeso () {return peso;}
public float getAltura () {return altura;}
public double getImc() { return imc;}
public void setAlturaCuadrado(float alturaCuadrado)
{this.alturaCuadrado = alturaCuadrado;}
public void setX (int x) { this.x = x; }
public void setContador(int contador) {this.contador =
contador;}
public void setNombre (String nombre) {this.nombre = nombre;}
public void setN_pacientes(int n_pacientes) {this.n_pacientes =
n_pacientes;}
public void setEdad(int edad) {this.edad = edad;}
public void setPeso(double peso) {this.peso = peso;}
public void setAltura(float altura) {this.altura = altura;}
public void setImc(double imc) {this.imc = imc;}
public void obtenerDatos() {
Scanner teclado = new Scanner (System.in);
System.out.println("¿Cuantos pacientes quieres
introducir?");
n_pacientes = teclado.nextInt();
Paciente pacientes [] = new Paciente [n_pacientes];
for (int i = 0; i < n_pacientes;i++ ) {
pacientes [i] = new Paciente();
System.out.println("¿Cual es tu nombre?");
nombre = teclado.next();
pacientes[i].setNombre(nombre);
System.out.println("¿Cuál es tu edad?");
edad = teclado.nextInt();
pacientes[i].setEdad(edad);
System.out.println("¿Cuál es tu peso?");
peso = teclado.nextDouble();
pacientes[i].setPeso(peso);
System.out.println("¿Cuál es tu altura? (En metros)");
altura = teclado.nextFloat();
pacientes[i].setAltura(altura);
}
}
public void mostrarDatos() {
for (int j = 0; j < n_pacientes; j++) {
System.out.println(pacientes[j].getPeso());
}
}
Based on what you indicate with the new data, I will tell you how it could be done, obviously this could vary depending on where you are in the theory. Therefore this answer would be based on the things you showed in the question.
First, we would have the Class
Paciente
in this we will have the following data.The other 2 methods could go in this same class, in your main class or in your service class (that's why I was asking in which part of the theory you are going), but it could be like that.