How can I validate that my vending machine program accepts coins of 1 2 5 and 10 pesos and bills of 20 50 and 100 pesos

0

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");
    }
}

}

    
asked by David Montiel 18.10.2017 в 02:38
source

1 answer

1

Ok, to begin with, you have a lot of repeated code, but I think you're on the right track. Here are some recommendations and in the end I hope I can answer your question:

  • When you post a question, try formatting all the code to make it look good. I started to modify it, but I gave up because it's a lot of code.
  • Try to give a short example of the part where you have doubts or problems so that it is easier and punctual to help you.
  • Regarding your code:

  • When you declare several variables of the same type, you do not have to do it in different lines, you can do it in a single line:

    int a1, a2, a3, a4;
    
  • When your variables are going to have a start value, you can declare them and initialize them:

    int a1 = 8, a2 = 10, a3 = 12, a4 = 15;
    
  • The biggest code you have repeated is in the case part. All that section from where you ask for the money to where you calculate total , is the same code for all your cases. I recommend you take that part out and put it before switch .

  • In this section (where you ask for money) is where you can solve your problem:

  • In my opinion you do not need so many variables of coins and bills, in fact you do not need any. You could do something like this:

    dinero = Integer.parseInt(bufEntrada.readLine());
    if (checarSiDineroValido(dinero)) {
        total += dinero;
        ...
    

    as long as% co_of% is initialized with zero. In this section you will see that I added a method called total . In this method you could have a validation to check if what the user entered is really an acceptable currency or bill and what the method would return would simply be a checarSiDineroValido or a true .

    After this is when you could put the switch to verify that the amount you have in the total is enough to buy the product or not. That is:

    if (checarSiDineroValido(dinero)) {
        total += dinero;
        switch (opc) {
        ...
    

    Surely you are asking yourself "what happens if the money is not valid?". What you would do in false is to put a message indicating to the user that it is not acceptable or introduced it and then ask for the data again. This is done with a cycle. That is, it could be something like:

    boolean valido = false;
    do {
        dinero = Integer.parseInt(bufEntrada.readLine());
        if (checarSiDineroValido(dinero)) {
            total += dinero;
            switch (opc) {
            ...
            (dentro de tus casos revisar si el monto es suficiente)
                valido = true;
            (si no es suficiente entonces)
                valido = false
            ...
            }
            if (!valido)
                "mesaje para que ponga más dinero"
        } else {
            "mensaje de que no es válido"
            valido = false;
        }
    } while (!valido);
    

    I did not put all the code for you to have fun for a while, but I think that with what I put you as a pseudocodigo you can move forward and solve your problem.

    I hope this helps you.

        
    answered by 18.10.2017 в 13:56