Problem switching from Controller to Model

Asked

Viewed 230 times

3

I’m making a code where it involves MVC. To get past the View to the Controller I’m not having problems, but when will I pass the Controller to the Model I can’t do it, the variable is imported, but with no value.

View:

package bancomvc;

public class View {

    public static int valor;
    public static String nome;
    public static double saque;
    public static double limite;
    public static int opcao;

    public void exibeMenu() {

        System.out.println("\t Escolha a opção desejada");
        System.out.println("1 - Depositar");
        System.out.println("2 - Sacar");
        System.out.println("3 - Sair\n");
        System.out.print("Opção: ");
        Scanner scan = new Scanner(System.in);
        opcao = scan.nextInt();
        System.out.println("Digite o valor:");
        valor = scan.nextInt();

    }

    public void setOpcao(int opcao) {
        this.opcao = opcao;
    }

    public int getOpcao() {
        return opcao;
    }

    public void setValor(int valor) {
        this.valor = valor;
    }

    public int getValor() {
        return valor;
    }
}

Controller:

package bancomvc;

public class Controller {

    public static int recValor;
    public static int opc;

    public void Valores() throws ClassNotFoundException, SQLException {

        View cliente = new View();

        opc = cliente.opcao;
        recValor = cliente.valor;

        System.out.println(opc);

    }

    public void setOpc(int valor) {
        this.opc = valor;
    }

    public int getOpc() {
        return opc;
    }

    public void setValor(int valor) {
        this.recValor = valor;
    }

    public int getrecValor() {
        return recValor;

    }

Model:

package bancomvc;

public class Model {

    public static int op;
    public static int val;
    public static int Saldo;

    public void execução() throws ClassNotFoundException, SQLException {

        Controller ctrl = new Controller();

        op = ctrl.opc;
        val = ctrl.recValor;
        System.out.println(op);

    }

Bancomvc(Main class):

package bancomvc;

public class BancoMVC {

    public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
        View ver = new View();
        ver.exibeMenu();
        Model mod = new Model();
        mod.execução();
        Controller cont = new Controller();
        cont.Valores();

    }
}

The Get and Set I am probably using improperly, if someone can show me the path of the stones I thank :) (I am still beginner).

  • 1

    Some doubts: Your control and model classes are missing the last keys, did that occur when you pasted the code here? - In the vision class you are using a Scanner without importing it. Java does not know what a "Scanner" until you point out which package it is in (in case, java.util), was an oblivion?

  • No, I wiped the code a little bit with some things that would not be necessary here, you may notice that I removed all imports, the code contains no errors, but the result in the variable model is null...

  • @Matheuslopes If the answer below has served you, mark it as accepted so that Dener can earn points with it.

1 answer

1


@Matheuslopes I think it’s just the order you’re calling Model.executa() and Controller.Values() in the Bancomvc class, try switching to something like this:

public class BancoMVC {

    public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
        View ver = new View();
        ver.exibeMenu();
        Controller cont = new Controller();
        cont.Valores();//chame esse primeiro
        Model mod = new Model();
        mod.execução();//depois esse

    }
}
  • That’s right, by the way, obg :) I didn’t think there was any sequence to it :D

Browser other questions tagged

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