SOFTWARE ENGINEERING COMPUTER SCIENCE Divide this program into 3 classes Model,
ID: 3809309 • Letter: S
Question
SOFTWARE ENGINEERING
COMPUTER SCIENCE
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
Button blueButton, redButton;
TextField tf,tf1;
Label loginLabel,loginLabel1;
String password;
Map UserName = new HashMap();
Frame frame, frame1;
JPanel buttonPanel, buttonPanel1;
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("KhangPham", "student12+");
UserName.put("Professor", "teachclass");
UserName.put("TA1", "gradeproject");
UserName.put("TA2", "gradehw");
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);
frame1.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
}
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");
}
else{
tf1.setText(""); // password does not match
frame1.setTitle("Wrong Password");
frame1.setVisible(false);
frame1.setVisible(true);
}
}
}
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
This was a nice task to perform. In the following code, I have completely separated the model, view and the controller. First see the codes and below them is the description. Make sure all the 3 files are in the same package.
Source Code:
Model.java
import java.util.HashMap;
import java.util.Map;
public class Model {
private static Map UserName;
public static void initializeUserMap() {
UserName = new HashMap();
}
public static Map getUserList() {
return UserName;
}
public static void insertUserDetails(String uname, String password) {
UserName.put(uname, password);
}
}
View.java
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;
public class View extends WindowAdapter implements ActionListener {// main class
Button blueButton, redButton;
TextField tf, tf1;
Label loginLabel, loginLabel1;
String uname;
Frame frame, frame1;
JPanel buttonPanel, buttonPanel1;
public View(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
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);
frame1.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Controller.initialize();
String cmd = e.getActionCommand();
if (cmd.equals("Input Username")) { // if username has been submitted
String user = tf.getText(); // get the user name
try {
if (Controller.isValidUserName(user)) {
uname = user; // get username. will be used later for checking the valid password
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
String password = tf1.getText();
if (Controller.isValidPassword(uname, password)) { // password matches
frame1.setTitle("You have sucessfully logged in");
} else {
tf1.setText(""); // password does not match
frame1.setTitle("Wrong Password");
frame1.setVisible(false);
frame1.setVisible(true);
}
}
}
public static void main(String args[]) {
new View("Log in Screen");
}
}
Controller.java
import java.util.Map;
public class Controller {
public static void initialize() {
Model.initializeUserMap();
Model.insertUserDetails("KhangPham", "student12+");
Model.insertUserDetails("Professor", "teachclass");
Model.insertUserDetails("TA1", "gradeproject");
Model.insertUserDetails("TA2", "gradehw");
}
public static boolean isValidUserName(String uname) {
Map users = Model.getUserList();
if (users.containsKey(uname)) {
// you can directly return users.containsKey(uname) as it returns true or false so
// then no need of this if else but it will be something more complex so I simplified it
// if this containsKey returns true, I return true otherwise false.
return true;
} else {
return false;
}
}
public static boolean isValidPassword(String uname, String password) {
Map users = Model.getUserList();
return users.get(uname).equals(password);
}
}
Description:
In model, the UserName hashmap is defined, first it is initialized from the controller. This class will have only two methods, one for returning the present usernames along with the password and the other is to add a new user name if needed in future.
The main work is done inside the controller. I have defined an initialize() method inside the controller which calls initializeUserMap() from model to create a new HashMap. This is done only for the first and single time. Then after initializing it, I am inserting the user details to the map using insertUserDetails() method. So, when the controller's initialize() method is called, it will create a HashMap first and will insert the 4 username and password key-value pairs in that HashMap.
The next method controller is having is isValidUserName(). This is called from the view when username is entered and pressed submit button. So, the entered username is passed to this function.
Now we need to check in the HashMap stored in the Model whether this username is present or not. So, we first get the complete HashMap using getUserList() method in the first line of this function. Then there is a property containsKey() which returns true if the key is present or not in HashMap. So, if the username is there, it will return true otherwise false is returned from this function.
The next method in controller is isValidPassword() in which I have passed both the username and password entered. Here, this method is called from view only when the username is available so no need to check whether user is available or not. Just get the password and compare it with entered password.
In this method also, first the userList is taken and using get(), the password is found from the list and using equals(), two strings are compared. It returns true for valid password otherwise false.
Inside the view, when the submit button is pressed for the first time after writing the username, I am initializing the model using controller's method. Then at place I am checking for username present or not. I am storing this username in the local variable uname also so that it can be used further. For valid username, the password is checked and the similar logic is done as earlier program in the view part. Just the decision is taken by controller and returned to view and view does not have any information regarding model.
Note: In Model and Controller both, I have made all the methods static so that they can be used using the class name and we do not require to make an object of that.
I have tested this code and the working is similar to the uploaded program.
Do comment if there is any query. Thank you. :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.