- #1
Smiles1
- 8
- 0
I am trying to get two different menus to show up depending on whether the User is a learner or an admin. However, I'm having some difficulty with the program and the execution. I would appreciate any help or suggestions.
I don't know how to access a user to figure out if it's a learner or an admin. I have been trying to gain access to the method hasSpecialAccess to determine this but can't figure out how to reach it.
Would it be easier if I override this method in my children classes.. but then how would I take in the name and password from the view? :(
I tried to create some tests to maybe figure out what I'm doing wrong and found out that the method testClassHierarchy() in my testClassHierarchy() is giving me null values for the names and passwords.
I had to leave out some classes because I reached the limit on text.ADMIN
TEST HARNESS
USER CODE CONTAINS LOGIN PROBLEMS
LOGINMENUVIEW
View attachment 8423
I don't know how to access a user to figure out if it's a learner or an admin. I have been trying to gain access to the method hasSpecialAccess to determine this but can't figure out how to reach it.
Would it be easier if I override this method in my children classes.. but then how would I take in the name and password from the view? :(
I tried to create some tests to maybe figure out what I'm doing wrong and found out that the method testClassHierarchy() in my testClassHierarchy() is giving me null values for the names and passwords.
I had to leave out some classes because I reached the limit on text.ADMIN
Code:
import java.awt.Image;
public class Admin extends User {
private String adminName;
private String adminPassword;
private boolean hasSpecialAccess;
private Avatar avatarAdmin;
private Image supremeRuler = null;
private String rank;
public Admin(String adminName, String adminPassword, boolean hasSpecialAccess, Avatar avatarAdmin, String rank) {
super(adminName, adminPassword, avatarAdmin, hasSpecialAccess, rank);
}
public String getAdminName() {
return adminName;
}
public void setAdminName(String adminName) {
this.adminName = adminName;
}
public String getAdminPassword() {
return adminPassword;
}
public void setAdminPassword(String adminPassword) {
this.adminPassword = adminPassword;
}
@Override
public Boolean getHasSpecialAccess() {
return true;
}
public void setHasSpecialAccess(boolean hasSpecialAccess) {
this.hasSpecialAccess = hasSpecialAccess;
}
public Avatar getAvatarAdmin() {
return avatarAdmin;
}
public void setAvatarAdmin(Avatar avatarAdmin) {
this.avatarAdmin = avatarAdmin;
}
public Image getSupremeRuler() {
return supremeRuler;
}
public void setSupremeRuler(Image supremeRuler) {
this.supremeRuler = supremeRuler;
}
@Override
public String getRank() {
return "SupremeRuler";
}
@Override
public String toString() {
return "Admin{" + "adminName=" + adminName + ", adminPassword=" + adminPassword + ", hasSpecialAccess=" + hasSpecialAccess + ", avatarAdmin=" + avatarAdmin + ", supremeRuler=" + supremeRuler + ", rank=" + rank + '}';
}
}
TEST HARNESS
Code:
public class TestHarness {
//Test the Learner and FlashCard classes
public TestHarness() {
Image image1 = null;
Avatar avatarInstance = new Avatar("TiredTony", "3333", image1);
Learner learnerInstance = new Learner("CaptainCrunch", "donutBaker123", avatarInstance, true, "Tadpole");
if (learnerInstance != null) {
System.out.println("Test Learner created successfully");
System.out.println("Learner Name = " + learnerInstance.getLearnerName() + "Learner Password " + learnerInstance.getLearnerPassword());
//Setting Learner Name and Password
learnerInstance.setLearnerName("banana");
learnerInstance.setLearnerPassword("lordOfTheBananas");
//Testing object change
System.out.println("Learner Name = " + learnerInstance.getLearnerName() + "Learner Password " + learnerInstance.getLearnerPassword());
}
testClassHierarchy();
}
public void testClassHierarchy() {
ArrayList<User> userArray = new ArrayList<User>();
Image lemonImage = null;
Avatar avatar1 = new Avatar("LemonHead", "Yellow", lemonImage);
Learner learner1 = new Learner("LarryLarengetis", "Learnafed", avatar1, false, "Grasshopper");
Admin admin1 = new Admin("Terrance", "Turtle", true, avatar1, "SupremeRuler");
userArray.add(learner1);
userArray.add(admin1);
for (int i = 0; i < userArray.size(); i++) {
System.out.println(userArray.get(i).getHasSpecialAccess());
System.out.println(userArray.get(i).getRank());
}
}
}
Code:
public class User {
private String userName;
private String userPassword;
private Avatar avatar;
private Boolean hasSpecialAccess;
private String rank;
public User(String userName, String userPassword, Avatar avatar, Boolean hasSpecialAccess, String rank) {
this.userName = userName;
this.userPassword = userPassword;
this.avatar = avatar;
this.hasSpecialAccess = hasSpecialAccess;
this.rank = rank;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public Avatar getAvatar() {
return avatar;
}
public void setAvatar(Avatar avatar) {
this.avatar = avatar;
}
public Boolean getHasSpecialAccess() {
return hasSpecialAccess;
}
public void setHasSpecialAccess(Boolean hasSpecialAccess) {
this.hasSpecialAccess = hasSpecialAccess;
}
public String getRank() {
return rank;
}
//LOGIN PROBLEM
public void Login(String viewTextName, String viewTextPassword) {
if (this.userName.equals(viewTextName) && this.userPassword.equals(viewTextPassword)) {
System.out.println("Access Granted to the Learner view");
} else if (this.userName.equals(viewTextName) && this.userPassword.equals(viewTextPassword)) {
System.out.println("Access Granted to the Admin view");
} else {
System.out.println("NO ACCESS FOR YOU");
}
}
}
LOGINMENUVIEW
Code:
import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class LoginMenuView extends JFrame {
//Will display login menu which will ask for learner name and password, verify it's correct and then allow access to the homescreen.
private String displayMsg = " ";
private JLabel userNameLabel;
private JLabel passwordLabel;
private JTextField jTxtUsername;
private JTextField jTxtPassword;
private JButton loginButton;
private JButton cancelButton;
private JLabel welcomeBanner;
private Image img = null;
private Avatar avatar = new Avatar("Avataronian", "11", img);
private User user = new User("Noster-waffle", "banana33", avatar, false, "");
public LoginMenuView() {
JFrame frame = new JFrame();
welcomeBanner = new JLabel("Welcome to a Mandarin flashcard App");
welcomeBanner.setForeground(Color.yellow);
welcomeBanner.setFont(new Font("Algerian", Font.ITALIC, 30));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = (JPanel) frame.getContentPane();
panel.setLayout(null);
panel.setBackground(Color.yellow);
userNameLabel = new JLabel("Username:");
userNameLabel.setForeground(Color.black);
passwordLabel = new JLabel("Password:");
passwordLabel.setForeground(Color.black);
jTxtUsername = new JTextField(30);
jTxtPassword = new JTextField(30);
loginButton = new JButton("Login");
loginButton.setBackground(Color.darkGray);
loginButton.setForeground(Color.WHITE);
loginButton.addActionListener(new LoginButtonListener());
cancelButton = new JButton("Cancel");
cancelButton.setBackground(Color.darkGray);
cancelButton.setForeground(Color.WHITE);
welcomeBanner.setBounds(10, 0, 1000, 100);
userNameLabel.setBounds(100, 100, 100, 100);
jTxtUsername.setBounds(180, 138, 130, 25);
passwordLabel.setBounds(400, 100, 100, 100);
jTxtPassword.setBounds(480, 138, 130, 25);
loginButton.setBounds(178, 178, 130, 25);
cancelButton.setBounds(480, 178, 130, 25);
panel.add(welcomeBanner);
panel.add(userNameLabel);
panel.add(jTxtUsername);
panel.add(passwordLabel);
panel.add(jTxtPassword);
panel.add(loginButton);
panel.add(cancelButton);
frame.setTitle("Mandarin Flashcards");
frame.setSize(800, 500);
frame.setVisible(true);
}
public class LoginButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
String viewTextName = jTxtUsername.getText();
String viewTextPassword = jTxtPassword.getText();
user.Login(viewTextName, viewTextPassword);
}
}
}
View attachment 8423
Attachments
Last edited by a moderator: