jeudi 12 avril 2018

How do I create a random number of type int and then convert it into a String of random numbers? [duplicate]

This question already has an answer here:

Here is the code I have typed up so far. The general goal is to use the addEmployee method to add to HashMap employees. The key will be a name entered when calling the addEmployee method and the other string is meant to be a random sequence of numbers that will be used as the ID. How the generateID(String name) field is set up is that I generate a random number between the min and max values and then return that number, but afterward I don't know how the number is supposed to be changed into String object.

In terms of instructions, I believe I am not supposed to be using the HashSet, an ArrayList, or anything like that. Just the Random class to generate a number and the HashMap class to store two Strings (Name and the Random number generated)

import java.util.Random;
import java.util.HashMap;
import java.util.HashSet;
/**
 * 
 */
public class Company
{
    // instance variables - replace the example below with your own
    private Random randomGenerator = new Random();
    private HashMap<String, String> employees; 
    private String Name;
    private final int min = Integer.MIN_VALUE;
    private final int max = Integer.MAX_VALUE;
    /**
     * Constructor for objects of class Company
     */
    public Company(String name)
    {
        employees = new HashMap<>();
        Name = name.trim().toUpperCase();
    }

    /**
     * A method to return the name.
     */
    public String getName()
    {
        // Return the value of the Name field.
        return Name;
    }

    /**
     * Return all employees in the HashMap.
     */
    public void getEmployees()
    {
        for(int index = 0; index < employees.size(); index++)
        System.out.println(employees);
    }

    /**
     * A method to return number of key value pairs of employees/ID's
     * stored in the HashMap.
     */
    public int getTotalNumberEmployees()
    {
        // Assigns the size of the employees HashMap to the int variable size.
        int size = employees.size();
        // Returns the number of key value pairs stored in the HashMap.
        return size;
    }

    /**
     * A method to change the name.
     */
    public String changeName(String name)
    {
        // Assigns the field variable Name to the parameter name.
        Name = name.trim().toUpperCase();
        // Returns the Name variable.
        return Name;
    }

    /**
     * Add an employee to the HashMap.
     */
    public void addEmployee(String inputName)
    {        
        employees.put(inputName);
    }

    /**
     * A method to generate a random ID number for the name entered.
     */
    public int generateID(String name)
    {
        // Assigns the random number generated to the int variable result.
        int id = randomGenerator.nextInt(min - max) + min;
        // Returns the random number assigned to the result variable.
        return id;
    }

    /**
     * A method to return the names of all employees in the HashMap.
     */
    public void listEmployees()
    {
        for(String name : employees.keySet())
        {
        String key = name;
        String value = employees.get(name);
        System.out.println(" Employee Name: " + key);
        //System.out.println("ID Number: " + value+".");
        System.out.println();
        }
    }
}




Aucun commentaire:

Enregistrer un commentaire