2

I have a Name Class:

   public class Name {
       // Instance Variables
       private String title;
       private String firstName;    
       private String surname;  
   }

A Person Class:

   public abstract class Person {
       protected Name name;         
       protected String phoneNumber;

Default Constructor

   public Person(){
       name=new Name();
       phoneNumber=null;
    }

However, problem is in my employee class when I want to read in a new employee from keyboard input:

public class Employee extends Person implements Serializable{   

    private Date dob;                                           
    private double salary;
    private Date startDate;
    private int number;                     

    private static int nextNumber=1;    

    private final double MAX_SALARY = 100000;
    private final double INCREMENT = 500;   

    // Default Constructor
    public Employee(){
        super();        // Not needed
        dob=new Date();
        salary=0.0;
        startDate=new Date();

    }

    // Initialization Constructor
    public Employee(String t, String fN, String sn, String phoneNo, int d, int m, int y, 
                     double salary, int sD, int sM, int sY){
       // Call super class constructor 
       super(t, fN, sn, phoneNo);
       // And then initialise Employees own instance variables
       dob=new Date(d,m,y);
       this.salary=salary;
       startDate=new Date(sD,sM,sY);
}

Set number to static nextNumber before incrementing nextNumber number = nextNumber++;

    public void read(){
        Scanner kbInt = new Scanner(System.in); // Scanner is serializable
        Scanner kbString = new Scanner(System.in);

        System.out.println("EMPLOYEE DETAILS ==>");
        System.out.print("NUMBER : ");number=kbInt.nextInt();
        System.out.print("Title : ");name=kbString.nextLine();
        System.out.print("FIRSTNAME : ");firstName=kbString.nextLine();
        System.out.print("SURNAME : ");surname=kbString.nextLine();
        System.out.print("PHONE NO : ");phoneNumber=kbString.nextline();
        System.out.print("D.O.B. : ");dob=kbInt.nextInt();

    }

How do I read in a new employee eg (Title, firstName & surname) and dob (12 11 95)?

Felix Gerber
  • 1,555
  • 3
  • 23
  • 37
  • I don't know why you need 2 scanners pointing to the same input stream – TheLostMind Nov 12 '15 at 07:41
  • It's the way I learned it – user3493936 Nov 12 '15 at 07:45
  • You need a constructor in your `Name` class that accepts title, firstname and surname and creates a `Name` instance. Next you need to call this from `Person's constructor` next. You need to pass these fields to Person – TheLostMind Nov 12 '15 at 07:49
  • Thanks for your help, I have done that but my real problem is how to read (Title, Firstname, Surname) from the scanner when it can only use title=kbString.nextLine(); i.e. title, Firstname, Surname=kbString.nextLine(); will not work. – user3493936 Nov 12 '15 at 07:55
  • Using `nextLine()` after other `nextX()` methods of `Scanner` will cause issues. Please check the linked question. Also, using 2 scanners to the same stream doesn't help – TheLostMind Nov 12 '15 at 08:00
  • 1
    |Thank you for your advice – user3493936 Nov 12 '15 at 08:07

0 Answers0