Solved

How do I generate 20 random employees?

Posted on 2011-09-22
22
210 Views
Last Modified: 2012-05-12

Employee class:

public class Employee
{
    protected String eName;
    protected String eNumber;
    protected int hireYear;
    protected double weeklyPay;
   
    public Employee(String eName,String eNumber, int hireYear, double weeklyPay)
    {
        this.eName = eName;
        this.eNumber = eNumber;
        this.hireYear = hireYear;
        this.weeklyPay = weeklyPay;
    }
    public String getName()
        {
            return eName;
        }
    public String getNumber()
        {
            return eNumber;
        }
    public int getHireYear()
        {
            return hireYear;
        }
    public double getWeeklypay()
        {
            return weeklyPay;
        }
    public void setName(String name)
        {
            eName = name;
        }
    public void setNumber(String number)
        {
            eNumber = number;
        }
    public void setHireYear(int year)
        {
            hireYear = year;
        }
    public void setWeeeklyPay(int pay)
        {
            weeklyPay = pay;
            }

ProductionWorker class:

public class ProductionWorker extends Employee
{
    //value of how much a production worker makes hourly
    protected static double hourlySalary= 50.00;
    protected int productionGoal;
   
    //constructor that calls variables from Class Employee to create object ProductionWorker
    public ProductionWorker(String eName,String eNumber, int hireYear, double weeklySalary, double hourlySalary)
    {
        super(eName,eNumber,hireYear,weeklySalary);
        this.hourlySalary = hourlySalary;
    }

    public double getHourlySalary()
        {
            return hourlySalary;
        }

    public void setHourlySalary(double salary)
        {
            this.hourlySalary = salary;
        }
   
   public void setProductionGoal(int productionGoal)
   {
       this.productionGoal = productionGoal;
   }
   
   public int getProductionGoal()
   {
       return productionGoal;
   }


     /**
     * Equals method
     */
    public boolean equals()
    {
        for(double i = 6.00; i < hourlySalary; i++)
        {
            return true;
        }
        return false;
    }
}

TestEmployeeShiftSuperVisor class:

import java.util.ArrayList;
import java.util.Random;
/**
 * Write a description of class TestEmployeeShiftSuperVisor here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class TestEmployeeShiftSuperVisor extends Employee
{
    // instance variables - replace the example below with your own
    protected Random generator;
    protected ArrayList<Employee> employees;
    protected double yearlySalary;

    /**
     * Initializes an ArrayList of 20 employees to a reasonable but random values
     * (you may use class Random to generate the values).
     */
    public TestEmployeeShiftSuperVisor(String eName,String eNumber, int hireYear, double weeklySalary,
    double yearlySalary)
    {
        super(eName,eNumber,hireYear,weeklySalary);
        this.yearlySalary = yearlySalary;
        employees = new ArrayList<Employee>();
        generator = new Random();
        for(Employee generator : employees)
        {
            employees.size();
        }
    }
   
    /**
     * Displays complete information about each employee in created ArrayList
     */
    public void displayEmployeeInfo()
    {
        System.out.println(employees);
    }
}



0
Comment
Question by:ryanbecker24
  • 13
  • 9
22 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36582962
Well, you need to create methods which will be base on random number generator and
would generate each piece - say to have reasonable name - you can make up a list
of  n number of names make an array and then generate index less than n - and in this way you can have a reasonable
name
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36582976
For salary - you set up the interval of possible slaries and then use rtandom float getenerator which will make
a number between 0 and 1 adn split the intervall proportionally to your random number
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36582988
So you can combine all that into a method getRandomEmployee - and then call it 20 times - and you'll get them
0
 

Author Comment

by:ryanbecker24
ID: 36583004
Do you mean something like employees.add("Name","5",5,5.0,60.0) in a seperate method?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36583122
I meant some class like that.

and them in you code you just say

ArrayList<Employee> ar = new ArrayList<Employee)();

for(int j=0; j<20; j++)ar.add(RandomEmployee.getRandomEmployee());

I only compliled it so test - that it makse really different employees  it but I guess the idea should be clear
import java.util.Random;

public class RandomEmployee {


 static  String [] names = {"Brown", "Smith","Jones"};

 static double minSal = 300.0;
    static double maxSal = 1000.0;
   


