Results 1 to 2 of 2
  1. #1
    Oshley is offline Member
    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Unhappy Employee/ProductionWorker Class

    Hey, I am in a programming class in my university and I am super stuck on where else to go in my assignment (posted below). I am having trouble validating user input, ie having proper number formatting for employee number. This is the criteria:

    Design a class named Employee. The class should keep the following information in
    fields:
    • Employee name
    • Employee number in the format XXX-L, where each X is a digit within the range
    0-9 and the Lis aletter within the range A-M.
    • Hire date
    * Write one or more constructors and the appropriate accessor and mutator methods for the
    class.
    Next, write a class named ProductionWorker that extends the Employee class. The
    ProductionWorker class should have fields to hold the following information:
    • Shift (an integer)
    • Hourly pay rate (a double)
    The workday is divided into two shifts: day and night. The shift field will be an integer value
    representing the shift that the employee works. The day shift is shift 1 and the night shift is
    shift 2. Write one or more constructors and the appropriate accessor and mutator methods
    for the class. Demonstrate the classes by writing a program that uses a ProductionWorker
    object.

    I must use inheritance too. I couldn't attach the zip, so here is what I have below. Any help is much appreciated <33333333333

    Employee class

    PHP Code:
    public class Employee
    {
        //private fields
        public String name;
        public String number;
        public String hireDate;
        private boolean validNumber= false;
        
        //constructor method
        public Employee(String n, String num, String hd)
        {
            name = n;
            number = num;
            hireDate = hd;
        }
        
        public String getName()
        {
            return name;
        }
        
        public String getNumber()
        {       
            while (validNumber = false)
            {
                if (number.length()!=5)
                   {
                    System.out.println("Invalid employee number, please try again.");
                   
                }
                else if
                      (((!Character.isDigit(number.charAt(0))) || ((!Character.isDigit(number.charAt(1)))) ||
                      ((!Character.isDigit(number.charAt(2)))) || (number.charAt(3) !='-') ||
                      ((!Character.isDigit(number.charAt(4)))) || 
                      (!(number.charAt(4)>= 'A' && number.charAt(4)<='M'))))
                      
                      {
                        System.out.println("Invalid employee number, please try again.");
                        System.out.println("Enter your employee number: ");
                      }
                    
                 else
                    validNumber = true;
                }
            return number;
        }
        
        public String getHireDate()
        {
            return hireDate;
        }
        
        /*
         * Checking validity of employee number
         *
        public boolean isValidEmployeeNumber(String number)
        {
            boolean status = true;
            
            if (number.length()!=5)
             {   System.out.println("Invalid employee number.");
                number = s.nextLine();}
            else
            {
               if
                  (((!Character.isDigit(number.charAt(0))) || ((!Character.isDigit(number.charAt(1)))) ||
                  ((!Character.isDigit(number.charAt(2)))) || (number.charAt(3) !='-') ||
                  ((!Character.isDigit(number.charAt(4)))) || 
                  (!(number.charAt(4)>= 'A' && number.charAt(4)<='M'))))
                  
                  {
                      status = false;
                    }
                }
            return status;
        }
        */
        public String toString()
        {
            String str = "Name: " +name+ "\nEmployee Number: ";
            
            if(number =="")
            {
                str+="Invalid Employee Number";
            }
            else
            {
                str+=number;
            }
            str +=("\nHire Date: " +hireDate);
            return str;
        }
    }
    ------------------------------------------------------------------------------
    ProductionWorker class
    import java.util.*;
    import java.text.DecimalFormat;
    import java.text.DateFormat;

    PHP Code:
    public class ProductionWorker extends Employee //this is correct - need this
    {
       public static int dayShift =1;
       public static int nightShift=2;
       private int shift;
       
       private double payRate;
       private boolean rightShift =false;
       
       
       public String getShift() //EDIT#2: FIXED (this will be good enough for me!)
       {
           String shiftTime;
           
           if (shift==1)
                shiftTime = "Day";
           else if (shift==2) 
                shiftTime = "Night";
           else if (shift==3)
                shiftTime = "Alternating";
           else
                shiftTime = "On Call";
                       
           return shiftTime;
        }
    
       
       public void setShift(int s)
        {
            shift = s;
        }
        
       public double getPayRate()
       {
           return payRate;
        }
        
       public void setPayRate(double rate)
       {
            payRate = rate;    
       }
       
           
           
       /**
        * Constructor initializes a name, number and hire date
        */
       
       public ProductionWorker(//String name, String number, String hireDate, 
        String n, String num, String hd, int sh, double rate)
       {
          
           super(n, num, hd);
           shift = sh;
           payRate = rate;
        }
        
        public ProductionWorker(String n, String num, String hd)
        {
            super(n, num, hd);
            shift = dayShift;
            payRate = 0.0;
        }
      
        public String toString()
        {
            DecimalFormat dollar = new DecimalFormat("#,##0.00");
            
            String str = super.toString();
            
            str+= "\nShift: ";
            if(shift==dayShift)
                str+="Day";
            else if (shift ==nightShift)
                str+="Nights";
            else
                str+= "Invalid Shift Number";
                
            str+= ("\n Hourly Pay Rate: $" +
                dollar.format(payRate));
                
            return str;
        }
       
    }
    -------------------------------------------------------------------------
    ProductionWorkerDemo
    import java.util.*;
    import java.text.DecimalFormat;
    import java.text.DateFormat;

    PHP Code:
    public class ProductionWorkerDemo
    {
       
        public static void main(String[] args)
        {
            String name; String n;
            String number; String num;
            String hireDate; String hd;
            int shift;
            double payRate; double rate;
            
            Scanner s = new Scanner(System.in);
            
            System.out.println("Enter your name: ");
            name = s.nextLine();
            
            System.out.println("Enter your employee number: ");
            System.out.println("(Format: NNN-L)");
            number = s.nextLine();
                    
            System.out.println("Enter your hire date: ");
            hireDate = s.nextLine();
            
            System.out.println("Enter your payrate: ");
            payRate = s.nextDouble();
            
            System.out.println("Enter your shift (day=1, night=2): ");
            shift = s.nextInt();
            
            
            
            //Production worker object
            ProductionWorker myWorker = new ProductionWorker(name, number,
                                 hireDate, shift, payRate);
            
            System.out.println("---------- Employee Info -----------");
            System.out.println("Name: " + myWorker.getName());
            System.out.println("Employee Number: " + myWorker.getNumber());
            System.out.println("Hire Date: " + myWorker.getHireDate());
            System.out.println("Pay Rate: " +myWorker.getPayRate());
            System.out.println("Shift: " +myWorker.getShift());
        }
        
        
    }
    -----------------------------------------------------------------------------------------
    Last edited by Oshley; 11-19-2012 at 07:53 AM. Reason: [php]

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Employee/ProductionWorker Class

    What is the problem you are having with formatting? Are you getting errors?

Similar Threads

  1. Create an employee class
    By Bravid2001 in forum New To Java
    Replies: 2
    Last Post: 10-11-2011, 05:41 AM
  2. Class employee,company,test: Read data from text file.
    By jeskoston in forum New To Java
    Replies: 4
    Last Post: 03-01-2011, 02:50 AM
  3. employee availability
    By mikerada6 in forum JDBC
    Replies: 3
    Last Post: 12-20-2010, 12:18 PM
  4. employee
    By srish in forum Suggestions & Feedback
    Replies: 7
    Last Post: 07-21-2010, 05:52 AM
  5. Implement a class Employee-Help due tmrw!
    By Britt7 in forum New To Java
    Replies: 3
    Last Post: 10-27-2008, 02:37 AM

Tags for this Thread

Posting Permissions

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