简体   繁体   中英

Why doesn't my MVC code doesn't do anything?

First of all, I am a beginner in Java. I am learning it since 2 weeks for now, but I learnt C# before. I never heard about MVC before and currently I have a problem with it. I have to do a console based calculator in MVC (I don't really know why, i heard that MVC is for applications with UI) for a competition. I've successfully done it before for an application with UI, but I don't know how and where to handle the console input and output. Currently I have the following lines of codes:

package com.kristofgero;

public class Model {
    private double e;
    public void osszead(double a, double b) {
        e = a+b;
    }
    public void kivon(double a, double b) {
        e = a-b;
    }
    public void szoroz(double a, double b) {
        e = a*b;
    }
    public void oszt(double a, double b) {
        e = a/b;
    }
    public double getCalculationValue() {
        return e;
    }
}


package com.kristofgero;
import java.util.Scanner;

public class View {
    Scanner scanner = new Scanner(System.in);
    private double a = scanner.nextDouble();
    private double b = scanner.nextDouble();
    private double e = 0;
    public double getA() {
        return a;
    }
    public double getB() {
        return b;
    }
    public double getE() {
        return e;
    }
    void displayError(String hiba) {
        System.out.println(hiba);
    }
}

package com.kristofgero;

public class Controller {
    private View theView;
    private Model theModel;

    public Controller(View theView, Model theModel) {
        this.theView = theView;
        this.theModel = theModel;
    }
    class Calculate {
        public void calculateMethod() {
            double a = 0;
            double b = 0;
            double e = 0;
            try {
                a = theView.scanner.nextDouble();
                b = theView.scanner.nextDouble();
                String jel = theView.scanner.nextLine();
                switch (jel) {
                    case "+": e = a+b; break;
                    case "-": e = a-b; break;
                    case "*": e = a*b; break;
                    case "/": e = a/b; break;
                }
            } catch (Exception error) {
                theView.displayError("Két számot adjon meg!");
            }
        }
    }
}

package com.kristofgero;

public class Main {
    public static void main(String[] args) {
        View theView = new View();
        Model theModel = new Model();
        Controller theController = new Controller(theView, theModel);

    }
}

I have to do this calculator to read the correct operation with 2 numbers that the user has given before. Right now, my code doesn't really do anything.

Not sure why you put the calculateMethod in an inner class but you need to call that method in order to get input from the user.

public class Controller {
    private View theView;
    private Model theModel;

    public Controller(View theView, Model theModel) {
        this.theView = theView;
        this.theModel = theModel;
    }

        public void calculateMethod() {
            double a = 0;
            double b = 0;
            double e = 0;
            try {
                a = theView.scanner.nextDouble();
                b = theView.scanner.nextDouble();
                String jel = theView.scanner.nextLine();
                switch (jel) {
                    case "+": e = a+b; break;
                    case "-": e = a-b; break;
                    case "*": e = a*b; break;
                    case "/": e = a/b; break;
                }
            } catch (Exception error) {
                theView.displayError("Két számot adjon meg!");
            }
        }
}

public class Main {
    public static void main(String[] args) {
        View theView = new View();
        Model theModel = new Model();
        Controller theController = new Controller(theView, theModel);
        theController.calculateMethod();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM