I have set up a basic MVC project which is a student administration application based upon a CSV file as datastore. Each user has a specific role (student, lecturer, professor, leader of degree program). After successful login, the main window should have different actions to be performed based upon each role.
How can I hide the login view after successful login and show the main window depending on each user's role?
What about interfaces and abstract classes? Can I generalize/abstract the code better?
Regarding the different roles, I've thought about User
as an abstract class:
Student
inherits fromUser
Lecturer
inherits fromUser
Professor
andLeader
fromLecturer
Should the ActionListener
be in the view or in the controller?
Is it better to init a frame in the main class or should each view extends/inherits from JFrame instead of JPanel?
General feedback on the code so far is very welcome.
users.csv
user,password,role
user1,password1,student
user2,password2,professor
Studentenverwaltung.java
package com.studentenverwaltung;
import java.awt.EventQueue;
import javax.swing.JFrame;
import com.studentenverwaltung.controller.UserController;
import com.studentenverwaltung.model.User;
import com.studentenverwaltung.view.LoginPanel;
class Studentenverwaltung implements Runnable {
public static void main(String[] args) {
EventQueue.invokeLater(new Studentenverwaltung());
}
@Override
public void run() {
User user = new User();
LoginPanel loginPanel = new LoginPanel(user);
UserController userController = new UserController(user, loginPanel);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(loginPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
UserController.java
package com.studentenverwaltung.controller;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import com.studentenverwaltung.helpers.UserCredentials;
import com.studentenverwaltung.model.User;
import com.studentenverwaltung.persistence.FileUserDAO;
import com.studentenverwaltung.persistence.UserDAO;
import com.studentenverwaltung.view.LoginPanel;
public class UserController {
private User user;
private LoginPanel loginPanel;
public UserController(User user, LoginPanel loginPanel) {
this.user = user;
this.loginPanel = loginPanel;
this.loginPanel.login.addActionListener(new LoginListener());
}
class LoginListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String id = UserController.this.loginPanel.getId();
String password = UserController.this.loginPanel.getPassword();
if (LoginListener.this.authenticate(new UserCredentials(id,
password))) {
// show main window based upon specific user role and hide login
// window
System.out.println("Successfully logged in...");
}
}
private boolean authenticate(UserCredentials userCredentials) {
UserDAO userDAO = new FileUserDAO("Files/users.csv");
String id = userCredentials.getId();
String password = userCredentials.getPassword();
if (userDAO.getUser(id) != null
&& userDAO.getUser(id).checkPassword(password)) {
return true;
}
return false;
}
}
}
User.java
package com.studentenverwaltung.model;
import java.util.Observable;
public class User extends Observable {
private String id;
private String password;
private String role;
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
setChanged();
notifyObservers();
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
setChanged();
notifyObservers();
}
public String getRole() {
return this.role;
}
public void setRole(String role) {
this.role = role;
setChanged();
notifyObservers();
}
public boolean checkPassword(String password) {
return this.password.equals(password);
}
}
LoginPanel.java
package com.studentenverwaltung.view;
import java.awt.GridLayout;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import com.studentenverwaltung.model.User;
public class LoginPanel extends JPanel {
private static final long serialVersionUID = 1L;
private User user;
private JLabel idLbl;
private JTextField idTxt;
private JLabel pwdLbl;
private JTextField pwdTxt;
public JButton login;
public LoginPanel(User user) {
this.user = user;
this.user.addObserver(new UserObserver());
this.init();
}
private void init() {
this.setLayout(new GridLayout(3, 1));
this.idLbl = new JLabel("id");
this.add(idLbl);
this.idTxt = new JTextField(15);
this.add(idTxt);
this.pwdLbl = new JLabel("password");
this.add(pwdLbl);
this.pwdTxt = new JPasswordField(15);
this.add(pwdTxt);
this.login = new JButton("login");
// login.addActionListener(new LoginListener());
this.add(login);
}
public String getId() {
return this.idTxt.getText();
}
public String getPassword() {
return this.pwdTxt.getText();
}
private class UserObserver implements Observer {
@Override
public void update(Observable o, Object arg) {
// TODO Auto-generated method stub
}
}
}