    public static Employee getRandomEmployee(){

                              
          Random r = new Random();

        String name = names[r.nextInt(names.length)];

        double salary = minSal + Math.random()*(maxSal-minSal);
           int  eNumber = r.nextInt(1000000);


        int yearHired = 2011-r.nextInt(35);

        Employee e = new Employee(name, (eNumber + ""), yearHired, salary );
        return e;


    }




}

 class Employee
{
    protected String eName;
    protected String eNumber;
    protected int hireYear;
    protected double weeklyPay;

    public Employee(String eName,String eNumber, int hireYear, double weeklyPay)
    {
        this.eName = eName;
        this.eNumber = eNumber;
        this.hireYear = hireYear;
        this.weeklyPay = weeklyPay;
    }
}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36583215
This is now tested:

Use DecimlaFormat to format salary without too many digits,
and for real world you want to chek that eNumbers generted would not
hanppen to be the same - but if you select them from bigger number (iused a million) probablity is very small

import java.util.ArrayList;
import java.util.Random;

public class RandomEmployee {


 static  String [] names = {"Brown", "Smith","Jones"};

 static double minSal = 300.0;
    static double maxSal = 1000.0;



    public static Employee getRandomEmployee(){


          Random r = new Random();

        String name = names[r.nextInt(names.length)];

        double salary = minSal + Math.random()*(maxSal-minSal);
           int  eNumber = r.nextInt(1000000);


        int yearHired = 2011-r.nextInt(35);

        Employee e = new Employee(name, (eNumber + ""), yearHired, salary );
        return e;


    }


    public static void main(String[] args) {

        ArrayList<Employee>ar = new ArrayList<Employee>();
        ar.add(getRandomEmployee());
         ar.add(getRandomEmployee());
         ar.add(getRandomEmployee());
         ar.add(getRandomEmployee());

        System.out.println(ar);


    }



}

 class Employee
{
    protected String eName;
    protected String eNumber;
    protected int hireYear;
    protected double weeklyPay;

    public Employee(String eName,String eNumber, int hireYear, double weeklyPay)
    {
        this.eName = eName;
        this.eNumber = eNumber;
        this.hireYear = hireYear;
        this.weeklyPay = weeklyPay;
    }
    public String toString(){
        return (eName + "," + eNumber + "," + hireYear + "," + weeklyPay);
    }

}

Open in new window

Output:
[Smith,910516,2010,664.8099488541511, Brown,797020,1996,790.2581085099262, Smith,399495,2007,770.705224953015, Brown,346835,2006,356.58475962206217]

Open in new window

0
 

Author Comment

by:ryanbecker24
ID: 36595485
So, this is what I changed it to. I don't know if the code is right in the fillInfo() method. I can't tell because for some reason my program can't find the constructor Employee.

import java.util.ArrayList;
import java.util.Random;
/**
 * Write a description of class TestEmployeeShiftSuperVisor here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class TestEmployeeShiftSuperVisor extends Employee
{
    // instance variables - replace the example below with your own
    protected Random generator;
    protected ArrayList<Employee> employees;
    protected double yearlySalary;
    protected static String[] names = {"Ryan","Jones","Brown","Peter","Emma","Jennifer","Courtney",
    "Ross","Joe","Chandler","Ruth","Stewart","Monica","Rachel","Stewie","Chris","Meg","Louis","Cleveland",
    "Quagmire"};
    protected static double minHourlySalary = 6.00;
   
    /**
     * Initializes an ArrayList of 20 employees to a reasonable but random values
     * (you may use class Random to generate the values).
     */
    public TestEmployeeShiftSuperVisor(String eName,String eNumber, int hireYear, double weeklySalary,
    double yearlySalary, double hourlySalary, double minHourlySalary)
    {
        super(eName,eNumber,hireYear,weeklySalary,hourlySalary);
        this.yearlySalary = yearlySalary;
        this.minHourlySalary = minHourlySalary;
        employees = new ArrayList<Employee>();
        generator = new Random();
        fillInfo();
    }
   
    /**
     * Displays complete information about each employee in created ArrayList
     */
    public void displayEmployeeInfo()
    {
        System.out.println(employees);
    }
   
    /**
     * Get random employee
     */
    public static Employee getRandomEmployee()
    {
      Random generator = new Random();
      String name = generator.nextInt[(names.length)];
      double salary = minSal + Math.random()*(hourlySalary-minSal);
      int  eNumber = generator.nextInt(2);
      int yearHired = 2011-generator.nextInt(20);
      Employee e = new Employee(name, (eNumber + ""), yearHired, salary);
     return e;
    }
}
0
 

Author Comment

by:ryanbecker24
ID: 36595515
I fixed the constructor part. It says cannot find variable nextInt. But isnt that a defined method in the ArrayList class?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36595519

I do not see any Employee class in your code - so it cannot find any constructor

Look at my code - there is Employee class (at the bottom of my post) and constructor


I'm also not sure what is this:

    int  eNumber = generator.nextInt(2);

it will only generate integers less than 2, so only  0 or 1
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36595525
no, nextInt() is a method of class Random


   Random r = new Random();
   int randomIntLesssThan100 = r.nextInt(100);

0
 

Author Comment

by:ryanbecker24
ID: 36595812
I meant to type int eNumber = generator.nextInt(20). But that should be 21 right? The compiler is having problems with the name part.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 47

Expert Comment

by:for_yan
ID: 36595822
eNumber = generator.nextInt(20) will generate intgere from 0 to 19


>The compiler is having problems with the name part.

what that means?
0
 

Author Comment

by:ryanbecker24
ID: 36596602
it says cannot find symbol variable nextInt. This line String name = generator.nextInt[names.length];
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36596614

nextInt() is a method in class Random
As a method it should havbe parameters in parentheses, not in square barckets
String name = generator.nextInt(names.length);

Why would not you use the code which I posted and which is compiling and working?
0
 

Author Comment

by:ryanbecker24
ID: 36596683
This is the way you typed it String name = names[r.nextInt(names.length)]; I thought the code you posted was like a guide because I need 20 employees. I changed it to String name = generator.nextInt(names.length) and it says there are incompatible types.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36596727


String name = names[r.nextInt(names.length)]

and

this

String name = generator.nextInt[names.length];

are absolutely diferent

---
String name = names[r.nextInt(names.length)]

names - is array of Strings

r.netxInt(names.length) - is the random integer generated by r (random generateor) which is less than names.length - the size of the array

so name =  names[r.nextInt(names.length)] - is selection  of one of the elements of the array with the index r.nextInt(names.length)

 ----

String name = generator.nextInt[names.length];  - this is syntax error  - nextInt() is a method , it cannot use
square brckets - compiler of couyrse generates error when encounters nextInt[...] with square brackets

------


Start with my code  - compile and excute it exactly as it is - it compiles and executes - you can see the ouptput

After that explain waht you need to change iif anything





 




0
 
LVL 47

Expert Comment

by:for_yan
ID: 36596763
The array indexes are in square brackets

parameters of the methods are in round parentheses

This is extremely important difference - this how compiler
distingushes and analyzes the syntax of your code
when it encounters contradiction - method name after which comes square bracket
like here

generator.nextInt[names.length];

it causes absolute confusion with the compiler.

Those are all very important things - you need to get syntax right

Therefore I suggest that you use my code as it is
If you need to change something it is always easier to staert from the code
which works and then make one point changes - copile and try again
and if it deoes not work - then it means that your most recent change was wrong







0
 

Author Comment

by:ryanbecker24
ID: 36601082
I just realized that I need a employee number (string) in the format XXX-L, where each X is a digit within the range 0-9 and the L is a letter within the range A-M randomized. Is that a huge difference?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36601156
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Random;

public class RandomEmployee {


 static  String [] names = {"Brown", "Smith","Jones"};

 static double minSal = 300.0;
    static double maxSal = 1000.0;




    public static Employee getRandomEmployee(){




          Random r = new Random();

        String name = names[r.nextInt(names.length)];

        double salary = minSal + Math.random()*(maxSal-minSal);

        

           int  eNumber = r.nextInt(1000);
        String enumS = "" + eNumber;

        while(enumS.length()<3)enumS = "0"+enumS;

        String let = "" + ("ABCDEFGHIJKLM").charAt(r.nextInt(13));





          


        int yearHired = 2011-r.nextInt(35);

        Employee e = new Employee(name, enumS + "-" + let, yearHired, salary );
        return e;


    }


    public static void main(String[] args) {

        ArrayList<Employee>ar = new ArrayList<Employee>();
        ar.add(getRandomEmployee());
         ar.add(getRandomEmployee());
         ar.add(getRandomEmployee());
         ar.add(getRandomEmployee());

        System.out.println(ar);


    }



}

 class Employee
{
    static  NumberFormat format = new DecimalFormat("#####.##");;
    protected String eName;
    protected String eNumber;
    protected int hireYear;
    protected double weeklyPay;

    public Employee(String eName,String eNumber, int hireYear, double weeklyPay)
    {
        this.eName = eName;
        this.eNumber = eNumber;
        this.hireYear = hireYear;
        this.weeklyPay = weeklyPay;


    }
    public String toString(){

        return (eName + "," + eNumber + "," + hireYear + "," + format.format(weeklyPay));
    }

}

Open in new window



Output:
[Smith,283-K,2006,955.27, Smith,790-L,1984,763.58, Brown,269-B,2000,991.49, Brown,366-E,1987,744.18]

Open in new window

0
 

Author Comment

by:ryanbecker24
ID: 36602095
I also need to have a employeeNumber in the Employee class. What am I doing wrong?

public String employeeNumber()
    {
     Random generator = new Random();
     String enumS = "" + eNumber;
     while(enumS.length()<3)
     enumS = "0"+ enumS;
     String let = "" + ("ABCDEFGHIJKLM").charAt(generator.nextInt(13));
     return enumS;
    }
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36602173
This is employeeNumber() in Employee class

In your code - uyou need fisrt to generate number - less than 999:

   Random generator = new Random();
     String enumS = "" + generator.nextInt(1000); // isntead of  String enumS = "" + eNumber;
   
then in the end you want to combbine them together:

  return enumS + "-" + let;  // not return enumS - you have no letteer

In addition it is not a very good practice to create a new Random() generator  every time you create new employee -
so I cretaed one static Random for the class and used it continually
for all employees (see my code below).



 
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Random;

public class RandomEmployee {


 static  String [] names = {"Brown", "Smith","Jones"};

 static double minSal = 300.0;
    static double maxSal = 1000.0;




    public static Employee getRandomEmployee(){




          Random r = new Random();

        String name = names[r.nextInt(names.length)];

        double salary = minSal + Math.random()*(maxSal-minSal);

        

           int  eNumber = r.nextInt(1000);
        String enumS = "" + eNumber;

        while(enumS.length()<3)enumS = "0"+enumS;

        String let = "" + ("ABCDEFGHIJKLM").charAt(r.nextInt(13));





          


        int yearHired = 2011-r.nextInt(35);

        Employee e = new Employee(name,  yearHired, salary );
        return e;


    }


    public static void main(String[] args) {

        ArrayList<Employee>ar = new ArrayList<Employee>();
        ar.add(getRandomEmployee());
         ar.add(getRandomEmployee());
         ar.add(getRandomEmployee());
         ar.add(getRandomEmployee());

        System.out.println(ar);


    }



}

 class Employee
{
    static  NumberFormat format = new DecimalFormat("#####.##");;
     static Random r = new Random();
    protected String eName;
    protected String eNumber;
    protected int hireYear;
    protected double weeklyPay;

    public Employee(String eName, int hireYear, double weeklyPay)
    {
        this.eName = eName;
        this.eNumber = employeeNumber();
        this.hireYear = hireYear;
        this.weeklyPay = weeklyPay;


    }
    public String toString(){

        return (eName + "," + eNumber + "," + hireYear + "," + format.format(weeklyPay));
    }



public String employeeNumber()
    {

        int  eNumber = r.nextInt(1000);
          String enumS = "" + eNumber;

          while(enumS.length()<3)enumS = "0"+enumS;

          String let = "" + ("ABCDEFGHIJKLM").charAt(r.nextInt(13));


     return enumS + "-" + let;
    }

}

Open in new window


[Jones,708-M,1998,752.52, Jones,590-C,1985,330.01, Brown,136-H,1992,969.97, Smith,072-L,1989,306.13]

Open in new window

0
 

Author Closing Comment

by:ryanbecker24
ID: 36602449
Thank you!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now