I got some trouble in my app.
In my validate function, even when a and m are the same, and b and n too, it still return false.
Still in validate function, the ArrayList<staff> st = aa.getst(); call returns an empty list.
Staff:
import java.util.ArrayList;
public class staff {
private String StaffID;
private String StaffPW;
private ArrayList<staff> st;
public staff (String StaffID, String StaffPW) {
st = new ArrayList<staff>();
this.StaffID = StaffID;
this.StaffPW = StaffPW;
}
public void thestaff(){
staff d = new staff("Chan","123");
staff e = new staff("Wong","456");
staff f = new staff("Fong","789");
st.add (d);
st.add (e);
st.add (f);
}
public String getID() {
return StaffID;
}
public String getPW() {
return StaffPW;
}
public ArrayList<staff> getst() {
return st;
}
}
'
Login
import java.util.ArrayList;
public class Login {
private static staff aa;
private static String c,d;
public static boolean validate(String a, String b) {
aa = new staff(c,d);
ArrayList<staff> st = aa.getst();
System.out.println (st.size());
for (int i = 0; i < st.size(); i++) {
String m = st.get(i).getID();
String n = st.get(i).getPW();
if (a == m) and (b==n)
return true;
}
return false;
}
}
'
CreateGUI:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import domain.Login;
import domain.staff;
public class CreateGUI extends JFrame {
private ArrayList<staff> st = new ArrayList<staff>();
public CreateGUI() {
this.setLayout(new GridLayout(4, 2));
JTextField Text1;
JTextField Text2;
JLabel Label1 = new JLabel("Intranet Room Booking System");
JLabel Label2 = new JLabel("Staff ID:");
Text1 = new JTextField("");
JLabel Label3 = new JLabel("Staff PW:");
Text2 = new JTextField("");
JButton Button1 = new JButton("Login");
this.add(Label1);
this.add(new JLabel(""));
this.add(Label2);
this.add(Text1);
this.add(Label3);
this.add(Text2);
this.add(new JLabel(""));
this.add(Button1);
Button1.setBackground(Color.GREEN);
Button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String a = Text1.getText();
String b = Text2.getText();
if (a.isEmpty() == true || b.isEmpty() == true) {
JOptionPane.showMessageDialog(null,"Invalid entry!");
} else {
if (Login.validate(a, b)) JOptionPane.showMessageDialog(null,"Right!");
else JOptionPane.showMessageDialog(null,"Wrong!");;
}
}
});
this.setLocation(100, 100);
this.setSize(new Dimension(900, 250));
this.setVisible(true);
}
}
I found some of the problems:
the ArrayList in class login cannot be read;
in class login, a cannot equal to m;
i don't know how to fix the mistakes...thank you!