Question

In: Computer Science

Write a program to display privilege list of the roles and role list for privileges using...

Write a program to display privilege list of the roles and role list for privileges using many to many concept.

Problem Specification:

1. Prefill the data for roles and privileges.

2. Create a menu 1. Assign privilege 2. Cancel privilege 3. Search privilege by role 4. Search role by privilege.

3. Assign privilege -- To assign specific privilege for role.

4. Cancel Privilege -- To cancel specific privilege for role.

5. Search privilege by role -- To display all the privileges assigned for specific role.

6. Search role by privilege -- To display all the roles who are having specified privilege.

[Note :Strictly adhere to the object oriented specifications given as a part of the problem statement.Use the same class names, method names and attribute names.]

Create a class Role with the following private member variables/attributes.

  • private Integer id
  • private String name
  • private List<Privilege> privilegeList

1. Include a default constructor and 3-argument constructor with following argument order id, name , privilegeList

2. Include appropriate getters and setters.

Create a class Privilege with the following private member variables/attributes.

  • private Integer id
  • private String name
  • private List<Role> roleList

1. Include a default constructor and 3-argument constructor with following argument order id, name, roleList.

2. Include appropriate getters and setters.

Input and Output Format:

Refer sample input and output for formatting specifications.

All text in bold corresponds to input and the rest corresponds to output.

Sample Input and output :

Available roles:

Id Name

1 Manager

2 Shipper

3 Agent

4 Customer

Available privileges:

Id Name

1 Process Shipment

2 Create Shipment

3 Schedule Shipment

4 Cancel Shipment

1. Assign privilege

2. Cancel privilege

3. Search privilege by role

4. Search role by privilege

Enter the choice:

1

Enter the id for role:

1

Enter the id for privilege:

1

Do you want to process again?(yes/no)

yes

1. Assign privilege

2. Cancel privilege

3. Search privilege by role

4. Search role by privilege

Enter the choice:

1

Enter the id for role:

2

Enter the id for privilege:

1

Do you want to process again?(yes/no)

yes

1. Assign privilege

2. Cancel privilege

3. Search privilege by role

4. Search role by privilege

Enter the choice:

1

Enter the id for role:

3

Enter the id for privilege:

1

Do you want to process again?(yes/no)

yes

1. Assign privilege

2. Cancel privilege

3. Search privilege by role

4. Search role by privilege

Enter the choice:

1

Enter the id for role:

1

Enter the id for privilege:

2

Do you want to process again?(yes/no)

yes

1. Assign privilege

2. Cancel privilege

3. Search privilege by role

4. Search role by privilege

Enter the choice:

3

Enter the role name:

Manager

Process Shipment

Create Shipment

Do you want to process again?(yes/no)

yes

1. Assign privilege

2. Cancel privilege

3. Search privilege by role

4. Search role by privilege

Enter the choice:

4

Enter the privilege name:

Process Shipment

Manager

Shipper

Agent

Do you want to process again?(yes/no)

no

Don'ts:

1. Do not create packages for classes. Strictly adhere to the program structure given in the template code.   

2. Do not use Internet Explorer, highly recommended to use chrome browser to launch Tekstac platform.   

3. Do not create multiple classes inside a single file. Create separate file for each class.

3 classes--

Role.Java

Privilege.java

Main.Java

Solutions

Expert Solution

// Role.java

import java.util.List;

public class Role
{
   private Integer id;
   private String name;
   private List<Privilege> privilegeList;

   // default constructor
   public Role()
   {
       id = 0;
       name = "";
       privilegeList = null;
   }
  
   // paramterized constructor
   public Role(Integer id, String name, List<Privilege> list)
   {
       this.id = new Integer(id);
       this.name = name;
       this.privilegeList = list;
      
   }
  
   // setters
   public void setID(Integer id)
   {
       this.id = new Integer(id);
   }
  
   public void setName(String name)
   {
       this.name = name;
   }
  
   public void setPrivilegeList(List<Privilege> list)
   {
       privilegeList = list;
   }
  
   // getters
   public Integer getID()
   {
       return id;
   }
  
   public String getName()
   {
       return name;
   }
  
   public List<Privilege> getPrivilegeList()
   {
       return privilegeList;
   }
}
//end of Role.java

// Privilege.java

import java.util.List;

public class Privilege
{
   private Integer id;
   private String name;
   private List<Role> roleList;
  
   // default constructor
   public Privilege()
   {
       id = 0;
       name = "";
       roleList = null;
   }
  
   // paramterized constructor
   public Privilege(Integer id, String name, List<Role> list)
   {
       this.id = new Integer(id);
       this.name = name;
       roleList = list;
   }
  
   // setters
   public void setID(Integer id)
   {
       this.id = new Integer(id);
   }
  
   public void setName(String name)
   {
       this.name = name;
   }
  
   public void setRoleList(List<Role> list)
   {
       roleList = list;
   }
  
   // getters
   public Integer getID()
   {
       return id;
   }
  
   public String getName()
   {
       return name;
   }
  
   public List<Role> getRoleList()
   {
       return roleList;
   }
}
//end of Privilege.java

// Main.java

import java.util.List;
import java.util.Scanner;
import java.util.ArrayList;

public class Main {

   // method to display the menu
   private static void menu()
   {
       System.out.println("1. Assign privilege" + "\n" +
               "2. Cancel privilege" + "\n" +
               "3. Search privilege by role" + "\n" +
               "4. Search role by privilege");
      
   }
  
   // method to return the index of role in roles list
   private static int getRoleIndex(ArrayList<Role> roles, int id)
   {
       for(int i=0;i<roles.size();i++)
       {
           if(roles.get(i).getID() == id) // role found
               return i;
       }
         
       return -1; // role not found
   }
  
   // method to return the index of privilege in privileges list
   private static int getPrivilegeIndex(ArrayList<Privilege> privileges, int id)
   {
       for(int i=0;i<privileges.size();i++)
       {
           if(privileges.get(i).getID() == id) // privilege found
               return i;
       }
      
       return -1; // privilege not found
   }
  
   // method to return the role index in privilege
   private static int roleExists(Privilege privilege, int roleId)
   {
       List<Role> roles = privilege.getRoleList(); // get the list of roles for the privilege
       for(int i=0;i<roles.size();i++)
       {
           if(roles.get(i).getID() == roleId) // role found
               return i;
       }
      
       return -1; // role not found
   }

   // method to return the privilege index in role
   private static int privilegeExists(Role roles, int privilegeId)
   {
       List<Privilege> privileges = roles.getPrivilegeList(); // get the list of privilege
       for(int i=0;i<privileges.size();i++)
       {
           if(privileges.get(i).getID() == privilegeId) // privilege found
               return i;
       }
      
       return -1; // privilege not found
   }
  
