Division into different methods

Asked

Viewed 163 times

0

I’m learning in college to divide the system into different methods, each performing a function. However I had a problem with the following code:

import java.util.Scanner;
public class Sistema_Bichara{
    public static int Menu(){
        Scanner in = new Scanner(System.in);
        System.out.println("Digite o o número referente ao tamanho da pulseira\n1.Pequena (17cm) - R$180,00\n2.Média (18cm) - R$200,00\n3.Grande (20cm) - R$240,00\n4.Sair");
        int digita = in.nextInt();
        return digita;
    }
    public static double valorPulseira(int digita){
        double valor = 0;
        switch(digita){
            case 1:
            valor += 180;
            System.out.println("Total: R$180,00\nAgora escolha o(s) pingente(s):");
            break;
            case 2:
            valor += 200;
            System.out.println("Total: R$200,00\nAgora escolha o(s) pingente(s):");
            break;
            case 3:
            valor += 240;
            System.out.println("Total: R$240,00\nAgora escolha o(s) pingente(s):");
            break;
        }
        return valor;
    }
    public static int tamanhoPulseira(int digita){
        int tamanhopulseira = 0;
        switch(digita){
            case 1:
            tamanhopulseira = 17;
            break;
            case 2:
            tamanhopulseira = 18;
            break;
            case 3:
            tamanhopulseira = 20;
            break;
        }
        return tamanhopulseira;
    }
    public static int menuAcessorio(){
        System.out.println("1.Bichinhos (1,3cm) - R$225,00\n2.Bonequinhos (1,5cm) - R$185,00\n3.Berloques (0,5cm) - R$200,00\n4.Cubinhos (1cm) - R$190,00\n5.Rolinho (2cm) - Prata, Pedra e Esmaltados\n6.Ir para o pagamento");
        Scanner in = new Scanner(System.in);
        int digita2 = in.nextInt();
        return digita2;
    }
    public static double suporte(double valor){
    Scanner in = new Scanner(System.in);
    System.out.println("\nDeseja adquirir um suporte de segurança pelo preço de R$95,00?\n1.Sim\n2.Não");
    int digita = in.nextInt();
    if(digita == 1){
        valor += 95;
    }
    return valor;
   }
   public static int menuPagamento(){
       System.out.println("\nA loja oferece os seguintes tipos de pagamento:");
       System.out.println("\n1. À vista com 5% de desconto");
       System.out.println("\n2. 3 vezes no cartão sem acrescimos");
       System.out.println("\n3. 5 vezes no cartão com acresimo de 10% no preço total");
       System.out.println("\n4. 10 vezes no cartão com acresimo de 20% no preço total");
       System.out.println("\nDigite o número da forma de pagamento escolhida.");
       Scanner in = new Scanner(System.in);
       int digita3 = in.nextInt();
       return digita3;
    }
   public static double pagamento(double valor, int digita3){
    Scanner in = new Scanner(System.in);
    double pagar = 0;
    double valor2 = 0;
    switch(digita3){
        case 1:
        valor = valor*0.95;
        break;
        case 2:
        pagar = valor/3;
        break;
        case 3:
        valor2 = (valor*110)/100;
        pagar = valor2/5;
        break;
        case 4:
        valor2 = (valor*120)/100;
        pagar = valor2/10;
        break;
    }
    //Condição para mostrar o valor total de acordo com o pagamento escolhido
    if (digita3 == 3){
        System.out.println("\nO valor total da compra ficou de: R$" +valor2);
    } else if (digita3 == 4){
        System.out.println("\nO valor total da compra ficou de: R$" +valor2);
    } else {
        System.out.println("\nO valor total da compra ficou de: R$" +valor);
    }
    //Condição para mostrar o valor total e o valor das parcelas se a pessoa escolheu parcelado
    if(digita3 == 2){
    System.out.println("\nForma de pagameto: 3 parcelas de R$" +pagar);
    System.out.println("\nA loja aceita os seguintes cartões:\n1.Visa\n2.MasterCard\n3.Rede Shop\n4.American Express\n5.Hipercard\n6.Diners Club International");
    int digita = in.nextInt();
    }  else if(digita3 == 3){
    System.out.println("\nForma de pagameto: 5 parcelas de R$" +pagar);
    System.out.println("\nA loja aceita os seguintes cartões:\n1.Visa\n2.MasterCard\n3.Rede Shop\n4.American Express\n5.Hipercard\n6.Diners Club International");
    int digita = in.nextInt();
    }else if(digita3 == 4){
    System.out.println("\nForma de pagameto: 10 parcelas de R$" +pagar);
    System.out.println("\nA loja aceita os seguintes cartões:\n1.Visa\n2.MasterCard\n3.Rede Shop\n4.American Express\n5.Hipercard\n6.Diners Club International");
    int digita = in.nextInt();
    }
    System.out.println("\nObrigado por comprar conosco!");
    return valor;
}
public static void main(String args[]){
Scanner in = new Scanner(System.in);
int digita=Menu();
double valor = valorPulseira(digita);
int tamanhopulseira = tamanhoPulseira(digita);
double tamanhototal = 0;
int digita2;
do {
        digita2=menuAcessorio();
        switch(digita2){
        case 1:
        valor += 225;
        tamanhototal += 1.3;
        break;
        case 2:
        valor += 185;
        tamanhototal += 1.5;
        break;
        case 3:
        valor += 200;
        tamanhototal += 0.5;
        break;
        case 4:
        valor += 190;
        tamanhototal += 1;
        break;
        case 5:
        tamanhototal += 2;
        System.out.println("Escolha o material:\n1.Prata - R$150,00\n2.Pedras - R$220,00\n3.Esmaltados - R$198,00");
        int rolinho = in.nextInt();
        switch(rolinho){
            case 1:
            valor += 150;
            break;
            case 2:
            valor += 220;
            break;
            case 3:
            valor += 198;
            break;
        }
        }
        if(tamanhototal > tamanhopulseira){
            System.out.println("\nTamanho máximo da pulseira atingido\n1.Prosseguir para o pagamento.");
            int digita3 = in.nextInt();
        }
        if(digita==6){
            System.out.println("\nDigite 1 para prosseguir para o pagamento.");
            int digita4 = in.nextInt();
        }
    } while (digita2 != 6 && tamanhototal < tamanhopulseira);
valor += suporte(valor);
System.out.println("\nO preço total da compra fica de: R$" +valor);
int digita3 = menuPagamento();
double pagamento = pagamento(valor, digita3);

} }

