Question 2 (Marks: 20)
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:
NETWORK PROVIDER START
Vodacom 072
MTN 083
Cell C 084
The remaining numbers of the phone number must be randomly generated.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package network_generator;
import java.util.Random;
import java.util.Scanner;
import javax.swing.*;
/**
*
* @author Isaiah Japesa
*/
public class Network_generator {
private static Random generate;
private static String network_providers[] = { "Vodacom", "MTN", "Cell C" };
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
generate = new Random();
String Network[] = new String[3];
String Name_Of_Employees[] = new String[3];
String Phones[] = new String[3];
for (int n = 0; n < 3; n++) {
//System.out.print("Employee name " + (n + 1) + ": ");
Name_Of_Employees[n] = JOptionPane.showInputDialog("Enter Name of employee\t "+" "+ (n + 1));
//Name_Of_Employees[n] = input.nextLine();
Network[n] = getGeneratedProvider(Network);
Phones[n]="072 ";
if(Network[n]==network_providers[1]) {
Phones[n]="083 ";
}
if(Network[n]==network_providers[2]) {
Phones[n]="084 ";
}
for(int i=0;i<3;i++) {
Phones[n] += generate.nextInt(10);
}
Phones[n] += "-(";
for(int x=0;x<4;x++) {
Phones[n] += generate.nextInt(10);
}
Phones[n] += ")";
}
int n = 0;
JOptionPane.showMessageDialog(null, "Employee name 1:\t " + Name_Of_Employees[0]+"\n"+ "Employee network provider 1:\t" +
Network[0]+"\n" +"Employee phone number 1:\t " + Phones[0] + "\n"+
"Employee name 2:\t " + Name_Of_Employees[1]+"\n"+ "Employee network provider 2:\t" +
Network[1]+"\n" +"Employee phone number: 2\t " + Phones[1] + "\n"+
"Employee name 3:\t " + Name_Of_Employees[2]+"\n"+ "Employee network provider 3:\t" + Network[2]+"\n" +"Employee phone number 3:\t " + Phones[2] + "\n"
);
input.close();
}
//Will return network providers
private static String getGeneratedProvider(String providers[]) {
int index = -1;
while (index==-1) {
index = generate.nextInt(3);
for (int i= 0; i < 3; i++) {
if (providers[i] == network_providers[index]) {
index = -1;
break;
}
}
}
return network_providers[index];
}
}
Comments
Leave a comment