   public static void main(String[] args) {

       // pre-define roles and privileges
       ArrayList<Role> roles = new ArrayList<Role>();
       roles.add(new Role(1,"Manager",new ArrayList<Privilege>()));
       roles.add(new Role(2,"Shipper",new ArrayList<Privilege>()));
       roles.add(new Role(3,"Agent",new ArrayList<Privilege>()));
       roles.add(new Role(4,"Customer",new ArrayList<Privilege>()));
      
       ArrayList<Privilege> privileges = new ArrayList<Privilege>();
       privileges.add(new Privilege(1,"Process Shipment",new ArrayList<Role>()));
       privileges.add(new Privilege(2,"Create Shipment",new ArrayList<Role>()));
       privileges.add(new Privilege(3,"Schedule Shipment",new ArrayList<Role>()));
       privileges.add(new Privilege(4,"Cancel Shipment",new ArrayList<Role>()));
      
       Scanner scan = new Scanner(System.in);
       int choice, roleId, privilegeId;
       String contChoice;
      
       // display the available roles and privileges
       System.out.println("Available roles:");
       System.out.printf("%-10s%-20s\n","ID","Name");
       for(int i=0;i<roles.size();i++)
           System.out.printf("%-10d%-20s\n",roles.get(i).getID(),roles.get(i).getName());
      
       System.out.println("\nAvailable privileges:");
       System.out.printf("%-10s%-30s\n","ID","Name");
       for(int i=0;i<privileges.size();i++)
           System.out.printf("%-10d%-30s\n",privileges.get(i).getID(),privileges.get(i).getName());
      
       // loop that continues till the user wants
       do
       {
           menu(); // display the menu
           // input of choice
           System.out.print("Enter the choice: ");
           choice = scan.nextInt();
          
           // validate choice and re-prompt until valid
           while(choice < 1 || choice > 4)
           {
               System.out.println("Invalid choice. Re-enter: ");
               choice = scan.nextInt();
           }
          
           if(choice == 1) // assign privilege
           {
               // input of role id
               System.out.print("Enter the id for role: ");
               roleId = scan.nextInt();
               // validate role id and re-prompt until valid i.e exists in pre-defined role list
               while(getRoleIndex(roles,roleId) == -1)
               {
                   System.out.print("Invalid role ID. Re-enter: ");
                   roleId = scan.nextInt();  
               }
              
               Role role = roles.get(getRoleIndex(roles,roleId)); //get the role object
              
               // input of privilege id
               System.out.print("Enter the id for privilege: ");
               privilegeId = scan.nextInt();
               // validate privilege id and re-prompt until valid i.e exist in pre-defined privilege list
               while(getPrivilegeIndex(privileges,privilegeId) == -1)
               {
                   System.out.print("Invalid privilege ID. Re-enter: ");
                   privilegeId = scan.nextInt();
               }
              
               scan.nextLine();
              
               Privilege privilege = privileges.get(getPrivilegeIndex(privileges,privilegeId)); // get the privilege object
              
               // if role doesn't contain the privilege
               if(privilegeExists(role, privilegeId) == -1)
               {
                   //add privilege to role
                   List<Privilege> privilegeList = role.getPrivilegeList();
                   privilegeList.add(privilege);
                   role.setPrivilegeList(privilegeList);
                   // add role to privilege
                   List<Role> roleList = privilege.getRoleList();
                   roleList.add(role);
                   privilege.setRoleList(roleList);
               }
              
           }
           else if(choice == 2) // cancel privilege
           {
               // input of role id
               System.out.print("Enter the id for role: ");
               roleId = scan.nextInt();
               while(getRoleIndex(roles,roleId) == -1)
               {
                   System.out.print("Invalid role ID. Re-enter: ");
                   roleId = scan.nextInt();  
               }
              
               Role role = roles.get(getRoleIndex(roles,roleId));
              
               // input of privilege id
               System.out.print("Enter the id for privilege: ");
               privilegeId = scan.nextInt();
               while(getPrivilegeIndex(privileges,privilegeId) == -1)
               {
                   System.out.print("Invalid privilege ID. Re-enter: ");
                   privilegeId = scan.nextInt();
               }
              
               scan.nextLine();
              
               Privilege privilege = privileges.get(getPrivilegeIndex(privileges,privilegeId));
              
               int index = privilegeExists(role, privilegeId);
               // of privilege exists in role
               if(index != -1)
               {
                   // remove privilege from role
                   List<Privilege> privilegeList = role.getPrivilegeList();
                   privilegeList.remove(index);
                   role.setPrivilegeList(privilegeList);
                   index = roleExists(privilege, roleId);
                   // remove role from privilege
                   List<Role> roleList = privilege.getRoleList();
                   roleList.remove(index);
                   privilege.setRoleList(roleList);
               }
           }
           else if(choice == 3) // display privileges by role
           {
               System.out.print("Enter the role name: ");
               scan.nextLine();
               String roleName = scan.nextLine();
              
               for(int i=0;i<roles.size();i++)
               {
                   if(roles.get(i).getName().equalsIgnoreCase(roleName))
                   {
                       List<Privilege> privilegeList =roles.get(i).getPrivilegeList();
                       for(int j=0;j<privilegeList.size();j++)
                           System.out.println(privilegeList.get(j).getName());
                       break;
                   }
               }
           }
           else if(choice == 4) // display roles by privilege
           {
               System.out.print("Enter the privilege name: ");
               scan.nextLine();
               String privilegeName = scan.nextLine();
              
               for(int i=0;i<privileges.size();i++)
               {
                   if(privileges.get(i).getName().equalsIgnoreCase(privilegeName))
                   {
                       List<Role> roleList =privileges.get(i).getRoleList();
                       for(int j=0;j<roleList.size();j++)
                           System.out.println(roleList.get(j).getName());
                       break;
                   }
               }
           }
          
           System.out.print("Do you want to process again?(yes/no) ");
           contChoice = scan.nextLine();
       }while(contChoice.equalsIgnoreCase("yes"));
      
   }

}
//end of Main.java

