Page 1 of 2 12 LastLast
Results 1 to 20 of 35
  1. #1
    Winniee is offline Member
    Join Date
    Feb 2009
    Posts
    19
    Rep Power
    0

    Default Help on my project!!

    /**
    * @(#)Project.java
    *
    *
    * @author
    * @version 1.00 2009/1/13
    */

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.Vector;
    import java.util.Date;
    import java.text.*;

    public class Project extends JFrame{

    JPanel pNorth,pSouth;
    JButton btn1,btn2;
    JTextField tfUser;
    JPasswordField tfPass;
    JLabel lblUser, lblPass;
    Vector projectVec = new Vector();
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    String [] userName = {"0811111A","0822222B","08333333C"};//for the list of predetermined users
    String [] userPassword = {"p1", "p2"};//for the list of predetermined users
    String [] userType = {"Student", "Tutor"};//for checking user type



    public Project() {
    setTitle("Student Attendence System");
    setSize(300,150);
    setResizable(false);
    setLayout(new BorderLayout());


    Date date = new Date();
    String datetime = dateFormat.format(date);


    pNorth = new JPanel();
    pSouth = new JPanel();
    pNorth.setLayout(new GridLayout(2,2));
    pSouth.setLayout(new GridLayout(1,3));

    lblUser = new JLabel("user ID");
    tfUser = new JTextField();
    lblPass = new JLabel("Password");
    tfPass = new JPasswordField();
    btn1 = new JButton("Login");
    btn2 = new JButton("Clear");

    btn1.addActionListener(new LoginHandler());
    btn2.addActionListener(new ClearHandler());

    pNorth.add(lblUser);
    pNorth.add(tfUser);
    pNorth.add(lblPass);
    pNorth.add(tfPass);
    pSouth.add(btn1);
    pSouth.add(btn2);

    add(pNorth, "Center");
    add(pSouth, "South");
    setVisible(true);
    }
    class LoginHandler implements ActionListener
    {
    public void actionPerformed(ActionEvent evt)
    {

    String User = tfUser.getText();
    String pass = "";

    try{ pass = tfPass.getText();}
    catch(Exception ex){

    //create Project object
    ProjectRec p = new ProjectRec("0811111A","Date", "Student");
    projectVec.add(p);
    }
    Date date = new Date();
    String datetime = dateFormat.format(date);
    String loginShow = "Student ID: "+User +" Login at "+datetime;
    JOptionPane.showMessageDialog(null, loginShow , "Message", JOptionPane.PLAIN_MESSAGE);

    }
    }
    class ClearHandler implements ActionListener
    {
    public void actionPerformed(ActionEvent evt)
    {
    tfUser.setText(""); // clear the output fields
    tfPass.setText("");
    }
    }

    public boolean checkPassword(String nm, String pwd)
    {
    boolean ok = false;
    int userIndex = -1;

    for(int i = 0; i<userName.length; i++)
    {

    if(userName[i].equals(nm))
    {
    userIndex = i;
    }
    }
    if(userPassword[userIndex].equals(pwd))
    {
    ok = true;
    }
    for(int i = 0; i<userPassword.length; i++)
    {
    return ok;
    }
    return ok;
    }
    public static void main(String args[])
    {
    JFrame.setDefaultLookAndFeelDecorated(true);
    Project gui = new Project();
    }
    }


    I cant check for the correct password.. should i use datastorage & controller ?

  2. #2
    wolfcro is offline Member
    Join Date
    Feb 2009
    Location
    Italy
    Posts
    51
    Rep Power
    0

    Default

    if you are interested in password checking than the interested method is checkPassword. But WHERE do you invoke that method ???

    you should use IT in the LoginHandler:

    Java Code:
    if (checkPassword(User,pass)){
    		
    	Date date = new Date();
    	String datetime = dateFormat.format(date);
    	String loginShow = "Student ID: "+User +" Login at "+datetime;
    	JOptionPane.showMessageDialog(null, loginShow , "Message", JOptionPane.PLAIN_MESSAGE);
    } else {
    	JOptionPane.showMessageDialog(null, "User - password don't match" , "Message", JOptionPane.PLAIN_MESSAGE);
    }
    now let's talk about the checkPassword method.
    in that method the loop
    Java Code:
    for(int i = 0; i<userPassword.length; i++){ 
    return ok;
    }
    is totally useless....because on the first execution it will return so you should remove it and leave only
    return ok;

    this is a better version of the method:
    Java Code:
    public boolean checkPassword(String nm, String pwd)
    	{
    		int userIndex = -1;
    		
    		for(int i = 0; i<userName.length; i++)
    		{
    			if(userName[i].equals(nm))
    			{
    				userIndex = i;
    			}
    		}
    		
    		// check if user exists
    		if (userIndex == -1) return false;
    		
    		if(userPassword[userIndex].equals(pwd))
    		{
    			// user exists and password is correct
    			return true;
    		}
    		
    		// user exist and password is incorrect
    		return false;
    	}

    hope it helped

  3. #3
    Winniee is offline Member
    Join Date
    Feb 2009
    Posts
    19
    Rep Power
    0

    Default Thanks so much for ur help wolf!

    i have a few more questions.. sorry about that, cause project's due in 2 days. really stress out!!
    I need to differentiate between student login & Tutor login. When student logins the correct password & ID, the system will save their ID,date and time into a data structure and display the current date and time.

    When the tutor login,he will see the list of students cloaked in.
    This is my datastorage:
    import java.util.Vector;

    public class DataStorage
    {
    private Vector aRecVec;

    DataStorage()
    {
    aRecVec = new Vector(3);
    }

    public void StoreProjectRec(ProjectRec aRec)
    {
    aRecVec.addElement(aRec);
    }

    public ProjectRec getProjectRec(String name)
    {
    ProjectRec aRec = null;
    for (int i=0; i<aRecVec.size(); i++)
    {
    aRec = (ProjectRec)aRecVec.get(i);
    if (aRec.getUser().equals(name))
    break;
    aRec = null;
    }
    return aRec;
    }
    }

    ProjectRec
    public class ProjectRec
    {

    private String user;

    private String userType;

    private String date;


    public ProjectRec(String nm, String ut, String d)
    {
    user = nm;
    userType = ut;
    date = d;


    }

    public String getUser()
    {
    return user;
    }
    public void setUser(String u)
    {
    user = u;
    }
    public String getUserType()
    {
    return userType;
    }
    public void setUserType(String ut)
    {
    userType = ut;
    }
    public String getDate()
    {
    return date;
    }
    public void setDate(String d)
    {
    date = d;
    }

    }

    Controller:
    public class ProjectController {
    private DataStorage ds;
    private ProjectRec p;

    public ProjectController()
    {
    ds = new DataStorage();
    }
    public void createProjectRec(String nm, String pwd, String ut)
    {
    ProjectRec temRec = new ProjectRec(nm, pwd, ut);
    ds.StoreProjectRec(temRec);
    }
    public String [] getProjectInfo(String nm)
    {
    String [] info = new String[2];
    p = ds.getProjectRec(nm);

    info[0] = p.getUser();
    info[1] = p.getUserType();
    return info;

    }
    }

    please help :o

  4. #4
    wolfcro is offline Member
    Join Date
    Feb 2009
    Location
    Italy
    Posts
    51
    Rep Power
    0

    Default

    first of all ...... the method getProjectRec can be written better:

    Java Code:
    public ProjectRec getProjectRec(String name)
    {
          ProjectRec aRec = null;
          for (int i=0; i<aRecVec.size(); i++){
    
               aRec = (ProjectRec)aRecVec.get(i);
               if (aRec.getUser().equals(name)) return aRec;
          }
          return aRec;
    }
    i don't understand very well your question, but i will try to answer...
    i suppose you have to check which type is the user in your loginHandler and when you get the info by calling getProjectInfo(String nm) you will do what you have depending on the type

  5. #5
    wolfcro is offline Member
    Join Date
    Feb 2009
    Location
    Italy
    Posts
    51
    Rep Power
    0

    Default

    another thing in general.....
    i think you should use some more significant variable names and indentation of code.... it should improve the readability of your code

  6. #6
    Winniee is offline Member
    Join Date
    Feb 2009
    Posts
    19
    Rep Power
    0

    Default

    Thanks for the reply again wolf. Really appreciate ur help. =) Here's a more detailed question paper. I attached e file.. (:

    Back of the question paper:

    The pop-up window uses a JOptionPane to show the user the date and time that he has clocked-in. The staff will view the list of logins on a JTextArea.

    Perform an OO analysis of the program and examine the data requirements(Problem input,problem outputs,data & data structures) and algorithms required to implement the program). A Swing GUI should be designed.

    Show a message box with of the attendance list(present in blue and absent in red)based on the pre-defined in data structure of text file when the user closes the window.
    Attached Thumbnails Attached Thumbnails Help on my project!!-phpuwkssram.jpg  

  7. #7
    wolfcro is offline Member
    Join Date
    Feb 2009
    Location
    Italy
    Posts
    51
    Rep Power
    0

    Default

    yes i understand your project....
    but ask direct question where you have a problem....
    i can't write the project for you, but i will help you resolve errors or problems if i can :)

  8. #8
    Winniee is offline Member
    Join Date
    Feb 2009
    Posts
    19
    Rep Power
    0

    Default

    hehe sorry im new here. :)

    okay my questions is = > Sorting between tutor & Student.
    Eg. Tutor log in will show list of student clocked in while student will pop up what time he logs in. im stuck at this part =(

  9. #9
    wolfcro is offline Member
    Join Date
    Feb 2009
    Location
    Italy
    Posts
    51
    Rep Power
    0

    Default

    in the login handler we have to check the type when someone logins

    Java Code:
    if (checkPassword(User,pass)){
    		
    	Date date = new Date();
    	String datetime = dateFormat.format(date);
    	
            // we have to check the type
            String[] info = getProjectInfo(User);
          
            if (info[1].equals("Student")){
               // do things for student
            } else if (info[1].equals("Tutor")){
               // do things for tutor
            }  
    
    } else {
    	JOptionPane.showMessageDialog(null, "User - password don't match" , "Message", JOptionPane.PLAIN_MESSAGE);
    }

  10. #10
    Winniee is offline Member
    Join Date
    Feb 2009
    Posts
    19
    Rep Power
    0

    Default

    Thanks for the reply. =)

    Hmm.. is there a error in declaration in my ProjectController? Cause i received error called "cannot find symbol method getProjectInfo(java.lang.String)".
    Its my first time doing controller, so i'm not very sure if im correct.. sorry=(
    Last edited by Winniee; 02-10-2009 at 06:10 PM.

  11. #11
    wolfcro is offline Member
    Join Date
    Feb 2009
    Location
    Italy
    Posts
    51
    Rep Power
    0

    Default

    nono.....i made that error to have a more readable code....but i though you could correct it easly

    the getProjectInfo(User) method should be called on an ProjectController instance;

    somewhere on initialization of your project you should create a new ProjectController instance

    controller = new ProjectController();

    and then in my code you call the method
    controller.getProjectInfo(User);

  12. #12
    Winniee is offline Member
    Join Date
    Feb 2009
    Posts
    19
    Rep Power
    0

    Default

    public Project() {
    setTitle("Student Attendence System");
    setSize(300,150);
    setResizable(false);
    setLayout(new BorderLayout());

    ProjectController controller = new ProjectController();

    Date date = new Date();
    String datetime = dateFormat.format(date);


    pNorth = new JPanel();
    pSouth = new JPanel();
    pNorth.setLayout(new GridLayout(2,2));
    pSouth.setLayout(new GridLayout(1,3));

    lblUser = new JLabel("user ID");
    tfUser = new JTextField();
    lblPass = new JLabel("Password");
    tfPass = new JPasswordField();
    btn1 = new JButton("Login");
    btn2 = new JButton("Clear");

    btn1.addActionListener(new LoginHandler());
    btn2.addActionListener(new ClearHandler());

    pNorth.add(lblUser);
    pNorth.add(tfUser);
    pNorth.add(lblPass);
    pNorth.add(tfPass);
    pSouth.add(btn1);
    pSouth.add(btn2);

    add(pNorth, "Center");
    add(pSouth, "South");
    setVisible(true);
    }
    class LoginHandler implements ActionListener
    {
    public void actionPerformed(ActionEvent evt)
    {

    String User = tfUser.getText();
    String pass = "";

    try{ pass = tfPass.getText();}
    catch(Exception ex){

    //create Project object
    ProjectRec p = new ProjectRec("0811111A","Date", "Student");
    projectVec.add(p);
    }
    if (checkPassword(User,pass)){

    Date date = new Date();
    String datetime = dateFormat.format(date);


    ProjectController controller = new ProjectController();
    controller.getProjectInfo(User);

    String[] info = getProjectInfo(User);//check the type


    if (info[1].equals("Student")){
    // do things for student
    } else if (info[1].equals("Tutor")){
    // do things for tutor
    }
    else {
    JOptionPane.showMessageDialog(null, "Invalid user & Password!" , "Message", JOptionPane.PLAIN_MESSAGE);

    }
    }
    }
    }
    class ClearHandler implements ActionListener
    {
    public void actionPerformed(ActionEvent evt)
    {
    tfUser.setText(""); // clear the output fields
    tfPass.setText("");
    }
    }
    public boolean checkPassword(String nm, String pwd)
    {
    int userIndex = -1;

    for(int i = 0; i<userName.length; i++)
    {
    if(userName[i].equals(nm))
    {
    userIndex = i;
    }
    }

    // check if user exists
    if (userIndex == -1) return false;

    if(userPassword[userIndex].equals(pwd))
    {
    // user exists and password is correct
    return true;
    }

    // user exist and password is incorrect
    return false;
    }


    I'm so sorry! but could you tell me where i put the codes? :confused:
    Last edited by Winniee; 02-10-2009 at 06:41 PM.

  13. #13
    Winniee is offline Member
    Join Date
    Feb 2009
    Posts
    19
    Rep Power
    0

    Default

    oh i got it already! Thanks many many for your help :D.

  14. #14
    Winniee is offline Member
    Join Date
    Feb 2009
    Posts
    19
    Rep Power
    0

    Default

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.Vector;
    import java.util.Date;
    import java.text.*;

    public class Project extends JFrame{

    JPanel pNorth,pSouth;
    JButton btn1,btn2;
    JTextField tfUser;
    JPasswordField tfPass;
    JLabel lblUser, lblPass;
    Vector projectVec = new Vector();
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    String [] userName = {"0811111A","0822222B","08333333C"};//for the list of predetermined users
    String [] userPassword = {"p1", "p2","p3"};//for the list of predetermined users
    String [] userType = {"Student", "Tutor"};//for checking user type



    public Project() {
    setTitle("Student Attendence System");
    setSize(300,150);
    setResizable(false);
    setLayout(new BorderLayout());

    ProjectController controller = new ProjectController();

    Date date = new Date();
    String datetime = dateFormat.format(date);


    pNorth = new JPanel();
    pSouth = new JPanel();
    pNorth.setLayout(new GridLayout(2,2));
    pSouth.setLayout(new GridLayout(1,3));

    lblUser = new JLabel("user ID");
    tfUser = new JTextField();
    lblPass = new JLabel("Password");
    tfPass = new JPasswordField();
    btn1 = new JButton("Login");
    btn2 = new JButton("Clear");

    btn1.addActionListener(new LoginHandler());
    btn2.addActionListener(new ClearHandler());

    pNorth.add(lblUser);
    pNorth.add(tfUser);
    pNorth.add(lblPass);
    pNorth.add(tfPass);
    pSouth.add(btn1);
    pSouth.add(btn2);

    add(pNorth, "Center");
    add(pSouth, "South");
    setVisible(true);
    }
    class LoginHandler implements ActionListener
    {
    public void actionPerformed(ActionEvent evt)
    {

    String User = tfUser.getText();
    String pass = "";

    try{ pass = tfPass.getText();}
    catch(Exception ex){

    //create Project object
    ProjectRec p = new ProjectRec("0811111A","Date", "Student");
    projectVec.add(p);
    }
    if (checkPassword(User,pass)){

    Date date = new Date();
    String datetime = dateFormat.format(date);

    ProjectController controller = new ProjectController();

    String[] info = controller.getProjectInfo(User);//check the type


    if (info[1].equals("Student")){
    // do things for student
    } else if (info[1].equals("Tutor")){
    // do things for tutor
    }
    } else {
    JOptionPane.showMessageDialog(null, "Invalid user & Password!" , "Message", JOptionPane.PLAIN_MESSAGE);
    }
    }
    }
    class ClearHandler implements ActionListener
    {
    public void actionPerformed(ActionEvent evt)
    {
    tfUser.setText(""); // clear the output fields
    tfPass.setText("");
    }
    }
    public boolean checkPassword(String nm, String pwd)
    {
    int userIndex = -1;

    for(int i = 0; i<userName.length; i++)
    {
    if(userName[i].equals(nm))
    {
    userIndex = i;
    }
    }

    // check if user exists
    if (userIndex == -1) return false;

    if(userPassword[userIndex].equals(pwd))
    {
    // user exists and password is correct
    return true;
    }

    // user exist and password is incorrect
    return false;
    }
    public static void main(String args[])
    {
    JFrame.setDefaultLookAndFeelDecorated(true);
    Project gui = new Project();
    }
    }

    okay i can display the wrong password&username. But when i key in the correct password and user nothing appears. help!
    Last edited by Winniee; 02-11-2009 at 10:22 AM.

  15. #15
    wolfcro is offline Member
    Join Date
    Feb 2009
    Location
    Italy
    Posts
    51
    Rep Power
    0

    Default

    hehe this is a really funny error but easy to correct

    1) in the LoginHandler DO NOT create a new local ProjectController but use the one you already created in the Project constructor

    2) where do you add the data into your controller?
    if you have the information about users (name, pass, type) in a file you should load this data into you controller in the Project constructor after creating it

    as it is now....your controller is empty and of course it can't find you any user info :P

  16. #16
    Winniee is offline Member
    Join Date
    Feb 2009
    Posts
    19
    Rep Power
    0

    Default

    Hi u're finally here! Deadlines tml! AHHHA!

    Hmm.. how do you create the controller? :P

  17. #17
    Winniee is offline Member
    Join Date
    Feb 2009
    Posts
    19
    Rep Power
    0

    Default

    & wolfy, ProjectRec p = new ProjectRec("0811111A", "Date", "Student");

    What do i add inside new ProjectRec(??)

  18. #18
    wolfcro is offline Member
    Join Date
    Feb 2009
    Location
    Italy
    Posts
    51
    Rep Power
    0

    Default

    this line is the creation of an instance of the controller:

    Java Code:
    ProjectController controller = new ProjectController();
    I don't understand one thing.....what do you use for projectVec?
    another thing....

    if the arrays userName and userPassword contains predermined data to use in Login then you should add it's data to the controller after you create it in the constructor:

    Java Code:
    ProjectController controller = new ProjectController();
    for (int i=0;i<3;i++){
      //insert data into controller
      controller.createProjectRec(userName[i], userPassword[i], "Student");
    }
    // you can add data for tutors too if you want
    //  here is an example
    controller.createProjectRec("myProf", "hisPass", "Tutor");
    doing this will initialize the data in the controller so can do your checks on them after

  19. #19
    Winniee is offline Member
    Join Date
    Feb 2009
    Posts
    19
    Rep Power
    0

    Default

    Hmm that means vector is not needed?

  20. #20
    Moncleared is offline Member
    Join Date
    Jan 2009
    Posts
    92
    Rep Power
    0

    Default

    Just a tip. When you copy and paste your code into the text box on the forums, you can highlight it and hit the "Code" button which will format it (If indentation is present) and make it easier to read.

    GL on your Project

Page 1 of 2 12 LastLast

Similar Threads

  1. open existing project project ..
    By itaipee in forum Eclipse
    Replies: 1
    Last Post: 12-28-2008, 09:12 PM
  2. Help With Project!!!
    By jackhammer in forum New To Java
    Replies: 5
    Last Post: 12-04-2008, 06:10 AM
  3. Need Help With Project
    By maggie_2 in forum New To Java
    Replies: 1
    Last Post: 12-02-2008, 09:24 AM
  4. First Project Need Big Help
    By earl in forum New To Java
    Replies: 1
    Last Post: 01-18-2008, 07:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •