java – Problem switching from Controller to Model

Question:

I'm doing some code where it involves MVC. To switch from View to Controller I'm not having problems, but when I switch from Controller to Model I can't, as the variable is imported, but without any 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 (Principal 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'm probably using improperly, if anyone can show me the way to the stones I appreciate it 🙂 (I'm still a beginner).

Answer:

@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

    }
}
Scroll to Top