Can anyone tell me why the system does not respect the while in main and simply when the person selects an accessory, already says that it has reached the maximum size?

  • Your code doesn’t even compile. In the method main, the variables tamanhopulseira, valor, in and digita are not stated.

  • My mistake. I edited with the correct code sorry.

  • 1

    What problem you had with the code?

1 answer

2


Let’s take a look at this piece:

    int digita2 = menuAcessorio();
    double tamanhototal = 0;
    while (digita2 != 6 && tamanhototal < tamanhopulseira) {
        switch (digita2) {
            case 1:
                valor += 225;
                tamanhototal += 1.3;
                break;
            // Outros cases, que também aumentam o valor de  tamanhototal...
        }
        if (tamanhototal > tamanhopulseira) {
            System.out.println("\nTamanho máximo da pulseira atingido\n1.Prosseguir para o pagamento.");
            int digita3 = in.nextInt();
        }
        // Mais um if...
    }

What happens:

  1. The variable digita2 is defined before the while and doesn’t change value ever again.
  2. Within the while, the switch reads the value of digita2 and increases the value of tamanhototal.
  3. The condition of if shortly after the switch will initially be false.
  4. Steps 2 and 3 are repeated a lot of times (because of the while) and this will occur practically instantaneously until the value of tamanhototal get bigger than tamanhopulseira. This is because digita2 is never changed and there is nothing else that can prevent this process or request anything from the user before it is too late.
  5. The System.out.println is executed.
  6. He comes out of while.

I mean, I think you should read the digita2 somewhere inside the while.

Ah, and by the way, please give better names for your variables. Use digita, digita2 and digita3 is not a good programming practice. At least it would be good if you describe in the variable name what means the entered value.

  • 1

    Perfect was just that. It would look like this then the code? int digita2=menuAcessorio();&#xA; double tamanhototal = 0;&#xA; while (digita2 != 6 && tamanhototal < tamanhopulseira) {&#xA; digita2=menuAcessorio();

  • @William, almost that. But in this case the first menuAcessorio() will make the user type something that will serve no purpose (unless he type 6). You have to delete this call from menuAcessorio() before the while and just stick with what’s inside. I also think it’ll be easier if you use do-while instead of while.

  • 1

    The problem is that if I don’t set before the while won’t find the variable types 2 rsrs so you recommend the do while? The problem is that even with the while this problem will continue..

  • 1

    You can put something like int digita2 = 0; before the while, if you are not going to use the do-while. Or else, use the do-while even before it you put just int digita;. And by using the do-while, you already ensure that the assessment of the rectification condition is at the end, not at the beginning, and it will always perform the loop at least once.

  • 1

    as well as just the int digita; in the of while? I didn’t understand. It wouldn’t be int digita2 = 0; equal in the while?

  • 1

    @William Is, would be int digita2; before the do-while.

  • I edited the main would have like to take a look?

  • @William Well, and what’s going wrong now?

  • Nothing just wanted to know if this is how you had described rsrs

  • @William Is yes.

  • 1

    Perfect solved then. Thank you very much!

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.