Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

As the title says, for the last couple of hours, I've been trying to implement MVC design pattern into a project I've worked for a couple of days now. It took more time than expected and since the project is pretty big right now I will supply only a couple of classes where the my attempt to implement the MVC design pattern was applied.

The model class

public class Pain {


private String diagnosis;

public void diagnose(int sex, int painType, int painArea, int precisePainArea) {
    diagnosis = DiagnosisCalculator.diagnose(sex, painType, painArea, precisePainArea);
}
// the next 3 functions are designed to go into another model which i have to implement
public int resetInt(int x) {
    return ResetFunctions.resetInt(x);
}

public boolean resetBoolean(boolean b){
    return ResetFunctions.changeBoolean(b);
}

public String resetDiagnosis(String s){
    return ResetFunctions.resetString(s);
}

public String getDiagnosis() {
    return diagnosis;
}

public void setDiagnosis(String diagnosis) {
    this.diagnosis = diagnosis;
}

}

The view class

package MainGui;

import PreventionPanels.PreventionPanel;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import static java.awt.image.ImageObserver.HEIGHT;
import javax.swing.*;


public class GuiFrame extends JFrame {

private final JComboBox<String> sexCombo = makeComboBox("Muški", "Ženski"),
        painTypeCombo = makeComboBox("Akutni", "Hronični"),
        painAreaCombo = makeComboBox("Abdomen", "Udovi", "Glava"),
        precisePainAreaCombo = makeComboBox("1 - GL", "2 - GS", "3 - GD", "4 - SL", "5 - SS", "6 - SD", "7 - DL", "8 - DS", "9 - DD");

private final JTextField diagnosisField = new JTextField(20);
private final boolean setEnabled = false;
private final JButton diagnosisButton = new JButton("Dijagnoza");
private final JButton resetButton = new JButton("Ponovo");
// Images
public final ImageIcon rep = new ImageIcon(getClass().getResource("/images/under.png"));
public final ImageIcon rep2 = new ImageIcon(getClass().getResource("/images/rsz_s13.jpg"));
private final JLabel coverPicture = new JLabel(rep);
private final JLabel abdomenPicture = new JLabel(rep2);
private final boolean disablePicture = false;
private final boolean enablePicture = true;

public GuiFrame() {
    this.setTitle("Obavi pregled v1.0.5");
    this.setSize(850, 600);
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    this.setResizable(true);
    this.setLocationRelativeTo(null);
    this.setLayout(new BorderLayout());
    this.getRootPane().setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

    // Title
    JLabel title = new JLabel("Aplikacija za dijagnozu bolesti", SwingConstants.CENTER);
    JLabel newFrameLabel = new JLabel("<html><U><font color='blue'>Registruj se</font><U></html>", SwingConstants.RIGHT);
    title.setLayout(new GridLayout(0, 1));
    title.setFont(new Font("Times new Roman", Font.BOLD, 14));
    title.add(newFrameLabel);
    this.add(title, BorderLayout.NORTH);

    // Health questions
    JPanel questions = new JPanel();
    questions.setLayout(new GridLayout(0, 2, 30, 30));
    questions.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
    questions.add(new JLabel("Pol : "));
    questions.add(sexCombo);
    questions.add(new JLabel("Vrsta bola koju osećate : "));
    questions.add(this.painTypeCombo);
    painTypeCombo.addActionListener(ActiveEvent -> {
        typeCase();
    });
    questions.add(new JLabel("U kom delu tela osećate taj bol : "));
    questions.add(this.painAreaCombo);
    painAreaCombo.addActionListener(ActionEvent -> {
        partCase();
    });
    questions.add(new JLabel("Vaš bol osećate u (vidi sliku) : "));
    questions.add(this.precisePainAreaCombo);
    this.precisePainAreaCombo.setEnabled(setEnabled);

    // Recommended doctor - not fully added yet
    JLabel doctor = new JLabel("Preporučeni lekar za ovu bolest : ");
    doctor.setEnabled(false);
    questions.add(doctor);
    questions.add(new JLabel("Josip Broz Tito"));
    this.add(questions, BorderLayout.WEST);

    // Pictures
    JPanel pictures = new JPanel();
    pictures.setBorder(BorderFactory.createEtchedBorder());
    pictures.add(coverPicture);
    abdomenPicture.setVisible(disablePicture);
    pictures.add(abdomenPicture);
    this.add(pictures, BorderLayout.EAST);

    // Text area, buttons and Additional frames
    JPanel bottom = new JPanel();
    bottom.setLayout(new FlowLayout());
    bottom.setAlignmentY(Container.BOTTOM_ALIGNMENT);

    JLabel findDoctor = new JLabel("Pronadji lekara", SwingConstants.LEFT);
    findDoctor.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 100));
    bottom.add(findDoctor);

    JLabel prevention = new JLabel("<html><U><font color='red'>Prevencija</font><U></html>");
    prevention.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 100));
    prevention.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent e) {
            JFrame jf = new JFrame("Prevencija");
            jf.setContentPane(new PreventionPanel());
            jf.setSize(new Dimension(400, 400));
            jf.setVisible(true);
            jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            jf.setLocationRelativeTo(null);
        }
    });
    bottom.add(prevention);

    JPanel diagnosis = new JPanel();
    diagnosis.setBorder(BorderFactory.createEtchedBorder());
    diagnosis.setLayout(new BoxLayout(diagnosis, BoxLayout.Y_AXIS));
    diagnosis.setAlignmentX(Container.LEFT_ALIGNMENT);
    diagnosis.add(new JLabel("Preliminarna dijagnoza : "));
    diagnosisField.setEditable(false);
    diagnosis.add(diagnosisField);
    bottom.add(diagnosis);

    diagnosisButton.setMargin(new Insets(10, 10, 10, 10));
    bottom.add(diagnosisButton);

    resetButton.setMargin(new Insets(10, 10, 10, 10));
    bottom.add(resetButton);

    this.add(bottom, BorderLayout.SOUTH);
}

// Making combo boxes, all starting with the field -Odaberi-
private static JComboBox<String> makeComboBox(String... options) {
    JComboBox<String> combo = new JComboBox<>();
    combo.addItem("-Odaberi-");
    for (String opt : options) {
        combo.addItem(opt);
    }

    return combo;
}

//Adding getters and setters for... well, everything...
// MVC - You scary...
public int getSexCombo() {
    return sexCombo.getSelectedIndex();
}

public void setSexCombo(int x) {
    sexCombo.setSelectedIndex(x);
}

public int getPainTypeCombo() {
    return painTypeCombo.getSelectedIndex();
}

public void setPainTypeCombo(int x) {
    painTypeCombo.setSelectedIndex(x);
}

public int getPainAreaCombo() {
    return painAreaCombo.getSelectedIndex();
}

public void setPainAreaCombo(int x) {
    painAreaCombo.setSelectedIndex(x);
}

public int getPrecisePainAreaCombo() {
    return precisePainAreaCombo.getSelectedIndex();
}

public void setPrecisePainAreaCombo(int x) {
    precisePainAreaCombo.setSelectedIndex(x);
}

public boolean getPrecisePainAreaComboEditable() {
    return setEnabled;
}

public void setPrecisePainAreaEditable(boolean setEnabled) {
    precisePainAreaCombo.setEnabled(setEnabled);
}

public String getDiagnosisField() {
    return diagnosisField.getText();
}

public void setDiagnosisField(String diagnosis) {
    diagnosisField.setText(diagnosis);
}

public boolean getCoverPictureVisibility() {
    return enablePicture;
}

public void setCoverPictureVisiblility(boolean setEnabled) {
    coverPicture.setVisible(setEnabled);
}

public boolean getAbdomenPictureVisibility() {
    return disablePicture;
}

public void setAbdomenPictureVisiblility(boolean setEnabled) {
    abdomenPicture.setVisible(setEnabled);
}

// Adding listeners to buttons
public void addDiagnosisButtonListener(ActionListener listenForDiagnosisButton) {
    diagnosisButton.addActionListener(listenForDiagnosisButton);
}

public void addResetButtonListener(ActionListener listenForResetButton) {
    resetButton.addActionListener(listenForResetButton);
}
// this should be implemented somewhere else
private void partCase() {
    switch (this.painAreaCombo.getSelectedIndex()) {
        case 1:
            this.precisePainAreaCombo.setEnabled(true);
            this.coverPicture.setVisible(false);
            this.abdomenPicture.setVisible(true);
            break;
        case 2:
            this.precisePainAreaCombo.setEnabled(false);
            this.painAreaCombo.setSelectedIndex(0);
            JOptionPane.showMessageDialog(this.precisePainAreaCombo, "Opcija jos nije uneta", "Error", HEIGHT);
            break;
        case 3:
            this.precisePainAreaCombo.setEnabled(false);
            this.painAreaCombo.setSelectedIndex(0);
            JOptionPane.showMessageDialog(this.painAreaCombo, "Opcija jos nije uneta", "Error", HEIGHT);
            break;
        default:
            break;
    }
}
// this should be implemented somewhere else
private void typeCase() {
    switch (this.painTypeCombo.getSelectedIndex()) {
        case 1:

            break;
        case 2:
            this.precisePainAreaCombo.setEnabled(false);
            this.painTypeCombo.setSelectedIndex(0);
            JOptionPane.showMessageDialog(this.painTypeCombo, "Opcija jos nije uneta", "Error", HEIGHT);
            break;
    }

}
}

The controller class which handles the interactions between the view and the model class

package Controler;

import Model.Pain;
import MainGui.GuiFrame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class PainController {

private final Pain model;
private final GuiFrame view;

public PainController(Pain model, GuiFrame view) {
    this.model = model;
    this.view = view;

    this.view.addDiagnosisButtonListener(new DiagnosisListener());
    this.view.addResetButtonListener(new ResetListener());
}

class DiagnosisListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {

        int sex, painType, painArea, precisePainArea;

        sex = view.getSexCombo();
        painType = view.getPainTypeCombo();
        painArea = view.getPainAreaCombo();
        precisePainArea = view.getPrecisePainAreaCombo();
        model.diagnose(sex, painType, painArea, precisePainArea);

        view.setDiagnosisField(model.getDiagnosis());

    }

}

class ResetListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        String diagnosisTextField;
        int sex, painType, painArea, precisePainArea;
        boolean setPreciseComboBoxEnabled, setCoverPictureVisibility,
                setAbdomenPictureVisibility;

        sex = view.getSexCombo();
        painType = view.getPainTypeCombo();
        painArea = view.getPainAreaCombo();
        precisePainArea = view.getPrecisePainAreaCombo();
        setPreciseComboBoxEnabled = view.getPrecisePainAreaComboEditable();
        diagnosisTextField = view.getDiagnosisField();
        setCoverPictureVisibility = view.getCoverPictureVisibility();
        setAbdomenPictureVisibility = view.getAbdomenPictureVisibility();

        view.setSexCombo(model.resetInt(sex));
        view.setPainTypeCombo(model.resetInt(painType));
        view.setPainAreaCombo(model.resetInt(painArea));
        view.setPrecisePainAreaCombo(model.resetInt(precisePainArea));
             view.setPrecisePainAreaEditable(model.resetBoolean(setPreciseComboBoxEnabled));
        view.setDiagnosisField(model.resetDiagnosis(diagnosisTextField));
        view.setCoverPictureVisiblility(model.resetBoolean(setCoverPictureVisibility));
        view.setAbdomenPictureVisiblility(model.resetBoolean(setAbdomenPictureVisibility)); 
    }

}
}

And finally the Main class where everything is executed

import Controler.PainController;
import Model.Pain;
import MainGui.GuiFrame;


public class Main {

public static void main(String[] args) {

    GuiFrame frame = new GuiFrame();
    Pain painModel = new Pain();



    PainController controller = new PainController(painModel, frame);
    frame.setVisible(true);
}

}

So, my question here is, am i going the right way or should i go back and redo something and what ?

share|improve this question
    
Please provide a more specific title regarding the code's implementation. This one is too generic. – Jamal Feb 20 at 1:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.