You have a few mistakes here and there and things that are too much.
First:
You do not use the following libraries, you can delete them:
import java.util.Arrays;
import java.util.Scanner;
Second: In the main
, why do you make an object of the class to call the contadorVentas()
method? With the use of static
you can solve this problem. You make the method static
, example public static void contadorVentas()
and you can call the method directly without needing an instance of your own class. Remember that a class, method or field declared as static
can be accessed or invoked without the need to instantiate an object of the class.
Third: Why is your main
empty and all your logic is inside the contadorVentas()
method? The methods, functions or procedures should be used to solve things easily and quickly. I advise you to translate your logic to main
and use the other methods that you have below.
Fourth: Why are the declarations of your variables below main
?. If you do not need to be global, then you must declare them within main
.
Fifth: You never close the Scanner. When you finish with it, you must close it as follows: teclado.close();
Sixth: You have methods type void
and you do not return anything.
Seventh: Why do you have for
up to 100000? o.o I think between 5-10 is enough.
Eighth: Why do you show the major, minor, etc ... within the same for
?. The correct thing would be to ask for the numbers first and when leaving the for
then show the results.
Ninth: You have valorProducto
declared as type Double
, then to capture a Double
with Scanner
you need .nextDouble();
Among many other errors .... Your program can be greatly simplified. I have done it for you and I have commented it so that you can understand it. I could not understand what you mean by "sales value" and "value greater than 100,000" but, I've done what I've believed. You can edit it to your liking.
Your program fixed, corrected and doing what you need:
import java.util.Scanner;
import java.util.ArrayList;
public class Ferreteria {
public static void main(String[] args) {
// Creamos un ArrayList de tipo Double
ArrayList<Double> valores = new ArrayList<Double>();
// Lógica
Scanner teclado = new Scanner(System.in);
// Pediremos 3 valores
for (int contador = 0; contador < 3; contador++) {
System.out.println("Valor del producto #" + contador + ": ");
// Agregamos el valor capturado al ArrayList
valores.add(teclado.nextDouble());
}
// Cerrar Scanner
teclado.close();
// Calcular el mayor
double valorMayor = 0;
for (int contador = 0; contador < valores.size(); contador++) {
// Si el valor en la posición "contador" es mayor a "valorMayor"
if (valores.get(contador) > valorMayor) {
valorMayor = valores.get(contador);
}
}
System.out.println("El valor mayor es: " + valorMayor);
// Calcular el menor
double valorMenor = valorMayor;
for (int contador = 0; contador < valores.size(); contador++) {
// Si el valor en la posición "contador" es menor a "valorMenor"
if (valores.get(contador) < valorMenor) {
valorMenor = valores.get(contador);
}
}
System.out.println("El valor menor es: " + valorMenor);
// Calcular el promedio
double valorPromedio = 0;
for (int contador = 0; contador < valores.size(); contador++) {
// Sumamos todos los valores
valorPromedio += valores.get(contador);
}
// Dividimos entre el número de valores que tenga el ArrayList
System.out.println("El valor promedio es: " + valorPromedio / valores.size());
// Valor de venta
double valorVentaTotal = 0;
for (int contador = 0; contador < valores.size(); contador++) {
// Sumamos todos los valores
valorVentaTotal += valores.get(contador);
}
System.out.println("El valor de venta total es: " + valorVentaTotal);
// Cantidad de ventas mayores a 100,000
int cantidadVentasMayores = 0;
for (int contador = 0; contador < valores.size(); contador++) {
// Si el valor en la posición "contador" es mayor a 100,000
if (valores.get(contador) > 100000) {
cantidadVentasMayores++;
}
}
System.out.println("Productos que exceden los 100,000: " + cantidadVentasMayores);
}
}
I hope I have helped you!