Results 1 to 20 of 35
Thread: Help on my project!!
- 02-10-2009, 02:29 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 19
- Rep Power
- 0
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 ?
- 02-10-2009, 02:55 PM #2
Member
- Join Date
- Feb 2009
- Location
- Italy
- Posts
- 51
- Rep Power
- 0
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:
now let's talk about the checkPassword method.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); }
in that method the loop
is totally useless....because on the first execution it will return so you should remove it and leave onlyJava Code:for(int i = 0; i<userPassword.length; i++){ return ok; }
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
- 02-10-2009, 03:23 PM #3
Member
- Join Date
- Feb 2009
- Posts
- 19
- Rep Power
- 0
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
- 02-10-2009, 03:47 PM #4
Member
- Join Date
- Feb 2009
- Location
- Italy
- Posts
- 51
- Rep Power
- 0
first of all ...... the method getProjectRec can be written better:
i don't understand very well your question, but i will try to answer...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 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
- 02-10-2009, 03:50 PM #5
Member
- Join Date
- Feb 2009
- Location
- Italy
- Posts
- 51
- Rep Power
- 0
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
- 02-10-2009, 04:08 PM #6
Member
- Join Date
- Feb 2009
- Posts
- 19
- Rep Power
- 0
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.
- 02-10-2009, 04:31 PM #7
Member
- Join Date
- Feb 2009
- Location
- Italy
- Posts
- 51
- Rep Power
- 0
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 :)
- 02-10-2009, 04:34 PM #8
Member
- Join Date
- Feb 2009
- Posts
- 19
- Rep Power
- 0
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 =(
- 02-10-2009, 04:53 PM #9
Member
- Join Date
- Feb 2009
- Location
- Italy
- Posts
- 51
- Rep Power
- 0
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); }
- 02-10-2009, 05:02 PM #10
Member
- Join Date
- Feb 2009
- Posts
- 19
- Rep Power
- 0
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 05:10 PM.
- 02-10-2009, 05:16 PM #11
Member
- Join Date
- Feb 2009
- Location
- Italy
- Posts
- 51
- Rep Power
- 0
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);
- 02-10-2009, 05:27 PM #12
Member
- Join Date
- Feb 2009
- Posts
- 19
- Rep Power
- 0
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 05:41 PM.
- 02-10-2009, 06:05 PM #13
Member
- Join Date
- Feb 2009
- Posts
- 19
- Rep Power
- 0
oh i got it already! Thanks many many for your help :D.
- 02-10-2009, 06:07 PM #14
Member
- Join Date
- Feb 2009
- Posts
- 19
- Rep Power
- 0
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 09:22 AM.
- 02-11-2009, 11:19 AM #15
Member
- Join Date
- Feb 2009
- Location
- Italy
- Posts
- 51
- Rep Power
- 0
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
- 02-11-2009, 01:08 PM #16
Member
- Join Date
- Feb 2009
- Posts
- 19
- Rep Power
- 0
Hi u're finally here! Deadlines tml! AHHHA!
Hmm.. how do you create the controller? :P
- 02-11-2009, 01:27 PM #17
Member
- Join Date
- Feb 2009
- Posts
- 19
- Rep Power
- 0
& wolfy, ProjectRec p = new ProjectRec("0811111A", "Date", "Student");
What do i add inside new ProjectRec(??)
- 02-11-2009, 02:44 PM #18
Member
- Join Date
- Feb 2009
- Location
- Italy
- Posts
- 51
- Rep Power
- 0
this line is the creation of an instance of the controller:
I don't understand one thing.....what do you use for projectVec?Java Code:ProjectController controller = new ProjectController();
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:
doing this will initialize the data in the controller so can do your checks on them afterJava 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");
- 02-11-2009, 03:09 PM #19
Member
- Join Date
- Feb 2009
- Posts
- 19
- Rep Power
- 0
Hmm that means vector is not needed?
- 02-11-2009, 03:12 PM #20
Member
- Join Date
- Jan 2009
- Posts
- 92
- Rep Power
- 0
Similar Threads
-
open existing project project ..
By itaipee in forum EclipseReplies: 1Last Post: 12-28-2008, 08:12 PM -
Help With Project!!!
By jackhammer in forum New To JavaReplies: 5Last Post: 12-04-2008, 05:10 AM -
Need Help With Project
By maggie_2 in forum New To JavaReplies: 1Last Post: 12-02-2008, 08:24 AM -
First Project Need Big Help
By earl in forum New To JavaReplies: 1Last Post: 01-18-2008, 06:12 PM


LinkBack URL
About LinkBacks

Bookmarks