The organisation you work for has asked you to create an interactive application that will assist with the allocation of employees to a cell phone network provider and phone number generation. You are required to prompt a user for three employee names which will randomly assign the employee to either the Vodacom, MTN or Cell C network. The employees also require a randomly generated phone number. The start of each phone number will begin with the following, depending on the network provider:
import java.util.Random;
import java.util.Scanner;
public class PhoneNetworkProvider {
private static Random rnd = new Random();
private static String allProviders[] = { "Vodacom", "MTN", "Cell C" };
private static final int NUMBER_EMPLOYEES = 3;
private static String nameOfEmployees[] = new String[NUMBER_EMPLOYEES];
private static String networkProviderOfEmployees[] = new String[NUMBER_EMPLOYEES];
private static String phonesOfEmployees[] = new String[NUMBER_EMPLOYEES];
/**
* The start point of the program
* @param args
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < NUMBER_EMPLOYEES; i++) {
System.out.print("Enter the employee name " + (i + 1) + ": ");
nameOfEmployees[i] = scanner.nextLine();
networkProviderOfEmployees[i] = generateRandomProvider(networkProviderOfEmployees);
phonesOfEmployees[i] = getEmployeePhone(networkProviderOfEmployees[i]);
}
System.out.println("\nCELL PHONE NUMBER GENERATOR");
System.out.println("**********************************************");
for (int i = 0; i < NUMBER_EMPLOYEES; i++) {
System.out.println("The employee " + nameOfEmployees[i]+" has '"+networkProviderOfEmployees[i]+
"' network provider with phone number "+phonesOfEmployees[i]);
}
scanner.close();
}
/**
* Returns employee random provider
*
* @param employeeNetworkProviders
* @return
*/
private static String generateRandomProvider(String employeeNetworkProviders[]) {
int rndValue = -1;
while (rndValue == -1) {
rndValue = rnd.nextInt(allProviders.length);
for (int provider = 0; provider < allProviders.length; provider++) {
if (employeeNetworkProviders[provider] == allProviders[rndValue]) {
rndValue = -1;
break;
}
}
}
return allProviders[rndValue];
}
/**
* Returns employee phone
*
* @param networkProvider
* @return
*/
private static String getEmployeePhone(String networkProvider) {
String phoneOfEmployee = "072 ";
if (networkProvider == allProviders[1]) {
phoneOfEmployee = "083 ";
}
if (networkProvider == allProviders[2]) {
phoneOfEmployee = "084 ";
}
for (int p = 0; p < 3; p++) {
phoneOfEmployee += rnd.nextInt(10);
}
phoneOfEmployee += "-(";
for (int p = 0; p < 4; p++) {
phoneOfEmployee += rnd.nextInt(10);
}
phoneOfEmployee += ")";
return phoneOfEmployee;
}
}
Comments
Leave a comment