Output:


Related Solutions

Using c++, write a program that will display your name as a void function then will...
Using c++, write a program that will display your name as a void function then will perform the following by user-defined functions: a. to compute for the sum of two numbers (n1, n2) using function.
1) Using examples, define and describe roles, role expectations and role conflict. 2) List and describe...
1) Using examples, define and describe roles, role expectations and role conflict. 2) List and describe the five stages of group development and provide examples. 3) Identify the seven components of the communication process model, and briefly describe each by providing an example.
Using python Write a program that displays all of states in the U.S. and display each...
Using python Write a program that displays all of states in the U.S. and display each state that begins with the letter A.
Exercise 2.1: Basic digital signals (a) Write a MATLAB program to generate and display (using the...
Exercise 2.1: Basic digital signals (a) Write a MATLAB program to generate and display (using the stem function) the signals defined in Table 1. The MATLAB code of the first signal (dirac) is given in the report template as an example. (b) Write a MATLAB function [x, t] = sin_NU(f0, fs, T) to generate a sine signal. The output parameters x and t are the signal and time vectors, respectively. The input parameters are f0 (signal frequency in Hz), fs...
Write a rudimentary spreadsheet program using C++. Display a grid of cells with columns A through...
Write a rudimentary spreadsheet program using C++. Display a grid of cells with columns A through H and rows 1 through 20. Accept input in the first row of the screen. The commands are of the form column row entry, where entry is a number, a cell address preceded by a plus (e.g., +A5), a string, or a function preceded by an at sign, @. The functions are: max, min, avg, and sum. During execution of your program, build and...
Write a program that accepts user’s name, password and address and display them back using the...
Write a program that accepts user’s name, password and address and display them back using the format “Hi, I am user’s name. I live at user’s address.”. Restrictions: ▪ Use only three variables. ▪ Make sure you support spaces.
Write a C++ program to display toy name
Write a C++ program to display toy name
Using If-Else conditional statement write a Visual Basic Program to display: (i) “STOP” if the user...
Using If-Else conditional statement write a Visual Basic Program to display: (i) “STOP” if the user enters “R” (ii) “CAUTION” if the user enters “Y” (iii) “GO” if the user enters “G” (iv) “Invalid” if the user enters any other character
Java Program Use for loop 1.) Write a program to display the multiplication table of a...
Java Program Use for loop 1.) Write a program to display the multiplication table of a given integer. Multiplier and number of terms (multiplicand) must be user's input. Sample output: Enter the Multiplier: 5 Enter the number of terms: 3 5x0=0 5x1=5 5x2=10 5x3=15 2 Create a program that will allow the user to input an integer and display the sum of squares from 1 to n. Example, the sum of squares for 10 is as follows: (do not use...
USING PYTHON Write a program to create a number list. It will call a function to...
USING PYTHON Write a program to create a number list. It will call a function to calculate the average values in the list. Define main ():                        Declare variables and initialize them                        Create a list containing numbers (int/float)                        Call get_avg function that will return the calculated average values in the list.                                       Use a for loop to loop through the values in the list and calculate avg                        End main()
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT