Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Divide this program into 3 classes Model, View, and Controller. import java.awt.

ID: 3809035 • Letter: D

Question

Divide this program into 3 classes Model, View, and Controller.

    import java.awt.BorderLayout;
    import java.awt.Button;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Frame;
    import java.awt.Label;
    import java.awt.Rectangle;
    import java.awt.TextField;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import javax.swing.JPanel;
    import java.util.*;


    // We use Java awt (window toolkit) and Swing

    public class ButtonExample1 extends WindowAdapter implements ActionListener {// main class
        Frame frame, frame1;
        JPanel buttonPanel, buttonPanel1;
        Button blueButton, redButton;
        TextField tf,tf1;
        Label loginLabel,loginLabel1;
        String password;
        Map<String, String> UserName = new HashMap<String, String>();
        ArrayList<String> UserNames = new ArrayList<String>();
  
   public ButtonExample1(String title) {
      
       frame = prepareFrame(title); // get a frame
       tf= new TextField("", 20); // get a Text Field
       loginLabel = new Label("User Name", Label.RIGHT);
        loginLabel.setForeground(Color.BLUE);
        buttonPanel = prepareContainer(); // get a panel
        redButton = prepareButton("Submit", "Input Username"); // get a button
        UserName.put("user1","abc123");
      
       buttonPanel.add(loginLabel); // add label to panel
       buttonPanel.add(tf); // add text field to panel
      
       buttonPanel.add(redButton);//add button

      
       frame.add("Center", buttonPanel); // add the panel to the frame
       frame.setVisible(true); // make the frame visible
      
       frame.addWindowListener(new WindowAdapter(){
              public void windowClosing(WindowEvent we){
                System.exit(0);
              }
       });


   }

   public Frame prepareFrame(String title){
       Rectangle r = new Rectangle(10,10,500,500);
       frame = new Frame(title);
       frame.setSize(950, 500);
       frame.setLayout(new BorderLayout());
       frame.setMaximizedBounds(r);
       frame.setExtendedState(Frame.MAXIMIZED_BOTH);
       frame.addWindowListener(this); // add a handler for events on this frame
       return frame;
   }
  
   public Button prepareButton(String title, String actionCommand){
       Button redButton = new Button(title);
       redButton.setBackground(Color.RED);
       redButton.setActionCommand(actionCommand);
       redButton.addActionListener(this);
       return redButton;
   }
  
   public JPanel prepareContainer(){
       JPanel buttonPanel = new JPanel(new FlowLayout());
       // Dimension d = new Dimension(2000,2000);
        buttonPanel.setBounds(10,10,200,200);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int x = Math.max(( screenSize.width / 2 - frame.getSize().width / 2),0);
        int y = Math.max((screenSize.height / 2 - frame.getSize().height / 2),0);
      
           buttonPanel.setLocation(x,y);
           return buttonPanel;
          
      
   }

    public void createNew(){ // prepare a new screen for password
       String title = "Welcome";
       frame1=prepareFrame(title);
       tf1= new TextField("", 20);
       loginLabel1 = new Label("Please Enter your Password", Label.RIGHT);
        loginLabel1.setForeground(Color.BLUE);
        buttonPanel1 = prepareContainer();
       buttonPanel1.add(loginLabel1);
       buttonPanel1.add(tf1);
        blueButton = prepareButton("Submit","Input Password");
           buttonPanel1.add(blueButton);
       frame1.add("Center", buttonPanel1);
       frame1.setVisible(true);
              
    }
  
   public void actionPerformed(ActionEvent e) {
       // TODO Auto-generated method stub
      
       String cmd = e.getActionCommand();
      
       if (cmd.equals("Input Username")){ // if username has been submitted
           String enter = tf.getText(); // get the user name
          
           try{
               if (UserName.containsKey(enter)){
                       password = UserName.get(enter); // get password. will be used later
                       frame.setVisible(false); // make this screen invisible to start the password screen
                       createNew(); // start the password screen
                       return;
               }
               else {
                   tf.setText(""); // the username is not found in the database
                   frame.setTitle("The User Name entered is incorrect. Please enter your user name");
                   frame.setVisible(false);
                   frame.setVisible(true);
               }      
           }
           catch (Exception e4){
               e4.printStackTrace();
           }
          
          
                  
          
       }
       else if(cmd.equals("Input Password")){ // password submitted
           if(tf1.getText().equals(password)){ // password matches
               frame1.setTitle("You have sucessfully logged in");
               frame1.addWindowListener(new WindowAdapter(){
                      public void windowClosing(WindowEvent we){
                        System.exit(0);
                      }
               });
           }
           else{
               tf1.setText(""); // password does not match
               frame1.setTitle("Wrong Password");
               frame1.setVisible(false);
               frame1.setVisible(true);
               frame1.addWindowListener(new WindowAdapter(){
                      public void windowClosing(WindowEvent we){
                        System.exit(0);
                      }
               });
           }
          
       }
          
   }
   public static void main(String args[]){
       new ButtonExample1("Log in Screen");
   }
    }

The program first set up the login window. Then ask for a username. Once a username is submitted, the program would look for it inside of the map UserName. If there is a match, the program then asks for password. I want to divide it up to the MVC.

Explanation / Answer

Since you have written MVC in a single class, I am telling you that Model contains all the code segments which are gathering data and making available that data for future purposes.

and View contains all the code segements which are used to make graphical user interface to give input.

finally Controller contains all the action related code segments which are used to combine Model and View.

>>> In the given program "Entering the user name and password and saving them for authentiction comes under MODEL"

>>> and "TextFields , buttons and Labels comes under VIEW"

>>> and "Checking the Username and password and handling Button click are comes under Controller"