bold text Good evening I have a problem I am doing a program that simulates a vending machine of products the conditions are as follows that accepts coins of 1 2 5 and 10 pesos accept tickets of 100 50 and 20 pesos my problem is that when I enter a quantity it takes the value I enter, for example if I enter 15 it takes that value I would like to know how I can restrict that since it only accepts specific currencies
this is my code:
import java.io. *;
public class vending machine {
public static void main(String args[]) throws IOException {
BufferedReader bufEntrada = new BufferedReader(new InputStreamReader(System.in));
int a1;
int a2;
int a3;
int a4;
int b1;
int b2;
int b3;
int b4;
int billete;
int billete1;
int billete2;
int c1;
int c2;
int c3;
int c4;
int moneda;
int moneda1;
int moneda2;
int moneda3;
int opc;
int total;
a1 = 8;
a2 = 10;
a3 = 12;
a4 = 15;
b1 = 18;
b2 = 20;
b3 = 25;
b4 = 30;
c1 = 7;
c2 = 5;
c3 = 22;
c4 = 21;
moneda = 0;
moneda1 = 0;
moneda2 = 0;
moneda3 = 0;
billete = 0;
billete1 = 0;
billete2 = 0;
System.out.println("Esta Maquina solo acepta Monedes de $1.00 2.00 5.00 10.00");
System.out.println("Esta Maquina solo acepta Billetes de $20.00 50.00 100.000");
System.out.println("**************************************");
System.out.println("Ingrese la Opcion del producto deseado");
System.out.println("1.-A1:Nito $8.00");
System.out.println("2.-A2:Donas espolvoreadas $10.00");
System.out.println("3.-A3:Donas $12.00");
System.out.println("4.-A4:Roles de canela $15.00");
System.out.println("5.-B1:Mantecadas $18.00");
System.out.println("6.-B2:Rebanadas $20.00");
System.out.println("7.-B3:Panque de nuez $25.00");
System.out.println("8.-B4:Colchones $30.00");
System.out.println("9.-C1:Bimbuñuelos $7.00");
System.out.println("10.-C2:Multigrano $5.00");
System.out.println("11.-C3:Conchas $22.00");
System.out.println("12.-C4:Bimbolete $21.00");
System.out.println("**************************************");
opc = Integer.parseInt(bufEntrada.readLine());
switch (opc) {
case 1:
System.out.println("Ingrese el dinero");
try{
moneda = Integer.parseInt(bufEntrada.readLine());
moneda1 = Integer.parseInt(bufEntrada.readLine());
moneda2 = Integer.parseInt(bufEntrada.readLine());
moneda3 = Integer.parseInt(bufEntrada.readLine());
billete = Integer.parseInt(bufEntrada.readLine());
billete1 = Integer.parseInt(bufEntrada.readLine());
billete2 = Integer.parseInt(bufEntrada.readLine());
}catch(Exception ex){
}
total = moneda+moneda1+moneda2+moneda3+billete+billete1+billete2;
if (total>a1) {
total = total-a1;
System.out.println("Recoja su producto");
System.out.println("Su cambio es:$"+total);
} else {
System.out.println("Recoja su producto");
}
total = total-a1;
break;
case 2:
System.out.println("Ingrese el dinero");
try{
moneda = Integer.parseInt(bufEntrada.readLine());
moneda1 = Integer.parseInt(bufEntrada.readLine());
moneda2 = Integer.parseInt(bufEntrada.readLine());
moneda3 = Integer.parseInt(bufEntrada.readLine());
billete = Integer.parseInt(bufEntrada.readLine());
billete1 = Integer.parseInt(bufEntrada.readLine());
billete2 = Integer.parseInt(bufEntrada.readLine());
}catch(Exception ex){
}
total = moneda+moneda1+moneda2+moneda3+billete+billete1+billete2;
if (total>a2) {
total = total-a2;
System.out.println("Recoja su producto");
System.out.println("Su cambio es:$"+total);
} else {
System.out.println("Recoja su producto");
}
total = total-a2;
break;
case 3:
System.out.println("Ingrese el dinero");
try{
moneda = Integer.parseInt(bufEntrada.readLine());
moneda1 = Integer.parseInt(bufEntrada.readLine());
moneda2 = Integer.parseInt(bufEntrada.readLine());
moneda3 = Integer.parseInt(bufEntrada.readLine());
billete = Integer.parseInt(bufEntrada.readLine());
billete1 = Integer.parseInt(bufEntrada.readLine());
billete2 = Integer.parseInt(bufEntrada.readLine());
}catch(Exception ex){
}
total = moneda+moneda1+moneda2+moneda3+billete+billete1+billete2;
if (total>a3) {
total = total-a3;
System.out.println("Recoja su producto");
System.out.println("Su cambio es:$"+total);
} else {
System.out.println("Recoja su producto");
}
total = total-a3;
break;
case 4:
System.out.println("Ingrese el dinero");
try{
moneda = Integer.parseInt(bufEntrada.readLine());
moneda1 = Integer.parseInt(bufEntrada.readLine());
moneda2 = Integer.parseInt(bufEntrada.readLine());
moneda3 = Integer.parseInt(bufEntrada.readLine());
billete = Integer.parseInt(bufEntrada.readLine());
billete1 = Integer.parseInt(bufEntrada.readLine());
billete2 = Integer.parseInt(bufEntrada.readLine());
}catch(Exception ex){
}
total = moneda+moneda1+moneda2+moneda3+billete+billete1+billete2;
if (total>a4) {
total = total-a4;
System.out.println("Recoja su producto");
System.out.println("Su cambio es:$"+total);
} else {
System.out.println("Recoja su producto");
}
total = total-a4;
break;
case 5:
System.out.println("Ingrese el dinero");
try{
moneda = Integer.parseInt(bufEntrada.readLine());
moneda1 = Integer.parseInt(bufEntrada.readLine());
moneda2 = Integer.parseInt(bufEntrada.readLine());
moneda3 = Integer.parseInt(bufEntrada.readLine());
billete = Integer.parseInt(bufEntrada.readLine());
billete1 = Integer.parseInt(bufEntrada.readLine());
billete2 = Integer.parseInt(bufEntrada.readLine());
}catch(Exception ex){
}
total = moneda+moneda1+moneda2+moneda3+billete+billete1+billete2;
if (total>b1) {
total = total-b1;
System.out.println("Recoja su producto");
System.out.println("Su cambio es:$"+total);
} else {
System.out.println("Recoja su producto");
}
total = total-b1;
break;
case 6:
System.out.println("Ingrese el dinero");
try{
moneda = Integer.parseInt(bufEntrada.readLine());
moneda1 = Integer.parseInt(bufEntrada.readLine());
moneda2 = Integer.parseInt(bufEntrada.readLine());
moneda3 = Integer.parseInt(bufEntrada.readLine());
billete = Integer.parseInt(bufEntrada.readLine());
billete1 = Integer.parseInt(bufEntrada.readLine());
billete2 = Integer.parseInt(bufEntrada.readLine());
}catch(Exception ex){
}
total = moneda+moneda1+moneda2+moneda3+billete+billete1+billete2;
if (total>b2) {
total = total-b2;
System.out.println("Recoja su producto");
System.out.println("Su cambio es:$"+total);
} else {
System.out.println("Recoja su producto");
}
total = total-b2;
break;
case 7:
System.out.println("Ingrese el dinero");
try{
moneda = Integer.parseInt(bufEntrada.readLine());
moneda1 = Integer.parseInt(bufEntrada.readLine());
moneda2 = Integer.parseInt(bufEntrada.readLine());
moneda3 = Integer.parseInt(bufEntrada.readLine());
billete = Integer.parseInt(bufEntrada.readLine());
billete1 = Integer.parseInt(bufEntrada.readLine());
billete2 = Integer.parseInt(bufEntrada.readLine());
}catch(Exception ex){
}
total = moneda+moneda1+moneda2+moneda3+billete+billete1+billete2;
if (total>b3) {
total = total-b3;
System.out.println("Recoja su producto");
System.out.println("Su cambio es:$"+total);
} else {
System.out.println("Recoja su producto");
}
total = total-b3;
break;
case 8:
System.out.println("Ingrese el dinero");
try{
moneda = Integer.parseInt(bufEntrada.readLine());
moneda1 = Integer.parseInt(bufEntrada.readLine());
moneda2 = Integer.parseInt(bufEntrada.readLine());
moneda3 = Integer.parseInt(bufEntrada.readLine());
billete = Integer.parseInt(bufEntrada.readLine());
billete1 = Integer.parseInt(bufEntrada.readLine());
billete2 = Integer.parseInt(bufEntrada.readLine());
}catch(Exception ex){
}
total = moneda+moneda1+moneda2+moneda3+billete+billete1+billete2;
if (total>b4) {
total = total-b4;
System.out.println("Recoja su producto");
System.out.println("Su cambio es:$"+total);
} else {
System.out.println("Recoja su producto");
}
total = total-b4;
break;
case 9:
System.out.println("Ingrese el dinero");
try{
moneda = Integer.parseInt(bufEntrada.readLine());
moneda1 = Integer.parseInt(bufEntrada.readLine());
moneda2 = Integer.parseInt(bufEntrada.readLine());
moneda3 = Integer.parseInt(bufEntrada.readLine());
billete = Integer.parseInt(bufEntrada.readLine());
billete1 = Integer.parseInt(bufEntrada.readLine());
billete2 = Integer.parseInt(bufEntrada.readLine());
}catch(Exception ex){
}
total = moneda+moneda1+moneda2+moneda3+billete+billete1+billete2;
if (total>c1) {
total = total-c1;
System.out.println("Recoja su producto");
System.out.println("Su cambio es:$"+total);
} else {
System.out.println("Recoja su producto");
}
total = total-c1;
break;
case 10:
System.out.println("Ingrese el dinero");
try{
moneda = Integer.parseInt(bufEntrada.readLine());
moneda1 = Integer.parseInt(bufEntrada.readLine());
moneda2 = Integer.parseInt(bufEntrada.readLine());
moneda3 = Integer.parseInt(bufEntrada.readLine());
billete = Integer.parseInt(bufEntrada.readLine());
billete1 = Integer.parseInt(bufEntrada.readLine());
billete2 = Integer.parseInt(bufEntrada.readLine());
}catch(Exception ex){
}
total = moneda+moneda1+moneda2+moneda3+billete+billete1+billete2;
if (total>c2) {
total = total-c2;
System.out.println("Recoja su producto");
System.out.println("Su cambio es:$"+total);
} else {
System.out.println("Recoja su producto");
}
total = total-c2;
break;
case 11:
System.out.println("Ingrese el dinero");
try{
moneda = Integer.parseInt(bufEntrada.readLine());
moneda1 = Integer.parseInt(bufEntrada.readLine());
moneda2 = Integer.parseInt(bufEntrada.readLine());
moneda3 = Integer.parseInt(bufEntrada.readLine());
billete = Integer.parseInt(bufEntrada.readLine());
billete1 = Integer.parseInt(bufEntrada.readLine());
billete2 = Integer.parseInt(bufEntrada.readLine());
}catch(Exception ex){
}
total = moneda+moneda1+moneda2+moneda3+billete+billete1+billete2;
if (total>c3) {
total = total-c3;
System.out.println("Recoja su producto");
System.out.println("Su cambio es:$"+total);
} else {
System.out.println("Recoja su producto");
}
total = total-c3;
break;
case 12:
System.out.println("Ingrese el dinero");
try{
moneda = Integer.parseInt(bufEntrada.readLine());
moneda1 = Integer.parseInt(bufEntrada.readLine());
moneda2 = Integer.parseInt(bufEntrada.readLine());
moneda3 = Integer.parseInt(bufEntrada.readLine());
billete = Integer.parseInt(bufEntrada.readLine());
billete1 = Integer.parseInt(bufEntrada.readLine());
billete2 = Integer.parseInt(bufEntrada.readLine());
}catch(Exception ex){
}
total = moneda+moneda1+moneda2+moneda3+billete+billete1+billete2;
if (total>c4) {
total = total-c4;
System.out.println("Recoja su producto");
System.out.println("Su cambio es:$"+total);
} else {
System.out.println("Recoja su producto");
}
total = total-c4;
break;
default:
System.out.println("Error");
}
}
}