×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Vusi Mbonani
Added: Aug 11, 2021 5:02 AM
Views: 3701
Tags: no tags
  1. /*
  2. * File Name: NetworkProviderAssignment.java
  3. * @Author: Vusi Daniel Mbonani* Student Number: 15000895
  4. * Email: 15000895@rcconnect.co.za
  5. * Course: Programming 1A
  6. * Code: PROG 5121
  7. * Question 2, Assignment 1
  8. * Description: This program called Network Provider Assingment it allocates
  9. * network and randomly generated cell phone numbers to the user.
  10. */
  11.  
  12. package networkproviderassignment;
  13.  
  14. //Importing the swing class JOptionPane.
  15. import javax.swing.JOptionPane;
  16.  
  17. public class NetworkProviderAssignment {
  18.     //Declaring Named Constant Variables that will hold the first three digits of the phone nubers..
  19.     final static String VODACOM = "072";
  20.     final static String MTN = "083";
  21.     final static String CELLC = "084";
  22.        
  23.     //Declaring the named string constant that will hold the name of the respective networks.
  24.     final static String NETWORK_ONE = "CELLC";
  25.     final static String NETWORK_TWO = "VODACOM";
  26.     final static String NETWORK_THREE = "MTN";
  27.  
  28.     public static void main(String[] args) {
  29.         //Declaring the local variables that will hold the names of the employees.
  30.         String firstEmployee, secondEmployee,thirdEmployee;
  31.         //Calling the getFirstName method and it returns the name.
  32.         firstEmployee = getFirstName();
  33.         //Calling the getSecondName method and it returns the name.
  34.         secondEmployee = getSecondName();
  35.         //Calling the getThirdName method and returns the name.
  36.         thirdEmployee = getThirdName();
  37.         //Calling the showNetworkInfo method that passes thre arguments.
  38.         showNetworkInfo(firstEmployee, secondEmployee, thirdEmployee);  
  39.     }
  40.    
  41.     //Getting the name from the user using the JOptionPane static method.
  42.     public static String getFirstName(){
  43.         String name;
  44.         name = JOptionPane.showInputDialog(null,"Enter name of Employee 1: ",
  45.                 "Network Provider Assignment",JOptionPane.INFORMATION_MESSAGE);
  46.         return name;
  47.     }
  48.    
  49.     //Getting the name from the user using the JOptionPane static method.
  50.      public static String getSecondName(){
  51.         String name;
  52.         name = JOptionPane.showInputDialog(null,"Enter name of Employee 2: ",
  53.                 "Network Provider Assignment",JOptionPane.INFORMATION_MESSAGE);
  54.         return name;
  55.     }
  56.      
  57.      //Getting the name from the user using the JOptionPane static method.
  58.       public static String getThirdName(){
  59.         String name;
  60.         name = JOptionPane.showInputDialog(null,"Enter name of Employee 3: ",
  61.                 "Network Provider Assignment",JOptionPane.INFORMATION_MESSAGE);
  62.         return name;
  63.     }
  64.      
  65.     /*Declaring a static method called generateThreeDigits its task is to generate
  66.      * random numbers for the three digits of the cell phone number. This method takes no argumets
  67.      * and it returns an integer value back to the main program*/
  68.     public static int generateThreeDigits(){
  69.         //Declaring the local variable to hold the random digits.
  70.         int threeDigits;
  71.        
  72.         /*Declaring the local variable upper and lower to hold the range of the randomly
  73.          *generated digits.*/
  74.         int upper = 999;
  75.         int lower = 100;
  76.        
  77.         /*Calling the static random method from the Math class to generate random
  78.         digits and store the digits to the variable declared.*/
  79.         threeDigits = lower + (int)(Math.random()*((upper - lower) +1));
  80.        
  81.         //Returning the random generated digits to the main program.
  82.         return threeDigits;
  83.     }
  84.    
  85.     /*Declaring a static method called generateFourDigits its task is to generate
  86.      * random numbers for last four digits of the cell phone number. This method takes no argumets
  87.      * and it returns an integer value back to the main program*/
  88.     public static int generateFourDigits(){
  89.        
  90.         /*Declaring the local variable upper and lower to hold the range of the randomly
  91.          *generated digits.*/
  92.         int upper = 9999;
  93.         int lower = 1000;
  94.        
  95.         /*Calling the static random method from the Math class to generate random
  96.         digits and store the digits to the variable declared.*/
  97.         int fourDigits = lower + (int)(Math.random()*((upper - lower) +1));
  98.        
  99.         //Returning the random generated digits to the main program.
  100.         return fourDigits;
  101.     }
  102.    
  103.     //Declaring a static void method called showNetworkInfo its task is to display the Network Information.
  104.     public static void showNetworkInfo(String firstEmployee, String secondEmployee, String thirdEmployee){
  105.          //Declaring a string that will be concatenated to the argument into the showMessageDialog method.
  106.         String heading = "CELL PHONE NUMBER GENERATION";
  107.         //Declaring a string that will be concatenated to the argument into the showMessageDialog method..
  108.         String starPattern = "\n************************************************\n";
  109.      
  110.         /*Calling showMessageDialog static Method that takes four arguments
  111.          *and shows the output*/
  112.         JOptionPane.showMessageDialog(null,heading+ starPattern+ firstEmployee+" "
  113.             + "will be on the "+NETWORK_ONE+" network with phone number "+CELLC+" "
  114.             + " "+generateThreeDigits()+" "+"-"+generateFourDigits() +"\n"+secondEmployee+" will be on the "
  115.             +NETWORK_TWO+" network with phone number "+VODACOM+" "+generateThreeDigits()+" "+"-"+generateFourDigits()+
  116.             "\n"+thirdEmployee+" will be on the "+NETWORK_THREE+" network with phone number "+MTN+" "
  117.             +generateThreeDigits()+" "+generateFourDigits(),"Network Provider Assignment",
  118.             JOptionPane.INFORMATION_MESSAGE);
  119.     }
  120. }