user3034812
user3034812

Reputation: 1

creating a dummy object in java

I'm working on a java program that has two classes. one class, called employee, has the declarations and the validations like name, age, and employee number. In my main class, I want to validate the employee number and the number alone. how do i create a dummy object(or dummy accessor called by some) to obtain it? here's what my program generally looks like:

package empdatabase;

public class empdatabase

{
public static employee [] emp = new employee[50];
public static int count = 0;

public static void main(String[] args) {


     }//end class main


 public static void usermenu()
 {
    String input = new String("");
    int choice = 0;

    input = JOptionPane.showInputDialog("user menu"+"\n 1. employee registration"
                                      + "\n 2. employee login")

    choice = Integer.parseInt(input);

    switch(choice)

    {

     case 1:
     if (count <50){
        emp[count] = new employee();
        emp[count].getemployee();


        emp[count].disp();

        count++;
        //usermenu();
        }//end if statment
                    break;

     case 2:
                   emplogin();
                   break;

     default:

                   JOptionPane.showMessageDialog("error, not a valid choice");


     }//end usermenu


     public static void emplogin()
     {
        String input = new String("");
        input = JOptionPane.showInputDialog("enter your employee ID");

        //dummy object goes here


      }//end login


 }//end empdatabase class



class employee{


     String empNumber;
  String First;
 String Last;
 int age;


employee() 
{
     empNumber = "";
 First = "";
 Last = "";
     age = 0;



}//end employee constructor



boolean ValidateLetter(String input)
            {
    for(int i = 0; i < input.length(); i++)
    { 

        if( !Character.isLetter(input.charAt(i)))
        { 
            return false;
        }//end if Char...


    }//end while i for loop
return true;


}//end ValidateLetter


    boolean checkNumber(String input)
{
    int i = 0;
    while( i < 9)
    {
             if(!Character.isDigit(input.charAt(i)))
                 return false;
             i++;
    }//end while
    return true;

}//end check number

    void disp()
{
    JOptionPane.showMessageDialog(null, "employee number: "+ empNumber+
                                          "\n name: " + first+ " "+ last);
}//end disp

}//end class employee

It's bare bones but it should give you an idea of what I need.

Upvotes: 0

Views: 8500

Answers (1)

Boann
Boann

Reputation: 50041

Since your employee.ValidateLetter and employee.checkNumber methods do not use any fields of the employee class, they should be static.

static boolean ValidateLetter(String input) { ... }

Now you can call them without any particular employee object:

if (employee.ValidateLetter(input)) {
    ...
}

Also please consider following the standard naming conventions as it will make your code much easier to read. E.g, something like Employee.isValidName rather than employee.ValidateLetter.

Upvotes: 1

Related Questions